A TokenReader reads sequential tokens from a file or string. More...

#include <cfl_tokenreader.h>

Public Types

enum  Mode { File, Stdin, String }
 Mode of operation: read from file, stdin or string. More...

Public Member Functions

 TokenReader (Mode mode, const std::string &rInString="")
 TokenReader constructor.
 TokenReader (const std::string &rFilename)
 Creates a TokenReader for reading a file.
 ~TokenReader (void)
 Destruct.
std::istream * Streamp (void)
 Access C++ stream.
bool Good (void) const
 Get state of TokenReader stream.
std::string FileName (void) const
 Get the filename.
Mode SourceMode (void) const
 Get file mode.
bool Peek (Token &token)
 Peek next token.
bool Get (Token &token)
 Get next token.
void Rewind (void)
 Rewind stream (must be a seekable stream)
bool ExistsBegin (const std::string &rLabel)
 This function searches for the specified section on the current level, it skips any sections on levels below, and it will wrap to the begin of the current section.
void ReadBegin (const std::string &rLabel)
 Open a section by specified label.
void ReadBegin (const std::string &rLabel, Token &rToken)
 Open a section by specified label.
void ReadEnd (const std::string &rLabel)
 Close the current section by matching the previous ReadBegin().
void SeekBegin (const std::string &rLabel)
 Find specified begin label.
void SeekBegin (const std::string &rLabel, Token &rToken)
 Find specified begin label.
bool Eos (const std::string &rLabel)
 Peek a token and check whether it ends the specified section.
long int ReadInteger (void)
 Read integer token.
double ReadFloat (void)
 Read float token.
const std::string & ReadString (void)
 Read string token.
const std::string & ReadOption (void)
 Read option token.
const std::string & ReadBinary (void)
 Read binary token.
const std::string & ReadText (void)
 Read plain text.
const std::string & ReadCharacterData (void)
 Read plain text.
const std::string & ReadSection (void)
 Read XML section.
bool operator>> (Token &token)
 Operator for get.
int Line (void) const
 Return number of lines read.
int Level (void) const
 Return current level of section nesting.
bool Recover (int level)
 Recover to specified section nesting.
std::string FileLine (void) const
 Return "filename:line".

Private Attributes

Mode mMode
 input mode
std::istream * mpStream
 istream object pointer
std::ifstream mFStream
 actual stream object, file input
std::istringstream * mpSStream
 actual stream object on heap, string input
std::string mFileName
 Filename.
int mLineCount
 Line counter.
long int mFilePos
 file position
int mLevel
 Level (of nested sections)
std::vector< std::string > mLevelLabel
 label of sections
std::vector< long int > mLevelPos
 file positions of sections
std::vector< long int > mLevelLine
 file line of sections
std::vector< int > mSeekLevel
 level of recent seek
std::string mLastString
 recent string buffer
Token mPeekToken
 peek buffer

Detailed Description

A TokenReader reads sequential tokens from a file or string.

It can get or peek the next token and it will track line numbers for informative diagnosis output.

The class also implements nested sections. That is, you may search for a (sub-)section within the current section and, hence, implement file formats that do not insist in a particular ordering of the sections (e.g. a Generator consists of states, transitions and events, no matter in which order).

Convenience functions are provided to read a token of a particular type and throws faudes::Exception on token mismatch. You may catch the exception as follows:

try {
some tokenreader operations
}
catch (faudes::Exception& ex) {
cerr << "Error reading file (details: " << ex.What() << ")" << endl;
}

Note that in addition to the documented execeptions all TokenReader functions do pass on faudes ios errors from the Token class.

Definition at line 60 of file cfl_tokenreader.h.

Member Enumeration Documentation

Mode of operation: read from file, stdin or string.

Enumerator:
File 
Stdin 
String 

Definition at line 66 of file cfl_tokenreader.h.

Constructor & Destructor Documentation

faudes::TokenReader::TokenReader ( Mode  mode,
const std::string &  rInString = "" 
)

TokenReader constructor.

Parameters
modeselect source: File, Stdin or String
rInStringstring to read from or filename
Exceptions
Exception

Definition at line 32 of file cfl_tokenreader.cpp.

faudes::TokenReader::TokenReader ( const std::string &  rFilename)

Creates a TokenReader for reading a file.

Parameters
rFilenameFile to read
Exceptions
Exception

Definition at line 68 of file cfl_tokenreader.cpp.

faudes::TokenReader::~TokenReader ( void  )

Destruct.

Definition at line 89 of file cfl_tokenreader.cpp.

Member Function Documentation

bool faudes::TokenReader::Eos ( const std::string &  rLabel)

Peek a token and check whether it ends the specified section.

This function is meant for scanning a section with a while- construct.

