VR-Link API Documentation for HLA 1.3
 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 MAK Technologies, Inc
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 #include <iostream>
25 #include <sstream>
26 #include <bitset>
27 
28 #ifndef VLCONFIG_INCLUDED_H_
29 #include "vlConfig.h"
30 #endif
31 
32 #if VXWORKS
33 #include <string.h>
34 extern "C"
35 {
36 #include <unistd.h>
37 }
38 #endif
39 
40 #ifdef _WIN32
41  #include <winsock2.h>
42  #include <ws2tcpip.h>
43 #endif
44 
45 #if DT_HAVE_UNISTD_H
46 #include <unistd.h>
47 #endif
48 
49 #ifdef PCFTP
50 #include <4bsddefs.h>
51 #endif
52 
53 extern "C" {
54 #include <stdlib.h>
55 #include <sys/types.h>
56 }
57 
58 #include "vlTime.h"
59 #include "math.h"
60 
61 #if DT_FD_ZERO_USES_MEMSET
62 #include <string.h>
63 #else
64 
65 #if DT_HAVE_BZERO_PROTO_IN_MEMORY_H
66 #include <memory.h>
67 #elif DT_HAVE_BZERO_PROTO_IN_CC_LIBC_H
68 #include <CC/libc.h>
69 #elif DT_HAVE_BZERO_PROTO_IN_BSTRING_H
70 #include <bstring.h>
71 #else
72 extern "C"
73 {
74  void bzero( char* b, int length );
75 }
76 #endif
77 
78 #endif
79 
80 #if __SUNPRO_CC
81 #include <sys/file.h>
82 #endif
83 
84 #ifdef VMS
85 #include <time.h>
86 #endif
87 #include <vector>
88 
89 #ifdef DtUSE_UTILITIES_NAMESPACE
90 namespace DtUSE_UTILITIES_NAMESPACE
91 {
92 #endif
93 
95 #define DtDELETE(x) \
96  if (x) \
97  { \
98  delete x; \
99  x = NULL; \
100  }
101 
102 #define DtDELETE_VOID(x, cast) \
103  if (x) \
104  { \
105  cast* toDelete = reinterpret_cast<cast*>(x); \
106  delete toDelete; \
107  x = 0; \
108  }
109 
110 
111 #ifndef DtISON
112 #define DtISON(a, x) (a & x)
113 #define DtISOFF(a, x) (!DtISON(a, x))
114 #define DtTURNON(a, x) (a |= x)
115 #define DtTURNOFF(a, x) (a &= ~x)
116 #endif
117 
118 
119 #define DtNUMBER(foo) (sizeof(foo)/sizeof(foo[0]))
120 #define DtOFFSET(type, field) ((size_t)&((type *)NULL)->field)
121 #define DtSIZEOF(type, field) sizeof(((type *)NULL)->field)
122 
123 #define DtRGBA_TO_UINT(r, g, b, a) ((((a) << 24) | ((r) << 16) | ((g) << 8) | (b)))
124 
125 DT_DLL_VLUTIL inline void DtUINT_TO_RGBA(unsigned int val, unsigned int& r, unsigned int& g, unsigned int& b, unsigned int& a)
126 {
127  a = (unsigned char)(val >> 24) & 0xFF;
128  r = (unsigned char)(val >> 16) & 0xFF;
129  g = (unsigned char)(val >> 8) & 0xFF;
130  b = (unsigned char)(val) & 0xFF;
131 }
132 
133 class DtString;
134 
135 
138 inline int DtROUND_TO_MULT( int num, int mult )
139 {
140  return (int)ceil( (float)num / (float)mult ) * mult;
141 }
142 
147 
148 #if DT_HAVE_STRTOUL
149 #define DtATOI(s) ((int)(strtoul(s, 0, 0)))
150 #else
151 #define DtATOI(s) (int)strtol(s, 0, 0)
152 #endif
153 
156 template<typename T>
157 T DtMax(T x,T y)
158 {
159  return (x>y ? x:y);
160 }
161 
164 template<typename T>
165 T DtMin(T x,T y)
166 {
167  return (x<y ? x:y);
168 }
169 
171 DT_DLL_VLUTIL char* DtDeg2DmsString( double val, char* buff, int prec );
172 
173 #if !VMS
174 DT_DLL_VLUTIL char* DtDeg2DmsString( double val );
176 #endif
177 
182 DT_DLL_VLUTIL DtString DtHexToBinary(const std::string& s, int sLength = 0);
183 
187 DT_DLL_VLUTIL DtString DtBinaryToHex(const std::string& s);
188 
192 DT_DLL_VLUTIL void DtDecimalToBinary(const DtU32 value, std::vector<int>& binVal);
193 
197 DT_DLL_VLUTIL int DtBinaryToDecimal(const std::string& s);
198 
203 #define DT_8_BIT_MASK(value, bit) (DtU8)((value & ((DtU8)1 << bit)) >>bit)
204 
209 #define DT_SET_8_BIT_MASK(value, bit) (value | ((DtU8)1 << bit) )
210 
216 #define DT_8_BITS_MASK(value, yBits, startBit) (DtU8)((value & ((((DtU8)1 << yBits) -1) <<startBit)) >> startBit )
217 
222 #define DT_16_BIT_MASK(value, bit) (DtU16)((value & ((DtU16)1 << bit)) >> bit)
223 
228 #define DT_SET_16_BIT_MASK(value, bit) (value | ((DtU16)1 << bit) )
229 
235 #define DT_16_BITS_MASK(value, yBits, startBit) (DtU16)((value & ((((DtU16)1 << yBits) -1) <<startBit)) >> startBit )
236 
241 #define DT_32_BIT_MASK(value, bit) (DtU32)((value & ((DtU32)1 << bit)) >> bit)
242 
247 #define DT_SET_32_BIT_MASK(value, bit) (value | ((DtU32)1 << bit) )
248 
253 DT_DLL_VLUTIL int DtIsInList( const int* lst, int num );
254 
255 
256 
258 DT_DLL_VLUTIL int DtSelect( int n, fd_set* i, fd_set* o, fd_set* e, DtTime t );
259 
260 #ifdef _WIN32
261 DT_DLL_VLUTIL inline void DtClose( int &fd )
263 {
264  if ( fd >= 0 ) closesocket( fd );
265  fd = -1;
266 }
267 #else
268 DT_DLL_VLUTIL inline void DtClose( int &fd )
270 {
271  if ( fd >= 0 ) close( fd );
272  fd = -1;
273 }
274 #endif
275 
276 
279 {
280 public:
282  {
283  FD_ZERO( &read0 );
284  read = read0;
285  maxfd = -1;
286  }
287  int select(DtTime timeout)
288  {
289  read = read0;
290  return DtSelect( maxfd+1, &read, NULL, NULL, timeout );
291  }
292  void addRead( int fd )
293  {
294  FD_SET( (unsigned int)fd, &read0 );
295  maxfd = DtMax( maxfd, fd );
296  }
297  void delRead( int fd )
298  {
299  FD_CLR( (unsigned int)fd, &read0 );
301  }
302  int canRead( int fd )
303  {
304  return ( (int)( FD_ISSET( (unsigned int)fd, &read ) ) );
305  }
306 
307 private:
308  int maxfd;
309  fd_set read0;
310  fd_set read;
311 };
312 
317 DT_DLL_VLUTIL char* DtStrdup( const char* str );
318 
321 DT_DLL_VLUTIL char* DtToLower( char* str );
322 
325 DT_DLL_VLUTIL char* DtToUpper( char* str );
326 
327 /* ---------------------- inet utils ---------------------- */
328 
331 
334 
336 const DtIpv4Addr loopbackAddr = 0x7f000001; /* 127.0.0.1 */
337 
340 DT_DLL_VLUTIL const char* DtInetAddrString( DtIpv4Addr addr );
341 
344 #if _PowerUX
346 #else
347 DT_DLL_VLUTIL DtIpv4Addr DtStringToInetAddr( const char* str );
348 #endif
349 
352 
355 #if _PowerUX
357 #else
359 #endif
360 
361 
365 {
366  DtMachineSize alignMask = ~alignment+1; // Two's compliment
367 
368  // No need to align for alignment of 1 or less
369  if (alignment > 1 && value != (value&alignMask))
370  {
371  // The value is not aligned
372  // Extend the value past the next alignment.
373  // Then use bitwise "and" operation with the 2's compliment of the alignment size
374  // to take out extra bits (if any) that were added in past the alignment.
375  value += alignment;
376  value &= alignMask;
377  }
378  return value;
379 }
380 
382 inline const void* alignPtr(const void* ptr, DtMachineSize alignment)
383 {
384  DtMachineSize value = reinterpret_cast<DtMachineSize>(ptr);
385  return reinterpret_cast<void*>(alignValue(value, alignment));
386 }
387 inline void* alignPtr(void* ptr, DtMachineSize alignment)
388 {
389  DtMachineSize value = reinterpret_cast<DtMachineSize>(ptr);
390  return reinterpret_cast<void*>(alignValue(value, alignment));
391 }
392 
396 {
397  DtMachineSize padding = 0;
398  if (alignment > 1)
399  {
400  // Not quite sure of the logic of this operation but it works.
401  padding = (~value)+1;
402  padding &= (alignment-1);
403  }
404 
405  return padding;
406 }
407 
409 inline DtMachineSize alignedPtrPadding(const void* ptr, DtMachineSize alignment)
410 {
411  DtMachineSize value = reinterpret_cast<DtMachineSize>(ptr);
412  return alignedPadding(value, alignment);
413 }
414 
415 
417 #if DT_NO_STRDUP
418 DT_DLL_VLUTIL char* strdup( const char* str );
419 #endif
420 #if DT_NO_CASECOMPARE
421 DT_DLL_VLUTIL int strcasecmp( const char* strA, const char* strB );
422 #endif
423 DT_DLL_VLUTIL int DtResizeString( char*& buf, int& oldLn, int newLn );
424 
428 DT_DLL_VLUTIL int DtArgc( char* cmdLine );
429 DT_DLL_VLUTIL char** DtArgv( char* cmdLine, int* argc = NULL );
430 
433 DT_DLL_VLUTIL const char* DtOperatingSystem();
434 
437 DT_DLL_VLUTIL const char* DtCompiler();
438 
441 DT_DLL_VLUTIL int DtGetPid();
442 
446 
447 #define DT_VXWORKS_APP(name) \
448 int main(int argc, char** argv); \
449 int name(char* cmdLine) \
450 { \
451  return realMain(DtArgc(cmdLine), DtArgv(cmdLine)); \
452 }
453 
456 DT_DLL_VLUTIL void printHexToStream( std::ostream& stream, unsigned int numberOfColumns,
457  unsigned char * ptr, unsigned int bytes );
458 
459 
460 #ifdef DtUSE_UTILITIES_NAMESPACE
461 }
462 #endif
463 
464 
465 #endif /* _VLUTIL_H_ */
466 
467 
468 
void DtDecimalToBinary(const DtU32 value, std::vector< int > &binVal)
Get the 32-bit binary value of a passed in decimal integer.
DtString DtToLower(const char *)
This function returns a new lowercase DtString.
Definition: vlStringUtil.h:180
DtU32 DtIpv4Addr
Definition: vlUtil.h:330
void DtUINT_TO_RGBA(unsigned int val, unsigned int &r, unsigned int &g, unsigned int &b, unsigned int &a)
Definition: vlUtil.h:125
fd_set read
Definition: vlUtil.h:310
int select(DtTime timeout)
Definition: vlUtil.h:287
int DtIsInList(const int *lst, int num)
Returns -1 if num is not in lst.
This file contains the declaration of DtClock and DtTime.
T DtMax(T x, T y)
Returns what ever is greater, x or y of type T.
Definition: vlUtil.h:157
DtMachineSize alignedPtrPadding(const void *ptr, DtMachineSize alignment)
Return the # padding bytes necessary to align the given pointer to the given byte alignment (in bytes...
Definition: vlUtil.h:409
int maxfd
Definition: vlUtil.h:308
void addRead(int fd)
Definition: vlUtil.h:292
const DtIpv4Addr loopbackAddr
Definition: vlUtil.h:336
DtIpv4Addr DtStringToInetAddr(const char *str)
Converts to internet dot notation to ulong in host order.
void printHexToStream(std::ostream &stream, unsigned int numberOfColumns, unsigned char *ptr, unsigned int bytes)
Prints Hex to a stream based on an address and the bytes.
int DtArgc(char *cmdLine)
Convert a command line (single string with args separated by spaces) to argv, argc format...
const char * DtInetAddrString(DtIpv4Addr addr)
Converts ulong to internet dot notation (returns internal static buffer).
int DtSelect(int n, fd_set *i, fd_set *o, fd_set *e, DtTime t)
DtString DtBinaryToHex(const std::string &s)
Get the hex value of a passed in 32-bit binary value.
const void * alignPtr(const void *ptr, DtMachineSize alignment)
Adjust the given pointer to the given byte alignment (in bytes)
Definition: vlUtil.h:382
char * DtStrdup(const char *str)
Strdup wrapper.
DtMachineSize alignValue(DtMachineSize value, DtMachineSize alignment)
Adjust the given value to the nearest multiple of the given alignment Efficient means to determine by...
Definition: vlUtil.h:364
int canRead(int fd)
Definition: vlUtil.h:302
DtIpv4Addr DtHexStringToInetAddr(const char *str)
Converts to internet dot notation to ulong in host order.
char * DtDeg2DmsString(double val, char *buff, int prec)
DtString DtToUpper(const char *)
This function returns a new uppercase DtString.
Definition: vlStringUtil.h:194
int DtGetPid()
Returns the process ID for the running process.
void delRead(int fd)
Definition: vlUtil.h:297
int DtResizeString(char *&buf, int &oldLn, int newLn)
For VXWORKS.
char ** DtArgv(char *cmdLine, int *argc=NULL)
DtMachineSize alignedPadding(DtMachineSize value, DtMachineSize alignment)
Return the adjustment necessary to align the given value to the given alignment Efficient means to de...
Definition: vlUtil.h:395
DtSelectParam()
Definition: vlUtil.h:281
const DtIpv4Addr DtInetAddrAny
Definition: vlUtil.h:333
DtString DtHexToBinary(const std::string &s, int sLength=0)
Get the 32-bit binary value of a passed in hex value.
int DtBinaryToDecimal(const std::string &s)
Get the decimal value given a binary value.
const char * DtOperatingSystem()
Returns the platform we&#39;re running on.
unsigned long DtU32
A 32 bit unsigned integer value.
Definition: vlMachineTypes.h:153
DtReal DtTime
A definition of DtTime.
Definition: vlTime.h:19
void DtClose(int &fd)
Definition: vlUtil.h:269
Instances of DtString are used to represent strings.
Definition: vlString.h:29
fd_set read0
Definition: vlUtil.h:309
const char * DtCompiler()
Returns the compiler.
Definition: vlUtil.h:278
int DtROUND_TO_MULT(int num, int mult)
Rounds num up to a multiple of mult.
Definition: vlUtil.h:138
int DtGetPPid()
Returns the process ID for the parent of the running process.
DtU32 DtMachineSize
A type that represents the addressing size.
Definition: vlMachineTypes.h:197
#define DT_DLL_VLUTIL
Definition: vlMachineTypes.h:109
T DtMin(T x, T y)
Returns the which ever is lesser, x or y of type T.
Definition: vlUtil.h:165
void bzero(char *b, int length)
Need bzero.

Document ID: Generated on Wed Mar 27 02:04:30 EDT 2024 from SVN revision 264570
Copyright © 2005-2024 MAK Technologies. All Rights Reserved (www.mak.com)