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 #ifndef VLCONFIG_INCLUDED_H_
14 #include "vlConfig.h"
15 #endif
16 
17 #if VXWORKS
18 #include <string.h>
19 extern "C"
20 {
21 #include <unistd.h>
22 }
23 #endif
24 
25 #ifdef _WIN32
26  #if DtTENA
27  // The TENA Middleware requires an expanded FD_SETSIZE; let ACE #define it
28  // before <WinSock2.h> is #included as a side effect of one of the MFC headers
29  #include <ACE/config.h>
30  #endif
31  #include <winsock2.h>
32  #include <ws2tcpip.h>
33 #endif
34 
35 #if DT_HAVE_UNISTD_H
36 #include <unistd.h>
37 #endif
38 
39 #ifdef PCFTP
40 #include <4bsddefs.h>
41 #endif
42 
43 extern "C" {
44 #include <stdlib.h>
45 #include <sys/types.h>
46 }
47 
48 #include "vlTime.h"
49 #include "math.h"
50 
51 #if DT_FD_ZERO_USES_MEMSET
52 #include <string.h>
53 #else
54 
55 #if DT_HAVE_BZERO_PROTO_IN_MEMORY_H
56 #include <memory.h>
57 #elif DT_HAVE_BZERO_PROTO_IN_CC_LIBC_H
58 #include <CC/libc.h>
59 #elif DT_HAVE_BZERO_PROTO_IN_BSTRING_H
60 #include <bstring.h>
61 #else
62 extern "C"
63 {
64  void bzero( char* b, int length );
65 }
66 #endif
67 
68 #endif
69 
70 #if __SUNPRO_CC
71 #include <sys/file.h>
72 #endif
73 
74 #ifdef VMS
75 #include <time.h>
76 #endif
77 
78 #ifdef DtUSE_UTILITIES_NAMESPACE
79 namespace DtUSE_UTILITIES_NAMESPACE
80 {
81 #endif
82 
84 #define DtDELETE(x) \
85  if (x) \
86  { \
87  delete x; \
88  x = NULL; \
89  }
90 
91 #define DtDELETE_VOID(x, cast) \
92  if (x) \
93  { \
94  cast* toDelete = reinterpret_cast<cast*>(x); \
95  delete toDelete; \
96  x = 0; \
97  }
98 
99 
100 #ifndef DtISON
101 #define DtISON(a, x) (a & x)
102 #define DtISOFF(a, x) (!DtISON(a, x))
103 #define DtTURNON(a, x) (a |= x)
104 #define DtTURNOFF(a, x) (a &= ~x)
105 #endif
106 
107 
108 #define DtNUMBER(foo) (sizeof(foo)/sizeof(foo[0]))
109 #define DtOFFSET(type, field) ((size_t)&((type *)NULL)->field)
110 #define DtSIZEOF(type, field) sizeof(((type *)NULL)->field)
111 
112 class DtString;
113 
114 
117 inline int DtROUND_TO_MULT( int num, int mult )
118 {
119  return (int)ceil( (float)num / (float)mult ) * mult;
120 }
121 
126 
127 #if DT_HAVE_STRTOUL
128 #define DtATOI(s) ((int)(strtoul(s, 0, 0)))
129 #else
130 #define DtATOI(s) (int)strtol(s, 0, 0)
131 #endif
132 
135 template<typename T>
136 T DtMax(T x,T y)
137 {
138  return (x>y ? x:y);
139 }
140 
143 template<typename T>
144 T DtMin(T x,T y)
145 {
146  return (x<y ? x:y);
147 }
148 
150 DT_DLL_VLUTIL char* DtDeg2DmsString( double val, char* buff, int prec );
151 
152 #if !VMS
153 
154 DT_DLL_VLUTIL char* DtDeg2DmsString( double val );
155 #endif
156 
161 DT_DLL_VLUTIL int DtIsInList( const int* lst, int num );
162 
163 
164 
166 DT_DLL_VLUTIL int DtSelect( int n, fd_set* i, fd_set* o, fd_set* e, DtTime t );
167 
168 #ifdef _WIN32
169 
170 DT_DLL_VLUTIL inline void DtClose( int &fd )
171 {
172  if ( fd >= 0 ) closesocket( fd );
173  fd = -1;
174 }
175 #else
176 
177 DT_DLL_VLUTIL inline void DtClose( int &fd )
178 {
179  if ( fd >= 0 ) close( fd );
180  fd = -1;
181 }
182 #endif
183 
184 
187 {
188 public:
190  {
191  FD_ZERO( &read0 );
192  read = read0;
193  maxfd = -1;
194  }
195  int select(DtTime timeout)
196  {
197  read = read0;
198  return DtSelect( maxfd+1, &read, NULL, NULL, timeout );
199  }
200  void addRead( int fd )
201  {
202  FD_SET( (unsigned int)fd, &read0 );
203  maxfd = DtMax( maxfd, fd );
204  }
205  void delRead( int fd )
206  {
207  FD_CLR( (unsigned int)fd, &read0 );
209  }
210  int canRead( int fd )
211  {
212  return ( (int)( FD_ISSET( (unsigned int)fd, &read ) ) );
213  }
214 
215 private:
216  int maxfd;
217  fd_set read0;
218  fd_set read;
219 };
220 
225 DT_DLL_VLUTIL char* DtStrdup( const char* str );
226 
229 DT_DLL_VLUTIL char* DtToLower( char* str );
230 
233 DT_DLL_VLUTIL char* DtToUpper( char* str );
234 
235 /* ---------------------- inet utils ---------------------- */
236 
239 
242 
244 const DtIpv4Addr loopbackAddr = 0x7f000001; /* 127.0.0.1 */
245 
248 DT_DLL_VLUTIL const char* DtInetAddrString( DtIpv4Addr addr );
249 
252 #if _PowerUX
254 #else
255 DT_DLL_VLUTIL DtIpv4Addr DtStringToInetAddr( const char* str );
256 #endif
257 
260 
263 #if _PowerUX
265 #else
267 #endif
268 
272 {
273  DtMachineSize alignMask = ~alignment+1; // Two's compliment
274 
275  // No need to align for alignment of 1 or less
276  if (alignment > 1 && value != (value&alignMask))
277  {
278  // The value is not aligned
279  // Extend the value past the next alignment.
280  // Then use bitwise "and" operation with the 2's compliment of the alignment size
281  // to take out extra bits (if any) that were added in past the alignment.
282  value += alignment;
283  value &= alignMask;
284  }
285  return value;
286 }
287 
289 inline const void* alignPtr(const void* ptr, DtMachineSize alignment)
290 {
291  DtMachineSize value = reinterpret_cast<DtMachineSize>(ptr);
292  return reinterpret_cast<void*>(alignValue(value, alignment));
293 }
294 inline void* alignPtr(void* ptr, DtMachineSize alignment)
295 {
296  DtMachineSize value = reinterpret_cast<DtMachineSize>(ptr);
297  return reinterpret_cast<void*>(alignValue(value, alignment));
298 }
299 
300 
304 {
305  DtMachineSize alignMask = ~alignment+1; // Two's compliment
306 
307  if (alignment > 1)
308  {
309  // Not quite sure of the logic of this operation but it works.
310  value = (~value)+1;
311  value &= (alignment-1);
312  }
313  return value;
314 }
315 
317 inline DtMachineSize alignedPtrPadding(const void* ptr, DtMachineSize alignment)
318 {
319  DtMachineSize value = reinterpret_cast<DtMachineSize>(ptr);
320  return alignedPadding(value, alignment);
321 }
322 
324 #if DT_NO_STRDUP
325 DT_DLL_VLUTIL char* strdup( const char* str );
326 #endif
327 #if DT_NO_CASECOMPARE
328 DT_DLL_VLUTIL int strcasecmp( const char* strA, const char* strB );
329 #endif
330 DT_DLL_VLUTIL int DtResizeString( char*& buf, int& oldLn, int newLn );
331 
335 DT_DLL_VLUTIL int DtArgc( char* cmdLine );
336 DT_DLL_VLUTIL char** DtArgv( char* cmdLine, int* argc = NULL );
337 
340 DT_DLL_VLUTIL const char* DtOperatingSystem();
341 
344 DT_DLL_VLUTIL const char* DtCompiler();
345 
348 DT_DLL_VLUTIL int DtGetPid();
349 
353 
354 #define DT_VXWORKS_APP(name) \
355 int main(int argc, char** argv); \
356 int name(char* cmdLine) \
357 { \
358  return realMain(DtArgc(cmdLine), DtArgv(cmdLine)); \
359 }
360 
361 
362 #ifdef DtUSE_UTILITIES_NAMESPACE
363 }
364 #endif
365 
366 
367 #endif /* _VLUTIL_H_ */
368 
369 

Document ID: Generated on Wed Aug 14 15:35:11 EDT 2013 from SVN revision 130445
Copyright © 2005-2013 VT MÄK. All Rights Reserved (www.mak.com)