FS2_Open
Open source remastering of the Freespace 2 engine
consoleparse.cpp File Reference
#include "debugconsole/consoleparse.h"
#include "parse/parselo.h"
#include <algorithm>
#include <cmath>
#include <cstdarg>
#include <cstring>
#include <limits.h>

Go to the source code of this file.

Enumerations

enum  state_int {
  si_start = 0, si_end = 1, si_invalid, si_sign,
  si_prefix, si_numeral, si_numeral_bin, si_numeral_octal,
  si_numeral_hex
}
 
enum  state_float {
  sf_start = 0, sf_end = 1, sf_invalid, sf_sign,
  sf_whole, sf_decimal, sf_fraction, sf_expprefix,
  sf_expsign, sf_exponent
}
 

Functions

bool ch_is_sign (char ch)
 
bool ch_is_numeral (char ch)
 
bool ch_is_decimal (char ch)
 
bool ch_is_binary (char ch)
 
bool ch_is_octal (char ch)
 
bool ch_is_hex (char ch)
 
bool ch_is_prefix (char ch)
 
bool ch_is_binary_prefix (char ch)
 
bool ch_is_octal_prefix (char ch)
 
bool ch_is_hex_prefix (char ch)
 
bool ch_is_exp_prefix (char ch)
 
void dc_get_token (SCP_string &out_str)
 Returns/Advances past a single token. More...
 
void dc_get_token_no_advance (SCP_string &out_str)
 Returns a single token, but does not advances Cp. More...
 
double dc_parse_double (const char *ch, dc_token type)
 Parses a double-precision floating point type. Supports, Whole, Fractional, and Mixed numbers, and supports scientific notation (exponent prefixed by 'e' or 'E'). More...
 
long dc_parse_long (const char *ch, dc_token type)
 Parses a long integral type. Supports decimal, binary, octal, and hexidecimal strings. More...
 
ulong dc_parse_ulong (const char *ch, dc_token type)
 Parses an unsigned long integral type. Supports decimal, binary, octal, and hexidecimal strings. More...
 
state_float dc_parse_double_sign (const char *&ch_ptr, SCP_string &buffer_str)
 
state_float dc_parse_double_whole (const char *&ch_ptr, SCP_string &buffer_str)
 
state_float dc_parse_double_decimal (const char *&ch_ptr, SCP_string &buffer_str)
 
state_float dc_parse_double_fraction (const char *&ch_ptr, SCP_string &buffer_str)
 
state_float dc_parse_double_expprefix (const char *&ch_ptr, SCP_string &buffer_str)
 
state_float dc_parse_double_expsign (const char *&ch_ptr, SCP_string &buffer_str)
 
state_float dc_parse_double_exponent (const char *&ch_ptr, SCP_string &buffer_str)
 
state_int dc_parse_long_prefix (const char *&ch_ptr, SCP_string &buffer_str, int &base)
 
state_int dc_parse_long_sign (const char *&ch_ptr, SCP_string &buffer_str)
 
state_int dc_parse_long_numeral (const char *&ch_ptr, SCP_string &buffer_str)
 
state_int dc_parse_long_numeral_bin (const char *&ch_ptr, SCP_string &buffer_str)
 
state_int dc_parse_long_numeral_hex (const char *&ch_ptr, SCP_string &buffer_str)
 
state_int dc_parse_long_numeral_octal (const char *&ch_ptr, SCP_string &buffer_str)
 
void dc_ignore_white_space (void)
 Advances the parser past whitespace characters. More...
 
void dc_ignore_gray_space (void)
 Advances the parser past grayspace characters. More...
 
bool dc_maybe_stuff_float (float *f)
 Tries to stuff a float from the Command_string. More...
 
bool dc_maybe_stuff_int (int *i)
 Tries to stuff an int from the Command_string. More...
 
bool dc_maybe_stuff_uint (uint *i)
 Tries to stuff an uint from the Command_string. More...
 
bool dc_maybe_stuff_ubyte (ubyte *i)
 Tries to stuff an ubyte from the Command_string. More...
 
bool dc_maybe_stuff_boolean (bool *b)
 Tries to stuff a bool from the Command_string. More...
 
