Google
Web alhem.net
Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members

utils.cpp File Reference

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <mysql/mysql.h>
#include <libmysqlwrapped.h>
#include <unistd.h>
#include <ctype.h>
#include <stdarg.h>
#include "utils.h"
#include "dbd.h"

Include dependency graph for utils.cpp:

Include dependency graph

Go to the source code of this file.

Functions

void zap (char *s, char c)
char * fget (char *s, int l, FILE *f)
char * datetime (void)
char * expiredatetime (void)
void strlwr (char *s)
void set_cookie (Cookies *cs, char *n, char *v)
void set_cookie (Cookies *cs, char *n, long v)
void strupr (char *s)
void strupr (std::string &str)
int syscall (char *s)
int name_ok (char *s)
void textareain (char *s)
void remove_br (char *s)
void pack_and_set_cookies (Cookies *cs)
void unpack_cookie (Cookies *cs)


Function Documentation

char* datetime void   ) 
 

Definition at line 54 of file utils.cpp.

00055 { 00056 static char dt[40]; 00057 time_t t = time(NULL); 00058 struct tm * tp = localtime(&t); 00059 00060 sprintf(dt,"%04d-%02d-%02d %02d:%02d:%02d", 00061 tp -> tm_year + 1900, 00062 tp -> tm_mon + 1, 00063 tp -> tm_mday, 00064 tp -> tm_hour, 00065 tp -> tm_min, 00066 tp -> tm_sec); 00067 return dt; 00068 }

char* expiredatetime void   ) 
 

Definition at line 70 of file utils.cpp.

Referenced by pack_and_set_cookies().

00071 { 00072 static char dt[80]; 00073 static char *days[7] = {"Sunday","Monday", 00074 "Tuesday","Wednesday","Thursday","Friday","Saturday"}; 00075 static char *months[12] = {"Jan","Feb","Mar","Apr","May", 00076 "Jun","Jul","Aug","Sep","Oct","Nov","Dev"}; 00077 time_t t = time(NULL); 00078 struct tm * tp = localtime(&t); 00079 00080 sprintf(dt,"%s, %02d-%s-%04d %02d:%02d:%02d GMT", 00081 days[tp -> tm_wday], 00082 tp -> tm_mday, 00083 months[tp -> tm_mon], 00084 tp -> tm_year + 1910, 00085 tp -> tm_hour, 00086 tp -> tm_min, 00087 tp -> tm_sec); 00088 return dt; 00089 }

char* fget char *  s,
int  l,
FILE *  f
 

Definition at line 47 of file utils.cpp.

00048 { 00049 char *i = fgets(s,l,f); 00050 s[strlen(s) - 1] = 0; 00051 return i; 00052 }

int name_ok char *  s  ) 
 

Definition at line 191 of file utils.cpp.

00192 { 00193 int r = 0; 00194 int i; 00195 00196 if (isalpha(*s) || *s == '_') 00197 { 00198 r = 1; 00199 for (i = 0; i < (int)strlen(s); i++) 00200 if (!isalnum(s[i]) && s[i] != '_') 00201 return 0; 00202 } 00203 return r; 00204 }

void pack_and_set_cookies Cookies *  cs  ) 
 

Definition at line 347 of file utils.cpp.

References COOKIE_DOMAIN, COOKIE_PATH, expiredatetime(), and out.

Referenced by run().

