/dt-command

Simple library for parsing text commands of simple format

Primary LanguageC

dt-command is a simple library for parsing text commands of simple format.

It allows to parse commands in following format:

command(arg0, arg1, arg2, ..., argn)\n

where argN consists of one or two values in following formats:
X string

X - is an integer, which stands for length of string. string may contain any characters except for '\0' (terminal zero).
X must be a positive value. string is delimited from X by exactly 1 space character.

Special cases are:
0
-1

"0: stands for "empty string" (string with length of zero).
"-1" stands for "no argument".
For both special cases no value is present, and space separating length and value is omitted too.

Arguments are separated by comma and space.
Argument count is variable and may be zero.
Command must have a command name, opening and closing argument brackets and end with newline symbol
Examples of valid commands:

cmd(6 value1, -1, 6 value2)
cmd2()
cmd3(-1)
cmd4(6 value1, 0, 5 value, -1, 39 one_more_value "with spaces and quotes")

Functions dt_validate_command and dt_parse_command require buffer to contain newline character, i.e. to have one full command in the buffer,
otherwise it'll cause undefined behaviour.

The returned structure contains command name, arguments count and array of arguments, where argument may be a NULL pointer if nil was passed,
or a pointer to string otherwise.