1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
|
/*****************************************************************
|
| Neptune Utils
|
| Copyright (c) 2002-2008, Axiomatic Systems, LLC.
| All rights reserved.
|
| Redistribution and use in source and binary forms, with or without
| modification, are permitted provided that the following conditions are met:
| * Redistributions of source code must retain the above copyright
| notice, this list of conditions and the following disclaimer.
| * Redistributions in binary form must reproduce the above copyright
| notice, this list of conditions and the following disclaimer in the
| documentation and/or other materials provided with the distribution.
| * Neither the name of Axiomatic Systems nor the
| names of its contributors may be used to endorse or promote products
| derived from this software without specific prior written permission.
|
| THIS SOFTWARE IS PROVIDED BY AXIOMATIC SYSTEMS ''AS IS'' AND ANY
| EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
| WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
| DISCLAIMED. IN NO EVENT SHALL AXIOMATIC SYSTEMS BE LIABLE FOR ANY
| DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
| (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
| LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
| ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
| (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
| SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
****************************************************************/
#ifndef _NPT_UTILS_H_
#define _NPT_UTILS_H_
/*----------------------------------------------------------------------
| includes
+---------------------------------------------------------------------*/
#include "NptConfig.h"
#include "NptTypes.h"
#include "NptStrings.h"
#include "NptMap.h"
#include "NptDataBuffer.h"
#include "NptHash.h"
#if defined (NPT_CONFIG_HAVE_STDIO_H)
#include <stdio.h>
#endif
#if defined (NPT_CONFIG_HAVE_STRING_H)
#include <string.h>
#endif
#if defined(NPT_CONFIG_HAVE_STDARG_H)
#include <stdarg.h>
#endif
#if defined(TARGET_WINDOWS_STORE)
#include <string>
#endif
/*----------------------------------------------------------------------
| macros
+---------------------------------------------------------------------*/
#define NPT_ARRAY_SIZE(_a) (sizeof(_a)/sizeof((_a)[0]))
/*----------------------------------------------------------------------
| byte I/O
+---------------------------------------------------------------------*/
extern void NPT_BytesFromInt64Be(unsigned char* buffer, NPT_UInt64 value);
extern void NPT_BytesFromInt32Be(unsigned char* buffer, NPT_UInt32 value);
extern void NPT_BytesFromInt24Be(unsigned char* buffer, NPT_UInt32 value);
extern void NPT_BytesFromInt16Be(unsigned char* buffer, NPT_UInt16 value);
extern NPT_UInt64 NPT_BytesToInt64Be(const unsigned char* buffer);
extern NPT_UInt32 NPT_BytesToInt32Be(const unsigned char* buffer);
extern NPT_UInt32 NPT_BytesToInt24Be(const unsigned char* buffer);
extern NPT_UInt16 NPT_BytesToInt16Be(const unsigned char* buffer);
extern void NPT_BytesFromInt64Le(unsigned char* buffer, NPT_UInt64 value);
extern void NPT_BytesFromInt32Le(unsigned char* buffer, NPT_UInt32 value);
extern void NPT_BytesFromInt24Le(unsigned char* buffer, NPT_UInt32 value);
extern void NPT_BytesFromInt16Le(unsigned char* buffer, NPT_UInt16 value);
extern NPT_UInt64 NPT_BytesToInt64Le(const unsigned char* buffer);
extern NPT_UInt32 NPT_BytesToInt32Le(const unsigned char* buffer);
extern NPT_UInt32 NPT_BytesToInt24Le(const unsigned char* buffer);
extern NPT_UInt16 NPT_BytesToInt16Le(const unsigned char* buffer);
/*----------------------------------------------------------------------
| conversion utilities
+---------------------------------------------------------------------*/
extern NPT_Result
NPT_ParseFloat(const char* str, float& result, bool relaxed = true);
extern NPT_Result
NPT_ParseInteger(const char* str, long& result, bool relaxed = true, NPT_Cardinal* chars_used = 0);
extern NPT_Result
NPT_ParseInteger(const char* str, unsigned long& result, bool relaxed = true, NPT_Cardinal* chars_used = 0);
extern NPT_Result
NPT_ParseInteger(const char* str, int& result, bool relaxed = true, NPT_Cardinal* chars_used = 0);
extern NPT_Result
NPT_ParseInteger(const char* str, unsigned int& result, bool relaxed = true, NPT_Cardinal* chars_used = 0);
extern NPT_Result
NPT_ParseInteger32(const char* str, NPT_Int32& result, bool relaxed = true, NPT_Cardinal* chars_used = 0);
extern NPT_Result
NPT_ParseInteger32(const char* str, NPT_UInt32& result, bool relaxed = true, NPT_Cardinal* chars_used = 0);
extern NPT_Result
NPT_ParseInteger64(const char* str, NPT_Int64& result, bool relaxed = true, NPT_Cardinal* chars_used = 0);
extern NPT_Result
NPT_ParseInteger64(const char* str, NPT_UInt64& result, bool relaxed = true, NPT_Cardinal* chars_used = 0);
/*----------------------------------------------------------------------
| formatting
+---------------------------------------------------------------------*/
void
NPT_FormatOutput(void (*function)(void* parameter, const char* message),
void* function_parameter,
const char* format,
va_list args);
void NPT_ByteToHex(NPT_Byte b, char* buffer, bool uppercase=false);
NPT_Result NPT_HexToByte(const char* buffer, NPT_Byte& b);
NPT_Result NPT_HexToBytes(const char* hex, NPT_DataBuffer& bytes);
NPT_String NPT_HexString(const unsigned char* data,
NPT_Size data_size,
const char* separator = NULL,
bool uppercase=false);
char NPT_NibbleToHex(unsigned int nibble, bool uppercase = true);
int NPT_HexToNibble(char hex);
/*----------------------------------------------------------------------
| parsing
+---------------------------------------------------------------------*/
NPT_Result
NPT_ParseMimeParameters(const char* encoded,
NPT_Map<NPT_String, NPT_String>& parameters);
/*----------------------------------------------------------------------
| environment variables
+---------------------------------------------------------------------*/
class NPT_Environment {
public:
static NPT_Result Get(const char* name, NPT_String& value);
static NPT_Result Set(const char* name, const char* value);
};
// compat for older APIs
#define NPT_GetEnvironment(_x,_y) NPT_Environment::Get(_x,_y)
/*----------------------------------------------------------------------
| string utils
+---------------------------------------------------------------------*/
#if defined (NPT_CONFIG_HAVE_STDIO_H)
#include <stdio.h>
#endif
#if defined (NPT_CONFIG_HAVE_STRING_H)
#include <string.h>
#endif
#if defined (NPT_CONFIG_HAVE_SNPRINTF)
#define NPT_FormatString NPT_snprintf
#else
int NPT_FormatString(char* str, NPT_Size size, const char* format, ...);
#endif
#if defined(NPT_CONFIG_HAVE_VSNPRINTF)
#define NPT_FormatStringVN(s,c,f,a) NPT_vsnprintf(s,c,f,a)
#else
extern int NPT_FormatStringVN(char *buffer, size_t count, const char *format, va_list argptr);
#endif
#if defined(NPT_CONFIG_HAVE_MEMCPY)
#define NPT_CopyMemory memcpy
#else
extern void NPT_CopyMemory(void* dest, void* src, NPT_Size size);
#endif
#if defined(NPT_CONFIG_HAVE_STRCMP)
#define NPT_StringsEqual(s1, s2) (strcmp((s1), (s2)) == 0)
#else
extern int NPT_StringsEqual(const char* s1, const char* s2);
#endif
#if defined(NPT_CONFIG_HAVE_STRNCMP)
#define NPT_StringsEqualN(s1, s2, n) (strncmp((s1), (s2), (n)) == 0)
#else
extern int NPT_StringsEqualN(const char* s1, const char* s2, unsigned long size);
#endif
#if defined(NPT_CONFIG_HAVE_STRLEN)
#define NPT_StringLength(s) (NPT_Size)(strlen(s))
#else
extern unsigned long NPT_StringLength(const char* s);
#endif
#if defined(NPT_CONFIG_HAVE_STRCPY)
#define NPT_CopyString(dst, src) ((void)NPT_strcpy((dst), (src)))
#else
extern void NPT_CopyString(char* dst, const char* src);
#endif
/**
* Copy up to n characters from src to dst.
* The destination buffer will be null-terminated, so it must
* have enough space for n+1 characters (n from the source plus
* the null terminator).
*/
#if defined(NPT_CONFIG_HAVE_STRNCPY)
#define NPT_CopyStringN(dst, src, n) \
do { ((void)NPT_strncpy((dst), (src), n)); (dst)[(n)] = '\0'; } while(0)
#else
extern int NPT_CopyStringN(char* dst, const char* src, unsigned long n);
#endif
#if defined(NPT_CONFIG_HAVE_MEMSET)
#define NPT_SetMemory memset
#else
extern void NPT_SetMemory(void* dest, int c, NPT_Size size);
#endif
#if defined(NPT_CONFIG_HAVE_MEMCMP)
#define NPT_MemoryEqual(s1, s2, n) (memcmp((s1), (s2), (n)) == 0)
#else
extern int NPT_MemoryEqual(const void* s1, const void* s2, unsigned long n);
#endif
#if defined(TARGET_WINDOWS_STORE)
std::wstring win32ConvertUtf8ToW(const std::string &text);
#endif
#endif // _NPT_UTILS_H_
|