]> git.cworth.org Git - vogl/blob - src/voglcore/vogl_strutils.h
Initial vogl checkin
[vogl] / src / voglcore / vogl_strutils.h
1 /**************************************************************************
2  *
3  * Copyright 2013-2014 RAD Game Tools and Valve Software
4  * Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC
5  * All Rights Reserved.
6  *
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:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
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
23  * THE SOFTWARE.
24  *
25  **************************************************************************/
26
27 // File: vogl_strutils.h
28 #pragma once
29
30 #include "vogl_core.h"
31
32 #ifdef WIN32
33 #define VOGL_PATH_SEPERATOR_CHAR '\\'
34 #else
35 #define VOGL_PATH_SEPERATOR_CHAR '/'
36 #endif
37
38 namespace vogl
39 {
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)
42     {
43         return (c >= '0') && (c <= '9');
44     }
45     inline bool vogl_isxdigit(int c)
46     {
47         return vogl_isdigit(c) || ((c >= 'a') && (c <= 'f')) || ((c >= 'A') && (c <= 'F'));
48     }
49     inline bool vogl_isspace(int c)
50     {
51         return (c == 0x20) || (c == 0x09) || (c == 0x0a) || (c == 0x0b) || (c == 0x0c) || (c == 0x0d);
52     }
53     inline bool vogl_islower(int c)
54     {
55         return (c >= 'a') && (c <= 'z');
56     }
57     inline bool vogl_isupper(int c)
58     {
59         return (c >= 'A') && (c <= 'Z');
60     }
61     inline bool vogl_isalpha(int c)
62     {
63         return vogl_islower(c) || vogl_isupper(c);
64     }
65
66     inline int vogl_toupper(int c)
67     {
68         return ((c >= 'a') && (c <= 'z')) ? (c - 'a' + 'A') : c;
69     }
70     inline int vogl_tolower(int c)
71     {
72         return ((c >= 'A') && (c <= 'Z')) ? (c - 'A' + 'a') : c;
73     }
74
75     char *vogl_strlwr(char *p);
76     char *vogl_strupr(char *p);
77
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);
80
81     int vogl_strcasecmp(const char *s1, const char *s2);
82     inline int vogl_stricmp(const char *s1, const char *s2)
83     {
84         return vogl_strcasecmp(s1, s2);
85     }
86
87     const char *vogl_strstr(const char *s1, const char *s2);
88
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)
91     {
92         return vogl_strncasecmp(s1, s2, n);
93     }
94
95     inline uint vogl_strlen(const char *pStr)
96     {
97         uint len = static_cast<uint>(strlen(pStr));
98         VOGL_ASSERT(len < cUINT32_MAX);
99         return static_cast<uint>(len);
100     }
101
102     // Must free returned string pointer using vogl_free(), not free().
103     char *vogl_strdup(const char *pStr);
104
105     int vogl_strcmp(const char *p, const char *q);
106
107     char *strcpy_safe(char *pDst, uint dst_len, const char *pSrc);
108
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);
113
114     dynamic_string int_to_string(int64_t value);
115     dynamic_string uint_to_string(uint64_t value);
116
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);
119
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);
127
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);
130
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);
134
135     // These wrappers return 0 on error.
136     inline int string_to_int(const char *pBuf, int def = 0)
137     {
138         int val = 0;
139         if (!string_ptr_to_int(pBuf, val))
140             val = def;
141         return val;
142     }
143     inline uint string_to_uint(const char *pBuf, uint def = 0)
144     {
145         uint val = 0;
146         if (!string_ptr_to_uint(pBuf, val))
147             val = def;
148         return val;
149     }
150     inline int64_t string_to_int64(const char *pBuf, int64_t def = 0)
151     {
152         int64_t val = 0;
153         if (!string_ptr_to_int64(pBuf, val))
154             val = def;
155         return val;
156     }
157     inline uint64_t string_to_uint64(const char *pBuf, uint64_t def = 0)
158     {
159         uint64_t val = 0;
160         if (!string_ptr_to_uint64(pBuf, val))
161             val = def;
162         return val;
163     }
164     inline bool string_to_bool(const char *p, bool def = 0)
165     {
166         bool val = false;
167         if (!string_ptr_to_bool(p, val))
168             val = def;
169         return val;
170     }
171     inline float string_to_float(const char *p, float def = 0)
172     {
173         float val = 0;
174         if (!string_ptr_to_float(p, val))
175             val = def;
176         return val;
177     }
178     inline double string_to_double(const char *p, double def = 0)
179     {
180         double val = 0;
181         if (!string_ptr_to_double(p, val))
182             val = def;
183         return val;
184     }
185
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);
190
191     bool strutils_test();
192
193 } // namespace vogl