VR-Link API Documentation for DIS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
vlUtil.h
Go to the documentation of this file.
1 /*******************************************************************************
2 ** Copyright (c) 1992-2010 VT MAK
3 ** All rights reserved.
4 *******************************************************************************/
5 #ifndef _vlutil_H_
6 #define _vlutil_H_
7 
12 
13 
14 // Check compiler flags
15 #ifdef _WIN32
16 #if _MSC_VER == 1400 //VC8
17 #if _SECURE_SCL != 0 || _HAS_ITERATOR_DEBUGGING != 0
18 #error Compiler Flags do not match! Do not use _SECURE_SCL or _HAS_ITERATOR_DEBUGGING
19 #endif // _SECURE_SCL != 0 || _HAS_ITERATOR_DEBUGGING != 0
20 #endif //_MSC_VER == 1400
21 #endif //_WIN32
22 
23 #include <iosfwd>
24 
25 #ifndef VLCONFIG_INCLUDED_H_
26 #include "vlConfig.h"
27 #endif
28 
29 #if VXWORKS
30 #include <string.h>
31 extern "C"
32 {
33 #include <unistd.h>
34 }
35 #endif
36 
37 #ifdef _WIN32
38  #include <winsock2.h>
39  #include <ws2tcpip.h>
40 #endif
41 
42 #if DT_HAVE_UNISTD_H
43 #include <unistd.h>
44 #endif
45 
46 #ifdef PCFTP
47 #include <4bsddefs.h>
48 #endif
49 
50 extern "C" {
51 #include <stdlib.h>
52 #include <sys/types.h>
53 }
54 
55 #include "vlTime.h"
56 #include "math.h"
57 
58 #if DT_FD_ZERO_USES_MEMSET
59 #include <string.h>
60 #else
61 
62 #if DT_HAVE_BZERO_PROTO_IN_MEMORY_H
63 #include <memory.h>
64 #elif DT_HAVE_BZERO_PROTO_IN_CC_LIBC_H
65 #include <CC/libc.h>
66 #elif DT_HAVE_BZERO_PROTO_IN_BSTRING_H
67 #include <bstring.h>
68 #else
69 extern "C"
70 {
71  void bzero( char* b, int length );
72 }
73 #endif
74 
75 #endif
76 
77 #if __SUNPRO_CC
78 #include <sys/file.h>
79 #endif
80 
81 #ifdef VMS
82 #include <time.h>
83 #endif
84 
85 #ifdef DtUSE_UTILITIES_NAMESPACE
86 namespace DtUSE_UTILITIES_NAMESPACE
87 {
88 #endif
89 
91 #define DtDELETE(x) \
92  if (x) \
93  { \
94  delete x; \
95  x = NULL; \
96  }
97 
98 #define DtDELETE_VOID(x, cast) \
99  if (x) \
100  { \
101  cast* toDelete = reinterpret_cast<cast*>(x); \
102  delete toDelete; \
103  x = 0; \
104  }
105 
106 
107 #ifndef DtISON
108 #define DtISON(a, x) (a & x)
109 #define DtISOFF(a, x) (!DtISON(a, x))
110 #define DtTURNON(a, x) (a |= x)
111 #define DtTURNOFF(a, x) (a &= ~x)
112 #endif
113 
114 
115 #define DtNUMBER(foo) (sizeof(foo)/sizeof(foo[0]))
116 #define DtOFFSET(type, field) ((size_t)&((type *)NULL)->field)
117 #define DtSIZEOF(type, field) sizeof(((type *)NULL)->field)
118 
119 class DtString;
120 
121 
124 inline int DtROUND_TO_MULT( int num, int mult )
125 {
126  return (int)ceil( (float)num / (float)mult ) * mult;
127 }
128 
133 
134 #if DT_HAVE_STRTOUL
135 #define DtATOI(s) ((int)(strtoul(s, 0, 0)))
136 #else
137 #define DtATOI(s) (int)strtol(s, 0, 0)
138 #endif
139 
142 template<typename T>
143 T DtMax(T x,T y)
144 {
145  return (x>y ? x:y);
146 }
147 
150 template<typename T>
151 T DtMin(T x,T y)
152 {
153  return (x<y ? x:y);
154 }
155 
157 DT_DLL_VLUTIL char* DtDeg2DmsString( double val, char* buff, int prec );
158 
159 #if !VMS
160 
161 DT_DLL_VLUTIL char* DtDeg2DmsString( double val );
162 #endif
163 
168 DT_DLL_VLUTIL int DtIsInList( const int* lst, int num );
169 
170 
171 
173 DT_DLL_VLUTIL int DtSelect( int n, fd_set* i, fd_set* o, fd_set* e, DtTime t );
174 
175 #ifdef _WIN32
176 
177 DT_DLL_VLUTIL inline void DtClose( int &fd )
178 {
179  if ( fd >= 0 ) closesocket( fd );
180  fd = -1;
181 }
182 #else
183 
184 DT_DLL_VLUTIL inline void DtClose( int &fd )
185 {
186  if ( fd >= 0 ) close( fd );
187  fd = -1;
188 }
189 #endif
190 
191 
194 {
195 public:
197  {
198  FD_ZERO( &read0 );
199  read = read0;
200  maxfd = -1;
201  }
202  int select(DtTime timeout)
203  {
204  read = read0;
205  return DtSelect( maxfd+1, &read, NULL, NULL, timeout );
206  }
207  void addRead( int fd )
208  {
209  FD_SET( (unsigned int)fd, &read0 );
210  maxfd = DtMax( maxfd, fd );
211  }
212  void delRead( int fd )
213  {
214  FD_CLR( (unsigned int)fd, &read0 );
216  }
217  int canRead( int fd )
218  {
219  return ( (int)( FD_ISSET( (unsigned int)fd, &read ) ) );
220  }
221 
222 private:
223  int maxfd;
224  fd_set read0;
225  fd_set read;
226 };
227 
232 DT_DLL_VLUTIL char* DtStrdup( const char* str );
233 
236 DT_DLL_VLUTIL char* DtToLower( char* str );
237 
240 DT_DLL_VLUTIL char* DtToUpper( char* str );
241 
242 /* ---------------------- inet utils ---------------------- */
243 
246 
249 
251 const DtIpv4Addr loopbackAddr = 0x7f000001; /* 127.0.0.1 */
252 
255 DT_DLL_VLUTIL const char* DtInetAddrString( DtIpv4Addr addr );
256 
259 #if _PowerUX
261 #else
262 DT_DLL_VLUTIL DtIpv4Addr DtStringToInetAddr( const char* str );
263 #endif
264 
267 
270 #if _PowerUX
272 #else
274 #endif
275 
276 
280 {
281  DtMachineSize alignMask = ~alignment+1; // Two's compliment
282 
283  // No need to align for alignment of 1 or less
284  if (alignment > 1 && value != (value&alignMask))
285  {
286  // The value is not aligned
287  // Extend the value past the next alignment.
288  // Then use bitwise "and" operation with the 2's compliment of the alignment size
289  // to take out extra bits (if any) that were added in past the alignment.
290  value += alignment;
291  value &= alignMask;
292  }
293  return value;
294 }
295 
297 inline const void* alignPtr(const void* ptr, DtMachineSize alignment)
298 {
299  DtMachineSize value = reinterpret_cast<DtMachineSize>(ptr);
300  return reinterpret_cast<void*>(alignValue(value, alignment));
301 }
302 inline void* alignPtr(void* ptr, DtMachineSize alignment)
303 {
304  DtMachineSize value = reinterpret_cast<DtMachineSize>(ptr);
305  return reinterpret_cast<void*>(alignValue(value, alignment));
306 }
307 
311 {
312  DtMachineSize padding = 0;
313  if (alignment > 1)
314  {
315  // Not quite sure of the logic of this operation but it works.
316  padding = (~value)+1;
317  padding &= (alignment-1);
318  }
319 
320  return padding;
321 }
322 
324 inline DtMachineSize alignedPtrPadding(const void* ptr, DtMachineSize alignment)
325 {
326  DtMachineSize value = reinterpret_cast<DtMachineSize>(ptr);
327  return alignedPadding(value, alignment);
328 }
329 
330 
332 #if DT_NO_STRDUP
333 DT_DLL_VLUTIL char* strdup( const char* str );
334 #endif
335 #if DT_NO_CASECOMPARE
336 DT_DLL_VLUTIL int strcasecmp( const char* strA, const char* strB );
337 #endif
338 DT_DLL_VLUTIL int DtResizeString( char*& buf, int& oldLn, int newLn );
339 
343 DT_DLL_VLUTIL int DtArgc( char* cmdLine );
344 DT_DLL_VLUTIL char** DtArgv( char* cmdLine, int* argc = NULL );
345 
348 DT_DLL_VLUTIL const char* DtOperatingSystem();
349 
352 DT_DLL_VLUTIL const char* DtCompiler();
353 
356 DT_DLL_VLUTIL int DtGetPid();
357 
361 
362 #define DT_VXWORKS_APP(name) \
363 int main(int argc, char** argv); \
364 int name(char* cmdLine) \
365 { \
366  return realMain(DtArgc(cmdLine), DtArgv(cmdLine)); \
367 }
368 
371 DT_DLL_VLUTIL void printHexToStream( std::ostream& stream, unsigned int numberOfColumns,
372  unsigned char * ptr, unsigned int bytes );
373 
374 
375 #ifdef DtUSE_UTILITIES_NAMESPACE
376 }
377 #endif
378 
379 
380 #endif /* _VLUTIL_H_ */
381 
382 
383 

Document ID: Generated on Mon Jun 22 21:18:40 EDT 2020 from SVN revision 213785
Copyright © 2005-2018 MAK Technologies. All Rights Reserved (www.mak.com)