bool dc_maybe_stuff_boolean (int *i)
 Tries to stuff an int with a bool value from the Command_string. More...
 
bool dc_maybe_stuff_string (char *out_str, size_t maxlen)
 Tries to stuff a string to out_str from the command line, stopping at the end of the command line. More...
 
bool dc_maybe_stuff_string (SCP_string &out_str)
 Tries to stuff a string to out_str from the command line, stopping at the end of the command line. More...
 
bool dc_maybe_stuff_string_white (char *str, size_t len)
 Tries to stuff a whitespace delimited string to out_str from the command line, stopping at the end of the command line. More...
 
bool dc_maybe_stuff_string_white (SCP_string &str)
 Tries to stuff a whitespace delimited string to out_str from the command line, stopping at the end of the command line. More...
 
void dc_required_string (char *pstr)
 Searches for a specified required string, throwing an errParse if not found. More...
 
int dc_required_string_either (char *str1, char *str2)
 Searchs for either of the specified required strings, throwing an errParse if neither are found. More...
 
uint dc_required_string_any (const uint n,...)
 Searches for specified required strings. More...
 
bool dc_optional_string (const char *pstr)
 Searches for an optional string. More...
 
bool dc_optional_string_either (const char *str1, const char *str2)
 Searches for an optional string and it's alias. More...
 
void dc_parse_init (SCP_string &str)
 Initializes the DC command line parser. More...
 
void dc_stuff_float (float *f)
 Stuffs a float to the given variable. More...
 
void dc_stuff_int (int *i)
 Stuffs an int to the given variable. Supports binary (0b), hexadecimal (0x), and octal (0o) formats. More...
 
void dc_stuff_uint (uint *i)
 Stuffs an unsigned int to the given variable. Supports binary (0b), hexadecimal (0x), and octal (0o) formats. More...
 
void dc_stuff_ubyte (ubyte *i)
 Stuffs an unsigned byte to the given variable. Supports binary (0b), hexadecimal (0x), and octal (0o) formats. More...
 
void dc_stuff_boolean (bool *b)
 stuffs a boolean evaluated integer or string into the given variable. More...
 
void dc_stuff_boolean (int *i)
 stuffs a boolean evaluated integer or string into the given variable. More...
 
void dc_stuff_string (char *out_str, size_t maxlen=MAX_TOKEN_LENGTH)
 Stuffs a string to out_str from the command line, stopping at the end of the command line. More...
 
void dc_stuff_string (SCP_string &out_str)
 Stuffs a string to out_str from the command line, stopping at the end of the command line. More...
 
void dc_stuff_string_white (char *out_str, size_t maxlen)
 Stuffs a whitespace delimited string to out_str from the command line, stopping at the end of the command line. More...
 
void dc_stuff_string_white (SCP_string &out_str)
 Stuffs a whitespace delimited string to out_str from the command line, stopping at the end of the command line. More...
 

Variables

char Command_string [MAX_CLI_LEN]
 Command string buffer. More...
 
char * Cp = NULL
 Pointer to the commant string. More...
 

Enumeration Type Documentation

Enumerator
sf_start 
sf_end 
sf_invalid 
sf_sign 

Sign character for mantessa.

sf_whole 

Whole value numeral.

sf_decimal 

Decimal character.

sf_fraction 

Fractional value numeral.

sf_expprefix 

Exponent prefix, 'e', 'E'.

sf_expsign 

Exponent sign.

sf_exponent 

Exponent value numeral.

Definition at line 42 of file consoleparse.cpp.

enum state_int
Enumerator
si_start 
si_end 
si_invalid 
si_sign 

Sign character, '-' '+'.

si_prefix 

prefix character sequence, 0b, 0o, or 0x

si_numeral 

Numeral state, 0 - 9.

si_numeral_bin 

Numeral altstate, 0, 1.

si_numeral_octal 

Numeral altstate, 0 - 7.

si_numeral_hex 

Numeral altstate, 0 - 9 and 'a' - 'f'.

Definition at line 30 of file consoleparse.cpp.

Function Documentation

bool ch_is_binary ( char  ch)
inline

Definition at line 167 of file consoleparse.cpp.

bool ch_is_binary_prefix ( char  ch)
inline

