CVR-Lib last update 20 Sep 2009

cvr::lispStreamHandler Class Reference
[Storable Interface]

The lispStreamHandler class offers an interface for the functor parameters and other classes to read() and write() them in a LISP-like format. More...

#include <cvrLispStreamHandler.h>

Inheritance diagram for cvr::lispStreamHandler:

Inheritance graph
[legend]
Collaboration diagram for cvr::lispStreamHandler:

Collaboration graph
[legend]

List of all members.

Classes

struct  stackElement
 type for each element of the stack More...

Public Member Functions

 lispStreamHandler ()
 lispStreamHandler (std::ostream &aStream)
 lispStreamHandler (std::istream &aStream)
 lispStreamHandler (const char *aStream)
 lispStreamHandler (const lispStreamHandler &other)
virtual ~lispStreamHandler ()
void use (std::ostream &aStream)
void use (std::istream &aStream)
void clear ()
lispStreamHandlercopy (const lispStreamHandler &other)
lispStreamHandleroperator= (const lispStreamHandler &other)
virtual const std::string & name () const
virtual lispStreamHandlerclone () const
virtual lispStreamHandlernewInstance () const
virtual bool writeBegin ()
virtual bool writeEnd ()
virtual bool readBegin ()
virtual bool readEnd ()
virtual bool writeSymbol (const std::string &data)
virtual bool readSymbol (std::string &data)
virtual bool trySymbol (const std::string &data)
virtual bool writeComment (const std::string &data)
virtual bool writeComment (const char *data)
virtual bool tryBegin ()
virtual bool tryEnd ()
virtual bool writeSpaces (const int s=1)
virtual bool writeEOL ()
virtual bool writeKeyValueSeparator ()
virtual bool writeDataSeparator ()
virtual bool readKeyValueSeparator ()
virtual bool readDataSeparator ()
virtual bool eof ()
virtual bool restoreLevel ()
virtual void appendContextStatus () const
write members
virtual bool write (const std::string &data)
virtual bool write (const char *data)
virtual bool write (const double &data)
virtual bool write (const float &data)
virtual bool write (const int data)
virtual bool write (const unsigned int &data)
virtual bool write (const char &data)
virtual bool write (const byte &data)
virtual bool write (const ubyte &data)
virtual bool write (const bool data)
virtual bool write (const long &data)
virtual bool write (const unsigned long &data)
virtual bool write (const short &data)
virtual bool write (const unsigned short &data)
read members
virtual bool read (std::string &data)
virtual bool read (double &data)
virtual bool read (float &data)
virtual bool read (int &data)
virtual bool read (unsigned int &data)
virtual bool read (char &data)
virtual bool read (byte &data)
virtual bool read (ubyte &data)
virtual bool read (bool &data)
virtual bool read (long &data)
virtual bool read (unsigned long &data)
virtual bool read (short &data)
virtual bool read (unsigned short &data)

Protected Types

enum  eTokenId {
  BeginToken, EndToken, SymbolToken, StringToken,
  ErrorToken
}
typedef std::map< std::string,
std::string > 
cacheType

Protected Member Functions

eTokenId getNextToken (std::string &token, const bool justTry=false)
eTokenId getNextTokenFromString (std::string &src, std::string::size_type &srcPos, std::string &token, const bool justTry=false)
void getNextLine (std::string &newline)
bool commentFilter ()
bool isTokenDelimiter (const char &c)
bool completeLevel (std::string &restOfLevel)

Protected Attributes

std::istream * inStream
std::string inString
std::string::size_type inStringPos
std::ostream * outStream
bool supressSpaces
bool tryEOL
std::list< stackElementstack

Static Protected Attributes

static const char openChar
static const char closeChar
static const char separator
static const char commentChar
static const char stringChar
static const char quoteChar


Detailed Description

The lispStreamHandler class offers an interface for the functor parameters and other classes to read() and write() them in a LISP-like format.

This is the CVR-Lib standard ioHandler.

If the standard functions atof() and operator<< for numbers consider the locale settings (this occurs in MS-VC++ or newer glibc and gcc with Linux), then the read() and write() methods will also be localed. A typical problem with this "feature" is that if you have a file saved with "LANG=C", the decimal point is a point (e.g. 1.532), and when you try to read it in another locale (for example LANG=de_DE.ISO-8859-1), then all numbers with a decimal point won't be read correctly, because the method will be expecting a decimal comma (e.g. 1,532).

