Last active
June 12, 2017 00:02
-
-
Save lallousx86/757a137ae5ddea45d1d8 to your computer and use it in GitHub Desktop.
strtok wrapper / tokenizer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
////////////////////////////////////////////////////////////////////////// | |
class QuickTokenizer | |
{ | |
private: | |
char *buf; | |
char *token; | |
char *ctx; | |
void FreeBuffers() | |
{ | |
if (this->token != NULL) | |
{ | |
free(this->token); | |
this->token = NULL; | |
} | |
if (this->buf != NULL) | |
{ | |
free(this->buf); | |
this->buf = NULL; | |
} | |
} | |
public: | |
QuickTokenizer() : buf(NULL), token(NULL) | |
{ | |
} | |
const char *Tokenize( | |
const char *str, | |
const char *tok) | |
{ | |
this->buf = _strdup(str); | |
this->token = _strdup(tok); | |
return strtok_s(buf, token, &ctx); | |
} | |
const char *NextToken() | |
{ | |
return strtok_s(NULL, token, &ctx); | |
} | |
~QuickTokenizer() | |
{ | |
FreeBuffers(); | |
} | |
void test() | |
{ | |
char *in_str = "a;b;c;d;e;f;g;h"; | |
for (const char *t = Tokenize(in_str, ";"); t != NULL; t = NextToken()) | |
{ | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment