last update 20 Sep 2009 |
#include <cvrLispStreamHandler.h>
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 () |
lispStreamHandler & | copy (const lispStreamHandler &other) |
lispStreamHandler & | operator= (const lispStreamHandler &other) |
virtual const std::string & | name () const |
virtual lispStreamHandler * | clone () const |
virtual lispStreamHandler * | newInstance () 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< stackElement > | stack |
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 |
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
// 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();
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
enum cvr::lispStreamHandler::eTokenId [protected] |
cvr::lispStreamHandler::lispStreamHandler | ( | ) |
default constructor
cvr::lispStreamHandler::lispStreamHandler | ( | std::ostream & | aStream | ) |
default constructor
cvr::lispStreamHandler::lispStreamHandler | ( | std::istream & | aStream | ) |
default constructor
cvr::lispStreamHandler::lispStreamHandler | ( | const char * | aStream | ) |
default constructor.
This constructor uses the given string as input, as if read from a stread.
cvr::lispStreamHandler::lispStreamHandler | ( | const lispStreamHandler & | other | ) |
copy constructor
virtual cvr::lispStreamHandler::~lispStreamHandler | ( | ) | [virtual] |
destructor
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] |
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 | ) |
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] |
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] |
virtual lispStreamHandler* cvr::lispStreamHandler::newInstance | ( | ) | const [virtual] |
lispStreamHandler& cvr::lispStreamHandler::operator= | ( | const lispStreamHandler & | other | ) |
virtual bool cvr::lispStreamHandler::read | ( | unsigned short & | data | ) | [virtual] |
read an unsigned short
Implements cvr::ioHandler.
virtual bool cvr::lispStreamHandler::read | ( | short & | data | ) | [virtual] |
read a short
Implements cvr::ioHandler.
virtual bool cvr::lispStreamHandler::read | ( | unsigned long & | data | ) | [virtual] |
read an unsigned long
Implements cvr::ioHandler.
virtual bool cvr::lispStreamHandler::read | ( | long & | data | ) | [virtual] |
read a long
Implements cvr::ioHandler.
virtual bool cvr::lispStreamHandler::read | ( | bool & | data | ) | [virtual] |
virtual bool cvr::lispStreamHandler::read | ( | ubyte & | data | ) | [virtual] |
read an unsigned byte value
Implements cvr::ioHandler.
virtual bool cvr::lispStreamHandler::read | ( | byte & | data | ) | [virtual] |
read a signed byte value
Implements cvr::ioHandler.
virtual bool cvr::lispStreamHandler::read | ( | char & | data | ) | [virtual] |
Read a char value.
The char can be given in two ways:
Implements cvr::ioHandler.
virtual bool cvr::lispStreamHandler::read | ( | unsigned int & | data | ) | [virtual] |
read an unsigned int value
Implements cvr::ioHandler.
virtual bool cvr::lispStreamHandler::read | ( | int & | data | ) | [virtual] |
read an integer value
Implements cvr::ioHandler.
virtual bool cvr::lispStreamHandler::read | ( | float & | data | ) | [virtual] |
read a float value
Implements cvr::ioHandler.
virtual bool cvr::lispStreamHandler::read | ( | double & | data | ) | [virtual] |
read a double value
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] |
virtual bool cvr::lispStreamHandler::readDataSeparator | ( | ) | [virtual] |
virtual bool cvr::lispStreamHandler::readEnd | ( | ) | [virtual] |
virtual bool cvr::lispStreamHandler::readKeyValueSeparator | ( | ) | [virtual] |
virtual bool cvr::lispStreamHandler::readSymbol | ( | std::string & | data | ) | [virtual] |
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...
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
Implements cvr::ioHandler.
virtual bool cvr::lispStreamHandler::write | ( | const short & | data | ) | [virtual] |
write a short
Implements cvr::ioHandler.
virtual bool cvr::lispStreamHandler::write | ( | const unsigned long & | data | ) | [virtual] |
write an unsigned long
Implements cvr::ioHandler.
virtual bool cvr::lispStreamHandler::write | ( | const long & | data | ) | [virtual] |
write a long
Implements cvr::ioHandler.
virtual bool cvr::lispStreamHandler::write | ( | const bool | data | ) | [virtual] |
virtual bool cvr::lispStreamHandler::write | ( | const ubyte & | data | ) | [virtual] |
virtual bool cvr::lispStreamHandler::write | ( | const byte & | data | ) | [virtual] |
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
Implements cvr::ioHandler.
virtual bool cvr::lispStreamHandler::write | ( | const int | data | ) | [virtual] |
write an integer value
Implements cvr::ioHandler.
virtual bool cvr::lispStreamHandler::write | ( | const float & | data | ) | [virtual] |
write a float value
Implements cvr::ioHandler.
virtual bool cvr::lispStreamHandler::write | ( | const double & | data | ) | [virtual] |
write a double value.
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] |
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] |
virtual bool cvr::lispStreamHandler::writeEnd | ( | ) | [virtual] |
virtual bool cvr::lispStreamHandler::writeEOL | ( | ) | [virtual] |
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] |
virtual bool cvr::lispStreamHandler::writeSymbol | ( | const std::string & | data | ) | [virtual] |
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
std::list<stackElement> cvr::lispStreamHandler::stack [protected] |
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 '"'
bool cvr::lispStreamHandler::supressSpaces [protected] |
flag to control spaces supression (for example, there is no need for spaces between parenthesis)
bool cvr::lispStreamHandler::tryEOL [protected] |
flag to indicate if an EOL is needed