cisst-saw
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Classes | Public Member Functions | List of all members
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;

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);

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);

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:

Constructor & Destructor Documentation

cmnStreamRawParser::cmnStreamRawParser ( )
inline
cmnStreamRawParser::~cmnStreamRawParser ( )

Member Function Documentation

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


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