00001
00002
00003
00004
00005
00006
00007
00008
00009
00019 #ifndef __COMMON_LIBS_H__
00020 #define __COMMON_LIBS_H__
00021
00022 #if !defined(WIN32) && (__GNUC__ == 3) && ( (__GNUC_MINOR__ == 1) || ( __GNUC_MINOR__ == 2 && __GNUC_PATCHLEVEL__ < 2 ) )
00023 #warning The use of GCC 3 with NoX-Wizard is very very dangerous. \
00024 GCC3.2.2+ of Debian GNU/Linux seems to be ok.
00025 #endif
00026
00027 #ifdef __BORLANDC__
00028 #define NDEBUG
00029 #define WIN32
00030 #ifdef __CONSOLE__
00031 #define _CONSOLE
00032 #else
00033 #define _WINDOWS
00034 #endif
00035 #endif
00036
00037 #if defined WIN32 || defined _WIN32
00038 #ifndef _WIN32
00039 #define _WIN32
00040 #endif
00041 #pragma pack(1)
00042
00043 #define snprintf _snprintf
00044 typedef int socklen_t;
00045 #endif
00046
00047 #ifndef __GNUC__
00048 #ifdef _MSC_VER
00049 #define strncasecmp strncmp
00050
00051 #else
00052 #define strncasecmp strncmpi
00053
00054 #endif
00055 #define strcasecmp strcmpi
00056 #endif
00057
00058 #if defined(__BEOS__) && !defined(__unix__)
00059 #define __unix__
00060 #endif
00061
00062 #ifdef __GNUC__
00063 #define PACK_NEEDED __attribute__ ((packed))
00064 #else
00065 #define PACK_NEEDED
00066 #endif
00067
00068 #ifdef _MSC_VER
00069 #pragma pack(push,8) //for Visual C++ using STLport
00070 #pragma warning(disable: 4786) //Gets rid of BAD stl warnings
00071 #pragma warning(disable: 4800) //needed couse now we can see the real warning
00072 #pragma warning(disable: 4127) //needed couse now we can see the real warning
00073 #pragma warning(disable: 4100) // don't warn anymore about unreferenced params
00074 #endif
00075 #ifdef __BORLANDC__
00076 #pragma warn -pck //Gets rid of BAD stl warnings
00077 #pragma warn -pch
00078 #pragma warn -par
00079 #pragma warn -sig
00080 #pragma warn -rch
00081 #endif
00082 #include <cstdio>
00083 #include <cstdlib>
00084 #include <ctime>
00085 #include <cmath>
00086 #include <string>
00087 #include <vector>
00088
00089 #ifdef __GNUC__
00090 #if (__GNUC__ == 2)
00091 #include <slist.h>
00092 #else
00093 #include <ext/slist>
00094 using __gnu_cxx::slist;
00095 #endif
00096 #else
00097 #include <slist>
00098 #endif
00099
00100 #include <map>
00101 #include <queue>
00102 #include <iterator>
00103 #include <algorithm>
00104 #include <stack>
00105 #include <fstream>
00106 #include <utility>
00107 #include <cstring>
00108 #include <sys/stat.h>
00109 #include <sys/types.h>
00110 #include <sys/timeb.h>
00111 #include <ctype.h>
00112
00113 #ifdef WIN32
00114 #include <winsock.h>
00115 #include <winbase.h>
00116 #include <io.h>
00117 #include <dos.h>
00118 #include <limits.h>
00119 #include <conio.h>
00120 #include <process.h>
00121 #endif
00122
00123 #ifdef __unix__
00124 #include <netinet/in.h>
00125 #include <sys/socket.h>
00126 #include <sys/types.h>
00127 #include <sys/time.h>
00128 #include <netdb.h>
00129 #include <unistd.h>
00130 #include <termios.h>
00131 #include <fcntl.h>
00132 #include <unistd.h>
00133 #include <sys/ioctl.h>
00134 #include <libgen.h>
00135
00136 #define ioctlsocket ioctl
00137
00138 #ifndef __BEOS__
00139 #include <signal.h>
00140 #include <sys/errno.h>
00141 #include <arpa/inet.h>
00142 #define closesocket(s) close(s)
00143 #else
00144 #include <be/NetKit.h>
00145 #include <be/NetworkKit.h>
00146 #include <be/net/socket.h>
00147 typedef int socklen_t;
00148 #endif
00149 #include <sys/utsname.h>
00150 #endif
00151
00152
00153 #ifdef _MSC_VER
00154 #pragma pack(pop)
00155 #ifndef STLPORT
00156 #pragma warning(disable: 4103)
00157 #pragma warning(disable: 4786)
00158 #define vsnprintf _vsnprintf
00159 #endif
00160 #endif
00161
00162 #ifdef __unix__
00163 typedef unsigned char BYTE ;
00164 #define SOCKET_ERROR -1
00165 char *strlwr(char *);
00166 char *strupr(char *);
00167 #endif
00168
00169 #if !defined WIN32 && !defined _WIN32
00170 void Sleep(unsigned long msec);
00171 #endif
00172
00173 #ifdef __BEOS__
00174 extern int errno;
00175 #endif
00176
00177 extern char* getOSVersionString();
00178 enum OSVersion { OSVER_UNKNOWN, OSVER_WIN9X, OSVER_WINNT, OSVER_NONWINDOWS };
00179 extern OSVersion getOSVersion();
00180
00181 using namespace std;
00182
00183 #ifdef _MSC_VER
00184 #pragma warning(disable: 4018)
00185 #endif
00186
00187 #ifndef MSG_NOSIGNAL
00188 #define MSG_NOSIGNAL 0
00189 #endif
00190
00191 #ifdef USE_THREADS
00192
00193
00194
00195
00196
00197
00198
00199
00200 #if defined(__OpenBSD__) || defined(__FreeBSD__)
00201 #include <pthread.h>
00202 #endif
00203
00204 #if defined __unix__ && !defined __BEOS__
00205 #define PTHREADS
00206 #endif
00207
00208 #if !defined WIN32 && !defined PTHREADS
00209 #error Your platform is not supported by Trivial Threads :[
00210 #endif
00211
00212 namespace tthreads {
00213
00214 class Mutex {
00215 private:
00216 bool m_bLocked;
00217 bool m_bDebug;
00218 char* m_szMutexName;
00219 #ifdef WIN32
00220 CRITICAL_SECTION m_cs;
00221 CRITICAL_SECTION m_cs2;
00222 #elif defined PTHREADS
00223 pthread_mutex_t* m_mutex;
00224 #endif
00225 inline void init(bool alreadylocked) {
00226 #ifdef WIN32
00227 InitializeCriticalSection(&m_cs);
00228 InitializeCriticalSection(&m_cs2);
00229 #elif defined PTHREADS
00230 m_mutex = (pthread_mutex_t *) malloc (sizeof (pthread_mutex_t));
00231 pthread_mutex_init (m_mutex, NULL);
00232 #endif
00233 m_bLocked = false;
00234 m_bDebug = false;
00235 if (alreadylocked) enter();
00236 }
00237 public:
00238 inline Mutex(bool alreadylocked = false) { init(alreadylocked); }
00239 inline Mutex(char *name, bool alreadylocked = false) { init(alreadylocked); setDebugMode(name); }
00240 inline ~Mutex() {
00241 if (m_bDebug) {
00242 printf("DBG-MUTEX: Mutex %s destroyed\n", m_szMutexName);
00243 if (m_szMutexName!=NULL) delete[] m_szMutexName;
00244 }
00245 #ifdef WIN32
00246 DeleteCriticalSection(&m_cs);
00247 DeleteCriticalSection(&m_cs2);
00248 #elif defined PTHREADS
00249 pthread_mutex_destroy (m_mutex);
00250 free(m_mutex);
00251 #endif
00252 }
00253 inline void enter () {
00254 if (m_bDebug) printf("DBG-MUTEX: Entering mutex %s\n", m_szMutexName);
00255 #ifdef WIN32
00256 EnterCriticalSection(&m_cs2);
00257 EnterCriticalSection(&m_cs);
00258 #elif defined PTHREADS
00259 pthread_mutex_lock(m_mutex);
00260 #endif
00261 m_bLocked = true;
00262 #ifdef WIN32
00263 LeaveCriticalSection(&m_cs2);
00264 #endif
00265 if (m_bDebug) printf("DBG-MUTEX: Entered mutex %s\n", m_szMutexName);
00266 }
00267 inline void leave () {
00268 if (m_bDebug) printf("DBG-MUTEX: Leaving mutex %s\n", m_szMutexName);
00269 m_bLocked = false;
00270 #ifdef WIN32
00271 LeaveCriticalSection(&m_cs);
00272 #elif defined PTHREADS
00273 pthread_mutex_unlock(m_mutex);
00274 #endif
00275 }
00276 inline bool tryEnter () {
00277 if (m_bDebug) printf("DBG-MUTEX: Try-Locking mutex %s\n", m_szMutexName);
00278 #ifdef WIN32
00279
00280
00281 EnterCriticalSection(&m_cs2);
00282
00283 if (m_bLocked) {
00284 LeaveCriticalSection(&m_cs2);
00285 return false;
00286 }
00287
00288 enter();
00289 LeaveCriticalSection(&m_cs2);
00290 return true;
00291 #elif defined PTHREADS
00292 return pthread_mutex_trylock(m_mutex)==0;
00293 #endif
00294 }
00295 void setDebugMode (char *name) { m_bDebug = false; m_szMutexName = new char[strlen(name)+2]; strcpy(m_szMutexName, name);}
00296 };
00297
00298
00299
00300
00301 template <typename T> class Atomic {
00302 protected:
00303 Mutex m_mutex;
00304 volatile T m_val;
00305 inline void enterIfEq(const T& compval) { while(true) { m_mutex.enter(); if (m_val==compval) return; m_mutex.leave(); Sleep(1); } }
00306 inline void enterIfNEq(const T& compval) { while(true) { m_mutex.enter(); if (m_val!=compval) return; m_mutex.leave(); Sleep(1); } }
00307 public:
00308 inline T setIfEq (const T& newval, const T& compval = 0) { enterIfEq(compval); T old = m_val; m_val = newval; m_mutex.leave(); return old;}
00309 inline T setIfNEq (const T& newval, const T& compval = 0) { enterIfNEq(compval); T old = m_val; m_val = newval; m_mutex.leave(); return old;}
00310 inline T decIfEq (const T& newval = 1, const T& compval = 0) { enterIfEq(compval); T old = m_val; m_val -= newval; m_mutex.leave(); return old;}
00311 inline T decIfNEq (const T& newval = 1, const T& compval = 0) { enterIfNEq(compval); T old = m_val; m_val -= newval; m_mutex.leave(); return old;}
00312 inline T incIfEq (const T& newval = 1, const T& compval = 0) { return dec (-newval, compval); }
00313 inline T incIfNEq (const T& newval = 1, const T& compval = 0) { return dec (-newval, compval); }
00314 inline T set (const T& newval) { m_mutex.enter(); T old = m_val; m_val = newval; m_mutex.leave(); return old;}
00315 inline T dec (const T& newval = 1) { m_mutex.enter(); T old = m_val; m_val -= newval; m_mutex.leave(); return old;}
00316 inline T inc (const T& newval = 1) { return dec(-newval); }
00317
00318 inline Atomic<T>& operator++() { inc(); return *this;}
00319 inline Atomic<T>& operator--() { dec(); return *this;}
00320 inline Atomic<T>& operator+=(const T& x) { inc(x); return *this;}
00321 inline Atomic<T>& operator-=(const T& x) { dec(x); return *this;}
00322 inline Atomic<T>& operator=(const T& newval) { set(newval); return *this;}
00323
00324 inline operator T() { return m_val; }
00325 inline bool operator==(const T& v) { return (m_val==v); }
00326 };
00327
00328
00329 class Semaphore : public Atomic<int> {
00330 public :
00331 Semaphore(int n = 1) { set(n); }
00332 inline void enter() { decIfNEq(); }
00333 inline bool tryEnter() {
00334 m_mutex.enter();
00335 if (m_val!=0) {
00336 m_val--;
00337 m_mutex.leave();
00338 return true;
00339 }
00340 m_mutex.leave();
00341 return false;
00342 }
00343 void exit() { inc(); }
00344 };
00345
00346
00347
00348 #ifdef WIN32
00349 #define TTHREAD void
00350 #define EXIT_TTHREAD { return; }
00351 #elif defined PTHREADS
00352 #define TTHREAD void*
00353 #define EXIT_TTHREAD { return NULL; }
00354 #endif
00355
00356 int startTThread( TTHREAD ( *funk )( void * ), void* param = NULL );
00357
00358 };
00359
00360 #ifdef WIN32
00361 inline bool pollHUPStatus () { return false; }
00362 inline bool pollCloseRequests () { return false; }
00363 inline void setup_signals (){ return; }
00364 inline void start_signal_thread() {return;}
00365 #elif defined PTHREADS
00366 extern bool pollHUPStatus ();
00367 extern bool pollCloseRequests ();
00368 extern void setup_signals ();
00369 extern void start_signal_thread();
00370 #endif
00371
00372 #endif // USE THREADS
00373
00374 extern std::string rtrim ( const string &sourceStr ,const string &whitespace = " \t" );
00375 extern std::wstring rtrim ( const wstring &sourceStr ,const wstring &whitespace=L" \t" );
00376 extern std::string ltrim ( const string &sourceStr ,const string &whitespace=" \t" );
00377 extern std::wstring ltrim ( const std::wstring & sourceStr ,const wstring &whitespace=L" \t" );
00378 extern std::string trim ( const std::string & sourceStr ,const string &whitespace=" \t" );
00379 extern std::wstring trim ( const std::wstring & sourceStr ,const wstring &whitespace=L" \t" );
00380
00381 #endif //__COMMON_LIBS_H__
00382
00383
00384
00385