00001
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef NL_COMMON_H
00025 #define NL_COMMON_H
00026
00027 #include "types_nl.h"
00028
00029 #include <cctype>
00030 #include <cstdio>
00031 #include <cmath>
00032 #include <cstring>
00033 #include <string>
00034 #include <vector>
00035 #include <cfloat>
00036 #include <cstdarg>
00037 #include <cstdlib>
00038 #include <algorithm>
00039
00040 #ifdef NL_OS_WINDOWS
00041 # include <process.h>
00042 # include <intrin.h>
00043 #else
00044 # include <cmath>
00045 # include <unistd.h>
00046 # include <sys/types.h>
00047 #endif
00048
00049 #include "string_common.h"
00050
00051
00053 namespace NLMISC
00054 {
00055
00058 #ifdef NL_CPU_INTEL
00059
00060 inline uint64 rdtsc()
00061 {
00062 uint64 ticks;
00063 # ifdef NL_OS_WINDOWS
00064 # ifdef NL_NO_ASM
00065 ticks = uint64(__rdtsc());
00066 # else
00067
00068 __asm rdtsc
00069 __asm mov DWORD PTR [ticks], eax
00070 __asm mov DWORD PTR [ticks + 4], edx
00071 # endif // NL_NO_ASM
00072 # else
00073 __asm__ volatile(".byte 0x0f, 0x31" : "=a" (ticks.low), "=d" (ticks.high));
00074 # endif // NL_OS_WINDOWS
00075 return ticks;
00076 }
00077
00078 #endif // NL_CPU_INTEL
00079
00080
00083 #define breakable \
00084 switch(1) case 1: default:
00085
00086
00089 const double Pi = 3.1415926535897932384626433832795;
00090
00091
00092
00093 #define sizeofarray(v) (sizeof(v) / sizeof((v)[0]))
00094
00097 inline float frand(float mod)
00098 {
00099 double r = (double) rand();
00100 r/= (double) RAND_MAX;
00101 return (float)(r * mod);
00102 }
00103
00104
00107 inline sint fsgn(double f)
00108 {
00109 if(f<0)
00110 return -1;
00111 else if(f>0)
00112 return 1;
00113 else
00114 return 0;
00115 }
00116
00117
00120 template<class T> inline T sqr(const T &v)
00121 {
00122 return v * v;
00123 }
00124
00125
00128 template<class T, class U, class V> inline void clamp(T &v, const U &min, const V &max)
00129 {
00130 v = (v < min) ? min : v;
00131 v = (v > max) ? max : v;
00132 }
00133
00134
00137 template<class T> inline T minof(const T& a, const T& b, const T& c)
00138 {return std::min(std::min(a,b),c);}
00139 template<class T> inline T minof(const T& a, const T& b, const T& c, const T& d)
00140 {return std::min(minof(a,b,c),d);}
00141 template<class T> inline T minof(const T& a, const T& b, const T& c, const T& d, const T& e)
00142 {return std::min(minof(a,b,c,d),e);}
00143 template<class T> inline T maxof(const T& a, const T& b, const T& c)
00144 {return std::max(std::max(a,b),c);}
00145 template<class T> inline T maxof(const T& a, const T& b, const T& c, const T& d)
00146 {return std::max(maxof(a,b,c),d);}
00147 template<class T> inline T maxof(const T& a, const T& b, const T& c, const T& d, const T& e)
00148 {return std::max(maxof(a,b,c,d),e);}
00149
00154 template<class T> inline void contReset (T& a)
00155 {
00156 a.~T();
00157 new (&a) T;
00158 }
00159
00165 uint raiseToNextPowerOf2 (uint v);
00166
00172 uint getPowerOf2 (uint v);
00173
00176 bool isPowerOf2 (sint32 v);
00177
00178
00181 inline float degToRad( float deg )
00182 {
00183 return deg * (float)Pi / 180.0f;
00184 }
00185
00186
00189 inline float radToDeg( float rad )
00190 {
00191 return rad * 180.0f / (float)Pi;
00192 }
00193
00194
00197 inline double isValidDouble (double v)
00198 {
00199 #ifdef NL_OS_WINDOWS
00200 return _finite(v) && !_isnan(v);
00201 #else
00202 return !std::isnan(v) && !std::isinf(v);
00203 #endif
00204 }
00205
00206
00211 std::string toLower ( const std::string &str );
00212 void toLower ( char *str );
00213 char toLower ( const char ch );
00214
00219 std::string toUpper ( const std::string &str);
00220 void toUpper ( char *str);
00221
00222
00223 template <class T> T trim (const T &str)
00224 {
00225 typename T::size_type start = 0;
00226 const typename T::size_type size = str.size();
00227 while (start < size && str[start] <= 32)
00228 start++;
00229 typename T::size_type end = size;
00230 while (end > start && str[end-1] <= 32)
00231 end--;
00232 return str.substr (start, end-start);
00233 }
00234
00235
00236 template <class T> T trimRightWhiteSpaces (const T &str)
00237 {
00238 typename T::size_type end = str.size();
00239 while (end > 0 && str[end-1] == ' ')
00240 end--;
00241 return str.substr (0, end);
00242 }
00243
00245
00247 inline std::string &strlwr ( std::string &str ) { str = toLower(str); return str; }
00248 inline std::string strlwr ( const std::string &str ) { return toLower(str); }
00249 inline char *strlwr ( char *str ) { toLower(str); return str; }
00250 inline std::string &strupr ( std::string &str ) { str = toUpper(str); return str; }
00251 inline std::string strupr ( const std::string &str ) { return toUpper(str); }
00252 inline char *strupr ( char *str ) { toUpper(str); return str; }
00253
00254
00261 #ifndef NL_OS_WINDOWS
00262 inline int stricmp(const char *lhs, const char *rhs) { return strcasecmp(lhs, rhs); }
00263 inline int strnicmp(const char *lhs, const char *rhs, size_t n) { return strncasecmp(lhs, rhs, n); }
00264 #endif
00265
00266 inline sint nlstricmp(const char *lhs, const char *rhs) { return stricmp(lhs, rhs); }
00267 inline sint nlstricmp(const std::string &lhs, const std::string &rhs) { return stricmp(lhs.c_str(), rhs.c_str()); }
00268 inline sint nlstricmp(const std::string &lhs, const char *rhs) { return stricmp(lhs.c_str(),rhs); }
00269 inline sint nlstricmp(const char *lhs, const std::string &rhs) { return stricmp(lhs,rhs.c_str()); }
00270
00273 int nlfseek64( FILE *stream, sint64 offset, int origin );
00274
00275
00276 sint64 nlftell64(FILE *stream);
00277
00282 class Exception : public std::exception
00283 {
00284 protected:
00285 std::string _Reason;
00286 public:
00287 Exception();
00288 Exception(const std::string &reason);
00289 Exception(const char *format, ...);
00290 virtual ~Exception() throw() {}
00291 virtual const char *what() const throw();
00292 };
00293
00294
00299 void nlSleep( uint32 ms );
00300
00301
00303 #ifdef NL_OS_WINDOWS
00304 # define getpid _getpid
00305 #endif
00306
00308 uint getThreadId();
00309
00311 std::string stringFromVector( const std::vector<uint8>& v, bool limited = true );
00312
00313
00315 sint64 atoiInt64 (const char *ident, sint64 base = 10);
00316
00318 void itoaInt64 (sint64 number, char *str, sint64 base = 10);
00319
00320
00322 std::string bytesToHumanReadable (const std::string &bytes);
00323 std::string bytesToHumanReadable (uint64 bytes);
00324
00326 uint32 humanReadableToBytes (const std::string &str);
00327
00329 std::string secondsToHumanReadable (uint32 time);
00330
00331
00333 uint32 fromHumanReadable (const std::string &str);
00334
00335
00338 bool launchProgram (const std::string &programName, const std::string &arguments);
00339
00341 bool killProgram(uint32 pid);
00342
00344 bool abortProgram(uint32 pid);
00345
00350
00351
00352
00353
00354
00355
00356
00357
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00401 template <class T> void explode (const T &src, const T &sep, std::vector<T> &res, bool skipEmpty = false)
00402 {
00403 std::string::size_type oldpos = 0, pos;
00404
00405 res.clear ();
00406
00407 do
00408 {
00409 pos = src.find (sep, oldpos);
00410 T s;
00411 if(pos == std::string::npos)
00412 s = src.substr (oldpos);
00413 else
00414 s = src.substr (oldpos, (pos-oldpos));
00415
00416 if (!skipEmpty || !s.empty())
00417 res.push_back (s);
00418
00419 oldpos = pos+sep.size();
00420 }
00421 while(pos != std::string::npos);
00422 }
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599 class CLog;
00600
00602 void displayByteBits( uint8 b, uint nbits, sint beginpos, bool displayBegin, NLMISC::CLog *log );
00603
00605 void displayDwordBits( uint32 b, uint nbits, sint beginpos, bool displayBegin, NLMISC::CLog *log );
00606
00609 #ifdef NL_OS_WINDOWS
00610 inline int nlisprint(int c)
00611 {
00612 if(c>255||c<0) return 0;
00613 return isprint(c);
00614 }
00615 #else
00616 #define nlisprint isprint
00617 #endif
00618
00619
00620 bool openURL (const char *url);
00621
00622
00623 bool openDoc (const char *document);
00624
00625
00626 inline float favoid0(float x)
00627 {
00628 if(x==0) return 0.00001f;
00629 return x;
00630 }
00631 inline double davoid0(double x)
00632 {
00633 if(x==0) return 0.00001;
00634 return x;
00635 }
00636
00637 template<class T>
00638 inline T iavoid0(T x)
00639 {
00640 if(x==0) return 1;
00641 return x;
00642 }
00643
00644
00645 }
00646
00647 #endif // NL_COMMON_H