Definition at line 191 of file consoleparse.cpp.

bool ch_is_decimal ( char  ch)
inline

Definition at line 161 of file consoleparse.cpp.

bool ch_is_exp_prefix ( char  ch)
inline

Definition at line 209 of file consoleparse.cpp.

bool ch_is_hex ( char  ch)
inline

Definition at line 179 of file consoleparse.cpp.

bool ch_is_hex_prefix ( char  ch)
inline

Definition at line 203 of file consoleparse.cpp.

bool ch_is_numeral ( char  ch)
inline

Definition at line 155 of file consoleparse.cpp.

bool ch_is_octal ( char  ch)
inline

Definition at line 173 of file consoleparse.cpp.

bool ch_is_octal_prefix ( char  ch)
inline

Definition at line 197 of file consoleparse.cpp.

bool ch_is_prefix ( char  ch)
inline

Definition at line 185 of file consoleparse.cpp.

bool ch_is_sign ( char  ch)
inline

Definition at line 149 of file consoleparse.cpp.

void dc_get_token ( SCP_string out_str)

Returns/Advances past a single token.

Similar in operation to dc_stuff_string_white, but won't throw any error messages and will only grab the first word. (dc_stuff_string_white may grab quoted strings)

Definition at line 214 of file consoleparse.cpp.

void dc_get_token_no_advance ( SCP_string out_str)

Returns a single token, but does not advances Cp.

Similar in operation to dc_stuff_string_white, but won't throw any error messages and will only grab the first word. (dc_stuff_string_white may grab quoted strings).

Definition at line 242 of file consoleparse.cpp.

void dc_ignore_gray_space ( void  )

Advances the parser past grayspace characters.

Definition at line 273 of file consoleparse.cpp.

void dc_ignore_white_space ( void  )

Advances the parser past whitespace characters.

Definition at line 267 of file consoleparse.cpp.

bool dc_maybe_stuff_boolean ( bool *  b)

Tries to stuff a bool from the Command_string.

Parameters
[in]bThe bool variable to maybe stuff.

If there's nothing on the command line, *b = false and false is returned

If there's something on the command line, and we're able to convert it, *b = the converted value, true is returned, and the parser is advanced past the token

If there's something on command line, but we can't convert it, an errParse is thrown

Definition at line 332 of file consoleparse.cpp.

bool dc_maybe_stuff_boolean ( int i)

Tries to stuff an int with a bool value from the Command_string.

Parameters
[in]iThe int variable to maybe stuff.

If there's nothing on the command line, *i = 0 and false is returned

If there's something on the command line, and we're able to convert it, *i = the converted value, true is returned, and the parser is advanced past the token

If there's something on command line, but we can't convert it, an errParse is thrown

Definition at line 345 of file consoleparse.cpp.

bool dc_maybe_stuff_float ( float f)

Tries to stuff a float from the Command_string.

Parameters
[in]fThe float variable to maybe stuff.

If there's nothing on the command line, *f = 0 and false is returned

If there's something on the command line, and we're able to convert it, *f = the converted value, true is returned, and the parser is advanced past the token

If there's something on command line, but we can't convert it, an errParse is thrown

Definition at line 279 of file consoleparse.cpp.

bool dc_maybe_stuff_int ( int i)

Tries to stuff an int from the Command_string.

Parameters
[in]iThe int variable to maybe stuff.

If there's nothing on the command line, *i = 0 and false is returned

If there's something on the command line, and we're able to convert it, *i = the converted value, true is returned, and the parser is advanced past the token

If there's something on command line, but we can't convert it, an errParse is thrown

Definition at line 293 of file consoleparse.cpp.

bool dc_maybe_stuff_string ( char *  str,
size_t  len 
)

Tries to stuff a string to out_str from the command line, stopping at the end of the command line.

Parameters
[out]strDestination string. If nothing was found, str = ""
[in]lenMaximum length to copy. (len <= sizeof(str)) && (len <= MAX_TOKEN_LENGTH)
Return values
trueif the operation was successful,
falseotherwise
Exceptions
errParseOverflowwhen parser cannot stuff the entirety of the found string into out_str

Definition at line 358 of file consoleparse.cpp.

