comparison deps/Platinum/ThirdParty/Neptune/Source/Core/NptUtils.h @ 0:3425707ddbf6

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