Skip to content

Instantly share code, notes, and snippets.

@lallousx86
Last active June 12, 2017 00:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lallousx86/757a137ae5ddea45d1d8 to your computer and use it in GitHub Desktop.
Save lallousx86/757a137ae5ddea45d1d8 to your computer and use it in GitHub Desktop.
strtok wrapper / tokenizer
//////////////////////////////////////////////////////////////////////////
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