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 #define LEMUR_MKDIR_NO_MODE
00026 #else
00027 #include <utility>
00028 #include <fstream>
00029 #include <sys/types.h>
00030 #include <sys/stat.h>
00031 #define LEMUR_USES_ENUM_OPENMODE
00032 #endif
00033
00034 #include <stdio.h>
00035
00036 #ifdef max
00037 #undef max
00038 #endif
00039
00040 #ifdef min
00041 #undef min
00042 #endif
00043
00044 namespace lemur_compat {
00046 template<typename _Type>
00047 _Type min( _Type x, _Type y ) {
00048 #ifdef LEMUR_BROKEN_MIN
00049 if( x < y )
00050 return x;
00051 else
00052 return y;
00053 #else
00054 return std::min<_Type>( x, y );
00055 #endif
00056 }
00057
00059 template<typename _Type>
00060 _Type max( _Type x, _Type y ) {
00061 #ifdef LEMUR_BROKEN_MAX
00062 if( x > y )
00063 return x;
00064 else
00065 return y;
00066 #else
00067 return std::max<_Type>( x, y );
00068 #endif
00069 };
00070
00073 template<typename _Type>
00074 void autoptr_reset( std::auto_ptr<_Type>& ptr ) {
00075 #ifdef LEMUR_BROKEN_AUTOPTR
00076 std::auto_ptr<_Type> garbage;
00077 garbage = ptr;
00078 #else
00079 ptr.reset();
00080 #endif
00081 };
00082
00085 template<typename _Type>
00086 void autoptr_reset( std::auto_ptr<_Type>& ptr, _Type& newValue ) {
00087 #ifdef LEMUR_BROKEN_AUTOPTR
00088 ptr = newValue;
00089 #else
00090 ptr.reset( newValue );
00091 #endif
00092 };
00093
00095 inline int remove( const char* fileName ) {
00096 #ifdef LEMUR_NO_REMOVE
00097 return ::unlink( fileName );
00098 #else
00099 return ::remove( fileName );
00100 #endif
00101 }
00102
00103 #ifdef LEMUR_USES_ENUM_OPENMODE
00104 inline std::_Ios_Openmode ios_mode_cast( int mode ) {
00105 return std::_Ios_Openmode(mode);
00106 }
00107 #else
00108 inline int ios_mode_cast( int mode ) {
00109 return mode;
00110 }
00111 #endif
00112
00113 #ifdef LEMUR_MKDIR_NO_MODE
00114 inline int mkdir( const char* path, int mode ) {
00115 return ::mkdir(path);
00116 }
00117 #else
00118 inline int mkdir( const char* path, int mode ) {
00119 return ::mkdir(path, mode);
00120 }
00121 #endif
00122
00123 inline double flipd( double native ) {
00124 double result;
00125 const unsigned char* input = (const unsigned char*) &native;
00126 unsigned char* output = (unsigned char*) &result;
00127
00128 output[7] = input[0];
00129 output[6] = input[1];
00130 output[5] = input[2];
00131 output[4] = input[3];
00132 output[3] = input[4];
00133 output[2] = input[5];
00134 output[1] = input[6];
00135 output[0] = input[7];
00136
00137 return result;
00138 }
00139
00140 inline UINT64 flipll( UINT64 native ) {
00141 UINT64 result;
00142 const unsigned char* input = (const unsigned char*) &native;
00143 unsigned char* output = (unsigned char*) &result;
00144
00145 output[7] = input[0];
00146 output[6] = input[1];
00147 output[5] = input[2];
00148 output[4] = input[3];
00149 output[3] = input[4];
00150 output[2] = input[5];
00151 output[1] = input[6];
00152 output[0] = input[7];
00153
00154 return result;
00155 }
00156
00157 #ifdef WIN32
00158 inline int strncasecmp( const char* one, const char* two, int length ) {
00159 return ::strnicmp( one, two, length );
00160 }
00161 #else
00162 inline int strncasecmp( const char* one, const char* two, int length ) {
00163 return ::strncasecmp( one, two, length );
00164 }
00165 #endif
00166
00167 #ifdef BIG_ENDIAN
00168 inline double htond( double native ) {
00169 return native;
00170 }
00171
00172 inline double ntohd( double native ) {
00173 return native;
00174 }
00175 #else
00176 inline double htond( double native ) {
00177 return flipd( native );
00178 }
00179
00180 inline double ntohd( double native ) {
00181 return flipd( native );
00182 }
00183 #endif
00184
00185 #ifdef BIG_ENDIAN
00186 inline UINT64 htonll( UINT64 native ) {
00187 return native;
00188 }
00189
00190 inline UINT64 ntohll( UINT64 native ) {
00191 return native;
00192 }
00193 #else
00194 inline UINT64 htonll( UINT64 native ) {
00195 return flipll( native );
00196 }
00197
00198 inline UINT64 ntohll( UINT64 native ) {
00199 return flipll( native );
00200 }
00201 #endif
00202
00203 void initializeNetwork();
00204 void closesocket( socket_t s );
00205
00206 };
00207
00208 #endif
00209