Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

lemur-compat.hpp

Go to the documentation of this file.
00001 
00015 #ifndef LEMUR_COMPAT_HPP
00016 #define LEMUR_COMPAT_HPP
00017 
00018 #include "lemur-platform.h"
00019 
00020 #include <memory>
00021 
00022 #ifdef WIN32
00023   #include <xutility>
00024   #include <direct.h>
00025   #include <string.h>
00026   #define LEMUR_MKDIR_NO_MODE
00027   #define LEMUR_STRNICMP
00028 #else
00029   #include <utility>
00030   #include <fstream>
00031   #include <sys/types.h>
00032   #include <sys/stat.h>
00033   #define LEMUR_USES_ENUM_OPENMODE
00034 #endif
00035 
00036 #include <stdio.h>
00037 #include <cstring>
00038 #include <cstdlib>
00039 
00040 #ifdef max
00041 #undef max
00042 #endif
00043 
00044 #ifdef min
00045 #undef min
00046 #endif
00047 
00049 namespace lemur_compat {
00051 template<typename _Type>
00052 _Type min( _Type x, _Type y ) {
00053 #ifdef LEMUR_BROKEN_MIN
00054   if( x < y )
00055     return x;
00056   else
00057     return y;
00058 #else
00059   return std::min<_Type>( x, y );
00060 #endif
00061 }
00062 
00064 template<typename _Type>
00065 _Type max( _Type x, _Type y ) {
00066 #ifdef LEMUR_BROKEN_MAX  
00067   if( x > y )
00068     return x;
00069   else
00070     return y;
00071 #else
00072   return std::max<_Type>( x, y );
00073 #endif
00074 }
00075 
00078 template<typename _Type>
00079 void autoptr_reset( std::auto_ptr<_Type>& ptr ) {
00080   #ifdef LEMUR_BROKEN_AUTOPTR
00081     std::auto_ptr<_Type> garbage;
00082     garbage = ptr;
00083   #else
00084     ptr.reset();
00085   #endif
00086 }
00087 
00090 template<typename _Type>
00091 void autoptr_reset( std::auto_ptr<_Type>& ptr, _Type& newValue ) {
00092   #ifdef LEMUR_BROKEN_AUTOPTR
00093     ptr = newValue;
00094   #else
00095     ptr.reset( newValue );
00096   #endif
00097 }
00098 
00100 inline int remove( const char* fileName ) {
00101 #ifdef LEMUR_NO_REMOVE
00102   return ::unlink( fileName );
00103 #else
00104   return ::remove( fileName );
00105 #endif
00106 }
00107 
00108 #ifdef LEMUR_USES_ENUM_OPENMODE
00109 inline std::_Ios_Openmode ios_mode_cast( int mode ) {
00110   return std::_Ios_Openmode(mode);
00111 }
00112 #else
00113 inline int ios_mode_cast( int mode ) {
00114   return mode;
00115 }
00116 #endif
00117 
00118 #ifdef LEMUR_MKDIR_NO_MODE
00119 inline int mkdir( const char* path, int mode ) {
00120   return ::_mkdir(path);
00121 }
00122 #else
00123 inline int mkdir( const char* path, int mode ) {
00124   return ::mkdir(path, mode);
00125 }
00126 #endif
00127 
00128 inline double flipd( double native ) {
00129   double result;
00130   const unsigned char* input = (const unsigned char*) &native;
00131   unsigned char* output = (unsigned char*) &result;
00132 
00133   output[7] = input[0];
00134   output[6] = input[1];
00135   output[5] = input[2];
00136   output[4] = input[3];
00137   output[3] = input[4];
00138   output[2] = input[5];
00139   output[1] = input[6];
00140   output[0] = input[7];
00141 
00142   return result;
00143 }
00144 
00145 inline UINT64 flipll( UINT64 native ) {
00146   UINT64 result;
00147   const unsigned char* input = (const unsigned char*) &native;
00148   unsigned char* output = (unsigned char*) &result;
00149 
00150   output[7] = input[0];
00151   output[6] = input[1];
00152   output[5] = input[2];
00153   output[4] = input[3];
00154   output[3] = input[4];
00155   output[2] = input[5];
00156   output[1] = input[6];
00157   output[0] = input[7];
00158 
00159   return result;
00160 }
00161 
00162 inline int strncasecmp( const char* one, const char* two, int length ) {
00163 #ifdef LEMUR_STRNICMP
00164   return ::_strnicmp( one, two, length );
00165 #else
00166   return ::strncasecmp( one, two, length );
00167 #endif
00168 }
00169 
00170 #if defined(WIN32) || defined(__SVR4)
00171 inline const char* strcasestr( const char* one, const char* two ) {
00172   const char* t = two;
00173   char oneLower = tolower(*one);
00174 
00175   for( ; *t; t++ ) {
00176     if (tolower(*t) == oneLower) {
00177 #ifdef WIN32
00178       int result = ::_strnicmp( one, t, strlen(one) );
00179 #else
00180       int result = strncasecmp( one, t, strlen(one) );
00181 #endif
00182 
00183       if( result == 0 )
00184         return t;
00185     }
00186   }
00187 
00188   return 0;
00189 }
00190 #else
00191 inline const char* strcasestr( const char* one, const char* two ) {
00192   return ::strcasestr( one, two );
00193 }
00194 #endif
00195 
00196 #if defined(WORDS_BIGENDIAN)
00197 inline double htond( double native ) {
00198   return native;
00199 }
00200 
00201 inline double ntohd( double native ) {
00202   return native;
00203 }
00204 #else
00205 inline double htond( double native ) {
00206   return flipd( native );
00207 }
00208 
00209 inline double ntohd( double native ) {
00210   return flipd( native );
00211 }
00212 #endif 
00213 
00214 #if defined(WORDS_BIGENDIAN)
00215 inline UINT64 htonll( UINT64 native ) {
00216   return native;
00217 }
00218 
00219 inline UINT64 ntohll( UINT64 native ) {
00220   return native;
00221 }
00222 #else
00223 inline UINT64 htonll( UINT64 native ) {
00224   return flipll( native );
00225 }
00226 
00227 inline UINT64 ntohll( UINT64 native ) {
00228   return flipll( native );
00229 }
00230 #endif
00231 
00232 void initializeNetwork();
00233 void closesocket( socket_t s );
00234 
00235 }
00236 
00237 #endif /* LEMUR_COMPAT_HPP */
00238 

Generated on Tue Jun 15 11:02:54 2010 for Lemur by doxygen 1.3.4