bool dc_maybe_stuff_string ( SCP_string str)

Tries to stuff a string to out_str from the command line, stopping at the end of the command line.

Parameters
[out]strDestination string. If nothing was found, str = ""
Return values
trueif the operation was successful,
falseotherwise

Definition at line 396 of file consoleparse.cpp.

bool dc_maybe_stuff_string_white ( char *  str,
size_t  len 
)

Tries to stuff a whitespace delimited string to out_str from the command line, stopping at the end of the command line.

Parameters
[out]strDestination string. If nothing was found, str = ""
[in]lenMaximum length to copy. (maxlen <= sizeof(str)) && (maxlen <= MAX_TOKEN_LENGTH)
Return values
trueif the operation was successful,
falseotherwise
Exceptions
errParseOverflowwhen parser cannot stuff the entirety of the found string into out_str

Definition at line 433 of file consoleparse.cpp.

bool dc_maybe_stuff_string_white ( SCP_string str)

Tries to stuff a whitespace delimited string to out_str from the command line, stopping at the end of the command line.

Parameters
[out]strDestination string. If nothing was found, str = ""
Return values
trueif the operation was successful,
falseotherwise

Definition at line 446 of file consoleparse.cpp.

bool dc_maybe_stuff_ubyte ( ubyte i)

Tries to stuff an ubyte from the Command_string.

Parameters
[in]iThe ubyte variable to maybe stuff.

If there's nothing on the command line, *i = 0 and false is returned

If there's something on the command line, and we're able to convert it, *i = the converted value, true is returned, and the parser is advanced past the token

If there's something on command line, but we can't convert it, an errParse is thrown

Definition at line 319 of file consoleparse.cpp.

bool dc_maybe_stuff_uint ( uint i)

Tries to stuff an uint from the Command_string.

Parameters
[in]iThe uint variable to maybe stuff.

If there's nothing on the command line, *i = 0 and false is returned

If there's something on the command line, and we're able to convert it, *i = the converted value, true is returned, and the parser is advanced past the token

If there's something on command line, but we can't convert it, an errParse is thrown

Definition at line 306 of file consoleparse.cpp.

bool dc_optional_string ( const char *  pstr)

Searches for an optional string.

Parameters
[in]pstrThe char[] to look for
Return values
trueif the string was found,
falseotherwise

Definition at line 558 of file consoleparse.cpp.

bool dc_optional_string_either ( const char *  str1,
const char *  str2 
)

Searches for an optional string and it's alias.

Parameters
[in]str1The char[] to look for
[in]str2The alternative char[] to look for
Return values
trueif the string was found,
falseotherwise

Definition at line 574 of file consoleparse.cpp.

double dc_parse_double ( const char *  ch,
dc_token  type 
)

Parses a double-precision floating point type. Supports, Whole, Fractional, and Mixed numbers, and supports scientific notation (exponent prefixed by 'e' or 'E').

Parameters
[in]chPoints to the start of the string to parse
[in]typeThe expected type. is thrown along with ch when an unexpected/malformed float is found
Returns
The value of the parsed token

The returned double may be cast to a single-precision float, but be sure to check it before doing so!

Definition at line 602 of file consoleparse.cpp.

state_float dc_parse_double_decimal ( const char *&  ch_ptr,
SCP_string buffer_str 
)

Definition at line 749 of file consoleparse.cpp.

state_float dc_parse_double_exponent ( const char *&  ch_ptr,
SCP_string buffer_str 
)

Definition at line 846 of file consoleparse.cpp.

state_float dc_parse_double_expprefix ( const char *&  ch_ptr,
SCP_string buffer_str 
)

Definition at line 803 of file consoleparse.cpp.

state_float dc_parse_double_expsign ( const char *&  ch_ptr,
SCP_string buffer_str 
)

Definition at line 827 of file consoleparse.cpp.

state_float dc_parse_double_fraction ( const char *&  ch_ptr,
SCP_string buffer_str 
)

Definition at line 776 of file consoleparse.cpp.

state_float dc_parse_double_sign ( const char *&  ch_ptr,
SCP_string buffer_str 
)

Definition at line 693 of file consoleparse.cpp.

state_float dc_parse_double_whole ( const char *&  ch_ptr,
SCP_string buffer_str 
)

