1 /**************************************************************************
3 * Copyright 2013-2014 RAD Game Tools and Valve Software
4 * Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 **************************************************************************/
27 // File: vogl_strutils.h
30 #include "vogl_core.h"
33 #define VOGL_PATH_SEPERATOR_CHAR '\\'
35 #define VOGL_PATH_SEPERATOR_CHAR '/'
40 // These functions exist mostly to work around cross platform runtime library issues, some also work around locale issues.
41 inline bool vogl_isdigit(int c)
43 return (c >= '0') && (c <= '9');
45 inline bool vogl_isxdigit(int c)
47 return vogl_isdigit(c) || ((c >= 'a') && (c <= 'f')) || ((c >= 'A') && (c <= 'F'));
49 inline bool vogl_isspace(int c)
51 return (c == 0x20) || (c == 0x09) || (c == 0x0a) || (c == 0x0b) || (c == 0x0c) || (c == 0x0d);
53 inline bool vogl_islower(int c)
55 return (c >= 'a') && (c <= 'z');
57 inline bool vogl_isupper(int c)
59 return (c >= 'A') && (c <= 'Z');
61 inline bool vogl_isalpha(int c)
63 return vogl_islower(c) || vogl_isupper(c);
66 inline int vogl_toupper(int c)
68 return ((c >= 'a') && (c <= 'z')) ? (c - 'a' + 'A') : c;
70 inline int vogl_tolower(int c)
72 return ((c >= 'A') && (c <= 'Z')) ? (c - 'A' + 'a') : c;
75 char *vogl_strlwr(char *p);
76 char *vogl_strupr(char *p);
78 int vogl_sprintf_s(char *buffer, size_t sizeOfBuffer, const char *format, ...) VOGL_ATTRIBUTE_PRINTF(3, 4);
79 int vogl_vsprintf_s(char *buffer, size_t sizeOfBuffer, const char *format, va_list args);
81 int vogl_strcasecmp(const char *s1, const char *s2);
82 inline int vogl_stricmp(const char *s1, const char *s2)
84 return vogl_strcasecmp(s1, s2);
87 const char *vogl_strstr(const char *s1, const char *s2);
89 int vogl_strncasecmp(const char *s1, const char *s2, size_t n);
90 inline int vogl_strnicmp(const char *s1, const char *s2, size_t n)
92 return vogl_strncasecmp(s1, s2, n);
95 inline uint vogl_strlen(const char *pStr)
97 uint len = static_cast<uint>(strlen(pStr));
98 VOGL_ASSERT(len < cUINT32_MAX);
99 return static_cast<uint>(len);
102 // Must free returned string pointer using vogl_free(), not free().
103 char *vogl_strdup(const char *pStr);
105 int vogl_strcmp(const char *p, const char *q);
107 char *strcpy_safe(char *pDst, uint dst_len, const char *pSrc);
109 bool int_to_string(int value, char *pDst, uint len);
110 bool uint_to_string(uint value, char *pDst, uint len);
111 bool uint64_to_string(uint64_t value, char *pDst, uint len);
112 bool int64_to_string(int64_t value, char *pDst, uint len);
114 dynamic_string int_to_string(int64_t value);
115 dynamic_string uint_to_string(uint64_t value);
117 bool uint64_to_string_with_commas(uint64_t value, char *pDst, uint len);
118 dynamic_string uint64_to_string_with_commas(uint64_t value);
120 // string_to_int/uint/etc.
121 // Supports hex values beginning with "0x", skips leading whitespace, returns false on overflow or conversion errors.
122 // pBuf will be moved forward the # of characters actually consumed.
123 bool string_ptr_to_int(const char *&pBuf, int &value);
124 bool string_ptr_to_uint(const char *&pBuf, uint &value);
125 bool string_ptr_to_int64(const char *&pBuf, int64_t &value);
126 bool string_ptr_to_uint64(const char *&pBuf, uint64_t &value);
128 // recognizes "true" or "false" (case insensitive), or 0 (false) 1 (true), anything else is an error
129 bool string_ptr_to_bool(const char *&p, bool &value);
131 bool string_ptr_to_float(const char *&p, float &value, uint round_digit = 512U);
132 bool string_ptr_to_double(const char *&p, double &value, uint round_digit = 512U);
133 bool string_ptr_to_double(const char *&p, const char *pEnd, double &value, uint round_digit = 512U);
135 // These wrappers return 0 on error.
136 inline int string_to_int(const char *pBuf, int def = 0)
139 if (!string_ptr_to_int(pBuf, val))
143 inline uint string_to_uint(const char *pBuf, uint def = 0)
146 if (!string_ptr_to_uint(pBuf, val))
150 inline int64_t string_to_int64(const char *pBuf, int64_t def = 0)
153 if (!string_ptr_to_int64(pBuf, val))
157 inline uint64_t string_to_uint64(const char *pBuf, uint64_t def = 0)
160 if (!string_ptr_to_uint64(pBuf, val))
164 inline bool string_to_bool(const char *p, bool def = 0)
167 if (!string_ptr_to_bool(p, val))
171 inline float string_to_float(const char *p, float def = 0)
174 if (!string_ptr_to_float(p, val))
178 inline double string_to_double(const char *p, double def = 0)
181 if (!string_ptr_to_double(p, val))
186 dynamic_string to_hex_string(int value);
187 dynamic_string to_hex_string(uint value);
188 dynamic_string to_hex_string(int64_t value);
189 dynamic_string to_hex_string(uint64_t value);
191 bool strutils_test();