cisst-saw
|
#include <cmnStreamRawParser.h>
Public Member Functions | |
cmnStreamRawParser () | |
~cmnStreamRawParser () | |
template<class _elementType > | |
bool | AddEntry (const std::string &name, _elementType &data, char delim= ' ', bool req=true) |
template<class _elementType > | |
bool | AddEntryStreamable (const std::string &name, _elementType &data, bool req=true) |
void | SetAllValid (bool val=true) |
bool | Parse (std::istream &inputStream) |
bool | IsValid (const std::string &name) const |
void | ToStream (std::ostream &outputStream) const |
cmnStreamRawParser provides the ability to parse {keyword, value} pairs from a stream. For example, consider the case where we have the following configuration variables:
These variables can be initialized from the following input file (called "test.txt"):
The code to do this is as follows:
If successful, the variables S, V1, V2, and D will be assigned the values from the file. Note that the variable names in the code do not have to be the same as the string names (in this example, the variables names are lower case and the strings are upper case).
For debugging (or to recreate the text file) call:
Note that the AddEntry method is templated, so that any data type can be used, as long as its cmnData class is defined (for the DeSerializeText and SerializeText methods). Similarly, the AddEntryStreamable method is templated, so that any data type can be used that defines the stream in (>>) and stream out (<<) operators.
Current limitations:
|
inline |
cmnStreamRawParser::~cmnStreamRawParser | ( | ) |
|
inline |
Add a {keyword,value} pair to be parsed from the input stream.
name | Variable name (key) |
data | Variable to be assigned when input stream is parsed |
delim | Delimiter between data elements (but not between keyword and data) |
|
inline |
Add a {keyword,value} pair to be parsed from the input stream. This method is for data types where the stream operators (<< and >>) can be used; in particular, for primitive C data types (e.g., int, double) and std::string.
name | Variable name (key) |
data | Variable to be assigned when input stream is parsed |
bool cmnStreamRawParser::IsValid | ( | const std::string & | name | ) | const |
Returns true if valid data was parsed.
bool cmnStreamRawParser::Parse | ( | std::istream & | inputStream | ) |
Parse the input stream, extracting the data for all keywords that are found. Returns true if valid data extracted for all required keywords.
void cmnStreamRawParser::SetAllValid | ( | bool | val = true | ) |
Set all entries valid or invalid.
void cmnStreamRawParser::ToStream | ( | std::ostream & | outputStream | ) | const |
Write all {keyword,value} pairs to the output stream. If valid data was not parsed, will write "(invalid)".