Definition at line 717 of file consoleparse.cpp.

void dc_parse_init ( SCP_string str)

Initializes the DC command line parser.

Definition at line 596 of file consoleparse.cpp.

long dc_parse_long ( const char *  ch,
dc_token  type 
)

Parses a long integral type. Supports decimal, binary, octal, and hexidecimal strings.

Parameters
[in]chPoints to the start of the string to parse.
[in]typeThe expected type. Is thrown along with ch when an unexpected/malformed integral is found

! Non-decimal values must be prefixed by their corresponding sequence. Binary: "0b", Octal: "0o", Hex: "0x"

The returned long may be cast to a smaller integral, but be sure to check it before doing so!

The only thing left making this function specific to the DC is the expected type. So, if you want to use this for parsing something other than the debug CL, you'll have to make a set of errParse classes that take a different expected type.

Definition at line 868 of file consoleparse.cpp.

state_int dc_parse_long_numeral ( const char *&  ch_ptr,
SCP_string buffer_str 
)
inline

Definition at line 1375 of file consoleparse.cpp.

state_int dc_parse_long_numeral_bin ( const char *&  ch_ptr,
SCP_string buffer_str 
)
inline

Definition at line 1397 of file consoleparse.cpp.

state_int dc_parse_long_numeral_hex ( const char *&  ch_ptr,
SCP_string buffer_str 
)
inline

Definition at line 1418 of file consoleparse.cpp.

state_int dc_parse_long_numeral_octal ( const char *&  ch_ptr,
SCP_string buffer_str 
)
inline

Definition at line 1438 of file consoleparse.cpp.

state_int dc_parse_long_prefix ( const char *&  ch_ptr,
SCP_string buffer_str,
int base 
)
inline

Definition at line 1309 of file consoleparse.cpp.

state_int dc_parse_long_sign ( const char *&  ch_ptr,
SCP_string buffer_str 
)
inline

Definition at line 1349 of file consoleparse.cpp.

ulong dc_parse_ulong ( const char *  ch,
dc_token  type 
)

Parses an unsigned long integral type. Supports decimal, binary, octal, and hexidecimal strings.

Parameters
[in]chPoints to the start of the string to parse.
[in]typeThe expected type. Is thrown along with ch when an unexpected/malformed integral is found

! Non-decimal values must be delimited by their corresponding sequence. Binary: "0b", Octal: "0o", Hex: "0x" The returned long may be cast to a smaller integral, but be sure to check it before doing so! The only thing left making this function specific to the DC is the expected type. So, if you want to use this for parsing something other than the debug CL, you'll have to make a set of errParse classes that take a different expected type.

Definition at line 968 of file consoleparse.cpp.

void dc_required_string ( char *  pstr)

Searches for a specified required string, throwing an errParse if not found.

Parameters
[in]pstrThe string to search for
Exceptions
errParseStringwith the required string

Definition at line 459 of file consoleparse.cpp.

uint dc_required_string_any ( const uint  n,
  ... 
)

Searches for specified required strings.

Parameters
[in]nThe number of char[] or c_str's given
[in]...A comma delimited list of one or more required strings.
Returns
The index of the specified required strings (as if they were an array)
Exceptions
errParseStringwith the required strings

Definition at line 516 of file consoleparse.cpp.

int dc_required_string_either ( char *  str1,
char *  str2 
)

Searchs for either of the specified required strings, throwing an errParse if neither are found.

Parameters
[in]str1The first string to search for
[in]str2The second string to search for
Return values
0if str1 was found, or
1if str2 was found
Exceptions
errParseStringwith the required strings

Definition at line 484 of file consoleparse.cpp.

void dc_stuff_boolean ( bool *  b)

stuffs a boolean evaluated integer or string into the given variable.

Parameters
[in]bThe bool variable to stuff to

Supports a number of literal strings as true and false, including "true", "false", "yes", "no" and the yes/no equivalents in other languages supported in localization. TODO: Make a static string map to handle this instead of being hard-coded

Exceptions
errParseif an unexpected or otherwise malformed float string is found.
errParseif nothing was found

Definition at line 1130 of file consoleparse.cpp.

