--- trunk/OOPSE-4/src/io/SectionParserManager.cpp 2005/01/12 22:41:40 1930 +++ trunk/OOPSE-4/src/io/SectionParserManager.cpp 2005/03/09 17:30:29 2097 @@ -39,8 +39,10 @@ * such damages. */ #include +#include #include "io/SectionParserManager.hpp" #include "utils/Trim.hpp" +#include "utils/simError.h" namespace oopse { @@ -63,12 +65,13 @@ void SectionParserManager::parse(std::istream& input, const int bufferSize = 65535; char buffer[bufferSize]; int lineNo = 0; + std::stack sectionNameStack; //scan through the input stream and find section names while(input.getline(buffer, bufferSize)) { ++lineNo; std::string line = trimLeftCopy(buffer); - //a line begins with "//" is comment + //a line begins with "//" is a comment line if ( line.empty() || (line.size() >= 2 && line[0] == '/' && line[1] == '/')) { continue; } else { @@ -78,28 +81,53 @@ void SectionParserManager::parse(std::istream& input, } else { std::string keyword = tokenizer.nextToken(); - if (keyword != "begin") { - continue; - } + if (keyword == "begin") { + std::string section = tokenizer.nextToken(); + sectionNameStack.push(section); - std::string section = tokenizer.nextToken(); - - i = std::find_if(sectionParsers_.begin(), sectionParsers_.end(), SameSectionParserFunctor(section)); - if (i == sectionParsers_.end()){ - //can not find corresponding section parser - std::cerr << "Can not find corresponding section parser for section: " << section << std::endl; + i = std::find_if(sectionParsers_.begin(), sectionParsers_.end(), SameSectionParserFunctor(section)); + if (i == sectionParsers_.end()){ + sprintf(painCave.errMsg, "SectionParserManager Error: Can not find corresponding section parser for %s\n", + section.c_str()); + painCave.isFatal = 1; + simError(); + } else { + if (i->isActive) { + sprintf(painCave.errMsg, "SectionParserManager Error:find multiple %s section\n", + section.c_str()); + painCave.isFatal = 1; + simError(); + } else { + i->isActive = true; + i->lineNo = lineNo; + i->offset = input.tellg(); + } + } + } else if (keyword == "end") { + std::string section = tokenizer.nextToken(); + if (sectionNameStack.top() == section) { + sectionNameStack.pop(); + } else { + sprintf(painCave.errMsg, "SectionParserManager Error: begin %s and end %s does not match at line %d\n", + sectionNameStack.top().c_str(), section.c_str(), lineNo); + painCave.isFatal = 1; + simError(); + } + } else { - i->isActive = true; - i->lineNo = lineNo; - i->offset = input.tellg(); + continue; } - } } - } + if (!sectionNameStack.empty()) { + sprintf(painCave.errMsg, "SectionParserManager Error: stack is not empty\n"); + painCave.isFatal = 1; + simError(); + } + //invoke parser for (i = sectionParsers_.begin(); i != sectionParsers_.end(); ++i) { if (i->isActive) {