NptUtils.h

00001 /*****************************************************************
00002 |
00003 |   Neptune Utils
00004 |
00005 |   (c) 2001-2006 Gilles Boccon-Gibod
00006 |   Author: Gilles Boccon-Gibod (bok@bok.net)
00007 |
00008  ****************************************************************/
00009 
00010 #ifndef _NPT_UTILS_H_
00011 #define _NPT_UTILS_H_
00012 
00013 /*----------------------------------------------------------------------
00014 |   includes
00015 +---------------------------------------------------------------------*/
00016 #include "NptConfig.h"
00017 #include "NptTypes.h"
00018 #include "NptStrings.h"
00019 
00020 #if defined (NPT_CONFIG_HAVE_STDIO_H)
00021 #include <stdio.h>
00022 #endif
00023 
00024 #if defined (NPT_CONFIG_HAVE_STRING_H)
00025 #include <string.h>
00026 #endif
00027 
00028 #if defined(NPT_CONFIG_HAVE_STDARG_H)
00029 #include <stdarg.h>
00030 #endif
00031 
00032 /*----------------------------------------------------------------------
00033 |   macros
00034 +---------------------------------------------------------------------*/
00035 #define NPT_ARRAY_SIZE(_a) (sizeof(_a)/sizeof((_a)[0]))
00036 
00037 /*----------------------------------------------------------------------
00038 |   byte I/O
00039 +---------------------------------------------------------------------*/
00040 extern void NPT_BytesFromInt32Be(unsigned char* buffer, NPT_UInt32 value);
00041 extern void NPT_BytesFromInt16Be(unsigned char* buffer, NPT_UInt16 value);
00042 extern NPT_UInt32 NPT_BytesToInt32Be(const unsigned char* buffer);
00043 extern NPT_UInt16 NPT_BytesToInt16Be(const unsigned char* buffer);
00044 
00045 extern void NPT_BytesFromInt32Le(unsigned char* buffer, NPT_UInt32 value);
00046 extern void NPT_BytesFromInt16Le(unsigned char* buffer, NPT_UInt16 value);
00047 extern NPT_UInt32 NPT_BytesToInt32Le(const unsigned char* buffer);
00048 extern NPT_UInt16 NPT_BytesToInt16Le(const unsigned char* buffer);
00049 
00050 extern void NPT_ByteToHex(NPT_Byte b, char* buffer);
00051 extern NPT_Result NPT_HexToByte(const char* buffer, NPT_Byte& b);
00052 
00053 /*----------------------------------------------------------------------
00054 |    conversion utilities
00055 +---------------------------------------------------------------------*/
00056 extern NPT_Result 
00057 NPT_ParseFloat(const char* str, float& result, bool relaxed = true);
00058 
00059 extern NPT_Result 
00060 NPT_ParseInteger(const char* str, long& result, bool relaxed = true, NPT_Cardinal* chars_used = 0);
00061 
00062 extern NPT_Result 
00063 NPT_ParseInteger32(const char* str, NPT_Int32& result, bool relaxed = true);
00064 
00065 /*----------------------------------------------------------------------
00066 |    formatting
00067 +---------------------------------------------------------------------*/
00068 void
00069 NPT_FormatOutput(void        (*function)(void* parameter, const char* message),
00070                  void*       function_parameter,
00071                  const char* format, 
00072                  va_list     args);
00073 
00074 /*----------------------------------------------------------------------
00075 |    environment variables
00076 +---------------------------------------------------------------------*/
00077 NPT_Result NPT_GetEnvironment(const char* name, NPT_String& value);
00078 
00079 /*----------------------------------------------------------------------
00080 |   string utils
00081 +---------------------------------------------------------------------*/
00082 #if defined (NPT_CONFIG_HAVE_STDIO_H)
00083 #include <stdio.h>
00084 #endif
00085 
00086 #if defined (NPT_CONFIG_HAVE_STRING_H)
00087 #include <string.h>
00088 #endif
00089 
00090 #if defined (NPT_CONFIG_HAVE_SNPRINTF)
00091 #define NPT_FormatString NPT_snprintf
00092 #else
00093 int NPT_FormatString(char* str, NPT_Size size, const char* format, ...);
00094 #endif
00095 
00096 #if defined(NPT_CONFIG_HAVE_VSNPRINTF)
00097 #define NPT_FormatStringVN(s,c,f,a) NPT_vsnprintf(s,c,f,a)
00098 #else
00099 extern int NPT_FormatStringVN(char *buffer, size_t count, const char *format, va_list argptr);
00100 #endif
00101 
00102 #if defined(NPT_CONFIG_HAVE_MEMCPY)
00103 #define NPT_CopyMemory memcpy
00104 #else
00105 extern void NPT_CopyMemory(void* dest, void* src, NPT_Size size);
00106 #endif
00107 
00108 #if defined(NPT_CONFIG_HAVE_STRCMP)
00109 #define NPT_StringsEqual(s1, s2) \
00110 (strcmp((s1), (s2)) == 0)
00111 #else
00112 extern int NPT_StringsEqual(const char* s1, const char* s2);
00113 #endif
00114 
00115 #if defined(NPT_CONFIG_HAVE_STRNCMP)
00116 #define NPT_StringsEqualN(s1, s2, n) \
00117 (strncmp((s1), (s2), (n)) == 0)
00118 #else
00119 extern int NPT_StringsEqualN(const char* s1, const char* s2, unsigned long size);
00120 #endif
00121 
00122 #if defined(NPT_CONFIG_HAVE_STRLEN)
00123 #define NPT_StringLength(s) \
00124 (NPT_Size)(strlen(s))
00125 #else
00126 extern unsigned long NPT_StringLength(const char* s);
00127 #endif
00128 
00129 #if defined(NPT_CONFIG_HAVE_STRCPY)
00130 #define NPT_CopyString(dst, src) ((void)strcpy((dst), (src)))
00131 #else
00132 extern void NPT_CopyString(char* dst, const char* src);
00133 #endif
00134 
00135 #if defined(NPT_CONFIG_HAVE_STRNCPY)
00136 #define NPT_CopyStringN(dst, src, n) ((void)NPT_strncpy((dst), (src), n))
00137 #else
00138 extern int NPT_CopyStringN(char* dst, const char* src, unsigned long n);
00139 #endif
00140 
00141 #if defined(NPT_CONFIG_HAVE_MEMSET)
00142 #define NPT_SetMemory memset
00143 #else
00144 extern void NPT_SetMemory(void* dest, int c, NPT_Size size);
00145 #endif
00146 
00147 #if defined(NPT_CONFIG_HAVE_MEMCMP)
00148 #define NPT_MemoryEqual(s1, s2, n) (memcmp((s1), (s2), (n)) == 0) 
00149 #else 
00150 extern int NPT_MemoryEqual(const void* s1, const void* s2, unsigned long n); 
00151 #endif
00152 
00153 #endif // _NPT_UTILS_H_