See also the read and write methods for the basic types for more information

Warning:
The given streams must be "ASCII" streams. If they are created with the binary flag, some weird effects can be expected.
Example:
 // the lisp stream formatting object
 cvr::lispStreamHandler lsh;

 // Write example:

 // open a stream in text mode
 std::ofstream out("testfile.bin");

 // tell the lisp stream handler to use the given stream
 lsh.use(out);

 cvr::write(lsh,"anInteger",5);
 cvr::write(lsh,"aString","hello world");

 out.close();

 // Read example

 // Open a stream in lisp mode
 std::ifstream in("testfile.bin");

 lsh.use(in);
 int i;
 cvr::read(lsh,"anInteger",i);

 std::string str;
 cvr::read(lsh,"aString",str);

 in.close();

Member Typedef Documentation

typedef std::map<std::string,std::string> cvr::lispStreamHandler::cacheType [protected]

type for the cache of each level, where the value for each symbol is stored


Member Enumeration Documentation

Types of tokens.

Enumerator:
BeginToken  Denotes begin of a level.
EndToken  Denotes end of a level.
SymbolToken  Some atomic token.
StringToken  A string token.
ErrorToken  Unrecognized token.


Constructor & Destructor Documentation

cvr::lispStreamHandler::lispStreamHandler (  ) 

default constructor

cvr::lispStreamHandler::lispStreamHandler ( std::ostream &  aStream  ) 

default constructor

Warning:
The given stream must be an "ASCII" stream. If it is created with the binary flag, some weird effects can be expected.

cvr::lispStreamHandler::lispStreamHandler ( std::istream &  aStream  ) 

default constructor

Warning:
The given stream must be an "ASCII" stream. If it is created with the binary flag, some weird effects can be expected.

cvr::lispStreamHandler::lispStreamHandler ( const char *  aStream  ) 

default constructor.

This constructor uses the given string as input, as if read from a stread.

Warning:
Note that the given string is NOT a filename, but a string containing the data "stream". This is useful especially for the JNI (java native interface). You can of course also use the std::ostringstream and std::istringstream with the other constructors or with the method use().

cvr::lispStreamHandler::lispStreamHandler ( const lispStreamHandler other  ) 

copy constructor

virtual cvr::lispStreamHandler::~lispStreamHandler (  )  [virtual]

destructor


Member Function Documentation

virtual void cvr::lispStreamHandler::appendContextStatus (  )  const [virtual]

Overload of context status information.

This function should help the users to find errors in their files. It just inserts some contextual information into the status string to help localizing wrong data.

It is useful for streams that can be edited by hand, because the users will make errors!

Reimplemented from cvr::ioHandler.

void cvr::lispStreamHandler::clear (  ) 

This method resets all internal state variables (for input and output streams as well) in a way that the next operations behave as if this handler were used for the first time.

This allows the use of the same handler instance after reopening a stream. Alternativelly, you can just call the use(std::istream&) or use(std::ostream&) members to clear the input or output state variables only. If you use this method "within" a read/write process, the behaviour will be unpredictable.

virtual lispStreamHandler* cvr::lispStreamHandler::clone (  )  const [virtual]

returns a pointer to a clone of this functor.

Implements cvr::ioHandler.

bool cvr::lispStreamHandler::commentFilter (  )  [protected]

this filter eliminates all comments at the beginning of inString

bool cvr::lispStreamHandler::completeLevel ( std::string &  restOfLevel  )  [protected]

complete actual level reads the data from the input string and input stream until the actual level has been readed.

lispStreamHandler& cvr::lispStreamHandler::copy ( const lispStreamHandler other  ) 

copy data of "other" functor.

Parameters:
other the functor to be copied
Returns:
a reference to this functor object

virtual bool cvr::lispStreamHandler::eof (  )  [virtual]

if the input stream is at the end of file return true, otherwise false if the stream hasn't been set yet, this function also returns true.

void cvr::lispStreamHandler::getNextLine ( std::string &  newline  )  [protected]

get the next "garbageThreshold" characters from the given stream, and ensures that the last readed character is a delimiter

eTokenId cvr::lispStreamHandler::getNextToken ( std::string &  token,
const bool  justTry = false 
) [protected]

read next token from the input string or from the input stream

See also:
getNextTokenFromString.

