cisst-saw
Loading...
Searching...
No Matches
cmnStreamRawParser Class Reference

#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

Detailed Description

cmnStreamRawParser provides the ability to parse {keyword, value} pairs from a stream. For example, consider the case where we have the following configuration variables:

vct3 v1, v2;
double d;
cmnGenericObjectProxy< double > cmnDouble
Definition cmnGenericObjectProxy.h:153
vctFixedSizeVector< double, 3 > vct3
Definition vctFixedSizeVectorTypes.h:46

These variables can be initialized from the following input file (called "test.txt"):

S 10
D 5
V1 1,2,3
V2 4,5,6

The code to do this is as follows:

Config.AddEntry("S", s);
Config.AddEntry("V1", v1, ',');
Config.AddEntry("V2", v2, ',');
Config.AddEntryStreamable("D", d);
std::ifstream file("test.txt");
bool success = Config.Parse(file);
cmnStreamRawParser()
Definition cmnStreamRawParser.h:195
bool AddEntryStreamable(const std::string &name, _elementType &data, bool req=true)
Definition cmnStreamRawParser.h:221
bool AddEntry(const std::string &name, _elementType &data, char delim=' ', bool req=true)
Definition cmnStreamRawParser.h:205
bool Parse(std::istream &inputStream)

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:

Config.ToStream(cout);
void ToStream(std::ostream &outputStream) const

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:

  • the # character indicates a comment line (hard-coded).
  • the code does not support a delimiter (other than whitespace) between the keyword and data.

Constructor & Destructor Documentation

◆ cmnStreamRawParser()

cmnStreamRawParser::cmnStreamRawParser ( )
inline

◆ ~cmnStreamRawParser()

cmnStreamRawParser::~cmnStreamRawParser ( )

Member Function Documentation

◆ AddEntry()

template<class _elementType>
bool cmnStreamRawParser::AddEntry ( const std::string & name,
_elementType & data,
char delim = ' ',
bool req = true )
inline

Add a {keyword,value} pair to be parsed from the input stream.

Parameters
nameVariable name (key)
dataVariable to be assigned when input stream is parsed
delimDelimiter between data elements (but not between keyword and data)
Returns
true if successful; false if not (e.g., duplicate entry)

◆ AddEntryStreamable()

template<class _elementType>
bool cmnStreamRawParser::AddEntryStreamable ( const std::string & name,
_elementType & data,
bool req = true )
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.

Parameters
nameVariable name (key)
dataVariable to be assigned when input stream is parsed
Returns
true if successful; false if not (e.g., duplicate entry)

◆ IsValid()

bool cmnStreamRawParser::IsValid ( const std::string & name) const

Returns true if valid data was parsed.

◆ Parse()

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.

◆ SetAllValid()

void cmnStreamRawParser::SetAllValid ( bool val = true)

Set all entries valid or invalid.

◆ ToStream()

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)".


The documentation for this class was generated from the following file: