Attachment 'SimpleParserBase.cc'

Download

   1 #include "SimpleParserBase.hh"
   2 
   3 #include <iostream>
   4 #include <fstream>
   5 #include <sstream>
   6 #include <algorithm>
   7 
   8 
   9 using namespace std;
  10 
  11 SimpleParserBase::~SimpleParserBase()
  12 {
  13   for ( TokenHandlerMap::iterator it = tokenHandlerMap.begin();
  14 	it != tokenHandlerMap.end(); ++it                       )
  15     delete it->second;
  16 }
  17 
  18 void SimpleParserBase::parse(const char* filename)
  19 {
  20   ifstream in( filename);
  21   string line;
  22 
  23   while( getline( in,line ) )
  24     {
  25       cleanLine( line );
  26       parseLine( line );
  27     }
  28 }
  29 
  30 bool SimpleParserBase::parseLineBase( string& line )
  31 {
  32   _currentToken = "";
  33   _currentValue = "";
  34   stringstream linestr( line );
  35   if ( linestr >> _currentToken )
  36     {
  37       getline( linestr, _currentValue );
  38       trim( _currentValue );
  39       return true;
  40     }
  41   //else
  42   return false;
  43 }
  44 
  45 bool SimpleParserBase::parseLine( string& line )
  46 {
  47   bool notempty = parseLineBase( line );
  48   if ( !notempty )
  49     return true;
  50     
  51   for ( TokenHandlerMap::iterator it = tokenHandlerMap.begin();
  52 	it != tokenHandlerMap.end(); ++it                       )
  53     {
  54       if ( _currentToken == it->first )
  55 	return it->second->process( this, _currentValue );
  56     }
  57   cerr << "SimpoeParserBase: Error: token [" << _currentToken 
  58        << "] invalid." << endl;
  59   return false;
  60 }
  61 
  62 
  63 /// remove leading and trailing whitespaces
  64 void SimpleParserBase::trim(string &line)
  65 {
  66   char const* delims = " \t\r\n";
  67   string::size_type  firstNotWhite = line.find_first_not_of(delims);
  68   line.erase(0,firstNotWhite);
  69 
  70   string::size_type lastNotWhite = line.find_last_not_of(delims);
  71   line.erase(lastNotWhite+1);
  72 }
  73 
  74 
  75 /// \todo fixme: ignore quoted escaped #
  76 void SimpleParserBase::cleanLine( string& line )
  77 {
  78   string::iterator startComment =
  79     find( line.begin(), line.end(), '#' );
  80   line.erase( startComment, line.end() );
  81 }
  82 
  83 
  84 
  85 //============ really needed? bug in gcc 3.4.x? ===========
  86 SimpleParserBase::TokenHandlerBase::~TokenHandlerBase()
  87 {}

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2009-04-15 11:34:30, 1.3 KB) [[attachment:ReaderParser.cc]]
  • [get | view] (2009-04-15 11:34:36, 0.6 KB) [[attachment:ReaderParser.hh]]
  • [get | view] (2009-04-15 11:33:32, 1.9 KB) [[attachment:SimpleParserBase.cc]]
  • [get | view] (2009-04-15 11:34:07, 2.2 KB) [[attachment:SimpleParserBase.hh]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.