void dc_stuff_boolean ( int i)

stuffs a boolean evaluated integer or string into the given variable.

Parameters
[in]iThe int variable to stuff to. 0 is false, 1 is true

Supports a number of literal strings as true and false, including "true", "false", "yes", "no" and the yes/no equivalents in other languages supported in localization. TODO: Make a static string map to handle this instead of being hard-coded

Exceptions
errParseif an unexpected or otherwise malformed float string is found.
errParseif nothing was found

Definition at line 1163 of file consoleparse.cpp.

void dc_stuff_float ( float f)

Stuffs a float to the given variable.

Parameters
[in]fThe float variable to stuff to
Exceptions
errParseif an unexpected or otherwise malformed float string is found.
errParseif nothing was found

Definition at line 1057 of file consoleparse.cpp.

void dc_stuff_int ( int i)

Stuffs an int to the given variable. Supports binary (0b), hexadecimal (0x), and octal (0o) formats.

Parameters
[in]iThe int variable to stuff to

The binary, hexadecimal, and octal formats must be prefixed by their associated string. Ex: "0xDEADBEEF" would be parsed properly while "DEADBEEF" would throw an error

Exceptions
errParseif an unexpected or otherwise malformed float string is found.
errParseif nothing was found

Definition at line 1075 of file consoleparse.cpp.

void dc_stuff_string ( char *  str,
size_t  maxlen 
)

Stuffs a string to out_str from the command line, stopping at the end of the command line.

Parameters
[out]strDestination string
[in]maxlenMaximum length to copy. (maxlen <= sizeof(str)) && (maxlen <= MAX_TOKEN_LENGTH)
Exceptions
errParsewhen nothing left was found on the command line
errParseOverflowwhen parser cannot stuff the entirety of the found string into out_str

Definition at line 1172 of file consoleparse.cpp.

void dc_stuff_string ( SCP_string str)

Stuffs a string to out_str from the command line, stopping at the end of the command line.

Parameters
[out]strDestination string
Exceptions
errParsewhen nothing left was found on the command line

Definition at line 1208 of file consoleparse.cpp.

void dc_stuff_string_white ( char *  str,
size_t  len 
)

Stuffs a whitespace delimited string to out_str from the command line, stopping at the end of the command line.

Parameters
[out]strDestination string
[in]lenMaximum length to copy. (len <= sizeof(str)) && (len <= MAX_TOKEN_LENGTH)
Exceptions
errParsewhen nothing left was found on the command line
errParseOverflowwhen parser cannot stuff the entirety of the found string into out_str

Definition at line 1241 of file consoleparse.cpp.

void dc_stuff_string_white ( SCP_string str)

Stuffs a whitespace delimited string to out_str from the command line, stopping at the end of the command line.

Parameters
[out]strDestination string
Exceptions
errParsewhen nothing left was found on the command line

Definition at line 1275 of file consoleparse.cpp.

void dc_stuff_ubyte ( ubyte i)

Stuffs an unsigned byte to the given variable. Supports binary (0b), hexadecimal (0x), and octal (0o) formats.

Parameters
[in]iThe ubyte variable to stuff to

The binary, hexadecimal, and octal formats must be prefixed by their associated string. Ex: "0x0F" would be parsed properly while "0F" would throw an error

Exceptions
errParseif an unexpected or otherwise malformed float string is found.
errParseif nothing was found

Definition at line 1111 of file consoleparse.cpp.

void dc_stuff_uint ( uint i)

Stuffs an unsigned int to the given variable. Supports binary (0b), hexadecimal (0x), and octal (0o) formats.

Parameters
[in]iThe unsigned int variable to stuff to

The binary, hexadecimal, and octal formats must be prefixed by their associated string. Ex: "0xDEADBEEF" would be parsed properly while "DEADBEEF" would throw an error

Exceptions
errParseif an unexpected or otherwise malformed float string is found.
errParseif nothing was found

Definition at line 1093 of file consoleparse.cpp.

Variable Documentation

char Command_string[MAX_CLI_LEN]

Command string buffer.

Definition at line 27 of file consoleparse.cpp.

char* Cp = NULL

Pointer to the commant string.

Definition at line 28 of file consoleparse.cpp.