33 : mMode(mode), mpStream(NULL), mFileName(
"")
38 FD_DV(
"TokenReader::Tokenreader(String, ...): " << rInString);
39 mpSStream=
new std::istringstream(rInString, std::istringstream::in | std::istringstream::binary);
45 FD_DV(
"TokenReader::Tokenreader(File, \"" << rInString <<
"\")");
46 mFStream.exceptions(std::ios::badbit|std::ios::failbit);
48 mFStream.open(rInString.c_str(), std::ios::in | std::ios::binary);
50 catch (std::ios::failure&) {
51 std::stringstream errstr;
52 errstr <<
"Exception opening/reading file \""<< rInString <<
"\"";
53 throw Exception(
"TokenReader::TokenReader", errstr.str(), 1);
60 std::stringstream errstr;
61 errstr <<
"Invalid Mode / Not implemented";
62 throw Exception(
"TokenReader::TokenReader(mode,instring)", errstr.str(), 1);
69 : mMode(File), mpStream(NULL), mFileName(rFilename)
72 FD_DV(
"TokenReader::Tokenreader(File, \"" << rFilename <<
"\")");
73 mFStream.exceptions(std::ios::badbit|std::ios::failbit);
75 mFStream.open(rFilename.c_str(), std::ios::in | std::ios::binary);
77 catch (std::ios::failure&) {
78 std::stringstream errstr;
79 errstr <<
"Exception opening/reading file \""<< rFilename <<
"\"";
80 throw Exception(
"TokenReader::TokenReader", errstr.str(), 1);
116 catch (std::ios::failure&) {
117 std::stringstream errstr;
118 errstr <<
"Exception opening/reading file in "<<
FileLine();
119 throw Exception(
"TokenReader::Rewind", errstr.str(), 1);
135 }
catch (std::ios::failure&) {
136 std::stringstream errstr;
137 errstr <<
"Exception opening/reading file in "<<
FileLine();
138 throw Exception(
"TokenReader::Peek", errstr.str(), 1);
146 FD_DV(
"TokenReader: Peek: " << token.
Str());
163 if(!res)
return false;
165 if(token.
IsBegin(
"br") || token.
IsEnd(
"br"))
return true;
185 #ifdef FAUDES_CHECKED
191 #ifdef FAUDES_CHECKED
201 FD_DV(
"TokenReader:Get(): " << token.
Str());
215 FD_DV(
"TokenReader::SeekBegin: " << rLabel <<
" at " <<
FileLine() <<
" level " <<
mLevel);
224 std::stringstream errstr;
225 errstr <<
"Section \"" << rLabel <<
"\" expected at " <<
FileLine() <<
" no more tokens";
226 throw Exception(
"TokenReader::SeekBegin", errstr.str(), 51);
235 std::stringstream errstr;
236 errstr <<
"Section \"" << rLabel <<
"\" expected at " <<
FileLine()
237 <<
"current section ended unexpected. Found: " << rToken.
StringValue() <<
" Type " << rToken.
Type();
238 throw Exception(
"TokenReader::SeekBegin", errstr.str(), 51);
256 FD_DV(
"Looking for Section \"" << rLabel <<
"\"");
261 FD_DV(
"section level " << level <<
" current pos " << startpos <<
" begin of section " <<
mLevelState[level].mStartPosition);
269 std::stringstream errstr;
270 errstr <<
"Section \"" << rLabel <<
"\" expected at " <<
FileLine() <<
", no token at all";
271 throw Exception(
"TokenReader::ReadBegin Peek", errstr.str(), 51);
279 if((
mFilePos>=startpos) && (repcnt==1)) {
280 std::stringstream errstr;
281 errstr <<
"Section \"" << rLabel <<
"\" expected at " <<
FileLine() <<
", did not find begin label";
282 throw Exception(
"TokenReader::ReadBegin: Missing", errstr.str(), 51);
286 std::stringstream errstr;
287 errstr <<
"Section \"" << rLabel <<
"\" expected at " <<
FileLine() <<
", did not find begin label";
288 throw Exception(
"TokenReader::ReadBegin: Missing", errstr.str(), 51);
305 catch (std::ios::failure&) {
306 std::stringstream errstr;
307 errstr <<
"Section \"" << rLabel <<
"\" expected at " <<
FileLine();
308 throw Exception(
"TokenReader::ReadBegin Rewind", errstr.str(), 1);
315 FD_DV(
"TokenReader::ExistsBegin(): looking for Section \"" << rLabel <<
"\"");
320 FD_DV(
"section level " << level <<
" current pos " << startpos <<
" begin of section " <<
mLevelState[level].mStartPosition);
336 if(token.
IsEnd() && (
mLevel == level) && (rwcnt==0)) {
343 if(rwcnt>1)
return false;
355 catch (std::ios::failure&) {
356 std::stringstream errstr;
357 errstr <<
"IO Error while scanning Section \"" << rLabel <<
"\" at " <<
FileLine();
358 throw Exception(
"TokenReader::ExistsBegin IO", errstr.str(), 1);
365 FD_DV(
"TokenReader::ReadEnd: " << rLabel <<
" at " <<
FileLine() );
375 std::stringstream errstr;
376 errstr <<
"end of section \"" << rLabel <<
"\" expected at " <<
FileLine();
377 throw Exception(
"TokenReader::ReadEnd", errstr.str(), 51);
386 std::stringstream errstr;
387 errstr <<
"end of Section \"" << rLabel <<
"\" expected at " <<
FileLine();
388 throw Exception(
"TokenReader::ReadEnd", errstr.str(), 51);
399 if(level<0)
return false;
401 if(level>
mLevel)
return false;
402 if(level==
mLevel)
return true;
414 if(level>
mLevel)
return false;
426 if(
mLevel<level)
return false;
447 std::stringstream errstr;
448 errstr <<
"section end \"" << rLabel <<
"\" expected at " <<
FileLine();
449 throw Exception(
"TokenReader::Eos", errstr.str(), 51);
459 std::stringstream errstr;
460 errstr <<
"Integer expected at " <<
FileLine();
461 throw Exception(
"TokenReader::TokenReader", errstr.str(), 50);
471 std::stringstream errstr;
472 errstr <<
"Float expected at " <<
FileLine();
473 throw Exception(
"TokenReader::TokenReader", errstr.str(), 50);
483 std::stringstream errstr;
484 errstr <<
"String expected at " <<
FileLine();
485 throw Exception(
"TokenReader::TokenReader", errstr.str(), 50);
496 std::stringstream errstr;
497 errstr <<
"Option expected at " <<
FileLine();
498 throw Exception(
"TokenReader::TokenReader", errstr.str(), 50);
508 std::stringstream errstr;
509 errstr <<
"Binary string expected at " <<
FileLine();
510 throw Exception(
"TokenReader::TokenReader", errstr.str(), 50);
522 std::stringstream errstr;
523 errstr <<
"Text element \""<< rLabel <<
"\" expected at " <<
FileLine();
524 throw Exception(
"TokenReader::TokenReader", errstr.str(), 50);
530 std::stringstream errstr;
531 errstr <<
"Text expected at " <<
FileLine();
532 throw Exception(
"TokenReader::TokenReader", errstr.str(), 50);
536 static const std::string line=
"\n\r\v";
537 std::size_t pos1=rText.find_first_not_of(line);
538 if(pos1!=std::string::npos)
539 rText=rText.substr(pos1);
542 std::size_t pos2=rText.find_last_not_of(line);
543 if(pos2!=std::string::npos)
546 static const std::string white=
" \t";
548 pos1=rText.find_first_not_of(white);
549 if(pos1!=std::string::npos)
550 rText=rText.substr(pos1);
553 std::size_t pos2=rText.find_last_not_of(white);
554 if(pos2!=std::string::npos)
559 if(!token.
IsEnd(rLabel)) {
560 std::stringstream errstr;
561 errstr <<
"End of text element \""<< rLabel <<
"\" expected at " <<
FileLine();
562 throw Exception(
"TokenReader::TokenReader", errstr.str(), 50);
573 std::stringstream errstr;
574 errstr <<
"Verbatim element \""<< rLabel <<
"\" expected at " <<
FileLine();
575 throw Exception(
"TokenReader::TokenReader", errstr.str(), 50);
584 if(cnt>0 && !token.
IsCdata())
break;
590 static const std::string line=
"\n\r\v";
591 std::size_t pos1=rString.find_first_not_of(line);
592 if(pos1!=std::string::npos)
593 rString=rString.substr(pos1);
596 std::size_t pos2=rString.find_last_not_of(line);
597 if(pos2!=std::string::npos)
598 rString.erase(pos2+1);
601 if(!token.
IsEnd(rLabel)) {
602 std::stringstream errstr;
603 errstr <<
"End of verbatim element \""<< rLabel <<
"\" expected at " <<
FileLine();
604 throw Exception(
"TokenReader::TokenReader", errstr.str(), 50);
613 FD_DV(
"TokenReader::ReadCharacterData(): tag in buffer");
621 std::stringstream errstr;
622 errstr <<
"Missformed character data at " <<
FileLine() <<
": " << rData;
623 throw Exception(
"TokenReader::TokenReader", errstr.str(), 50);
636 int clevel =
Level();
648 if(!
Peek(token))
break;
657 rSectionString=tw.
Str();
#define FD_DV(message)
Debug: optional low-level report on iterations and token IO.
#define FD_WARN(message)
Debug: always report warnings.
long int ReadInteger(void)
Read integer token.
std::string FileLine(void) const
Return "filename:line".
void ReadBinary(std::string &rData)
Read binary token.
void ReadCharacterData(std::string &rData)
Read plain text.
Mode
Mode of operation: read from file, stdin or string.
void ReadText(const std::string &rLabel, std::string &rText)
Read plain text.
Token mPeekToken
peek buffer
std::string ReadOption(void)
Read option token.
bool mFaudesComments
flag to ignore faudes comments marked by ''
bool Eos(const std::string &rLabel)
Peek a token and check whether it ends the specified section.
void SeekBegin(const std::string &rLabel)
Find specified begin label.
void ReadVerbatim(const std::string &rLabel, std::string &rText)
Read verbatim text.
bool Reset(int level=-1)
Reset to the begining of the specified level of section nesting.
int Level(void) const
Return current level of section nesting.
void ReadEnd(const std::string &rLabel)
Close the current section by matching the previous ReadBegin().
double ReadFloat(void)
Read float token.
bool Recover(int level)
Recover by skipping tokens until returning to the specified level of section nesting.
std::string ReadString(void)
Read string token.
void Rewind(void)
Rewind stream.
std::istringstream * mpSStream
actual stream object on heap, string input
void ReadSection(std::string &rSectionString)
Read XML section.
void ReadBegin(const std::string &rLabel)
Open a section by specified label.
std::istream * mpStream
istream object pointer
int mLineCount
Line counter.
std::ifstream mFStream
actual stream object, file input
bool Get(Token &token)
Get next token.
bool Peek(Token &token)
Peek next token.
std::istream * Streamp(void)
Access C++ stream.
int Line(void) const
Return number of lines read.
int mLevel
Level (of nested sections)
bool ExistsBegin(const std::string &rLabel)
Search for specified element.
std::string mFileName
Filename.
TokenReader(Mode mode, const std::string &rInString="")
TokenReader constructor.
std::string FileName(void) const
Access the filename.
~TokenReader(void)
Destruct.
std::vector< LState > mLevelState
long int mFilePos
file position
A TokenWriter writes sequential tokens to a file, a string or stdout.
std::string Str(void)
Retrieve output as string (if in String mode)
void WriteCharacterData(const std::string &rCharData)
Write character data.
void Write(Token &rToken)
Write next token.
void Endl(void)
Write endl separator.
Tokens model atomic data for stream IO.
bool IsCdata(void) const
Test token Type.
bool IsBinary(void) const
Test token Type.
void SetNone(void)
Initialize None token.
const std::string & PreceedingSpace(void) const
Preceeding space when writing to stream.
std::string Str(void) const
Pretty print string representation.
const std::string & StringValue(void) const
Get string value of a name token.
void ClrEnd(void)
Clear End type (resolve empty section)
@ End
<\label> (end of section)
bool IsString(void) const
Test token Type.
Int IntegerValue(void) const
Get integer value of a numeric token.
bool IsNone(void) const
Test token Type.
bool IsInteger(void) const
Test token Type.
static int ReadCharacterData(std::istream *pStream, std::string &rString, bool fcomments)
Read chracter data from an input file stream.
static int ReadEscapedString(std::istream *pStream, char stop, std::string &rString)
Read a std::string value from an input file stream.
bool IsEmpty(void) const
Test token Type.
bool IsBegin(void) const
Test token Type.
const std::string & OptionValue(void) const
Get option value of a name token.
int Read(std::istream *pStream, bool fcomments=true)
Read Token from input stream.
bool IsEnd(void) const
Test token Type.
bool IsFloat(void) const
Test token Type.
bool IsOption(void) const
Test token Type.
void SetEnd(const std::string &rName)
Initialize as End token.
faudes::Float FloatValue(void) const
Get float value of a numeric token.
TokenType Type(void) const
Get token Type.
libFAUDES resides within the namespace faudes.
std::string ToStringInteger(Int number)
integer to string