This is a class for parsing text. More...
#include "TextParser.hpp"
Classes | |
class | ParseError |
Error when parsing a text/file. More... | |
Public Member Functions | |
TextParserT (const char *Input, std::string Delims="", bool IsFileName=true, const char CommentChar='\0') | |
The constructor. More... | |
std::string | GetNextToken () |
Returns the next token. More... | |
int | GetNextTokenAsInt () |
Returns the next token as an int. More... | |
float | GetNextTokenAsFloat () |
Returns the next token as a float. More... | |
void | PutBack (const std::string &Token) |
Puts back the string Token, such that the next call to GetNextToken() returns Token. More... | |
std::string | PeekNextToken () |
Returns a peek at the next token without reading over it. More... | |
void | AssertAndSkipToken (const std::string &Token) |
Makes sure that the next token is equal to Token. More... | |
bool | WasLastTokenQuoted () const |
Returns whether the last read "real" token was a "quoted" token. More... | |
std::string | SkipLine () |
Skips tokens until the end of the current line of text. More... | |
std::string | SkipBlock (const std::string &OpeningToken, const std::string &ClosingToken, bool CallerAlreadyReadOpeningToken) |
Skips a whole "block" of tokens, e.g. More... | |
unsigned long | GetReadPosByte () const |
Returns the current read position in the input file in byte. More... | |
float | GetReadPosPercent () const |
Returns the current read position in the input file in percent. More... | |
bool | IsAtEOF () const |
Returns whether the parser has reached the EOF or not. More... | |
This is a class for parsing text.
It has the following features: a) C++ style comments ("// ...") are recognized. b) Quoted tokens are recognized (ie. "1 2 3" is returned as ONE token).
TextParserT::TextParserT | ( | const char * | Input, |
std::string | Delims = "" , |
||
bool | IsFileName = true , |
||
const char | CommentChar = '\0' |
||
) |
The constructor.
If IsFileName=true, Input specifies the name of the input file. If IsFileName=false, Input is interpreted as the input string itself. If the constructor experiences any problems (e.g. the input file cannot be read), it creates a text parser for the text of length 0 (the empty text). Delims specifies token delimiters that are returned as separate tokens even if they are not bordered by white-space. CommentChar specifies the character that starts a comment. The default 0 makes "//" being recognized as comment start. If CommentChar has another value, that character is considered as initiating a comment.
Input | An input string or the filename of an input file depending on IsFileName. |
Delims | Specifies token delimiters that are returned as separate tokens even if they are not bordered by white-space. |
IsFileName | Whether the input string is a real string to parse or the name of a file to parse. |
CommentChar | Char that initiates a comment line (default is '//'). |
void TextParserT::AssertAndSkipToken | ( | const std::string & | Token | ) |
Makes sure that the next token is equal to Token.
Otherwise, a ParseError is thrown. This is short for: "if (TP.GetNextToken()!=Token) throw TextParserT::ParseError();".
Token | The string asserted as the next token. |
ParseError |
std::string TextParserT::GetNextToken | ( | ) |
Returns the next token.
ParseError is thrown when an error is encountered (e.g. EOF), after which the parsing cannot be continued.
ParseError |
float TextParserT::GetNextTokenAsFloat | ( | ) |
Returns the next token as a float.
ParseError is thrown when an error is encountered (e.g. EOF), after which the parsing cannot be continued.
ParseError |
int TextParserT::GetNextTokenAsInt | ( | ) |
Returns the next token as an int.
ParseError is thrown when an error is encountered (e.g. EOF), after which the parsing cannot be continued.
ParseError |
unsigned long TextParserT::GetReadPosByte | ( | ) | const |
Returns the current read position in the input file in byte.
Tokens that have been put back are not taken into account.
float TextParserT::GetReadPosPercent | ( | ) | const |
Returns the current read position in the input file in percent.
Tokens that have been put back are not taken into account.
bool TextParserT::IsAtEOF | ( | ) | const |
Returns whether the parser has reached the EOF or not.
std::string TextParserT::PeekNextToken | ( | ) |
Returns a peek at the next token without reading over it.
This is equivalent to: "std::string T=GetNextToken(); PutBack(T); return T;".
ParseError |
void TextParserT::PutBack | ( | const std::string & | Token | ) |
Puts back the string Token, such that the next call to GetNextToken() returns Token.
This function can be called multiple times in sequence, GetNextToken() will return all put back tokens in a stack-like manner. No checks are performed if Token was actually returned by a previous call by GetNextToken().
Token | The string to put back in the string parsed by TextParserT. |
std::string TextParserT::SkipBlock | ( | const std::string & | OpeningToken, |
const std::string & | ClosingToken, | ||
bool | CallerAlreadyReadOpeningToken | ||
) |
Skips a whole "block" of tokens, e.g.
a { ... } or ( ... ) block. The block can contain nested blocks. It can (and must) be stated if the caller has already read the opening token.
OpeningToken | Opening token of the block. |
ClosingToken | Closing token of the block. |
CallerAlreadyReadOpeningToken | The opening token has already been read by the caller. |
std::string TextParserT::SkipLine | ( | ) |
Skips tokens until the end of the current line of text.
This method always succeeds (it never throws).
|
inline |
Returns whether the last read "real" token was a "quoted" token.
"Real" token means that this method doesn't take into account tokens that have been put back with the PutBack() method. That is, when more than one token has been put back at a time (i.e. PutBack() has been called two times in a row without an intermediate call to one of the Get...() methods), the result of this method is not reliable and should not be used.