VR-Link API Documentation for HLA 1.3
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 #include <iosfwd>
14 
15 #ifndef VLCONFIG_INCLUDED_H_
16 #include "vlConfig.h"
17 #endif
18 
19 #if VXWORKS
20 #include <string.h>
21 extern "C"
22 {
23 #include <unistd.h>
24 }
25 #endif
26 
27 #ifdef _WIN32
28  #include <winsock2.h>
29  #include <ws2tcpip.h>
30 #endif
31 
32 #if DT_HAVE_UNISTD_H
33 #include <unistd.h>
34 #endif
35 
36 #ifdef PCFTP
37 #include <4bsddefs.h>
38 #endif
39 
40 extern "C" {
41 #include <stdlib.h>
42 #include <sys/types.h>
43 }
44 
45 #include "vlTime.h"
46 #include "math.h"
47 
48 #if DT_FD_ZERO_USES_MEMSET
49 #include <string.h>
50 #else
51 
52 #if DT_HAVE_BZERO_PROTO_IN_MEMORY_H
53 #include <memory.h>
54 #elif DT_HAVE_BZERO_PROTO_IN_CC_LIBC_H
55 #include <CC/libc.h>
56 #elif DT_HAVE_BZERO_PROTO_IN_BSTRING_H
57 #include <bstring.h>
58 #else
59 extern "C"
60 {
61  void bzero( char* b, int length );
62 }
63 #endif
64 
65 #endif
66 
67 #if __SUNPRO_CC
68 #include <sys/file.h>
69 #endif
70 
71 #ifdef VMS
72 #include <time.h>
73 #endif
74 
75 #ifdef DtUSE_UTILITIES_NAMESPACE
76 namespace DtUSE_UTILITIES_NAMESPACE
77 {
78 #endif
79 
81 #define DtDELETE(x) \
82  if (x) \
83  { \
84  delete x; \
85  x = NULL; \
86  }
87 
88 #define DtDELETE_VOID(x, cast) \
89  if (x) \
90  { \
91  cast* toDelete = reinterpret_cast<cast*>(x); \
92  delete toDelete; \
93  x = 0; \
94  }
95 
96 
97 #ifndef DtISON
98 #define DtISON(a, x) (a & x)
99 #define DtISOFF(a, x) (!DtISON(a, x))
100 #define DtTURNON(a, x) (a |= x)
101 #define DtTURNOFF(a, x) (a &= ~x)
102 #endif
103 
104 
105 #define DtNUMBER(foo) (sizeof(foo)/sizeof(foo[0]))
106 #define DtOFFSET(type, field) ((size_t)&((type *)NULL)->field)
107 #define DtSIZEOF(type, field) sizeof(((type *)NULL)->field)
108 
109 class DtString;
110 
111 
114 inline int DtROUND_TO_MULT( int num, int mult )
115 {
116  return (int)ceil( (float)num / (float)mult ) * mult;
117 }
118 
123 
124 #if DT_HAVE_STRTOUL
125 #define DtATOI(s) ((int)(strtoul(s, 0, 0)))
126 #else
127 #define DtATOI(s) (int)strtol(s, 0, 0)
128 #endif
129 
132 template<typename T>
133 T DtMax(T x,T y)
134 {
135  return (x>y ? x:y);
136 }
137 
140 template<typename T>
141 T DtMin(T x,T y)
142 {
143  return (x<y ? x:y);
144 }
145 
147 DT_DLL_VLUTIL char* DtDeg2DmsString( double val, char* buff, int prec );
148 
149 #if !VMS
150 
151 DT_DLL_VLUTIL char* DtDeg2DmsString( double val );
152 #endif
153 
158 DT_DLL_VLUTIL int DtIsInList( const int* lst, int num );
159 
160 
161 
163 DT_DLL_VLUTIL int DtSelect( int n, fd_set* i, fd_set* o, fd_set* e, DtTime t );
164 
165 #ifdef _WIN32
166 
167 DT_DLL_VLUTIL inline void DtClose( int &fd )
168 {
169  if ( fd >= 0 ) closesocket( fd );
170  fd = -1;
171 }
172 #else
173 
174 DT_DLL_VLUTIL inline void DtClose( int &fd )
175 {
176  if ( fd >= 0 ) close( fd );
177  fd = -1;
178 }
179 #endif
180 
181 
184 {
185 public:
187  {
188  FD_ZERO( &read0 );
189  read = read0;
190  maxfd = -1;
191  }
192  int select(DtTime timeout)
193  {
194  read = read0;
195  return DtSelect( maxfd+1, &read, NULL, NULL, timeout );
196  }
197  void addRead( int fd )
198  {
199  FD_SET( (unsigned int)fd, &read0 );
200  maxfd = DtMax( maxfd, fd );
201  }
202  void delRead( int fd )
203  {
204  FD_CLR( (unsigned int)fd, &read0 );
206  }
207  int canRead( int fd )
208  {
209  return ( (int)( FD_ISSET( (unsigned int)fd, &read ) ) );
210  }
211 
212 private:
213  int maxfd;
214  fd_set read0;
215  fd_set read;
216 };
217 
222 DT_DLL_VLUTIL char* DtStrdup( const char* str );
223 
226 DT_DLL_VLUTIL char* DtToLower( char* str );
227 
230 DT_DLL_VLUTIL char* DtToUpper( char* str );
231 
232 /* ---------------------- inet utils ---------------------- */
233 
236 
239 
241 const DtIpv4Addr loopbackAddr = 0x7f000001; /* 127.0.0.1 */
242 
245 DT_DLL_VLUTIL const char* DtInetAddrString( DtIpv4Addr addr );
246 
249 #if _PowerUX
251 #else
252 DT_DLL_VLUTIL DtIpv4Addr DtStringToInetAddr( const char* str );
253 #endif
254 
257 
260 #if _PowerUX
262 #else
264 #endif
265 
266 
270 {
271  DtMachineSize alignMask = ~alignment+1; // Two's compliment
272 
273  // No need to align for alignment of 1 or less
274  if (alignment > 1 && value != (value&alignMask))
275  {
276  // The value is not aligned
277  // Extend the value past the next alignment.
278  // Then use bitwise "and" operation with the 2's compliment of the alignment size
279  // to take out extra bits (if any) that were added in past the alignment.
280  value += alignment;
281  value &= alignMask;
282  }
283  return value;
284 }
285 
287 inline const void* alignPtr(const void* ptr, DtMachineSize alignment)
288 {
289  DtMachineSize value = reinterpret_cast<DtMachineSize>(ptr);
290  return reinterpret_cast<void*>(alignValue(value, alignment));
291 }
292 inline void* alignPtr(void* ptr, DtMachineSize alignment)
293 {
294  DtMachineSize value = reinterpret_cast<DtMachineSize>(ptr);
295  return reinterpret_cast<void*>(alignValue(value, alignment));
296 }
297 
301 {
302  DtMachineSize padding = 0;
303  if (alignment > 1)
304  {
305  // Not quite sure of the logic of this operation but it works.
306  padding = (~value)+1;
307  padding &= (alignment-1);
308  }
309 
310  return padding;
311 }
312 
314 inline DtMachineSize alignedPtrPadding(const void* ptr, DtMachineSize alignment)
315 {
316  DtMachineSize value = reinterpret_cast<DtMachineSize>(ptr);
317  return alignedPadding(value, alignment);
318 }
319 
320 
322 #if DT_NO_STRDUP
323 DT_DLL_VLUTIL char* strdup( const char* str );
324 #endif
325 #if DT_NO_CASECOMPARE
326 DT_DLL_VLUTIL int strcasecmp( const char* strA, const char* strB );
327 #endif
328 DT_DLL_VLUTIL int DtResizeString( char*& buf, int& oldLn, int newLn );
329 
333 DT_DLL_VLUTIL int DtArgc( char* cmdLine );
334 DT_DLL_VLUTIL char** DtArgv( char* cmdLine, int* argc = NULL );
335 
338 DT_DLL_VLUTIL const char* DtOperatingSystem();
339 
342 DT_DLL_VLUTIL const char* DtCompiler();
343 
346 DT_DLL_VLUTIL int DtGetPid();
347 
351 
352 #define DT_VXWORKS_APP(name) \
353 int main(int argc, char** argv); \
354 int name(char* cmdLine) \
355 { \
356  return realMain(DtArgc(cmdLine), DtArgv(cmdLine)); \
357 }
358 
361 DT_DLL_VLUTIL void printHexToStream( std::ostream& stream, unsigned int numberOfColumns,
362  unsigned char * ptr, unsigned int bytes );
363 
364 
365 #ifdef DtUSE_UTILITIES_NAMESPACE
366 }
367 #endif
368 
369 
370 #endif /* _VLUTIL_H_ */
371 
372 
373 

Document ID: Generated on Wed Oct 23 22:25:48 EDT 2013 from SVN revision 132765
Copyright © 2005-2013 VT MÄK. All Rights Reserved (www.mak.com)