1 // ------------------------------------------------
7 // (c) 2002 peercast.org
8 // ------------------------------------------------
9 // This program is free software; you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation; either version 2 of the License, or
12 // (at your option) any later version.
14 // This program is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
18 // ------------------------------------------------
26 #define RAND(a,b) (((a = 36969 * (a & 65535) + (a >> 16)) << 16) + \
27 (b = 18000 * (b & 65535) + (b >> 16)) )
28 extern char *stristr(const char *s1, const char *s2);
29 extern char *trimstr(char *s);
32 #define MAX_CGI_LEN 1024
33 // ------------------------------------
59 String(const char *p, TYPE t=T_ASCII)
64 // set from straight null terminated string
65 void set(const char *p, TYPE t=T_ASCII)
67 strncpy(data,p,MAX_LEN-1);
72 // set from quoted or unquoted null terminated string
73 void setFromString(const char *str, TYPE t=T_ASCII);
76 void setFromStopwatch(unsigned int t);
79 void setFromTime(unsigned int t);
82 // from single word (end at whitespace)
83 void setFromWord(const char *str)
86 for(i=0; i<MAX_LEN-1; i++)
89 if ((data[i]==0) || (data[i]==' '))
96 // set from null terminated string, remove first/last chars
97 void setUnquote(const char *p, TYPE t=T_ASCII)
102 if (slen >= MAX_LEN) slen = MAX_LEN;
103 strncpy(data,p+1,slen-2);
115 void ASCII2ESC(const char *,bool);
116 void ASCII2HTML(const char *);
117 void ASCII2META(const char *,bool);
118 void ESC2ASCII(const char *);
119 void HTML2ASCII(const char *);
120 void HTML2UNICODE(const char *);
121 void BASE642ASCII(const char *);
122 void UNKNOWN2UNICODE(const char *,bool);
124 void ASCII2SJIS(const char *); //JP-EX
127 static int base64WordToChars(char *,const char *);
129 static bool isSame(const char *s1, const char *s2) {return strcmp(s1,s2)==0;}
131 bool startsWith(const char *s) const {return strncmp(data,s,strlen(s))==0;}
133 bool isEmpty() {return data[0]==0;}
134 bool isSame(::String &s) const {return strcmp(data,s.data)==0;}
135 bool isSame(const char *s) const {return strcmp(data,s)==0;}
136 bool contains(::String &s) {return stristr(data,s.data)!=NULL;}
137 bool contains(const char *s) {return stristr(data,s)!=NULL;}
138 void append(const char *s)
140 if ((strlen(s)+strlen(data) < (MAX_LEN-1)))
151 void prepend(const char *s)
160 bool operator == (const char *s) const {return isSame(s);}
161 bool operator != (const char *s) const {return !isSame(s);}
163 operator const char *() const {return data;}
165 void convertTo(TYPE t);
167 char *cstr() {return data;}
169 static bool isWhitespace(char c) {return c==' ' || c=='\t';}
175 // ------------------------------------
179 Random(int s=0x14235465)
186 return RAND(a[0],a[1]);
197 // ------------------------------------
205 virtual class ClientSocket *createSocket() = 0;
206 virtual bool startThread(class ThreadInfo *) = 0;
207 virtual void sleep(int) = 0;
208 virtual void appMsg(long,long = 0) = 0;
209 virtual unsigned int getTime() = 0;
210 virtual double getDTime() = 0;
211 virtual unsigned int rnd() = 0;
212 virtual void getURL(const char *) = 0;
213 virtual void exit() = 0;
214 virtual bool hasGUI() = 0;
215 virtual void callLocalURL(const char *,int)=0;
216 virtual void executeFile(const char *) = 0;
217 virtual void endThread(ThreadInfo *) {}
218 virtual void waitThread(ThreadInfo *, int timeout = 30000) {}
222 #ifdef __BIG_ENDIAN__
223 unsigned short convertEndian(unsigned short v) { return SWAP2(v); }
224 unsigned int convertEndian(unsigned int v) { return SWAP4(v); }
226 unsigned short convertEndian(unsigned short v) { return v; }
227 unsigned int convertEndian(unsigned int v) { return v; }
233 unsigned int idleSleepTime;
234 unsigned int rndSeed;
235 unsigned int numThreads;
237 class LogBuffer *logBuf;
244 typedef __int64 int64_t;
247 // ------------------------------------
253 event = ::CreateEvent(NULL, // no security attributes
254 TRUE, // manual-reset
255 FALSE,// initially non-signaled
261 ::CloseHandle(event);
269 void wait(int timeout = 30000)
271 switch(::WaitForSingleObject(event, timeout))
274 throw TimeoutException();
276 //case WAIT_OBJECT_0:
293 // ------------------------------------
294 typedef int (WINAPI *THREAD_FUNC)(ThreadInfo *);
295 typedef unsigned int THREAD_HANDLE;
296 #define THREAD_PROC int WINAPI
297 #define vsnprintf _vsnprintf
299 // ------------------------------------
305 InitializeCriticalSection(&cs);
311 EnterCriticalSection(&cs);
316 LeaveCriticalSection(&cs);
325 // ------------------------------------
331 #define _BIG_ENDIAN 1
334 typedef long long int64_t;
336 typedef int (*THREAD_FUNC)(ThreadInfo *);
337 #define THREAD_PROC int
338 typedef pthread_t THREAD_HANDLE;
340 // ------------------------------------
341 #define stricmp strcasecmp
342 #define strnicmp strncasecmp
345 // ------------------------------------
358 void wait(int timeout = 30000)
368 // ------------------------------------
372 pthread_mutex_t mutex;
376 const pthread_mutexattr_t mattr =
379 PTHREAD_MUTEX_RECURSIVE
381 PTHREAD_MUTEX_RECURSIVE_NP
385 pthread_mutex_init( &mutex, &mattr );
390 pthread_mutex_destroy( &mutex );
396 pthread_mutex_lock(&mutex);
401 pthread_mutex_unlock(&mutex);
413 WLockBlock(WLock *l){
420 LOG_DEBUG("LOCK OFF by destructor");
433 // ------------------------------------
437 //typedef int (__stdcall *THREAD_FUNC)(ThreadInfo *);
450 volatile bool active;
451 volatile bool finish;
454 THREAD_HANDLE handle;
461 // ------------------------------------
474 LogBuffer(int i, int l)
479 buf = new char[lineLen*maxLines];
480 times = new unsigned int [maxLines];
481 types = new TYPE [maxLines];
488 void write(const char *, TYPE);
489 static const char *getTypeStr(TYPE t) {return logTypes[t];}
490 void dumpHTML(class Stream &);
494 unsigned int currLine,maxLines,lineLen;
497 static const char *logTypes[];
501 #define RWLOCK_READ_MAX 32
506 LockBlock(WLock &l){ flg = false; lock = l; }
507 ~LockBlock(){ if (flg) lock.off(); }
508 void lockon(){ flg = true; lock.on(); }
509 void lockoff(){ flg = false; lock.off(); }
516 // ------------------------------------
519 // ------------------------------------
523 #define CHECK_ENDIAN2(v) v=SWAP2(v)
524 #define CHECK_ENDIAN3(v) v=SWAP3(v)
525 #define CHECK_ENDIAN4(v) v=SWAP4(v)
527 #define CHECK_ENDIAN2
528 #define CHECK_ENDIAN3
529 #define CHECK_ENDIAN4
533 // ------------------------------------