HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SYS_ParseNumber.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: SYS_ParseNumber.h ( SYS Library, C++)
7  *
8  * COMMENTS: A comprehensive set of functions to parse numbers,
9  * integers and floating point.
10  */
11 
12 #ifndef __SYS_ParseNumber__
13 #define __SYS_ParseNumber__
14 
15 #include "SYS_API.h"
16 
17 #include "SYS_Types.h"
18 
19 /// List of possible states the parsing ended in.
20 enum class SYS_ParseStatus
21 {
22  Success, ///< A number was successfully parsed.
23  NoNumberFound, ///< No numeric characters were found. A value of zero
24  /// is returned and @c end will be set to the value of
25  /// @c begin.
26  InvalidArgs, ///< Invalid arguments were passed in (e.g. bad number base, invalid range).
27  Overflow
28 };
29 
30 enum class SYS_ParseFlags
31 {
32  None = 0x00, ///< Default use.
33  DigitSeparator = 0x01, ///< Allow underscore ('_') to be used as a digit separator.
34 };
35 
37 { return SYS_ParseFlags(int(a) | int(b)); }
38 
39 static inline bool operator&(SYS_ParseFlags a, SYS_ParseFlags b)
40 { return (int(a) & int(b)) == int(b); }
41 
42 /// Parse a string, delimited by @c begin and @c end , into an integer of a
43 /// certain bit size.
44 /// Unlike @c strtod and family, @c end should always be a valid pointer to the
45 /// character one past the last character to be parsed, or @c nullptr. If @c
46 /// end is nullptr, then the string will be parsed until the end of the string,
47 /// which, in that case, should be zero-terminated.
48 /// Upon return, @c end will point to the next character after the last one
49 /// parsed. By default the number base is automatically determined by the
50 /// number prefix (if any), although a specific base can be given to override
51 /// this automatic choice. The range of supported bases is from 2 to 36,
52 /// inclusive. A value of zero can be given for the function to automatically
53 /// determine the base, from the prefix. In the absence of prefix the number is
54 /// assumed to be decimal. Invalid @c base values will cause the function to
55 /// return immediately with @c SYS_ParseStatus::InvalidArgs.
56 /// @{
58 SYSparseInteger(const char *begin, const char *&end, int8 &number, int base=0,
61 SYSparseInteger(const char *begin, const char *&end, uint8 &number, int base=0,
64 SYSparseInteger(const char *begin, const char *&end, int16 &number, int base=0,
67 SYSparseInteger(const char *begin, const char *&end, uint16 &number, int base=0,
70 SYSparseInteger(const char *begin, const char *&end, int32 &number, int base=0,
73 SYSparseInteger(const char *begin, const char *&end, uint32 &number, int base=0,
76 SYSparseInteger(const char *begin, const char *&end, int64 &number, int base=0,
79 SYSparseInteger(const char *begin, const char *&end, uint64 &number, int base=0,
81 // Some systems have size_t as a seperate type from uint. Some don't.
82 #if defined(MBSD)
83 static inline SYS_ParseStatus
84 SYSparseInteger(const char *begin, const char *&end, size_t &number, int base=0,
86 {
87  uint64 num64 = number;
88  SYS_ParseStatus result = SYSparseInteger(begin, end, num64, base, flags);
89  number = num64;
90  return result;
91 }
92 #endif
93 /// @}
94 
95 /// Parse a delimited string into a float of a certain bit size.
96 /// If @c end is nullptr, then the string will be parsed until end. Upon return,
97 /// @c end will point to the character after the last one parsed.
98 /// @{
100 SYSparseFloat(const char *begin, const char *&end, fpreal16 &number,
103 SYSparseFloat(const char *begin, const char *&end, fpreal32 &number,
106 SYSparseFloat(const char *begin, const char *&end, fpreal64 &number,
108 /// @}
109 
110 
111 // Compatibility shims
112 static inline int32
113 SYSatoi32(const char *buf, int base=10)
114 {
115  int32 n = 0;
116  const char *end = nullptr;
117  SYSparseInteger(buf, end, n, base);
118  return n;
119 }
120 
121 static inline uint32
122 SYSatou32(const char *buf, int base=10)
123 {
124  uint32 n = 0;
125  const char *end = nullptr;
126  SYSparseInteger(buf, end, n, base);
127  return n;
128 }
129 
130 static inline int64
131 SYSatoi64(const char *buf, int base=10)
132 {
133  int64 n = 0;
134  const char *end = nullptr;
135  SYSparseInteger(buf, end, n, base);
136  return n;
137 }
138 
139 static inline uint64
140 SYSatou64(const char *buf, int base=10)
141 {
142  uint64 n = 0;
143  const char *end = nullptr;
144  SYSparseInteger(buf, end, n, base);
145  return n;
146 }
147 
148 static inline int32
149 SYSstrtoi32(const char *buf, char **endptr=nullptr, int base=0)
150 {
151  int32 n = 0;
152  const char *end = nullptr;
153  SYSparseInteger(buf, end, n, base);
154  if (endptr)
155  *endptr = const_cast<char *>(end);
156  return n;
157 }
158 static inline uint32
159 SYSstrtou32(const char *buf, char **endptr=nullptr, int base=0)
160 {
161  uint32 n = 0;
162  const char *end = nullptr;
163  SYSparseInteger(buf, end, n, base);
164  if (endptr)
165  *endptr = const_cast<char *>(end);
166  return n;
167 }
168 static inline int64
169 SYSstrtoi64(const char *buf, char **endptr=nullptr, int base=0)
170 {
171  int64 n = 0;
172  const char *end = nullptr;
173  SYSparseInteger(buf, end, n, base);
174  if (endptr)
175  *endptr = const_cast<char *>(end);
176  return n;
177 }
178 static inline uint64
179 SYSstrtou64(const char *buf, char **endptr=nullptr, int base=0)
180 {
181  uint64 n = 0;
182  const char *end = nullptr;
183  SYSparseInteger(buf, end, n, base);
184  if (endptr)
185  *endptr = const_cast<char *>(end);
186  return n;
187 }
188 
189 static inline fpreal64
190 SYSstrtod(const char *buf, char **endptr=nullptr)
191 {
192  fpreal64 n = 0;
193  const char *end = nullptr;
194  SYSparseFloat(buf, end, n);
195  if (endptr)
196  *endptr = const_cast<char *>(end);
197  return n;
198 }
199 
200 static inline fpreal32
201 SYSstrtof32(const char *buf, char **endptr=nullptr)
202 {
203  fpreal32 n = 0;
204  const char *end = nullptr;
205  SYSparseFloat(buf, end, n);
206  if (endptr)
207  *endptr = const_cast<char *>(end);
208  return n;
209 }
210 
211 static inline fpreal64
212 SYSstrtof64(const char *buf, char **endptr=nullptr)
213 {
214  fpreal64 n = 0;
215  const char *end = nullptr;
216  SYSparseFloat(buf, end, n);
217  if (endptr)
218  *endptr = const_cast<char *>(end);
219  return n;
220 }
221 
222 static inline fpreal32
223 SYSatof32(const char *buf)
224 {
225  fpreal32 n = 0;
226  const char *end = nullptr;
227  SYSparseFloat(buf, end, n);
228  return n;
229 }
230 
231 static inline fpreal64
232 SYSatof64(const char *buf)
233 {
234  fpreal64 n = 0;
235  const char *end = nullptr;
236  SYSparseFloat(buf, end, n);
237  return n;
238 }
239 
240 static inline int SYSstrtoi(const char *buf) { return SYSstrtoi32(buf); }
241 static inline uint SYSstrtou(const char *buf) { return SYSstrtou32(buf); }
242 
243 static inline int SYSatoi(const char *buf, int base=10)
244  { return SYSatoi32(buf, base); }
245 static inline uint SYSatou(const char *buf, int base=10)
246  { return SYSatou32(buf, base); }
247 
248 static inline fpreal
249 SYSatof(const char *buf)
250 {
251  fpreal n = 0;
252  const char *end = nullptr;
253  SYSparseFloat(buf, end, n);
254  return n;
255 }
256 
257 
258 /// Convert string to int, but allow for '_' as digits separator (1_332_344)
259 static inline int64
260 SYSstrtoi_underscore(const char *buf, int base=10)
261 {
262  int64 n = 0;
263  const char *end = nullptr;
265  return n;
266 }
267 
268 
269 /// Convert string to real, but allow for '_' as digits separator (1_024.34)
270 static inline fpreal64
271 SYSstrtod_underscore(const char *buf)
272 {
273  fpreal64 n = 0;
274  const char *end = nullptr;
276  return n;
277 }
278 
279 #endif // __SYS_ParseNumber__
typedef int(APIENTRYP RE_PFNGLXSWAPINTERVALSGIPROC)(int)
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glcorearb.h:2540
GLbitfield flags
Definition: glcorearb.h:1596
unsigned short uint16
Definition: SYS_Types.h:38
int int32
Definition: SYS_Types.h:39
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1222
Invalid arguments were passed in (e.g. bad number base, invalid range).
**But if you need a result
Definition: thread.h:613
unsigned long long uint64
Definition: SYS_Types.h:117
float fpreal32
Definition: SYS_Types.h:200
double fpreal64
Definition: SYS_Types.h:201
unsigned char uint8
Definition: SYS_Types.h:36
GLdouble n
Definition: glcorearb.h:2008
SYS_API SYS_ParseStatus SYSparseInteger(const char *begin, const char *&end, int8 &number, int base=0, SYS_ParseFlags flags=SYS_ParseFlags::None)
GLuint GLuint end
Definition: glcorearb.h:475
Allow underscore ('_') to be used as a digit separator.
long long int64
Definition: SYS_Types.h:116
const TypeMask operator|(const TypeMask &m1, const TypeMask &m2)
Definition: GA_PrimCompat.h:79
signed char int8
Definition: SYS_Types.h:35
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1222
SYS_API SYS_ParseStatus SYSparseFloat(const char *begin, const char *&end, fpreal16 &number, SYS_ParseFlags flags=SYS_ParseFlags::None)
const TypeMask operator&(const TypeMask &m1, const TypeMask &m2)
Definition: GA_PrimCompat.h:84
SYS_ParseStatus
List of possible states the parsing ended in.
SYS_ParseFlags
short int16
Definition: SYS_Types.h:37
fpreal64 fpreal
Definition: SYS_Types.h:277
A number was successfully parsed.
unsigned int uint32
Definition: SYS_Types.h:40
#define SYS_API
Definition: SYS_API.h:11
unsigned int uint
Definition: SYS_Types.h:45
PcpNodeRef_ChildrenIterator begin(const PcpNodeRef::child_const_range &r)
Support for range-based for loops for PcpNodeRef children ranges.
Definition: node.h:483