osMacros.h

00001 /*
00002  * Copyright (c) 1997-2010 Objective Systems, Inc.
00003  *
00004  * This software is furnished under a license and may be used and copied
00005  * only in accordance with the terms of such license and with the
00006  * inclusion of the above copyright notice. This software or any other
00007  * copies thereof may not be provided or otherwise made available to any
00008  * other person. No title to and ownership of the software is hereby
00009  * transferred.
00010  *
00011  * The information in this software is subject to change without notice
00012  * and should not be construed as a commitment by Objective Systems, Inc.
00013  *
00014  * PROPRIETARY NOTICE
00015  *
00016  * This software is an unpublished work subject to a confidentiality agreement
00017  * and is protected by copyright and trade secret law.  Unauthorized copying,
00018  * redistribution or other use of this work is prohibited.
00019  *
00020  * The above notice of copyright on this source code product does not indicate
00021  * any actual or intended publication of such source code.
00022  *
00023  *****************************************************************************/
00024 
00025 #ifndef _OSMACROS_H_
00026 #define _OSMACROS_H_
00027 
00028 #if defined(_MSC_VER)
00029 // this disables 'conditional expression is constant' warnings
00030 // caused by using do { ... } while(0) in defines.
00031 #pragma warning(disable: 4127)
00032 #endif
00033 
00034 /* Check if object null before invoke get method */
00035 #define OS_CHECKED_GET(objptr,getmethod) \
00036 (0 == objptr) ? 0 : (objptr)->getmethod()
00037 
00038 /* Min/max tests */
00039 #define OS_MAX(a,b) (((a)>(b))?(a):(b))
00040 #define OS_MIN(a,b) (((a)<(b))?(a):(b))
00041 
00042 /* Test if character is a hex character */
00043 #define OS_ISHEXCHAR(c) \
00044 ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f'))
00045 
00046 /* Test if Character is printable */
00047 #define OS_ISPRINTABLE(c) \
00048 (c >= 0x20 && c <= 0x7e)
00049 
00050 /* Test if string variable (const char*) is empty */
00051 #define OS_ISEMPTY(s) (s == 0 || *s == '\0')
00052 
00053 /* Nibble-to-hex char conversion macros */
00054 #define OS_HEXCHARTONIBBLE(ch,b) do { \
00055 if (ch >= '0' && ch <= '9') b = (unsigned char)(ch - '0'); \
00056 else if (ch >= 'a' && ch <= 'f') b = (unsigned char)((ch - 'a') + 10); \
00057 else if (ch >= 'A' && ch <= 'F') b = (unsigned char)((ch - 'A') + 10); \
00058 else b = 0xFF; } while(0)
00059 
00060 #define OS_NIBBLETOHEXCHAR(b,ch) do { \
00061 if (b >= 0 && b <= 9) ch = (char)(b + '0'); \
00062 else if (b >= 0x0a && b <= 0x0f) ch = (char)((b - 10)+ 'a'); \
00063 else ch = '?'; } while(0)
00064 
00070 #define OSRTARRAYSIZE(x) (sizeof(x)/sizeof(x[0]))
00071 
00075 #define OSUTF8(str) ((const OSUTF8CHAR*)str)
00076 
00080 #define OSUTF8LEN(str) strlen((const char*)str)
00081 
00082 /* Generic list iteration macros */
00083 
00084 #define ForAllIter(p,listIter,type) \
00085 for(listIter.setFirst(),p=(type)listIter.getCurrentItem();!listIter.isDone();\
00086 listIter.setNext(),p=(type)listIter.getCurrentItem())
00087 
00088 #define ForAllIterRev(p,listIter,type) \
00089 for(listIter.setLast(),p=(type)listIter.getCurrentItem();!listIter.isDone();\
00090 listIter.setPrev(),p=(type)listIter.getCurrentItem())
00091 
00092 /* UTF-8 character size for -static option */
00093 
00094 #define OSUTF8CHAR_SIZE 1
00095 
00096 /* snprintf macros */
00097 
00098 #include <stdio.h>
00099 #include <stdarg.h>
00100 
00101 #if defined(_MSC_VER)
00102 #  if _MSC_VER >= 1400
00103 #    define os_snprintf(str, size, ...) \
00104      _snprintf_s(str, size, _TRUNCATE, __VA_ARGS__)
00105 #    define os_vsnprintf(str, size, fmt, ...) \
00106      _vsnprintf_s(str, size, _TRUNCATE, fmt, __VA_ARGS__)
00107 #  else
00108 #    define os_snprintf _snprintf
00109 #    define os_vsnprintf _vsnprintf
00110 #  endif
00111 #elif defined(__HP_aCC)
00112 /* at least aCC on HP-UX 11 expects format as non-const! */
00113 #  define os_snprintf(str, size, ...) sprintf(str, __VA_ARGS__)
00114 #  define os_vsnprintf(str, size, fmt, ...) \
00115       vsnprintf(str, size, (char *)fmt, __VA_ARGS__)
00116 #elif defined(_NO_SNPRINTF_) && !defined(__vxworks) && !defined (__SYMBIAN32__)
00117 #define os_snprintf(str, size, ...) sprintf(str, __VA_ARGS__)
00118 #define os_vsnprintf(str, size, fmt, ...) vsprintf(str, fmt, __VA_ARGS__)
00119 /* We use gcc 2.95, which requires ##ARGS */
00120 #elif defined(__vxworks) || defined(__SYMBIAN32__)
00121 #define os_snprintf(str,size,ARGS...) sprintf(str,##ARGS)
00122 #define os_vsnprintf(str,size,fmt,ARGS...) vsprintf(str,fmt,##ARGS)
00123 #else
00124 #define os_snprintf snprintf
00125 #define os_vsnprintf vsnprintf
00126 #endif
00127 
00128 #endif
00129