SeekBegin("MySec");
while(!Eos("MySec")) {
...
};
SeekEnd("MySec");
Parameters
rLabelToken label to specify section
Exceptions
ExceptionUnexpected eof (id 51)
Returns
True at end of section

Definition at line 389 of file cfl_tokenreader.cpp.

bool faudes::TokenReader::ExistsBegin ( const std::string &  rLabel)

This function searches for the specified section on the current level, it skips any sections on levels below, and it will wrap to the begin of the current section.

In the case of success, it returns true, else false. In contrast to other token i/o methodes, this method will not throw any execptions.

Parameters
rLabelToken label to specify section
Returns
True if sectiob exists

Definition at line 295 of file cfl_tokenreader.cpp.

std::string faudes::TokenReader::FileLine ( void  ) const

Return "filename:line".

Definition at line 559 of file cfl_tokenreader.cpp.

std::string faudes::TokenReader::FileName ( void  ) const

Get the filename.

Returns dummy values for console or string mode.

Returns
Filename

Definition at line 125 of file cfl_tokenreader.cpp.

bool faudes::TokenReader::Get ( Token token)

Get next token.

False indicates eof.

Parameters
tokenReference to token
Exceptions
Exceptionfaudes exception ios (id 1)
Returns
True for a valid token, false for eof

Definition at line 151 of file cfl_tokenreader.cpp.

bool faudes::TokenReader::Good ( void  ) const

Get state of TokenReader stream.

Returns
std::stream.good()
int faudes::TokenReader::Level ( void  ) const
inline

Return current level of section nesting.

Returns
Number of lines read

Definition at line 414 of file cfl_tokenreader.h.

int faudes::TokenReader::Line ( void  ) const

Return number of lines read.

Returns
Number of lines read

Definition at line 554 of file cfl_tokenreader.cpp.

bool faudes::TokenReader::operator>> ( Token token)
inline

Operator for get.

Definition at line 396 of file cfl_tokenreader.h.

bool faudes::TokenReader::Peek ( Token token)

Peek next token.

False indicates eof.

Parameters
tokenReference to token
Exceptions
Exception
Returns
True for a valid token, false for eof

Definition at line 130 of file cfl_tokenreader.cpp.

void faudes::TokenReader::ReadBegin ( const std::string &  rLabel)

Open a section by specified label.

This function searches for the section on this level, it skips any sections on levels below this level, and it will wrap to the begin of the current section. In the case of success, the matching begin token is the last token read. After processing the section, a matching ReadEnd(label) must be called.

Parameters
rLabelToken label to specify section
Exceptions
ExceptionSection begin label not found (id 51)

Definition at line 238 of file cfl_tokenreader.cpp.

void faudes::TokenReader::ReadBegin ( const std::string &  rLabel,
Token rToken 
)

Open a section by specified label.

This version ReadBegin(const std::string&) will return the actual begin tag in its second argument.

Parameters
rLabelToken label to specify section
rTokenBegin tag as found in token stream.
Exceptions
ExceptionSection begin label not found (id 51)

Definition at line 244 of file cfl_tokenreader.cpp.

const std::string & faudes::TokenReader::ReadBinary ( void  )

Read binary token.

You can access the binary array via StringValue();

Returns
Binary data as std::string
Exceptions
ExceptionToken mismatch (id 50)

Definition at line 456 of file cfl_tokenreader.cpp.

const std::string & faudes::TokenReader::ReadCharacterData ( void  )

Read plain text.

Read all text until and excluding the next markup. This method does no interpretation whatever. It can be used to implement carbon copy of text sections.

Exceptions
ExceptionStream mismatch (id 50)
Returns
Text read.

Definition at line 504 of file cfl_tokenreader.cpp.

void faudes::TokenReader::ReadEnd ( const std::string &  rLabel)

Close the current section by matching the previous ReadBegin().

Reads all tokens up to and including end of current section.

Parameters
rLabelToken label to specify section
Exceptions
ExceptionSection end label not found (id 51)

Definition at line 344 of file cfl_tokenreader.cpp.

double faudes::TokenReader::ReadFloat ( void  )

Read float token.

Exceptions
ExceptionToken mismatch (id 50)
Returns
value of index token

Definition at line 417 of file cfl_tokenreader.cpp.

long int faudes::TokenReader::ReadInteger ( void  )

Read integer token.

Exceptions
ExceptionToken mismatch (id 50)
Returns
value of index token

Definition at line 405 of file cfl_tokenreader.cpp.

const std::string & faudes::TokenReader::ReadOption ( void  )

Read option token.

Exceptions
ExceptionToken mismatch (id 50)
Returns
value of name token

Definition at line 443 of file cfl_tokenreader.cpp.

