libFAUDES

Sections

Index

faudes::TokenReader Class Reference

#include <tokenreader.h>

List of all members.


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 59 of file 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 complete file.
 ~TokenReader (void)
 Destruct.
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).
void ReadBegin (const std::string &rLabel)
 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)
 Open a section by specified label.
void SeekEnd (const std::string &rLabel)
 Close the current section by matching the previous SeekBegin().
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.
bool operator>> (Token &token)
 Operator for get.
int Line (void) const
 Return number of lines read.
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< 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
bool mHasPeekToken
 validity of peek buffer
Token mPeekToken
 peek buffer

Member Enumeration Documentation

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

Enumerator:
File 
Stdin 
String 

Definition at line 65 of file tokenreader.h.


Constructor & Destructor Documentation

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

TokenReader constructor.

Parameters:
mode select source: File, Stdin or String
rInString string to read from or filename
Exceptions:
Exception 

Definition at line 31 of file tokenreader.cpp.

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

Creates a TokenReader for reading complete file.

Parameters:
rFilename File to read
Exceptions:
Exception 

Definition at line 67 of file tokenreader.cpp.

faudes::TokenReader::~TokenReader ( void   ) 

Destruct.

Definition at line 88 of file tokenreader.cpp.


Member Function Documentation

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

Get state of TokenReader stream.

Returns:
std::stream.good()

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

Get the filename.

Return dummy values for console or string mode.

Returns:
Filename

Definition at line 117 of file tokenreader.cpp.

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

Get file mode.

Returns:
Mode

Definition at line 121 of file tokenreader.h.

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

Peek next token.

False indicates eof.

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

Definition at line 122 of file tokenreader.cpp.

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

Get next token.

False indicates eof.

Parameters:
token Reference to token
Exceptions:
Exception faudes exception ios (id 1)
Returns:
True for a valid token, false for eof

Definition at line 144 of file tokenreader.cpp.

void faudes::TokenReader::Rewind ( void   ) 

Rewind stream (must be a seekable stream).

Exceptions:
Exception 

Definition at line 93 of file 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:
rLabel Token label to specify section
Exceptions:
Exception Section begin label not found (id 51)

Definition at line 237 of file 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:
rLabel Token label to specify section
Exceptions:
Exception Section end label not found (id 51)

Definition at line 286 of file tokenreader.cpp.

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

Open a section by specified label.

This function searches for the section on this level and any descending level. However, this funtion will not rewind to the beginning of the current section (this may change in a future revision). In the case of success, the matching begin token is the last token read. After processing the section, a matching SeekEnd(label) must be called.

Parameters:
rLabel Label to specify section
Exceptions:
Exception Section begin not found (id 51)

Definition at line 173 of file tokenreader.cpp.

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

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

Reads all tokens up to the level when SeekBegin() was called.

Parameters:
rLabel Token label to specify section
Exceptions:
Exception Section end label not found (id 51) Previous level not found/ unmachted call (id 52)

Definition at line 209 of file tokenreader.cpp.

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:
rLabel Token label to specify section
Exceptions:
Exception Unexpected eof (id 51)
Returns:
True at end of section

Definition at line 312 of file tokenreader.cpp.

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

Read integer token.

Exceptions:
Exception Token mismatch (id 50)
Returns:
value of index token

Definition at line 328 of file tokenreader.cpp.

double faudes::TokenReader::ReadFloat ( void   ) 

Read float token.

Exceptions:
Exception Token mismatch (id 50)
Returns:
value of index token

Definition at line 340 of file tokenreader.cpp.

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

Read string token.

Exceptions:
Exception Token mismatch (id 50)
Returns:
value of name token

Definition at line 352 of file tokenreader.cpp.

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

Read option token.

Exceptions:
Exception Token mismatch (id 50)
Returns:
value of name token

Definition at line 366 of file 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:
Exception Token mismatch (id 50)

Definition at line 379 of file tokenreader.cpp.

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

Operator for get.

Definition at line 300 of file tokenreader.h.

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

Return number of lines read.

Returns:
Number of lines read

Definition at line 395 of file tokenreader.cpp.

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

Return "filename:line".

Definition at line 400 of file tokenreader.cpp.


Member Data Documentation

input mode

Definition at line 319 of file tokenreader.h.

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

istream object pointer

Definition at line 322 of file tokenreader.h.

std::ifstream faudes::TokenReader::mFStream [private]

actual stream object, file input

Definition at line 325 of file tokenreader.h.

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

actual stream object on heap, string input

Definition at line 328 of file tokenreader.h.

std::string faudes::TokenReader::mFileName [private]

Filename.

Definition at line 331 of file tokenreader.h.

Line counter.

Definition at line 334 of file tokenreader.h.

long int faudes::TokenReader::mFilePos [private]

file position

Definition at line 337 of file tokenreader.h.

Level (of nested sections).

Definition at line 340 of file tokenreader.h.

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

file positions of sections

Definition at line 343 of file tokenreader.h.

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

file line of sections

Definition at line 346 of file tokenreader.h.

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

level of recent seek

Definition at line 349 of file tokenreader.h.

std::string faudes::TokenReader::mLastString [private]

recent string buffer

Definition at line 352 of file tokenreader.h.

validity of peek buffer

Definition at line 355 of file tokenreader.h.

peek buffer

Definition at line 358 of file tokenreader.h.


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

libFAUDES 2.14g --- 2009-12-3 --- c++ source docu by doxygen 1.5.6