![]() |
VR-Link API Documentation for DIS
|
00001 /******************************************************************************* 00002 ** Copyright (c) 1992-2010 VT MAK 00003 ** All rights reserved. 00004 *******************************************************************************/ 00005 #ifndef _vlutil_H_ 00006 #define _vlutil_H_ 00007 00012 00013 #ifndef VLCONFIG_INCLUDED_H_ 00014 #include "vlConfig.h" 00015 #endif 00016 00017 #if VXWORKS 00018 #include <string.h> 00019 extern "C" 00020 { 00021 #include <unistd.h> 00022 } 00023 #endif 00024 00025 #ifdef _WIN32 00026 #if DtTENA 00027 // The TENA Middleware requires an expanded FD_SETSIZE; let ACE #define it 00028 // before <WinSock2.h> is #included as a side effect of one of the MFC headers 00029 #include <ACE/config.h> 00030 #endif 00031 #include <winsock2.h> 00032 #include <ws2tcpip.h> 00033 #endif 00034 00035 #if DT_HAVE_UNISTD_H 00036 #include <unistd.h> 00037 #endif 00038 00039 #ifdef PCFTP 00040 #include <4bsddefs.h> 00041 #endif 00042 00043 extern "C" { 00044 #include <stdlib.h> 00045 #include <sys/types.h> 00046 } 00047 00048 #include "vlTime.h" 00049 #include "math.h" 00050 00051 #if DT_FD_ZERO_USES_MEMSET 00052 #include <string.h> 00053 #else //! Need bzero. 00054 00055 #if DT_HAVE_BZERO_PROTO_IN_MEMORY_H 00056 #include <memory.h> 00057 #elif DT_HAVE_BZERO_PROTO_IN_CC_LIBC_H 00058 #include <CC/libc.h> 00059 #elif DT_HAVE_BZERO_PROTO_IN_BSTRING_H 00060 #include <bstring.h> 00061 #else 00062 extern "C" 00063 { 00064 void bzero( char* b, int length ); 00065 } 00066 #endif 00067 00068 #endif //! DT_FD_ZERO_USES_MEMSET 00069 00070 #if __SUNPRO_CC 00071 #include <sys/file.h> 00072 #endif 00073 00074 #ifdef VMS 00075 #include <time.h> 00076 #endif 00077 00078 #ifdef DtUSE_UTILITIES_NAMESPACE 00079 namespace DtUSE_UTILITIES_NAMESPACE 00080 { 00081 #endif 00082 00084 #define DtDELETE(x) \ 00085 if (x) \ 00086 { \ 00087 delete x; \ 00088 x = NULL; \ 00089 } 00090 00091 #define DtDELETE_VOID(x, cast) \ 00092 if (x) \ 00093 { \ 00094 cast* toDelete = reinterpret_cast<cast*>(x); \ 00095 delete toDelete; \ 00096 x = 0; \ 00097 } 00098 00099 00100 #ifndef DtISON 00101 #define DtISON(a, x) (a & x) 00102 #define DtISOFF(a, x) (!DtISON(a, x)) 00103 #define DtTURNON(a, x) (a |= x) 00104 #define DtTURNOFF(a, x) (a &= ~x) 00105 #endif 00106 00107 00108 #define DtNUMBER(foo) (sizeof(foo)/sizeof(foo[0])) 00109 #define DtOFFSET(type, field) ((size_t)&((type *)NULL)->field) 00110 #define DtSIZEOF(type, field) sizeof(((type *)NULL)->field) 00111 00112 class DtString; 00113 00114 00117 inline int DtROUND_TO_MULT( int num, int mult ) 00118 { 00119 return (int)ceil( (float)num / (float)mult ) * mult; 00120 } 00121 00126 00127 #if DT_HAVE_STRTOUL 00128 #define DtATOI(s) ((int)(strtoul(s, 0, 0))) 00129 #else 00130 #define DtATOI(s) (int)strtol(s, 0, 0) 00131 #endif 00132 00135 template<typename T> 00136 T DtMax(T x,T y) 00137 { 00138 return (x>y ? x:y); 00139 } 00140 00143 template<typename T> 00144 T DtMin(T x,T y) 00145 { 00146 return (x<y ? x:y); 00147 } 00148 00150 DT_DLL_VLUTIL char* DtDeg2DmsString( double val, char* buff, int prec ); 00151 00152 #if !VMS 00153 00154 DT_DLL_VLUTIL char* DtDeg2DmsString( double val ); 00155 #endif 00156 00161 DT_DLL_VLUTIL int DtIsInList( const int* lst, int num ); 00162 00163 00164 00166 DT_DLL_VLUTIL int DtSelect( int n, fd_set* i, fd_set* o, fd_set* e, DtTime t ); 00167 00168 #ifdef _WIN32 00169 00170 DT_DLL_VLUTIL inline void DtClose( int &fd ) 00171 { 00172 if ( fd >= 0 ) closesocket( fd ); 00173 fd = -1; 00174 } 00175 #else 00176 00177 DT_DLL_VLUTIL inline void DtClose( int &fd ) 00178 { 00179 if ( fd >= 0 ) close( fd ); 00180 fd = -1; 00181 } 00182 #endif 00183 00184 00186 class DT_DLL_VLUTIL DtSelectParam 00187 { 00188 public: 00189 DtSelectParam() 00190 { 00191 FD_ZERO( &read0 ); 00192 read = read0; 00193 maxfd = -1; 00194 } 00195 int select(DtTime timeout) 00196 { 00197 read = read0; 00198 return DtSelect( maxfd+1, &read, NULL, NULL, timeout ); 00199 } 00200 void addRead( int fd ) 00201 { 00202 FD_SET( (unsigned int)fd, &read0 ); 00203 maxfd = DtMax( maxfd, fd ); 00204 } 00205 void delRead( int fd ) 00206 { 00207 FD_CLR( (unsigned int)fd, &read0 ); 00209 } 00210 int canRead( int fd ) 00211 { 00212 return ( (int)( FD_ISSET( (unsigned int)fd, &read ) ) ); 00213 } 00214 00215 private: 00216 int maxfd; 00217 fd_set read0; 00218 fd_set read; 00219 }; 00220 00225 DT_DLL_VLUTIL char* DtStrdup( const char* str ); 00226 00229 DT_DLL_VLUTIL char* DtToLower( char* str ); 00230 00233 DT_DLL_VLUTIL char* DtToUpper( char* str ); 00234 00235 /* ---------------------- inet utils ---------------------- */ 00236 00238 typedef DtU32 DtIpv4Addr; 00239 00241 const DtIpv4Addr DtInetAddrAny = 0; 00242 00244 const DtIpv4Addr loopbackAddr = 0x7f000001; /* 127.0.0.1 */ 00245 00248 DT_DLL_VLUTIL const char* DtInetAddrString( DtIpv4Addr addr ); 00249 00252 #if _PowerUX 00253 DT_DLL_VLUTIL DtIpv4Addr DtStringToInetAddr( char* str ); 00254 #else 00255 DT_DLL_VLUTIL DtIpv4Addr DtStringToInetAddr( const char* str ); 00256 #endif 00257 00259 DT_DLL_VLUTIL DtIpv4Addr DtStringToInetAddr(const DtString& str ); 00260 00263 #if _PowerUX 00264 DT_DLL_VLUTIL DtIpv4Addr DtHexStringToInetAddr( char* str ); 00265 #else 00266 DT_DLL_VLUTIL DtIpv4Addr DtHexStringToInetAddr( const char* str ); 00267 #endif 00268 00269 00271 #if DT_NO_STRDUP 00272 DT_DLL_VLUTIL char* strdup( const char* str ); 00273 #endif 00274 #if DT_NO_CASECOMPARE 00275 DT_DLL_VLUTIL int strcasecmp( const char* strA, const char* strB ); 00276 #endif 00277 DT_DLL_VLUTIL int DtResizeString( char*& buf, int& oldLn, int newLn ); 00278 00282 DT_DLL_VLUTIL int DtArgc( char* cmdLine ); 00283 DT_DLL_VLUTIL char** DtArgv( char* cmdLine, int* argc = NULL ); 00284 00287 DT_DLL_VLUTIL const char* DtOperatingSystem(); 00288 00291 DT_DLL_VLUTIL const char* DtCompiler(); 00292 00295 DT_DLL_VLUTIL int DtGetPid(); 00296 00299 DT_DLL_VLUTIL int DtGetPPid(); 00300 00301 #define DT_VXWORKS_APP(name) \ 00302 int main(int argc, char** argv); \ 00303 int name(char* cmdLine) \ 00304 { \ 00305 return realMain(DtArgc(cmdLine), DtArgv(cmdLine)); \ 00306 } 00307 00308 00309 #ifdef DtUSE_UTILITIES_NAMESPACE 00310 } 00311 #endif 00312 00313 00314 #endif /* _VLUTIL_H_ */ 00315 00316