00348 { 00349 //FILE *fil = fopen("/tmp/dbdlog","at"); 00350 std::string packed; 00351 cgi::Base64 b('@'); 00352 for (cookie_v::iterator it = cs -> GetCookies().begin(); it != cs -> GetCookies().end(); it++) 00353 { 00354 COOKIE *p = *it; 00355 if (strcmp(p -> name, "dbpacked")) 00356 { 00357 std::string key = p -> name; 00358 std::string valx = p -> value; 00359 std::string val = b.encode_buffer(valx.size() ? valx : " null"); 00360 //fprintf(fil,"%s(%s)%s\n",p -> name,p -> value,val.c_str()); 00361 if (packed.size()) 00362 packed += ":"; 00363 packed += key + ":" + val; 00364 } 00365 } 00366 //fprintf(fil,"---------\n%s\n", packed.c_str()); 00367 fprintf(out,"Set-Cookie: %s=%s; domain=%s; path=%s; expires=%s\r\n", 00368 "fdc",packed.c_str(), 00369 COOKIE_DOMAIN, 00370 COOKIE_PATH, 00371 expiredatetime()); 00372 //fprintf(fil,"\n"); 00373 //fclose(fil); 00374 }

void remove_br char *  s  ) 
 

Definition at line 267 of file utils.cpp.

References out.

00268 { 00269 char *s2 = new char[strlen(s) + 1440]; 00270 int i; 00271 fflush(out); 00272 00273 // for (j = 0; j < (int)strlen(s); j++) 00274 // { 00275 // while (!strncasecmp(s + j,"<br>",4)) 00276 // memmove(s + j,s + j + 4,strlen(s + j) + 1); 00277 // } 00278 *s2 = 0; 00279 for (i = 0; i < (int)strlen(s); i++) 00280 { 00281 if (!strncmp(s + i,"&aring;",7)) 00282 { 00283 sprintf(s2 + strlen(s2),"%c",0xe5); 00284 i += 6; 00285 } else 00286 if (!strncmp(s + i,"&auml;",6)) 00287 { 00288 sprintf(s2 + strlen(s2),"%c",0xe4); 00289 i += 5; 00290 } else 00291 if (!strncmp(s + i,"&ouml;",6)) 00292 { 00293 sprintf(s2 + strlen(s2),"%c",0xf6); 00294 i += 5; 00295 } else 00296 if (!strncmp(s + i,"&Aring;",7)) 00297 { 00298 sprintf(s2 + strlen(s2),"%c",0xc5); 00299 i += 6; 00300 } else 00301 if (!strncmp(s + i,"&Auml;",6)) 00302 { 00303 sprintf(s2 + strlen(s2),"%c",0xc4); 00304 i += 5; 00305 } else 00306 if (!strncmp(s + i,"&Ouml;",6)) 00307 { 00308 sprintf(s2 + strlen(s2),"%c",0xd6); 00309 i += 5; 00310 } else 00311 if (s[i] == '&') 00312 { 00313 strcat(s2,"&amp;"); 00314 } else 00315 { 00316 sprintf(s2 + strlen(s2),"%c",s[i]); 00317 } 00318 } 00319 //printf("strlen(s)=%d strlen(s2)=%d<br>\n",strlen(s),strlen(s2)); 00320 fflush(out); 00321 strcpy(s,s2); 00322 delete s2; 00323 }

void set_cookie Cookies *  cs,
char *  n,
long  v
 

Definition at line 117 of file utils.cpp.

Referenced by run().

00118 { 00119 char slask[20]; 00120 00121 sprintf(slask,"%ld",v); 00122 set_cookie(cs,n,slask); 00123 }

void set_cookie Cookies *  cs,
char *  n,
char *  v
 

Definition at line 104 of file utils.cpp.

00105 { 00106 // set-cookie response 00107 /* 00108 fprintf(out,"Set-Cookie: %s=%s; domain=%s; path=%s; expires=%s\n", 00109 n,v, 00110 COOKIE_DOMAIN, 00111 COOKIE_PATH, 00112 expiredatetime()); 00113 */ 00114 cs -> replacevalue(n,v); 00115 }

void strlwr char *  s  ) 
 

Definition at line 92 of file utils.cpp.

00093 { 00094 int i = 0; 00095 while (s[i]) 00096 { 00097 if (s[i] >= 'A' && s[i] <= 'Z') 00098 s[i] += 32; 00099 i++; 00100 } 00101 }

void strupr std::string &  str  ) 
 