const std::string & faudes::TokenReader::ReadSection ( void  )

Read XML section.

Reads the current section, including all character data and markup, until and excluding the matching end tag. This method does no interpretation whatever. the result is a string that represents the respective section in XML format.

Exceptions
ExceptionStream mismatch (id 50)
Returns
Text read.

Definition at line 528 of file cfl_tokenreader.cpp.

const std::string & faudes::TokenReader::ReadString ( void  )

Read string token.

Exceptions
ExceptionToken mismatch (id 50)
Returns
value of name token

Definition at line 429 of file cfl_tokenreader.cpp.

const std::string & faudes::TokenReader::ReadText ( void  )

Read plain text.

Read all text until and excluding the next markup. This method interprets known enteties and swallows beginning and ending white space. Effetively, ot is used to read ASCII compatible text.

Exceptions
ExceptionStream mismatch (id 50)
Returns
Text read.

Definition at line 470 of file cfl_tokenreader.cpp.

bool faudes::TokenReader::Recover ( int  level)

Recover to specified section nesting.

The current implementation will mess up the Seek-stack. Thus, Recover() only works when no Seekbegin() was used.

Returns
True on success

Definition at line 376 of file cfl_tokenreader.cpp.

void faudes::TokenReader::Rewind ( void  )

Rewind stream (must be a seekable stream)

Exceptions
Exception

Definition at line 99 of file cfl_tokenreader.cpp.

void faudes::TokenReader::SeekBegin ( const std::string &  rLabel)

Find specified begin label.

This function searches for the section on this level and any descending level. It does not read the specified section tag, but stops just one token before.

Technical note: Former versions of libFAUDES also read the actual begin token and required a matching call of SeekEnd(). As of version 2.18a, this is not supported anymore. The previous behaviour was rarely needed and can be mimiqued by an ordinary ReadEnd() with a subsequent Recover(level).

Parameters
rLabelLabel to specify section
Exceptions
ExceptionSection begin not found (id 51)

Definition at line 197 of file cfl_tokenreader.cpp.

void faudes::TokenReader::SeekBegin ( const std::string &  rLabel,
Token rToken 
)

Find specified begin label.

This version SeekBegin(const std::string&) will return the actual begin tag in its second argument.

Parameters
rLabelToken label to specify section
rTokenBegin tag as found in token stream.
Exceptions
ExceptionSection begin label not found (id 51)

Definition at line 203 of file cfl_tokenreader.cpp.

Mode faudes::TokenReader::SourceMode ( void  ) const
inline

Get file mode.

Returns
Mode

Definition at line 128 of file cfl_tokenreader.h.

std::istream * faudes::TokenReader::Streamp ( void  )

Access C++ stream.

Definition at line 94 of file cfl_tokenreader.cpp.

Member Data Documentation

std::string faudes::TokenReader::mFileName
private

Filename.

Definition at line 447 of file cfl_tokenreader.h.

long int faudes::TokenReader::mFilePos
private

file position

Definition at line 453 of file cfl_tokenreader.h.

std::ifstream faudes::TokenReader::mFStream
private

actual stream object, file input

Definition at line 441 of file cfl_tokenreader.h.

std::string faudes::TokenReader::mLastString
private

recent string buffer

Definition at line 471 of file cfl_tokenreader.h.

int faudes::TokenReader::mLevel
private

Level (of nested sections)

Definition at line 456 of file cfl_tokenreader.h.

std::vector<std::string> faudes::TokenReader::mLevelLabel
private

label of sections

Definition at line 459 of file cfl_tokenreader.h.

std::vector<long int> faudes::TokenReader::mLevelLine
private

file line of sections

Definition at line 465 of file cfl_tokenreader.h.

std::vector<long int> faudes::TokenReader::mLevelPos
private

file positions of sections

Definition at line 462 of file cfl_tokenreader.h.

int faudes::TokenReader::mLineCount
private

Line counter.

Definition at line 450 of file cfl_tokenreader.h.

Mode faudes::TokenReader::mMode
private

input mode

Definition at line 435 of file cfl_tokenreader.h.

Token faudes::TokenReader::mPeekToken
private

peek buffer

Definition at line 474 of file cfl_tokenreader.h.

std::istringstream* faudes::TokenReader::mpSStream
private

actual stream object on heap, string input

Definition at line 444 of file cfl_tokenreader.h.

std::istream* faudes::TokenReader::mpStream
private

istream object pointer

Definition at line 438 of file cfl_tokenreader.h.

std::vector<int> faudes::TokenReader::mSeekLevel
private

level of recent seek

Definition at line 468 of file cfl_tokenreader.h.


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

libFAUDES 2.26g --- 2015.08.17 --- c++ api documentaion by doxygen