41#define NV_MAX_TOKEN_LEN 1024
42#define NV_MAX_DELIM_COUNT 16
55 char mTokBuf[NV_MAX_TOKEN_LEN];
58 char mDelims[NV_MAX_DELIM_COUNT];
64 explicit Tokenizer(
const char* src,
const char* delims =
nullptr)
71 if (
nullptr == delims)
83 while (mNumDelims < NV_MAX_DELIM_COUNT && delims[i])
85 const char c = delims[i++];
87 if (c ==
' ' || c ==
'\t' ||
88 c ==
'"' || c ==
'\'' ||
89 c ==
'\n' || c ==
'\r')
91 mDelims[mNumDelims++] = c;
96 void setConsumeWS(
bool ws) { mConsumeWS = ws; }
98 inline bool isWhitespace(
const char c)
100 return (
' ' == c ||
'\t' == c);
103 inline bool isQuote(
const char c)
105 return (
'"' == c ||
'\'' == c);
108 inline bool isEOL(
const char c)
110 return (
'\n' == c ||
'\r' == c);
113 inline bool isTerm(
const char c)
115 return (isWhitespace(c) || isEOL(c));
118 inline bool isDelim(
const char c)
120 for (uint32_t i = 0; i < mNumDelims; i++)
121 if (c == mDelims[i])
return true;
127 inline bool atEOF() {
129 return (0 == *mSrcBuf);
132 char consumeWhitespace()
136 while (!atEOF() && isWhitespace(*mSrcBuf))
137 mTermChar = *mSrcBuf++;
141 char consumeOneDelim()
147 if (!atEOF() && isDelim(*mSrcBuf)) {
148 mTermChar = *mSrcBuf++;
151 mTermChar = *mSrcBuf;
157 while (!atEOF() && !isEOL(*mSrcBuf))
159 while (!atEOF() && isEOL(*mSrcBuf))
165 char startedWithQuote = 0;
174 if (!atEOF() && isQuote(*mSrcBuf)) {
175 startedWithQuote = *mSrcBuf;
181 if (isEOL(*mSrcBuf)) {
182 mTermChar = *mSrcBuf;
186 if (startedWithQuote) {
187 if (startedWithQuote == *mSrcBuf) {
190 mTermChar = startedWithQuote;
194 else if (isDelim(*mSrcBuf) || isWhitespace(*mSrcBuf)) {
196 mTermChar = *mSrcBuf;
200 mTokBuf[mTokLen] = *mSrcBuf;
206 mTokBuf[mTokLen] = 0;
208 return (mTokLen > 0 || startedWithQuote);
211 bool requireToken(
const char *find)
213 if (find ==
nullptr || find[0] == 0)
218 const size_t findlen = strlen(find);
219 if (findlen != mTokLen)
221 if (0 != memcmp(mTokBuf, find, findlen))
227 bool requireTokenDelim(
const char *find)
229 if (!requireToken(find))
231 if (!consumeOneDelim())
247 returnTok.assign(mTokBuf);
271 returnTok.assign(mTokBuf);
284 if (mTokLen > outmax)
287 memcpy(out, mTokBuf, outmax - 1);
291 memcpy(out, mTokBuf, mTokLen + 1);
302 out = (float)strtod(mTokBuf,
nullptr);
310 char firstdelim = 0, delim = 0;
314 delim = consumeOneDelim();
315 if (delim == firstdelim)
320 out[i++] = (float)strtod(mTokBuf,
nullptr);
323 delim = consumeOneDelim();
324 if (delim && isEOL(delim))
326 if (i == 1) firstdelim = delim;
335 char firstdelim = 0, delim = 0;
339 delim = consumeOneDelim();
340 if (delim == firstdelim)
345 out[i++] = (int32_t)strtol(mTokBuf,
nullptr, 0);
348 delim = consumeOneDelim();
349 if (delim && isEOL(delim))
351 if (i == 1) firstdelim = delim;
363 out = (int32_t)strtol(mTokBuf,
nullptr, 0);
374 out = (uint32_t)strtoul(mTokBuf,
nullptr, 0);
386 (mTokBuf[0] ==
'0' || mTokBuf[0] ==
'1')) {
387 out = (mTokBuf[0] ==
'1');
390 else if ((0 == strcmp(mTokBuf,
"true")) ||
391 (0 == strcmp(mTokBuf,
"TRUE")) ||
392 (0 == strcmp(mTokBuf,
"yes")) ||
393 (0 == strcmp(mTokBuf,
"YES"))) {
397 else if ((0 == strcmp(mTokBuf,
"false")) ||
398 (0 == strcmp(mTokBuf,
"FALSE")) ||
399 (0 == strcmp(mTokBuf,
"no")) ||
400 (0 == strcmp(mTokBuf,
"NO"))) {
A class for tokenizing input character streams.
Definition: tokenizer.h:52
bool getTokenString(std::string &returnTok)
get next token as a std::string
Definition: tokenizer.h:265
bool getTokenString(char out[], const uint32_t outmax)
get next token as a char array with maximum size.
Definition: tokenizer.h:276
uint32_t getTokenIntArray(int32_t out[], uint32_t size)
get next tokens as array of integer numbers
Definition: tokenizer.h:332
bool getLastToken(std::string &returnTok)
get last read token as a std::string
Definition: tokenizer.h:243
bool getTokenFloat(float &out)
get next token as a floating-point number
Definition: tokenizer.h:296
uint32_t getTokenFloatArray(float out[], uint32_t size)
get next tokens as array of floating point numbers
Definition: tokenizer.h:307
bool getTokenUint(uint32_t &out)
get next token as an unsigned integer
Definition: tokenizer.h:368
bool getTokenInt(int32_t &out)
get next token as an integer
Definition: tokenizer.h:357
const char * getLastTokenPtr()
accessor to get last read token const char *
Definition: tokenizer.h:252
char getTermChar() const
accessor to get character that caused 'stop' of last token read
Definition: tokenizer.h:238
uint32_t getLastTokenLen() const
accessor to get last read token length
Definition: tokenizer.h:258
bool getTokenBool(bool &out)
get next token as some form of boolean value/string
Definition: tokenizer.h:379
Definition: collider.cpp:182