00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "Parse.h"
00021 #include <algorithm>
00022
00023
00024
00025
00026
00027
00028
00029
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
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
00064
00065
00066
00067
00068
00069
00070 set<char> charsImplyingAnIdentifierWasRead;
00071 charsImplyingAnIdentifierWasRead.insert('=');
00072 charsImplyingAnIdentifierWasRead.insert('{');
00073
00074 while(!input.eof()) {
00075
00076 input >> openingVal;
00077 if (openingVal == '}') {
00078 return;
00079 }
00080
00081 if (openingVal == '#') {
00082 getline(input,junk, '\n');
00083 #ifdef DEBUG_PARSE_PARSER
00084 debugOut << "\tfound comment " << "\"" << junk << "\"" << endl;
00085 #endif
00086 continue;
00087 }
00088
00089 input.putback(openingVal);
00090
00091 identifier = Util::getNextIdentifier(input, charsImplyingAnIdentifierWasRead);
00092
00093 input >> junkChar;
00094
00095 Util::makeLowercase(identifier);
00096 Util::eraseSpaces(identifier);
00097 ActionMap_t::iterator i = actionMap.find(identifier);
00098
00099
00100 if (i != actionMap.end()) {
00101
00102
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
00111
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()) {
00135 i++;
00136 }
00137
00138 identifier = argv[i];
00139
00140
00141 i++;
00142 minValueIndex = i;
00143
00144 while (i<argc && std::find(tagChars.begin(), tagChars.end(), argv[i][0])==tagChars.end()) {
00145 i++;
00146 }
00147 maxValueIndex = i;
00148
00149
00150 for (int j = minValueIndex; j < maxValueIndex; j++) {
00151 values += argv[j];
00152 values += ' ';
00153 }
00154
00155 ActionMap_t::iterator parseActionPair = actionMap.find(identifier);
00156 if (parseActionPair != actionMap.end()) {
00157 std::istringstream inStream(values);
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