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)
99 size_t slen = strlen(p);
102 if (slen >= MAX_LEN) slen = MAX_LEN;
103 strncpy(data,p+1,slen-2);
112 memset(data, 0, MAX_LEN);
116 void ASCII2ESC(const char *,bool);
117 void ASCII2HTML(const char *);
118 void ASCII2META(const char *,bool);
119 void ESC2ASCII(const char *);
120 void HTML2ASCII(const char *);
121 void HTML2UNICODE(const char *);
122 void BASE642ASCII(const char *);
123 void UNKNOWN2UNICODE(const char *,bool);
125 void ASCII2SJIS(const char *); //JP-EX
128 static int base64WordToChars(char *,const char *);
130 static bool isSame(const char *s1, const char *s2) {return strcmp(s1,s2)==0;}
132 bool startsWith(const char *s) const {return strncmp(data,s,strlen(s))==0;}
134 bool isEmpty() {return data[0]==0;}
135 bool isSame(::String &s) const {return strcmp(data,s.data)==0;}
136 bool isSame(const char *s) const {return strcmp(data,s)==0;}
137 bool contains(::String &s) {return stristr(data,s.data)!=NULL;}
138 bool contains(const char *s) {return stristr(data,s)!=NULL;}
139 void append(const char *s)
141 if ((strlen(s)+strlen(data) < (MAX_LEN-1)))
152 void prepend(const char *s)
161 bool operator == (const char *s) const {return isSame(s);}
162 bool operator != (const char *s) const {return !isSame(s);}
164 operator const char *() const {return data;}
166 void convertTo(TYPE t);
168 char *cstr() {return data;}
170 static bool isWhitespace(char c) {return c==' ' || c=='\t';}
176 // ------------------------------------
180 Random(int s=0x14235465)
187 return RAND(a[0],a[1]);
198 // ------------------------------------
206 virtual class ClientSocket *createSocket() = 0;
207 virtual bool startThread(class ThreadInfo *) = 0;
208 virtual void sleep(int) = 0;
209 virtual void appMsg(long,long = 0) = 0;
210 virtual unsigned int getTime() = 0;
211 virtual double getDTime() = 0;
212 virtual unsigned int rnd() = 0;
213 virtual void getURL(const char *) = 0;
214 virtual void exit() = 0;
215 virtual bool hasGUI() = 0;
216 virtual void callLocalURL(const char *,int)=0;
217 virtual void executeFile(const char *) = 0;
218 virtual void endThread(ThreadInfo *) {}
219 virtual void waitThread(ThreadInfo *, int timeout = 30000) {}
223 #ifdef __BIG_ENDIAN__
224 unsigned short convertEndian(unsigned short v) { return SWAP2(v); }
225 unsigned int convertEndian(unsigned int v) { return SWAP4(v); }
227 unsigned short convertEndian(unsigned short v) { return v; }
228 unsigned int convertEndian(unsigned int v) { return v; }
234 unsigned int idleSleepTime;
235 unsigned int rndSeed;
236 unsigned int numThreads;
238 class LogBuffer *logBuf;
245 typedef __int64 int64_t;
247 #ifndef _UINTPTR_T_DEFINED
248 typedef unsigned int uintptr_t;
251 // ------------------------------------
257 event = ::CreateEvent(NULL, // no security attributes
258 TRUE, // manual-reset
259 FALSE,// initially non-signaled
265 ::CloseHandle(event);
273 void wait(int timeout = 30000)
275 switch(::WaitForSingleObject(event, timeout))
278 throw TimeoutException();
280 //case WAIT_OBJECT_0:
297 // ------------------------------------
298 typedef int (WINAPI *THREAD_FUNC)(ThreadInfo *);
299 typedef uintptr_t THREAD_HANDLE;
300 #define THREAD_PROC int WINAPI
301 #define vsnprintf _vsnprintf
303 // ------------------------------------
309 InitializeCriticalSection(&cs);
315 EnterCriticalSection(&cs);
320 LeaveCriticalSection(&cs);
329 // ------------------------------------
335 #define _BIG_ENDIAN 1
338 typedef long long int64_t;
340 typedef int (*THREAD_FUNC)(ThreadInfo *);
341 #define THREAD_PROC int
342 typedef pthread_t THREAD_HANDLE;
344 // ------------------------------------
345 #define stricmp strcasecmp
346 #define strnicmp strncasecmp
349 // ------------------------------------
362 void wait(int timeout = 30000)
372 // ------------------------------------
376 pthread_mutex_t mutex;
380 const pthread_mutexattr_t mattr =
383 PTHREAD_MUTEX_RECURSIVE
385 PTHREAD_MUTEX_RECURSIVE_NP
389 pthread_mutex_init( &mutex, &mattr );
394 pthread_mutex_destroy( &mutex );
400 pthread_mutex_lock(&mutex);
405 pthread_mutex_unlock(&mutex);
417 WLockBlock(WLock *l){
424 LOG_DEBUG("LOCK OFF by destructor");
437 // ------------------------------------
441 //typedef int (__stdcall *THREAD_FUNC)(ThreadInfo *);
454 volatile bool active;
455 volatile bool finish;
458 THREAD_HANDLE handle;
465 // ------------------------------------
478 LogBuffer(int i, int l)
483 buf = new char[lineLen*maxLines];
484 times = new unsigned int [maxLines];
485 types = new TYPE [maxLines];
492 void write(const char *, TYPE);
493 static const char *getTypeStr(TYPE t) {return logTypes[t];}
494 void dumpHTML(class Stream &);
498 unsigned int currLine,maxLines,lineLen;
501 static const char *logTypes[];
505 #define RWLOCK_READ_MAX 32
510 LockBlock(WLock &l){ flg = false; lock = l; }
511 ~LockBlock(){ if (flg) lock.off(); }
512 void lockon(){ flg = true; lock.on(); }
513 void lockoff(){ flg = false; lock.off(); }
520 // ------------------------------------
523 // ------------------------------------
527 #define CHECK_ENDIAN2(v) v=SWAP2(v)
528 #define CHECK_ENDIAN3(v) v=SWAP3(v)
529 #define CHECK_ENDIAN4(v) v=SWAP4(v)
531 #define CHECK_ENDIAN2
532 #define CHECK_ENDIAN3
533 #define CHECK_ENDIAN4
537 // ------------------------------------