Definition at line 165 of file utils.cpp.

00166 { 00167 for (size_t i = 0; i < str.size(); i++) 00168 { 00169 if (str[i] >= 'a' && str[i] <= 'z') 00170 { 00171 str[i] -= 32; 00172 } 00173 } 00174 }

void strupr char *  s  ) 
 

Definition at line 155 of file utils.cpp.

00156 { 00157 int i; 00158 00159 for (i = 0; i < (int)strlen(s); i++) 00160 if (s[i] > 96 && s[i] <= 'z') 00161 s[i] -= 32; 00162 }

int syscall char *  s  ) 
 

Definition at line 176 of file utils.cpp.

00177 { 00178 int r; 00179 00180 // printf("\nmaking syscall('%s')<br>\n",s); 00181 // fflush(out); 00182 00183 // strcat(s," > /dev/null 2>/dev/null"); 00184 r = system(s); 00185 00186 // if (r) 00187 // printf("System call '%s' returned status %d<br>\n",s,r); 00188 return r; 00189 }

void textareain char *  s  ) 
 

Definition at line 210 of file utils.cpp.

References out.

00211 { 00212 char *s2 = new char[strlen(s) + 1440]; 00213 int i; 00214 fflush(out); 00215 00216 *s2 = 0; 00217 for (i = 0; i < (int)strlen(s); i++) 00218 { 00219 switch ( (unsigned char)s[i]) 00220 { 00221 case 0xe5: 00222 strcat(s2,"&aring;"); 00223 break; 00224 case 0xe4: 00225 strcat(s2,"&auml;"); 00226 break; 00227 case 0xf6: 00228 strcat(s2,"&ouml;"); 00229 break; 00230 case 0xc5: 00231 strcat(s2,"&Aring;"); 00232 break; 00233 case 0xc4: 00234 strcat(s2,"&Auml;"); 00235 break; 00236 case 0xd6: 00237 strcat(s2,"&Ouml;"); 00238 break; 00239 default: 00240 sprintf(s2 + strlen(s2),"%c",s[i]); 00241 } 00242 } 00243 strcpy(s,s2); 00244 /* 00245 char *slask = new char[strlen(s) * 2 + 144]; 00246 int i,cr = 0; 00247 00248 *slask = 0; 00249 for (i = 0; i < (int)strlen(s); i++) 00250 if ((s[i] == 13 || s[i] == 10) && !cr) 00251 { 00252 sprintf(slask + strlen(slask),"<br>"); 00253 cr++; 00254 } else 00255 { 00256 sprintf(slask + strlen(slask),"%c",s[i]); 00257 cr = 0; 00258 } 00259 strcpy(s,slask); 00260 delete slask; 00261 */ 00262 }

void unpack_cookie Cookies *  cs  ) 
 

Definition at line 377 of file utils.cpp.

Referenced by run().

00378 { 00379 char slask[10000]; 00380 if (cs && cs -> getvalue("fdc",slask,10000)) 00381 { 00382 cgi::Parse pa(slask,":"); 00383 char key[100]; 00384 char value[1000]; 00385 cgi::Base64 b('@'); 00386 pa.getword(key); 00387 pa.getword(value); 00388 while (*key) 00389 { 00390 b.decode_to_buffer(value, slask, 10000); 00391 if (!strcmp(slask," null")) 00392 *slask = 0; 00393 cs -> replacevalue(key, slask); 00394 // 00395 pa.getword(key); 00396 pa.getword(value); 00397 } 00398 } 00399 }

void zap char *  s,
char  c
 

Definition at line 38 of file utils.cpp.

00039 { 00040 int i; 00041 00042 for (i = 0; i < (int)strlen(s); i++) 00043 if (s[i] == c) 00044 s[i] = 0; 00045 }


Generated on Thu Feb 10 22:42:39 2005 for Distributed URL Classification Tool by doxygen 1.3.7