eTokenId cvr::lispStreamHandler::getNextTokenFromString ( std::string &  src,
std::string::size_type &  srcPos,
std::string &  token,
const bool  justTry = false 
) [protected]

read next token from the given string this member get the next token into the given string

Following tokens are recognized:

beginToken := '('

endToken := ')'

symbolToken := {alpha | digit} | float

stringToken := quote {alpha | digit | other | escape quote} quote

ErrorToken

where

alpha = "A" | "B" | ... "Z" | "a" | "b" | ... "z"

digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" |

other = "!" | "," | "$" | "%" | "&" | "/" | "[" | "]" | "{" | "}" | "?" | "'" | "#" | "+" | "*" | "-" | "." | "," | ":" | ";"

quote = '"'

escape = '\'

float = ['+' | '-'] {digit} '.' {digit} [exponent]

exponent = eid ['+' | '-'] digit {digit}

eid = 'E' | 'e'

comments will be ignored! They begin with a ';' The ErrorToken is not a real token. This is the way to signalized that no other token could be readed! If justTry = true the source string will remain untouched

bool cvr::lispStreamHandler::isTokenDelimiter ( const char &  c  )  [inline, protected]

returns true if the given char can indicate the end of a token

virtual const std::string& cvr::lispStreamHandler::name (  )  const [virtual]

Returns the name of this class.

Implements cvr::ioHandler.

virtual lispStreamHandler* cvr::lispStreamHandler::newInstance (  )  const [virtual]

returns a pointer to a new instance of this functor.

Implements cvr::ioHandler.

lispStreamHandler& cvr::lispStreamHandler::operator= ( const lispStreamHandler other  ) 

copy data of "other" functor.

Parameters:
other the functor to be copied
Returns:
a reference to this functor object

virtual bool cvr::lispStreamHandler::read ( unsigned short &  data  )  [virtual]

read an unsigned short

Warning:
Be aware of the locale you are using. The number must be read with the same locale used when it was written

Implements cvr::ioHandler.

virtual bool cvr::lispStreamHandler::read ( short &  data  )  [virtual]

read a short

Warning:
Be aware of the locale you are using. The number must be read with the same locale used when it was written

Implements cvr::ioHandler.

virtual bool cvr::lispStreamHandler::read ( unsigned long &  data  )  [virtual]

read an unsigned long

Warning:
Be aware of the locale you are using. The number must be read with the same locale used when it was written

Implements cvr::ioHandler.

virtual bool cvr::lispStreamHandler::read ( long &  data  )  [virtual]

read a long

Warning:
Be aware of the locale you are using. The number must be read with the same locale used when it was written

Implements cvr::ioHandler.

virtual bool cvr::lispStreamHandler::read ( bool &  data  )  [virtual]

read a boolan

Implements cvr::ioHandler.

virtual bool cvr::lispStreamHandler::read ( ubyte data  )  [virtual]

read an unsigned byte value

Warning:
Be aware of the locale you are using. The number must be read with the same locale used when it was written

Implements cvr::ioHandler.

virtual bool cvr::lispStreamHandler::read ( byte data  )  [virtual]

read a signed byte value

Warning:
Be aware of the locale you are using. The number must be read with the same locale used when it was written

Implements cvr::ioHandler.

virtual bool cvr::lispStreamHandler::read ( char &  data  )  [virtual]

Read a char value.

The char can be given in two ways:

  • a numerical representation in decimal code (e.g. 65 represents 'A').
  • a character representation enclosed in sigle quotes (e.g. 'A')

Warning:
Be aware of the locale you are using. The number must be read with the same locale used when it was written

Implements cvr::ioHandler.

virtual bool cvr::lispStreamHandler::read ( unsigned int &  data  )  [virtual]

read an unsigned int value

Warning:
Be aware of the locale you are using. The number must be read with the same locale used when it was written

Implements cvr::ioHandler.

virtual bool cvr::lispStreamHandler::read ( int &  data  )  [virtual]

read an integer value

Warning:
Be aware of the locale you are using. The number must be read with the same locale used when it was written

Implements cvr::ioHandler.

virtual bool cvr::lispStreamHandler::read ( float &  data  )  [virtual]

read a float value

Warning:
Be aware of the locale you are using. The number must be read with the same locale used when it was written

Implements cvr::ioHandler.

virtual bool cvr::lispStreamHandler::read ( double &  data  )  [virtual]

