ERC CISST - cisst software

cmnStreamRawParser Class Reference

#include <cmnStreamRawParser.h>

Collaboration diagram for cmnStreamRawParser:

Collaboration graph
[legend]
List of all members.

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:

     cmnDouble s;
     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:

     cmnStreamRawParser Config;
     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 it contains the FromStreamRaw and ToStreamRaw 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:

Definition at line 93 of file cmnStreamRawParser.h.

Public Member Functions

Classes


Member Function Documentation

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

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

Parameters:
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)
Returns:
true if successful; false if not (e.g., duplicate entry)

Definition at line 183 of file cmnStreamRawParser.h.

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

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

Definition at line 199 of file cmnStreamRawParser.h.

void cmnStreamRawParser::SetAllValid ( bool  val = true  ) 

Set all entries valid or invalid.

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 keywords.

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:
erc-cisst-devel<at>lists.johnshopkins.edu