This section of the archives stores flipcode's complete Developer Toolbox collection,
featuring a variety of mini-articles and source code contributions from our readers.

Parsing Console Commands
Submitted by

The C function strtok (defined in string.h) is great for writing basic text
parsers. For those who haven't encountered it before, strtok splits a string
into tokens delimited by user defined separators, eg. whitespace, newlines
or perhaps commas.

Sadly, it's not quite up to the task of parsing in-game console commands
that take strings containing spaces as arguments, eg. <code>say_team "A
string with spaces"</code> as it cannot be made to understand that
delimiters should be ignored when enclosed by quotes.

I love the simplicity of using strtok, so I decided to code 'ConsoleStrTok'
which works in a similar fashion to strtok, and makes the whole process of
parsing console commands dead easy. ConsoleStrTok tokenizes the input
string, returning each token as a string. A token is either a string of
non-whitespace characters, or a string of arbitrary characters that were
enclosed by quotes in the input string. This way you can treat each token
the same, with no need for special case code.

Parsing is similar to using strtok - for the first call you pass the pointer
to the string to be tokenized, and for subsequent calls you pass
in NULL. Each time you get back a pointer the current token or NULL if
no more can be found.

The input string will have null characters inserted at the end of each
token. Keep a copy if you need the original again!

Token delimiters are hard coded as whitespace (tab, space, newline)

Parsing multiple strings at once is not possible, as I use an internal
static variable. For most purposes this won't be a problem, but if you
really need to parse multiple strings at once, I recommend placing the
routine into its own class, with pNext as a non-static member variable.

As a bonus side effect, parsing "say_team" "hello" will
return the same tokens as say_team "hello" So you could
even have console commands containing spaces, if you're feeling
different! ;)

The C function strtok (defined in string.h) is great for writing basic text
parsers. For those who haven't encountered it before, strtok splits a string
into tokens delimited by user defined separators, eg. whitespace, newlines
or perhaps commas.

Sadly, it's not quite up to the task of parsing in-game console commands
that take strings containing spaces as arguments, eg. <code>say_team "A
string with spaces"</code> as it cannot be made to understand that
delimiters should be ignored when enclosed by quotes.

I love the simplicity of using strtok, so I decided to code 'ConsoleStrTok'
which works in a similar fashion to strtok, and makes the whole process of
parsing console commands dead easy. ConsoleStrTok tokenizes the input
string, returning each token as a string. A token is either a string of
non-whitespace characters, or a string of arbitrary characters that were
enclosed by quotes in the input string. This way you can treat each token
the same, with no need for special case code.

Parsing is similar to using strtok - for the first call you pass the pointer
to the string to be tokenized, and for subsequent calls you pass
in NULL. Each time you get back a pointer the current token or NULL if
no more can be found.

* The input string will have null characters inserted at the end of each
token. Keep a copy if you need the original again!
* Token delimiters are hard coded as whitespace (tab, space, newline)
* Parsing multiple strings at once is not possible, as I use an internal
static variable. For most purposes this won't be a problem, but if you
really need to parse multiple strings at once, I recommend placing the
routine into its own class, with pNext as a non-static member variable.
* As a bonus side effect, parsing <code>"say_team" "hello"</code> will
return the same tokens as <code>say_team "hello"</code> So you could
even have console commands containing spaces, if you're feeling
different! ;)