read a double value

Warning:
Be aware of the locale you are using. The number must be read with the same locale used when it was written

Implements cvr::ioHandler.

virtual bool cvr::lispStreamHandler::read ( std::string &  data  )  [virtual]

read a std::string

The string should be quoted (i.e. between '"' and '"'), expect if its a single word. The two-char sequences '\"' and '\' will be replaced by '"' and '\' respectivelly.

Implements cvr::ioHandler.

virtual bool cvr::lispStreamHandler::readBegin (  )  [virtual]

the begin token or tokens: here a "("

Reimplemented from cvr::ioHandler.

virtual bool cvr::lispStreamHandler::readDataSeparator (  )  [virtual]

write key/value separator A space is expected

Implements cvr::ioHandler.

virtual bool cvr::lispStreamHandler::readEnd (  )  [virtual]

the end token or tokens: here a ")"

Reimplemented from cvr::ioHandler.

virtual bool cvr::lispStreamHandler::readKeyValueSeparator (  )  [virtual]

write key/value separator A space is expected

Implements cvr::ioHandler.

virtual bool cvr::lispStreamHandler::readSymbol ( std::string &  data  )  [virtual]

read a symbol in the given std::string

Implements cvr::ioHandler.

virtual bool cvr::lispStreamHandler::restoreLevel (  )  [virtual]

restore all the information in the handler taken in the actual level.

If "complete" is true, the begin-token is also restored

Implements cvr::ioHandler.

virtual bool cvr::lispStreamHandler::tryBegin (  )  [virtual]

try to read the begin token from the handler.

If present, returns true and the token is removed from the handler, if not present returns false and leaves the handle as it was before calling this member... This is useful in trees or other complicated data structures.

Implements cvr::ioHandler.

virtual bool cvr::lispStreamHandler::tryEnd (  )  [virtual]

try to read the end token from the handler.

If present, returns true and the token is removed from the handler, if not present returns false and leaves the handle as it was before calling this member... This is usually used when reading lists of data, where the number of elements is unknown.

Implements cvr::ioHandler.

virtual bool cvr::lispStreamHandler::trySymbol ( const std::string &  data  )  [virtual]

try to read the given symbol from the handler.

If present, returns true and the token is removed from the handler, if not present returns false and leaves the stream as is...

Parameters:
data the symbol to be readed

Implements cvr::ioHandler.

void cvr::lispStreamHandler::use ( std::istream &  aStream  )  [virtual]

indicate the input stream to be used

Calling this method you will reinitialize the state of the parser (see also clear()).

Implements cvr::ioHandler.

void cvr::lispStreamHandler::use ( std::ostream &  aStream  )  [virtual]

indicate the output stream to be used

Calling this method you will reinitialize the state of the parser (see also clear()).

Implements cvr::ioHandler.

virtual bool cvr::lispStreamHandler::write ( const unsigned short &  data  )  [virtual]

write an unsigned short

Warning:
Be aware of the locale you are using. The number must be read with the same locale used when it was written

Implements cvr::ioHandler.

virtual bool cvr::lispStreamHandler::write ( const short &  data  )  [virtual]

write a short

Warning:
Be aware of the locale you are using. The number must be read with the same locale used when it was written

Implements cvr::ioHandler.

virtual bool cvr::lispStreamHandler::write ( const unsigned long &  data  )  [virtual]

write an unsigned long

Warning:
Be aware of the locale you are using. The number must be read with the same locale used when it was written

Implements cvr::ioHandler.

virtual bool cvr::lispStreamHandler::write ( const long &  data  )  [virtual]

write a long

Warning:
Be aware of the locale you are using. The number must be read with the same locale used when it was written

Implements cvr::ioHandler.

virtual bool cvr::lispStreamHandler::write ( const bool  data  )  [virtual]

write a boolean

Implements cvr::ioHandler.

virtual bool cvr::lispStreamHandler::write ( const ubyte data  )  [virtual]

write a unsigned 8-bit value

Implements cvr::ioHandler.

virtual bool cvr::lispStreamHandler::write ( const byte data  )  [virtual]

write an 8-bit signed value

Implements cvr::ioHandler.

virtual bool cvr::lispStreamHandler::write ( const char &  data  )  [virtual]

write a char

