1 /*****************************************************************
5 | Copyright (c) 2002-2008, Axiomatic Systems, LLC.
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.
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.
30 ****************************************************************/
35 /*----------------------------------------------------------------------
37 +---------------------------------------------------------------------*/
38 #include "NptConfig.h"
40 #include "NptStrings.h"
42 #include "NptDataBuffer.h"
45 #if defined (NPT_CONFIG_HAVE_STDIO_H)
49 #if defined (NPT_CONFIG_HAVE_STRING_H)
53 #if defined(NPT_CONFIG_HAVE_STDARG_H)
57 #if defined(TARGET_WINDOWS_STORE)
60 /*----------------------------------------------------------------------
62 +---------------------------------------------------------------------*/
63 #define NPT_ARRAY_SIZE(_a) (sizeof(_a)/sizeof((_a)[0]))
65 /*----------------------------------------------------------------------
67 +---------------------------------------------------------------------*/
68 extern void NPT_BytesFromInt64Be(unsigned char* buffer
, NPT_UInt64 value
);
69 extern void NPT_BytesFromInt32Be(unsigned char* buffer
, NPT_UInt32 value
);
70 extern void NPT_BytesFromInt24Be(unsigned char* buffer
, NPT_UInt32 value
);
71 extern void NPT_BytesFromInt16Be(unsigned char* buffer
, NPT_UInt16 value
);
72 extern NPT_UInt64
NPT_BytesToInt64Be(const unsigned char* buffer
);
73 extern NPT_UInt32
NPT_BytesToInt32Be(const unsigned char* buffer
);
74 extern NPT_UInt32
NPT_BytesToInt24Be(const unsigned char* buffer
);
75 extern NPT_UInt16
NPT_BytesToInt16Be(const unsigned char* buffer
);
77 extern void NPT_BytesFromInt64Le(unsigned char* buffer
, NPT_UInt64 value
);
78 extern void NPT_BytesFromInt32Le(unsigned char* buffer
, NPT_UInt32 value
);
79 extern void NPT_BytesFromInt24Le(unsigned char* buffer
, NPT_UInt32 value
);
80 extern void NPT_BytesFromInt16Le(unsigned char* buffer
, NPT_UInt16 value
);
81 extern NPT_UInt64
NPT_BytesToInt64Le(const unsigned char* buffer
);
82 extern NPT_UInt32
NPT_BytesToInt32Le(const unsigned char* buffer
);
83 extern NPT_UInt32
NPT_BytesToInt24Le(const unsigned char* buffer
);
84 extern NPT_UInt16
NPT_BytesToInt16Le(const unsigned char* buffer
);
86 /*----------------------------------------------------------------------
87 | conversion utilities
88 +---------------------------------------------------------------------*/
90 NPT_ParseFloat(const char* str
, float& result
, bool relaxed
= true);
93 NPT_ParseInteger(const char* str
, long& result
, bool relaxed
= true, NPT_Cardinal
* chars_used
= 0);
96 NPT_ParseInteger(const char* str
, unsigned long& result
, bool relaxed
= true, NPT_Cardinal
* chars_used
= 0);
99 NPT_ParseInteger(const char* str
, int& result
, bool relaxed
= true, NPT_Cardinal
* chars_used
= 0);
102 NPT_ParseInteger(const char* str
, unsigned int& result
, bool relaxed
= true, NPT_Cardinal
* chars_used
= 0);
105 NPT_ParseInteger32(const char* str
, NPT_Int32
& result
, bool relaxed
= true, NPT_Cardinal
* chars_used
= 0);
108 NPT_ParseInteger32(const char* str
, NPT_UInt32
& result
, bool relaxed
= true, NPT_Cardinal
* chars_used
= 0);
111 NPT_ParseInteger64(const char* str
, NPT_Int64
& result
, bool relaxed
= true, NPT_Cardinal
* chars_used
= 0);
114 NPT_ParseInteger64(const char* str
, NPT_UInt64
& result
, bool relaxed
= true, NPT_Cardinal
* chars_used
= 0);
116 /*----------------------------------------------------------------------
118 +---------------------------------------------------------------------*/
120 NPT_FormatOutput(void (*function
)(void* parameter
, const char* message
),
121 void* function_parameter
,
125 void NPT_ByteToHex(NPT_Byte b
, char* buffer
, bool uppercase
=false);
126 NPT_Result
NPT_HexToByte(const char* buffer
, NPT_Byte
& b
);
127 NPT_Result
NPT_HexToBytes(const char* hex
, NPT_DataBuffer
& bytes
);
128 NPT_String
NPT_HexString(const unsigned char* data
,
130 const char* separator
= NULL
,
131 bool uppercase
=false);
132 char NPT_NibbleToHex(unsigned int nibble
, bool uppercase
= true);
133 int NPT_HexToNibble(char hex
);
135 /*----------------------------------------------------------------------
137 +---------------------------------------------------------------------*/
139 NPT_ParseMimeParameters(const char* encoded
,
140 NPT_Map
<NPT_String
, NPT_String
>& parameters
);
142 /*----------------------------------------------------------------------
143 | environment variables
144 +---------------------------------------------------------------------*/
145 class NPT_Environment
{
147 static NPT_Result
Get(const char* name
, NPT_String
& value
);
148 static NPT_Result
Set(const char* name
, const char* value
);
150 // compat for older APIs
151 #define NPT_GetEnvironment(_x,_y) NPT_Environment::Get(_x,_y)
153 /*----------------------------------------------------------------------
155 +---------------------------------------------------------------------*/
156 #if defined (NPT_CONFIG_HAVE_STDIO_H)
160 #if defined (NPT_CONFIG_HAVE_STRING_H)
164 #if defined (NPT_CONFIG_HAVE_SNPRINTF)
165 #define NPT_FormatString NPT_snprintf
167 int NPT_FormatString(char* str
, NPT_Size size
, const char* format
, ...);
170 #if defined(NPT_CONFIG_HAVE_VSNPRINTF)
171 #define NPT_FormatStringVN(s,c,f,a) NPT_vsnprintf(s,c,f,a)
173 extern int NPT_FormatStringVN(char *buffer
, size_t count
, const char *format
, va_list argptr
);
176 #if defined(NPT_CONFIG_HAVE_MEMCPY)
177 #define NPT_CopyMemory memcpy
179 extern void NPT_CopyMemory(void* dest
, void* src
, NPT_Size size
);
182 #if defined(NPT_CONFIG_HAVE_STRCMP)
183 #define NPT_StringsEqual(s1, s2) (strcmp((s1), (s2)) == 0)
185 extern int NPT_StringsEqual(const char* s1
, const char* s2
);
188 #if defined(NPT_CONFIG_HAVE_STRNCMP)
189 #define NPT_StringsEqualN(s1, s2, n) (strncmp((s1), (s2), (n)) == 0)
191 extern int NPT_StringsEqualN(const char* s1
, const char* s2
, unsigned long size
);
194 #if defined(NPT_CONFIG_HAVE_STRLEN)
195 #define NPT_StringLength(s) (NPT_Size)(strlen(s))
197 extern unsigned long NPT_StringLength(const char* s
);
200 #if defined(NPT_CONFIG_HAVE_STRCPY)
201 #define NPT_CopyString(dst, src) ((void)NPT_strcpy((dst), (src)))
203 extern void NPT_CopyString(char* dst
, const char* src
);
207 * Copy up to n characters from src to dst.
208 * The destination buffer will be null-terminated, so it must
209 * have enough space for n+1 characters (n from the source plus
210 * the null terminator).
212 #if defined(NPT_CONFIG_HAVE_STRNCPY)
213 #define NPT_CopyStringN(dst, src, n) \
214 do { ((void)NPT_strncpy((dst), (src), n)); (dst)[(n)] = '\0'; } while(0)
216 extern int NPT_CopyStringN(char* dst
, const char* src
, unsigned long n
);
219 #if defined(NPT_CONFIG_HAVE_MEMSET)
220 #define NPT_SetMemory memset
222 extern void NPT_SetMemory(void* dest
, int c
, NPT_Size size
);
225 #if defined(NPT_CONFIG_HAVE_MEMCMP)
226 #define NPT_MemoryEqual(s1, s2, n) (memcmp((s1), (s2), (n)) == 0)
228 extern int NPT_MemoryEqual(const void* s1
, const void* s2
, unsigned long n
);
231 #if defined(TARGET_WINDOWS_STORE)
232 std::wstring
win32ConvertUtf8ToW(const std::string
&text
);
235 #endif // _NPT_UTILS_H_