# Home    # nevrax.com   
Nevrax
Nevrax.org
#News
#Mailing-list
#Documentation
#CVS
#Bugs
#License
Docs
 
Documentation  
Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages   Search  

common.h

Go to the documentation of this file.
00001 
00007 /* Copyright, 2000 Nevrax Ltd.
00008  *
00009  * This file is part of NEVRAX NEL.
00010  * NEVRAX NEL is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2, or (at your option)
00013  * any later version.
00014 
00015  * NEVRAX NEL is distributed in the hope that it will be useful, but
00016  * WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00018  * General Public License for more details.
00019 
00020  * You should have received a copy of the GNU General Public License
00021  * along with NEVRAX NEL; see the file COPYING. If not, write to the
00022  * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00023  * MA 02111-1307, USA.
00024  */
00025 
00026 #ifndef NL_COMMON_H
00027 #define NL_COMMON_H
00028 
00029 #include "nel/misc/types_nl.h"
00030 
00031 #include <ctype.h>
00032 #include <stdio.h>
00033 #include <stdarg.h>
00034 #include <stdlib.h>
00035 #include <math.h>
00036 #include <algorithm>
00037 #include <string>
00038 #include <vector>
00039 #include <sstream>
00040 
00041 #ifdef NL_OS_WINDOWS
00042 #       include <process.h>
00043 #else
00044 #       include <math.h>
00045 #       include <unistd.h>
00046 #       include <sys/types.h>
00047 #endif
00048 
00050 namespace       NLMISC
00051 {
00052 
00053 // Windows posix function remapping
00054 #ifdef NL_OS_WINDOWS
00055 #define vsnprintf _vsnprintf
00056 #endif // NL_OS_WINDOWS
00057 
00058 
00059 
00066 const int MaxCStringSize = 1024*2;
00067 
00068 
00071 const double Pi = 3.1415926535897932384626433832795;
00072 
00073 
00094 #define NLMISC_CONVERT_VARGS(_dest,_format,_size) \
00095 char _cstring[_size]; \
00096 va_list _args; \
00097 va_start (_args, _format); \
00098 int _res = vsnprintf (_cstring, _size-1, _format, _args); \
00099 if (_res == -1 || _res == _size-1) \
00100 { \
00101         _cstring[_size-1] = '\0'; \
00102 } \
00103 va_end (_args); \
00104 _dest = _cstring
00105 
00106 
00107 
00115 sint smprintf( char *buffer, size_t count, const char *format, ... );
00116 
00117 
00120 inline float frand(float mod)
00121 {
00122         double  r = (double) rand();
00123         r/= (double) RAND_MAX;
00124         return (float)(r * mod);
00125 }
00126 
00127 
00130 inline sint fsgn(double f)
00131 {
00132         if(f<0)
00133                 return -1;
00134         else if(f>0)
00135                 return 1;
00136         else
00137                 return 0;
00138 }
00139 
00140 
00143 template<class T>       inline T sqr(const T &v)
00144 {
00145         return v * v;
00146 }
00147 
00148 
00151 template<class T, class U, class V>     inline void clamp(T &v, const U &min, const V &max)
00152 {
00153         v = (v < min) ? min : v;
00154         v = (v > max) ? max : v;
00155 }
00156 
00157 
00160 template<class T>       inline T minof(const T& a,  const T& b,  const T& c)
00161         {return std::min(std::min(a,b),c);}
00162 template<class T>       inline T minof(const T& a,  const T& b,  const T& c,  const T& d)
00163         {return std::min(minof(a,b,c),d);}
00164 template<class T>       inline T minof(const T& a,  const T& b,  const T& c,  const T& d,  const T& e)
00165         {return std::min(minof(a,b,c,d),e);}
00166 template<class T>       inline T maxof(const T& a,  const T& b,  const T& c)
00167         {return std::max(std::max(a,b),c);}
00168 template<class T>       inline T maxof(const T& a,  const T& b,  const T& c,  const T& d)
00169         {return std::max(maxof(a,b,c),d);}
00170 template<class T>       inline T maxof(const T& a,  const T& b,  const T& c,  const T& d,  const T& e)
00171         {return std::max(maxof(a,b,c,d),e);}
00172 
00177 template<class T>       inline void contReset (T& a)
00178 {
00179         a.~T();
00180 #undef new
00181         new (&a) T;
00182 #define new NL_NEW
00183 }
00184 
00190 uint                    raiseToNextPowerOf2 (uint v);
00191 
00197 uint                    getPowerOf2 (uint v);
00198 
00201 bool                    isPowerOf2 (sint32 v);
00202 
00203 
00206 inline float    degToRad( float deg )
00207 {
00208         return deg * (float)Pi / 180.0f;
00209 }
00210 
00211 
00214 inline float    radToDeg( float rad )
00215 {
00216         return rad * 180.0f / (float)Pi;
00217 }
00218 
00219 
00222 inline double   isValidDouble (double v)
00223 {
00224 #ifdef NL_OS_WINDOWS
00225         return _finite(v) && !_isnan(v);
00226 #else
00227         return !isnan(v) && !isinf(v);
00228 #endif
00229 }
00230 
00231 
00235 std::string             &strlwr ( std::string &str );
00236 std::string             strlwr ( const std::string &str );
00237 
00241 char                    *strlwr ( char *str );
00242 
00246 std::string             &strupr ( std::string &str );
00247 
00251 char                    *strupr ( char *str );
00252 
00256 sint                    nlstricmp(const char *lhs, const char *rhs);
00257 
00260 sint                    nlstricmp(const std::string &lhs, const std::string &rhs);
00261 
00262 
00263 
00268 class Exception : public std::exception
00269 {
00270 protected:
00271         std::string     _Reason;
00272 public:
00273         Exception();
00274         Exception(const std::string &reason);
00275         Exception(const char *format, ...);
00276         virtual ~Exception() throw() {};
00277         virtual const char      *what() const throw();
00278 };
00279 
00280 
00285 void nlSleep( uint32 ms );
00286 
00287 
00289 #ifdef NL_OS_WINDOWS
00290 #       define getpid _getpid
00291 #endif
00292 
00294 uint getThreadId();
00295 
00297 std::string stringFromVector( const std::vector<uint8>& v, bool limited = true );
00298 
00299 
00301 sint64 atoiInt64 (const char *ident, sint64 base = 10);
00302 
00304 void itoaInt64 (sint64 number, char *str, sint64 base = 10);
00305 
00306 
00308 std::string bytesToHumanReadable (uint32 bytes);
00309 
00310 
00313 bool launchProgram (const std::string &programName, const std::string &arguments);
00314 
00315 
00316 
00317 
00318 
00323 template<class T> std::string toString (const T &t)
00324 {
00325         std::stringstream ss;
00326         ss << t;
00327         return ss.str();
00328 }
00329 
00330 
00335 inline std::string toString (const char *format, ...)
00336 {
00337         std::string Result;
00338         NLMISC_CONVERT_VARGS (Result, format, NLMISC::MaxCStringSize);
00339         return Result;
00340 }
00341 
00342 
00343 /* All the code above is used to add our types (uint8, ...) in the stringstream (used by the toString() function).
00344  * So we can use stringstream operator << and >> with all NeL simple types (except for ucchar and ucstring)
00345  */
00346 
00347 #ifdef NL_OS_WINDOWS
00348 
00349 #if _MSC_VER < 1300     // visual or older (on visual .NET, we don't need to do that)
00350 
00351 #define NLMISC_ADD_BASIC_ISTREAM_OPERATOR(__type,__casttype) \
00352 template <class _CharT, class _Traits> \
00353 std::basic_istream<_CharT, _Traits>& __cdecl \
00354 operator>>(std::basic_istream<_CharT, _Traits>& __is, __type& __z) \
00355 { \
00356         __casttype __z2 = (__casttype) __z; \
00357         __is.operator>>(__z2); \
00358         __z = (__type) __z2; \
00359         return __is; \
00360 } \
00361  \
00362 template <class _CharT, class _Traits> \
00363 std::basic_ostream<_CharT, _Traits>& __cdecl \
00364 operator<<(std::basic_ostream<_CharT, _Traits>& __os, const __type& __z) \
00365 { \
00366         std::basic_ostringstream<_CharT, _Traits, std::allocator<_CharT> > __tmp; \
00367         __tmp << (__casttype) __z; \
00368         return __os << __tmp.str(); \
00369 }
00370 
00371 NLMISC_ADD_BASIC_ISTREAM_OPERATOR(uint8, unsigned int);
00372 NLMISC_ADD_BASIC_ISTREAM_OPERATOR(sint8, signed int);
00373 NLMISC_ADD_BASIC_ISTREAM_OPERATOR(uint16, unsigned int);
00374 NLMISC_ADD_BASIC_ISTREAM_OPERATOR(sint16, signed int);
00375 NLMISC_ADD_BASIC_ISTREAM_OPERATOR(uint32, unsigned int);
00376 NLMISC_ADD_BASIC_ISTREAM_OPERATOR(sint32, signed int);
00377 
00378 #endif // _MSC_VER < 1300
00379 
00380 
00381 template <class _CharT, class _Traits>
00382 std::basic_istream<_CharT, _Traits>& __cdecl
00383 operator>>(std::basic_istream<_CharT, _Traits>& __is, uint64& __z)
00384 {
00385         __z = 0;
00386         bool neg = false;
00387         char c;
00388         do
00389         {
00390                 __is >> c;
00391         }
00392         while (isspace(c));
00393 
00394         if (c == '-')
00395         {
00396                 neg = true;
00397                 __is >> c;
00398         }
00399 
00400         while (isdigit(c))
00401         {
00402                 __z *= 10;
00403                 __z += c-'0';
00404                 __is >> c;
00405                 if (__is.fail())
00406                         break;
00407         }
00408 
00409         if (neg) __z = 0;
00410 
00411         return __is;
00412 }
00413 
00414 template <class _CharT, class _Traits>
00415 std::basic_ostream<_CharT, _Traits>& __cdecl
00416 operator<<(std::basic_ostream<_CharT, _Traits>& __os, const uint64& __z)
00417 {
00418         std::basic_ostringstream<_CharT, _Traits, std::allocator<_CharT> > __res;
00419 
00420         if (__z == 0)
00421         {
00422                 __res << '0';
00423         }
00424         else
00425         {
00426                 std::basic_ostringstream<_CharT, _Traits, std::allocator<_CharT> > __tmp;
00427                 uint64  __z2 = __z;
00428                 while (__z2 != 0)
00429                 {
00430                         __tmp << (char)((__z2%10)+'0');
00431                         __z2 /= 10;
00432                 }
00433 
00434                 uint __s = __tmp.str().size();
00435                 for (uint i = 0; i < __s; i++)
00436                         __res << __tmp.str()[__s - 1 - i];
00437         }
00438         return __os << __res.str();
00439 }
00440 
00441 template <class _CharT, class _Traits>
00442 std::basic_istream<_CharT, _Traits>& __cdecl
00443 operator>>(std::basic_istream<_CharT, _Traits>& __is, sint64& __z)
00444 {
00445         __z = 0;
00446         bool neg = false;
00447         char c;
00448         do
00449         {
00450                 __is >> c;
00451         }
00452         while (isspace(c));
00453 
00454         if (c == '-')
00455         {
00456                 neg = true;
00457                 __is >> c;
00458         }
00459 
00460         while (isdigit(c))
00461         {
00462                 __z *= 10;
00463                 __z += c-'0';
00464                 __is >> c;
00465                 if (__is.fail())
00466                         break;
00467         }
00468 
00469         if (neg) __z = -__z;
00470 
00471         return __is;
00472 }
00473 
00474 template <class _CharT, class _Traits>
00475 std::basic_ostream<_CharT, _Traits>& __cdecl
00476 operator<<(std::basic_ostream<_CharT, _Traits>& __os, const sint64& __z)
00477 {
00478         std::basic_ostringstream<_CharT, _Traits, std::allocator<_CharT> > __res;
00479 
00480         if (__z == 0)
00481         {
00482                 __res << '0';
00483         }
00484         else 
00485         {
00486                 sint64  __z2 = __z;
00487 
00488                 if (__z2 < 0)
00489                 {
00490                         __res << '-';
00491                 }
00492 
00493                 std::basic_ostringstream<_CharT, _Traits, std::allocator<_CharT> > __tmp;
00494                 while (__z2 != 0)
00495                 {
00496                         if (__z2 < 0)
00497                         {
00498                                 __tmp << (char)((-(__z2%10))+'0');
00499                         }
00500                         else
00501                         {
00502                                 __tmp << (char)((__z2%10)+'0');
00503                         }
00504                         __z2 /= 10;
00505                 }
00506 
00507                 uint __s = __tmp.str().size();
00508                 for (uint i = 0; i < __s; i++)
00509                         __res << __tmp.str()[__s - 1 - i];
00510         }
00511         return __os << __res.str();
00512 }
00513 
00514 #endif // NL_OS_WINDOWS
00515 
00516 
00517 
00518 
00519 }       // NLMISC
00520 
00521 #endif  // NL_COMMON_H