The char is writen as numerical code if there is no standard ASCII representation. If there is an ASCII representation (i.e. its value is greater or equal 32 (' ') and smaller or equal 127 ('~'), then the ASCII character will be writen enclosed in single quotes.

Implements cvr::ioHandler.

virtual bool cvr::lispStreamHandler::write ( const unsigned int &  data  )  [virtual]

write an unsigned integer value

Warning:
Be aware of the locale you are using. The number must be read with the same locale used when it was written

Implements cvr::ioHandler.

virtual bool cvr::lispStreamHandler::write ( const int  data  )  [virtual]

write an integer value

Warning:
Be aware of the locale you are using. The number must be read with the same locale used when it was written

Implements cvr::ioHandler.

virtual bool cvr::lispStreamHandler::write ( const float &  data  )  [virtual]

write a float value

Warning:
Be aware of the locale you are using. The number must be read with the same locale used when it was written

Implements cvr::ioHandler.

virtual bool cvr::lispStreamHandler::write ( const double &  data  )  [virtual]

write a double value.

Warning:
Be aware of the locale you are using. The number must be read with the same locale used when it was written

Implements cvr::ioHandler.

virtual bool cvr::lispStreamHandler::write ( const char *  data  )  [virtual]

write a character string.

This method will write just the string if it does not contain spaces. Otherwise the string will be enclosed by quotes.

Implements cvr::ioHandler.

virtual bool cvr::lispStreamHandler::write ( const std::string &  data  )  [virtual]

write a std::string.

This method will write just the string if it does not contain spaces. Otherwise the string will be enclosed by quotes.

Implements cvr::ioHandler.

virtual bool cvr::lispStreamHandler::writeBegin (  )  [virtual]

the begin token or tokens: here a "(".

Reimplemented from cvr::ioHandler.

virtual bool cvr::lispStreamHandler::writeComment ( const char *  data  )  [virtual]

write comment writes the input data without any preprocessing, just ensuring that the comment format is given

Implements cvr::ioHandler.

virtual bool cvr::lispStreamHandler::writeComment ( const std::string &  data  )  [virtual]

write comment writes the input data without any preprocessing, just ensuring that the comment format is given

Implements cvr::ioHandler.

virtual bool cvr::lispStreamHandler::writeDataSeparator (  )  [virtual]

write key/value separator.

In this case the data separator is a space

Implements cvr::ioHandler.

virtual bool cvr::lispStreamHandler::writeEnd (  )  [virtual]

the end token or tokens: here a ")".

Reimplemented from cvr::ioHandler.

virtual bool cvr::lispStreamHandler::writeEOL (  )  [virtual]

write end of line

Implements cvr::ioHandler.

virtual bool cvr::lispStreamHandler::writeKeyValueSeparator (  )  [virtual]

write key/value separator.

In this case the key/value separator is a space

Implements cvr::ioHandler.

virtual bool cvr::lispStreamHandler::writeSpaces ( const int  s = 1  )  [virtual]

write spaces (default value 1).

A space-token is a char with value 32.

Implements cvr::ioHandler.

virtual bool cvr::lispStreamHandler::writeSymbol ( const std::string &  data  )  [virtual]

write a std::string

Implements cvr::ioHandler.


Member Data Documentation

const char cvr::lispStreamHandler::closeChar [static, protected]

closing char.

Usually ")"

const char cvr::lispStreamHandler::commentChar [static, protected]

comment char.

Usually ';'

std::istream* cvr::lispStreamHandler::inStream [protected]

pointer to the input stream

std::string cvr::lispStreamHandler::inString [protected]

the input stream will be cached into this string

std::string::size_type cvr::lispStreamHandler::inStringPos [protected]

actual reading position in the input string

const char cvr::lispStreamHandler::openChar [static, protected]

opening char.

Usually "("

std::ostream* cvr::lispStreamHandler::outStream [protected]

pointer to the output stream

const char cvr::lispStreamHandler::quoteChar [static, protected]

quote char.

Usually "'"

const char cvr::lispStreamHandler::separator [static, protected]

separator char.

Usually a space

the data stack.

All readed symbols and their values are stored here temporarly to allow different sorting of the symbols in the stream

const char cvr::lispStreamHandler::stringChar [static, protected]

string char.

Usually '"'

flag to control spaces supression (for example, there is no need for spaces between parenthesis)

flag to indicate if an EOL is needed


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

Generated on Sun Sep 20 22:08:56 2009 for CVR-Lib by Doxygen 1.5.8