Hosting courtesy of Sourceforge

SourceForge Logo
Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

Parse.cpp

Go to the documentation of this file.
00001 //
00002 //      Copyright (C) 2002 Robert Renaud
00003 //
00004 //      This program is free software; you can redistribute it and/or
00005 //      modify it under the terms of the GNU General Public License
00006 //      as published by the Free Software Foundation; either version 2
00007 //      of the License, or (at your option) any later version.
00008 //
00009 //      This program is distributed in the hope that it will be useful,
00010 //      but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 //      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
00012 //
00013 //      See the GNU General Public License for more details.
00014 //
00015 //      You should have received a copy of the GNU General Public License
00016 //      along with this program; if not, write to the Free Software
00017 //      Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00018 //
00019 
00020 #include "Parse.h"
00021 #include <algorithm>
00022 
00023 /*
00024 Parse::Parser::Parser(const Parser& otherParser)  :
00025 input(otherParser.input) 
00026 {
00027 for (ActionMap_t::iterator i = otherParser.actionMap.begin(); 
00028 i != otherParser.actionMap.end(); ++i) {
00029 actionMap.insert(*i);
00030 }
00031 }
00032 */
00033 
00034 Parse::Parser::~Parser() {
00035         for (ActionMap_t::iterator i = actionMap.begin(); i != actionMap.end(); ++i) {
00036         i->second->finishingAction();   
00037                 delete i->second;
00038         }
00039 }
00040 
00041 void Parse::Parser::addAction(string identifier, ParseAction* action) {
00042         Util::makeLowercase(identifier);
00043         actionMap[identifier] = action;
00044 }
00045 
00046 Parse::SimpleParser::SimpleParser(std::istream &inputFile)
00047 :
00048 input(inputFile)
00049 { }
00050 
00051 
00052 // #include <iostream>  cout debugging...
00053 
00054 void Parse::SimpleParser::read() {
00055         #ifdef DEBUG_PARSE_PARSER
00056                 debugOut << "SimpleParser::read()" << endl;
00057         #endif
00058         char openingVal;
00059         string identifier;
00060         string junk;
00061         char junkChar;
00062         
00063 /*      std::cout << "outputting known identifiers" << endl;
00064         for (ActionMap_t::iterator i = actionMap.begin(); i != actionMap.end(); ++i) {
00065         std::cout << "\t" << i->first << endl;
00066         }
00067         std::cout << "done with known identifiers" << endl;
00068 */
00069         
00070         set<char> charsImplyingAnIdentifierWasRead;
00071         charsImplyingAnIdentifierWasRead.insert('=');
00072         charsImplyingAnIdentifierWasRead.insert('{');
00073         
00074         while(!input.eof()) {
00075                 
00076                 input >> openingVal;
00077                 if (openingVal == '}') { // closing bracket, this function is done
00078                         return;
00079                 }
00080                 
00081                 if (openingVal == '#') { // # indicates comment
00082                         getline(input,junk, '\n'); // eat the line
00083                         #ifdef DEBUG_PARSE_PARSER
00084                                 debugOut << "\tfound comment " << "\"" << junk << "\"" << endl;
00085                         #endif
00086                         continue;  // go back to start of while
00087                 }
00088                 
00089                 input.putback(openingVal); // this was part of an identifier, put it back
00090                 
00091                 identifier = Util::getNextIdentifier(input, charsImplyingAnIdentifierWasRead);
00092                 
00093                 input >> junkChar; // get the terminating char left in input by gNI
00094 
00095                 Util::makeLowercase(identifier);
00096                 Util::eraseSpaces(identifier);
00097                 ActionMap_t::iterator i = actionMap.find(identifier);                   
00098         
00099                 // if the identifier was added previously to the map
00100                 if (i != actionMap.end()) {
00101                         // call the correpsonding action func to read the value from input
00102                 //      cout << identifier << endl;
00103                         #ifdef DEBUG_PARSE_PARSER
00104                                 debugOut << "\tcalling getValue() for identifier \"" << identifier << "\"" << endl;
00105                         #endif
00106                         i->second->getValue(input);
00107                 } else {
00108                         if (!input.eof()) {
00109                                 input >> junk;
00110                         /*      std::cout << identifier << " not recognized, corresponding junk data "
00111                                         << junk << endl;
00112                         */
00113                         }
00114                 }
00115         }
00116 }
00117 
00118 Parse::CommandLineParser::CommandLineParser(int a, char** b, string tc) :
00119 argc(a),
00120 argv(b),
00121 tagChars(tc)
00122 { }
00123 
00124 
00125 void Parse::CommandLineParser::read() { 
00126 
00127         int i = 1;
00128         while (i < argc) {
00129                 int minValueIndex;
00130                 int maxValueIndex;              
00131                 std::string identifier;
00132                 std::string values;
00133 
00134                 while (i<argc && std::find(tagChars.begin(), tagChars.end(), argv[i][0])==tagChars.end()) {  // skip to first identifier
00135             i++;
00136                 }
00137                 
00138                 identifier = argv[i];
00139                 //cout << "identifier "<< identifier << " at string pos " << i << endl;
00140 
00141                 i++;            
00142                 minValueIndex = i;              
00143                 // find how many value strings there are
00144                 while (i<argc && std::find(tagChars.begin(), tagChars.end(), argv[i][0])==tagChars.end()) {
00145                         i++;
00146                 }       
00147                 maxValueIndex = i;
00148 
00149                 //cout << "minValueIndex = " << minValueIndex << " , maxValueIndex = " << maxValueIndex << endl << endl;
00150                 for (int j = minValueIndex; j < maxValueIndex; j++) {
00151                         values += argv[j];  // concat value char* to string
00152                         values += ' ';
00153                 }
00154 
00155                 ActionMap_t::iterator parseActionPair = actionMap.find(identifier);
00156                 if (parseActionPair != actionMap.end()) {
00157                         std::istringstream inStream(values); // change string to stream
00158                         parseActionPair->second->getValue(inStream);
00159                 }
00160         }                       
00161 }
00162                 
00163 Parse::DelegateToSubParser::DelegateToSubParser(Parser* p) :
00164 parser(p)
00165 { }
00166 
00167 Parse::DelegateToSubParser::~DelegateToSubParser() {
00168         delete parser;
00169 }
00170 
00171 void Parse::DelegateToSubParser::getValue(std::istream& in) {
00172         parser->read();
00173 }
00174 

Generated on Tue May 21 07:26:51 2002 for BomberLAN by doxygen1.2.12 written by Dimitri van Heesch, © 1997-2001