| 1 |
< |
/* |
| 1 |
> |
/* |
| 2 |
|
* Copyright (c) 2005 The University of Notre Dame. All Rights Reserved. |
| 3 |
|
* |
| 4 |
|
* The University of Notre Dame grants you ("Licensee") a |
| 46 |
|
|
| 47 |
|
namespace oopse { |
| 48 |
|
|
| 49 |
< |
SectionParserManager::~SectionParserManager() { |
| 49 |
> |
SectionParserManager::~SectionParserManager() { |
| 50 |
|
SectionParserManager::iterator i; |
| 51 |
|
for (i = sectionParsers_.begin(); i != sectionParsers_.end(); ++i) { |
| 52 |
< |
delete (i->sectionParser); |
| 52 |
> |
delete (i->sectionParser); |
| 53 |
|
} |
| 54 |
|
sectionParsers_.clear(); |
| 55 |
< |
} |
| 55 |
> |
} |
| 56 |
|
|
| 57 |
< |
void SectionParserManager::parse(std::istream& input, ForceField& ff) { |
| 57 |
> |
void SectionParserManager::parse(std::istream& input, ForceField& ff) { |
| 58 |
|
|
| 59 |
|
//reset active flags |
| 60 |
|
SectionParserManager::iterator i; |
| 61 |
|
for (i = sectionParsers_.begin(); i != sectionParsers_.end(); ++i) { |
| 62 |
< |
i->isActive = false; |
| 62 |
> |
i->isActive = false; |
| 63 |
|
} |
| 64 |
|
|
| 65 |
|
const int bufferSize = 65535; |
| 68 |
|
std::stack<std::string> sectionNameStack; |
| 69 |
|
//scan through the input stream and find section names |
| 70 |
|
while(input.getline(buffer, bufferSize)) { |
| 71 |
< |
++lineNo; |
| 71 |
> |
++lineNo; |
| 72 |
|
|
| 73 |
< |
std::string line = trimLeftCopy(buffer); |
| 74 |
< |
//a line begins with "//" is a comment line |
| 75 |
< |
if ( line.empty() || (line.size() >= 2 && line[0] == '/' && line[1] == '/')) { |
| 76 |
< |
continue; |
| 77 |
< |
} else { |
| 78 |
< |
StringTokenizer tokenizer(line); |
| 79 |
< |
if (tokenizer.countTokens() < 2) { |
| 80 |
< |
continue; |
| 81 |
< |
} else { |
| 82 |
< |
std::string keyword = tokenizer.nextToken(); |
| 73 |
> |
std::string line = trimLeftCopy(buffer); |
| 74 |
> |
//a line begins with "//" is a comment line |
| 75 |
> |
if ( line.empty() || (line.size() >= 2 && line[0] == '/' |
| 76 |
> |
&& line[1] == '/') ) { |
| 77 |
> |
continue; |
| 78 |
> |
} else { |
| 79 |
> |
StringTokenizer tokenizer(line); |
| 80 |
> |
if (tokenizer.countTokens() < 2) { |
| 81 |
> |
continue; |
| 82 |
> |
} else { |
| 83 |
> |
std::string keyword = tokenizer.nextToken(); |
| 84 |
|
|
| 85 |
< |
if (keyword == "begin") { |
| 86 |
< |
std::string section = tokenizer.nextToken(); |
| 87 |
< |
sectionNameStack.push(section); |
| 88 |
< |
|
| 89 |
< |
i = std::find_if(sectionParsers_.begin(), sectionParsers_.end(), SameSectionParserFunctor(section)); |
| 90 |
< |
if (i == sectionParsers_.end()){ |
| 91 |
< |
sprintf(painCave.errMsg, "SectionParserManager Error: Can not find corresponding section parser for %s\n", |
| 92 |
< |
section.c_str()); |
| 93 |
< |
painCave.isFatal = 1; |
| 94 |
< |
simError(); |
| 95 |
< |
} else { |
| 96 |
< |
if (i->isActive) { |
| 97 |
< |
sprintf(painCave.errMsg, "SectionParserManager Error:find multiple %s section\n", |
| 98 |
< |
section.c_str()); |
| 99 |
< |
painCave.isFatal = 1; |
| 100 |
< |
simError(); |
| 101 |
< |
} else { |
| 102 |
< |
i->isActive = true; |
| 103 |
< |
i->lineNo = lineNo; |
| 104 |
< |
i->offset = input.tellg(); |
| 105 |
< |
} |
| 106 |
< |
} |
| 107 |
< |
} else if (keyword == "end") { |
| 108 |
< |
std::string section = tokenizer.nextToken(); |
| 109 |
< |
if (sectionNameStack.top() == section) { |
| 110 |
< |
sectionNameStack.pop(); |
| 111 |
< |
} else { |
| 112 |
< |
sprintf(painCave.errMsg, "SectionParserManager Error: begin %s and end %s does not match at line %d\n", |
| 113 |
< |
sectionNameStack.top().c_str(), section.c_str(), lineNo); |
| 114 |
< |
painCave.isFatal = 1; |
| 115 |
< |
simError(); |
| 116 |
< |
} |
| 117 |
< |
|
| 118 |
< |
} else { |
| 119 |
< |
continue; |
| 120 |
< |
} |
| 121 |
< |
} |
| 122 |
< |
} |
| 123 |
< |
|
| 85 |
> |
if (keyword == "begin") { |
| 86 |
> |
std::string section = tokenizer.nextToken(); |
| 87 |
> |
sectionNameStack.push(section); |
| 88 |
> |
|
| 89 |
> |
i = std::find_if(sectionParsers_.begin(), sectionParsers_.end(), |
| 90 |
> |
SameSectionParserFunctor(section)); |
| 91 |
> |
if (i == sectionParsers_.end()){ |
| 92 |
> |
sprintf(painCave.errMsg, |
| 93 |
> |
"SectionParserManager Error: Can not find corresponding " |
| 94 |
> |
"section parser for %s\n", |
| 95 |
> |
section.c_str()); |
| 96 |
> |
painCave.isFatal = 1; |
| 97 |
> |
simError(); |
| 98 |
> |
} else { |
| 99 |
> |
if (i->isActive) { |
| 100 |
> |
sprintf(painCave.errMsg, "SectionParserManager Error:find multiple %s " |
| 101 |
> |
"section\n", |
| 102 |
> |
section.c_str()); |
| 103 |
> |
painCave.isFatal = 1; |
| 104 |
> |
simError(); |
| 105 |
> |
} else { |
| 106 |
> |
i->isActive = true; |
| 107 |
> |
i->lineNo = lineNo; |
| 108 |
> |
i->offset = input.tellg(); |
| 109 |
> |
} |
| 110 |
> |
} |
| 111 |
> |
} else if (keyword == "end") { |
| 112 |
> |
std::string section = tokenizer.nextToken(); |
| 113 |
> |
if (sectionNameStack.top() == section) { |
| 114 |
> |
sectionNameStack.pop(); |
| 115 |
> |
} else { |
| 116 |
> |
sprintf(painCave.errMsg, "SectionParserManager Error: begin %s and end %s does not match at line %d\n", |
| 117 |
> |
sectionNameStack.top().c_str(), section.c_str(), lineNo); |
| 118 |
> |
painCave.isFatal = 1; |
| 119 |
> |
simError(); |
| 120 |
> |
} |
| 121 |
> |
|
| 122 |
> |
} else { |
| 123 |
> |
continue; |
| 124 |
> |
} |
| 125 |
> |
} |
| 126 |
> |
} |
| 127 |
> |
|
| 128 |
|
} |
| 129 |
< |
|
| 129 |
> |
|
| 130 |
|
if (!sectionNameStack.empty()) { |
| 131 |
< |
sprintf(painCave.errMsg, "SectionParserManager Error: stack is not empty\n"); |
| 132 |
< |
painCave.isFatal = 1; |
| 133 |
< |
simError(); |
| 131 |
> |
sprintf(painCave.errMsg, "SectionParserManager Error: stack is not empty\n"); |
| 132 |
> |
painCave.isFatal = 1; |
| 133 |
> |
simError(); |
| 134 |
|
} |
| 135 |
< |
|
| 135 |
> |
|
| 136 |
|
//invoke parser |
| 137 |
|
for (i = sectionParsers_.begin(); i != sectionParsers_.end(); ++i) { |
| 138 |
< |
if (i->isActive) { |
| 139 |
< |
//C++ standard does not guarantee seekg reset EOF, in that case, seekg will fail |
| 140 |
< |
//It is always a good idea to call clear() before seek |
| 141 |
< |
input.clear(); |
| 142 |
< |
input.seekg(i->offset); |
| 143 |
< |
(i->sectionParser)->parse(input, ff, i->lineNo); |
| 144 |
< |
} |
| 138 |
> |
if (i->isActive) { |
| 139 |
> |
//C++ standard does not guarantee seekg reset EOF, in that case, seekg will fail |
| 140 |
> |
//It is always a good idea to call clear() before seek |
| 141 |
> |
input.clear(); |
| 142 |
> |
input.seekg(i->offset); |
| 143 |
> |
(i->sectionParser)->parse(input, ff, i->lineNo); |
| 144 |
> |
(i->sectionParser)->validateSection(); |
| 145 |
> |
} |
| 146 |
|
} |
| 147 |
|
|
| 148 |
< |
} |
| 149 |
< |
|
| 150 |
< |
void SectionParserManager::push_front(SectionParser* sp) { |
| 148 |
> |
} |
| 149 |
> |
|
| 150 |
> |
void SectionParserManager::push_front(SectionParser* sp) { |
| 151 |
|
SectionParserManager::iterator i; |
| 152 |
|
i = findSectionParser(sp->getSectionName()); |
| 153 |
|
if (i != sectionParsers_.end()) { |
| 154 |
< |
std::cerr << sp->getSectionName() << " section parser is alway existed" << std::endl; |
| 155 |
< |
return; |
| 154 |
> |
std::cerr << sp->getSectionName() << " section parser already exists" |
| 155 |
> |
<< std::endl; |
| 156 |
> |
return; |
| 157 |
|
} |
| 158 |
|
|
| 159 |
|
SectionParserContext context; |
| 160 |
|
|
| 161 |
|
if (sectionParsers_.empty()) { |
| 162 |
< |
context.priority = beginPriority_; |
| 162 |
> |
context.priority = beginPriority_; |
| 163 |
|
} else { |
| 164 |
< |
context.priority = sectionParsers_.front().priority - priorityDifference_; |
| 164 |
> |
context.priority = sectionParsers_.front().priority - priorityDifference_; |
| 165 |
|
} |
| 166 |
< |
|
| 166 |
> |
|
| 167 |
|
context.sectionParser = sp; |
| 168 |
|
context.lineNo = 0; |
| 169 |
|
context.offset = 0; |
| 170 |
|
context.isActive = false; |
| 171 |
|
|
| 172 |
|
sectionParsers_.push_front(context); |
| 173 |
< |
} |
| 173 |
> |
} |
| 174 |
|
|
| 175 |
< |
void SectionParserManager::push_back(SectionParser* sp) { |
| 175 |
> |
void SectionParserManager::push_back(SectionParser* sp) { |
| 176 |
|
SectionParserManager::iterator i; |
| 177 |
|
i = findSectionParser(sp->getSectionName()); |
| 178 |
|
if (i != sectionParsers_.end()) { |
| 179 |
< |
std::cerr << sp->getSectionName() << " section parser is alway existed" << std::endl; |
| 180 |
< |
return; |
| 179 |
> |
std::cerr << sp->getSectionName() << " section parser already exists" |
| 180 |
> |
<< std::endl; |
| 181 |
> |
return; |
| 182 |
|
} |
| 183 |
|
|
| 184 |
|
SectionParserContext context; |
| 185 |
|
if (sectionParsers_.empty()) { |
| 186 |
< |
context.priority = beginPriority_; |
| 186 |
> |
context.priority = beginPriority_; |
| 187 |
|
} else { |
| 188 |
< |
context.priority = sectionParsers_.back().priority + priorityDifference_; |
| 188 |
> |
context.priority = sectionParsers_.back().priority + priorityDifference_; |
| 189 |
|
} |
| 190 |
|
|
| 191 |
|
context.sectionParser = sp; |
| 195 |
|
|
| 196 |
|
sectionParsers_.push_back(context); |
| 197 |
|
|
| 198 |
< |
} |
| 198 |
> |
} |
| 199 |
|
|
| 200 |
< |
void SectionParserManager::insert(SectionParser* sp, int priority) { |
| 200 |
> |
void SectionParserManager::insert(SectionParser* sp, int priority) { |
| 201 |
|
SectionParserManager::iterator i; |
| 202 |
|
i = findSectionParser(sp->getSectionName()); |
| 203 |
|
if (i != sectionParsers_.end()) { |
| 204 |
< |
std::cerr << sp->getSectionName() << " section parser is alway existed" << std::endl; |
| 204 |
> |
std::cerr << sp->getSectionName() << " section parser already exists" |
| 205 |
> |
<< std::endl; |
| 206 |
|
} |
| 207 |
|
|
| 208 |
|
SectionParserContext context; |
| 213 |
|
context.isActive = false; |
| 214 |
|
|
| 215 |
|
if (sectionParsers_.empty()) { |
| 216 |
< |
sectionParsers_.push_back(context); |
| 216 |
> |
sectionParsers_.push_back(context); |
| 217 |
|
} else { |
| 218 |
|
|
| 219 |
< |
for (i = sectionParsers_.begin(); i != sectionParsers_.end(); ++i) { |
| 220 |
< |
if (i->priority == priority) { |
| 221 |
< |
std::cerr << "Priority " << priority << " already used" << std::endl; |
| 222 |
< |
return; |
| 223 |
< |
} else if (i->priority > priority) { |
| 224 |
< |
sectionParsers_.insert(i, context); |
| 225 |
< |
break; |
| 226 |
< |
} |
| 219 |
> |
for (i = sectionParsers_.begin(); i != sectionParsers_.end(); ++i) { |
| 220 |
> |
if (i->priority == priority) { |
| 221 |
> |
std::cerr << "Priority " << priority << " already used" << std::endl; |
| 222 |
> |
return; |
| 223 |
> |
} else if (i->priority > priority) { |
| 224 |
> |
sectionParsers_.insert(i, context); |
| 225 |
> |
break; |
| 226 |
> |
} |
| 227 |
|
|
| 228 |
< |
} |
| 228 |
> |
} |
| 229 |
|
} |
| 230 |
|
|
| 231 |
< |
} |
| 231 |
> |
} |
| 232 |
|
|
| 233 |
|
|
| 234 |
< |
SectionParserManager::iterator SectionParserManager::findSectionParser(const std::string& sectionName) { |
| 234 |
> |
SectionParserManager::iterator SectionParserManager::findSectionParser(const std::string& sectionName) { |
| 235 |
|
SectionParserManager::iterator i; |
| 236 |
|
for (i = sectionParsers_.begin(); i != sectionParsers_.end(); ++i) { |
| 237 |
< |
if (i->sectionParser->getSectionName() == sectionName) { |
| 238 |
< |
break; |
| 239 |
< |
} |
| 237 |
> |
if (i->sectionParser->getSectionName() == sectionName) { |
| 238 |
> |
break; |
| 239 |
> |
} |
| 240 |
|
} |
| 241 |
|
|
| 242 |
|
return i; |
| 243 |
< |
} |
| 243 |
> |
} |
| 244 |
|
|
| 245 |
|
} |