In this article, I share with you a simple C++ class that wraps the string tokenization function strtok(). The QuickTokenizer class makes it easy to tokenize and enumerate the tokens in a thread-safe manner.
The class code
////////////////////////////////////////////////////////////////////////// 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(); } };