| 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 | 
| 6 |  | * redistribute this software in source and binary code form, provided | 
| 7 |  | * that the following conditions are met: | 
| 8 |  | * | 
| 9 | < | * 1. Acknowledgement of the program authors must be made in any | 
| 10 | < | *    publication of scientific results based in part on use of the | 
| 11 | < | *    program.  An acceptable form of acknowledgement is citation of | 
| 12 | < | *    the article in which the program was described (Matthew | 
| 13 | < | *    A. Meineke, Charles F. Vardeman II, Teng Lin, Christopher | 
| 14 | < | *    J. Fennell and J. Daniel Gezelter, "OOPSE: An Object-Oriented | 
| 15 | < | *    Parallel Simulation Engine for Molecular Dynamics," | 
| 16 | < | *    J. Comput. Chem. 26, pp. 252-271 (2005)) | 
| 17 | < | * | 
| 18 | < | * 2. Redistributions of source code must retain the above copyright | 
| 9 | > | * 1. Redistributions of source code must retain the above copyright | 
| 10 |  | *    notice, this list of conditions and the following disclaimer. | 
| 11 |  | * | 
| 12 | < | * 3. Redistributions in binary form must reproduce the above copyright | 
| 12 | > | * 2. Redistributions in binary form must reproduce the above copyright | 
| 13 |  | *    notice, this list of conditions and the following disclaimer in the | 
| 14 |  | *    documentation and/or other materials provided with the | 
| 15 |  | *    distribution. | 
| 28 |  | * arising out of the use of or inability to use software, even if the | 
| 29 |  | * University of Notre Dame has been advised of the possibility of | 
| 30 |  | * such damages. | 
| 31 | + | * | 
| 32 | + | * SUPPORT OPEN SCIENCE!  If you use OpenMD or its source code in your | 
| 33 | + | * research, please cite the appropriate papers when you publish your | 
| 34 | + | * work.  Good starting points are: | 
| 35 | + | * | 
| 36 | + | * [1]  Meineke, et al., J. Comp. Chem. 26, 252-271 (2005). | 
| 37 | + | * [2]  Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006). | 
| 38 | + | * [3]  Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008). | 
| 39 | + | * [4]  Kuang & Gezelter,  J. Chem. Phys. 133, 164101 (2010). | 
| 40 | + | * [5]  Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011). | 
| 41 |  | */ | 
| 42 |  | #include <algorithm> | 
| 43 | + | #include <stack> | 
| 44 | + | #include <cstdio> | 
| 45 |  | #include "io/SectionParserManager.hpp" | 
| 46 |  | #include "utils/Trim.hpp" | 
| 47 | + | #include "utils/simError.h" | 
| 48 |  |  | 
| 49 | < | namespace oopse { | 
| 49 | > | namespace OpenMD { | 
| 50 |  |  | 
| 51 | < | SectionParserManager::~SectionParserManager() { | 
| 51 | > | SectionParserManager::~SectionParserManager() { | 
| 52 |  | SectionParserManager::iterator i; | 
| 53 |  | for (i = sectionParsers_.begin(); i != sectionParsers_.end(); ++i) { | 
| 54 | < | delete (i->sectionParser); | 
| 54 | > | delete (i->sectionParser); | 
| 55 |  | } | 
| 56 |  | sectionParsers_.clear(); | 
| 57 | < | } | 
| 57 | > | } | 
| 58 |  |  | 
| 59 | < | void SectionParserManager::parse(std::istream& input, ForceField& ff) { | 
| 59 | > | void SectionParserManager::parse(std::istream& input, ForceField& ff) { | 
| 60 |  |  | 
| 61 |  | //reset active flags | 
| 62 |  | SectionParserManager::iterator i; | 
| 63 |  | for (i = sectionParsers_.begin(); i != sectionParsers_.end(); ++i) { | 
| 64 | < | i->isActive = false; | 
| 64 | > | i->isActive = false; | 
| 65 |  | } | 
| 66 |  |  | 
| 67 |  | const int bufferSize = 65535; | 
| 68 |  | char buffer[bufferSize]; | 
| 69 |  | int lineNo = 0; | 
| 70 | + | std::stack<std::string> sectionNameStack; | 
| 71 |  | //scan through the input stream and find section names | 
| 72 |  | while(input.getline(buffer, bufferSize)) { | 
| 73 | < | ++lineNo; | 
| 73 | > | ++lineNo; | 
| 74 |  |  | 
| 75 | < | std::string line = trimLeftCopy(buffer); | 
| 76 | < | //a line begins with "//" is comment | 
| 77 | < | if ( line.empty() || (line.size() >= 2 && line[0] == '/' && line[1] == '/')) { | 
| 78 | < | continue; | 
| 79 | < | } else { | 
| 80 | < | StringTokenizer tokenizer(line); | 
| 81 | < | if (tokenizer.countTokens() < 2) { | 
| 82 | < | continue; | 
| 83 | < | } else { | 
| 84 | < | std::string keyword = tokenizer.nextToken(); | 
| 75 | > | std::string line = trimLeftCopy(buffer); | 
| 76 | > | //a line begins with "//" is a comment line | 
| 77 | > | if ( line.empty() || (line.size() >= 2 && line[0] == '/' | 
| 78 | > | && line[1] == '/') ) { | 
| 79 | > | continue; | 
| 80 | > | } else { | 
| 81 | > | StringTokenizer tokenizer(line); | 
| 82 | > | if (tokenizer.countTokens() < 2) { | 
| 83 | > | continue; | 
| 84 | > | } else { | 
| 85 | > | std::string keyword = tokenizer.nextToken(); | 
| 86 |  |  | 
| 87 | < | if (keyword != "begin") { | 
| 88 | < | continue; | 
| 89 | < | } | 
| 90 | < |  | 
| 91 | < | std::string section = tokenizer.nextToken(); | 
| 92 | < |  | 
| 93 | < | i = std::find_if(sectionParsers_.begin(), sectionParsers_.end(), SameSectionParserFunctor(section)); | 
| 94 | < | if (i == sectionParsers_.end()){ | 
| 95 | < | //can not find corresponding section parser | 
| 96 | < | std::cerr << "Can not find corresponding section parser for section: " << section << std::endl; | 
| 97 | < | } else { | 
| 98 | < | i->isActive = true; | 
| 99 | < | i->lineNo = lineNo; | 
| 100 | < | i->offset = input.tellg(); | 
| 101 | < | } | 
| 102 | < |  | 
| 103 | < | } | 
| 104 | < | } | 
| 105 | < |  | 
| 106 | < |  | 
| 87 | > | if (keyword == "begin") { | 
| 88 | > | std::string section = tokenizer.nextToken(); | 
| 89 | > | sectionNameStack.push(section); | 
| 90 | > |  | 
| 91 | > | i = std::find_if(sectionParsers_.begin(), sectionParsers_.end(), | 
| 92 | > | SameSectionParserFunctor(section)); | 
| 93 | > | if (i == sectionParsers_.end()){ | 
| 94 | > | sprintf(painCave.errMsg, | 
| 95 | > | "SectionParserManager Error: Can not find corresponding " | 
| 96 | > | "section parser for %s\n", | 
| 97 | > | section.c_str()); | 
| 98 | > | painCave.isFatal = 1; | 
| 99 | > | simError(); | 
| 100 | > | } else { | 
| 101 | > | if (i->isActive) { | 
| 102 | > | sprintf(painCave.errMsg, "SectionParserManager Error:find multiple %s " | 
| 103 | > | "section\n", | 
| 104 | > | section.c_str()); | 
| 105 | > | painCave.isFatal = 1; | 
| 106 | > | simError(); | 
| 107 | > | } else { | 
| 108 | > | i->isActive = true; | 
| 109 | > | i->lineNo = lineNo; | 
| 110 | > | i->offset = input.tellg(); | 
| 111 | > | } | 
| 112 | > | } | 
| 113 | > | } else if (keyword == "end") { | 
| 114 | > | std::string section = tokenizer.nextToken(); | 
| 115 | > | if (sectionNameStack.top() == section) { | 
| 116 | > | sectionNameStack.pop(); | 
| 117 | > | } else { | 
| 118 | > | sprintf(painCave.errMsg, "SectionParserManager Error: begin %s and end %s does not match at line %d\n", | 
| 119 | > | sectionNameStack.top().c_str(), section.c_str(), lineNo); | 
| 120 | > | painCave.isFatal = 1; | 
| 121 | > | simError(); | 
| 122 | > | } | 
| 123 | > |  | 
| 124 | > | } else { | 
| 125 | > | continue; | 
| 126 | > | } | 
| 127 | > | } | 
| 128 | > | } | 
| 129 | > |  | 
| 130 |  | } | 
| 131 | < |  | 
| 131 | > |  | 
| 132 | > | if (!sectionNameStack.empty()) { | 
| 133 | > | sprintf(painCave.errMsg, "SectionParserManager Error: stack is not empty\n"); | 
| 134 | > | painCave.isFatal = 1; | 
| 135 | > | simError(); | 
| 136 | > | } | 
| 137 | > |  | 
| 138 |  | //invoke parser | 
| 139 |  | for (i = sectionParsers_.begin(); i != sectionParsers_.end(); ++i) { | 
| 140 | < | if (i->isActive) { | 
| 141 | < | //C++ standard does not guarantee seekg  reset EOF, in that case, seekg will fail | 
| 142 | < | //It is always a good idea to call clear() before seek | 
| 143 | < | input.clear(); | 
| 144 | < | input.seekg(i->offset); | 
| 145 | < | (i->sectionParser)->parse(input, ff, i->lineNo); | 
| 146 | < | } | 
| 140 | > | if (i->isActive) { | 
| 141 | > | //C++ standard does not guarantee seekg  reset EOF, in that case, seekg will fail | 
| 142 | > | //It is always a good idea to call clear() before seek | 
| 143 | > | input.clear(); | 
| 144 | > | input.seekg(i->offset); | 
| 145 | > | (i->sectionParser)->parse(input, ff, i->lineNo); | 
| 146 | > | (i->sectionParser)->validateSection(); | 
| 147 | > | } | 
| 148 |  | } | 
| 149 |  |  | 
| 150 | < | } | 
| 151 | < |  | 
| 152 | < | void SectionParserManager::push_front(SectionParser* sp) { | 
| 150 | > | } | 
| 151 | > |  | 
| 152 | > | void SectionParserManager::push_front(SectionParser* sp) { | 
| 153 |  | SectionParserManager::iterator i; | 
| 154 |  | i = findSectionParser(sp->getSectionName()); | 
| 155 |  | if (i != sectionParsers_.end()) { | 
| 156 | < | std::cerr << sp->getSectionName() << " section parser is alway existed" << std::endl; | 
| 157 | < | return; | 
| 156 | > | std::cerr << sp->getSectionName() << " section parser already exists" | 
| 157 | > | << std::endl; | 
| 158 | > | return; | 
| 159 |  | } | 
| 160 |  |  | 
| 161 |  | SectionParserContext context; | 
| 162 |  |  | 
| 163 |  | if (sectionParsers_.empty()) { | 
| 164 | < | context.priority = beginPriority_; | 
| 164 | > | context.priority = beginPriority_; | 
| 165 |  | } else { | 
| 166 | < | context.priority = sectionParsers_.front().priority - priorityDifference_; | 
| 166 | > | context.priority = sectionParsers_.front().priority - priorityDifference_; | 
| 167 |  | } | 
| 168 | < |  | 
| 168 | > |  | 
| 169 |  | context.sectionParser = sp; | 
| 170 |  | context.lineNo = 0; | 
| 171 |  | context.offset = 0; | 
| 172 |  | context.isActive = false; | 
| 173 |  |  | 
| 174 |  | sectionParsers_.push_front(context); | 
| 175 | < | } | 
| 175 | > | } | 
| 176 |  |  | 
| 177 | < | void SectionParserManager::push_back(SectionParser* sp) { | 
| 177 | > | void SectionParserManager::push_back(SectionParser* sp) { | 
| 178 |  | SectionParserManager::iterator i; | 
| 179 |  | i = findSectionParser(sp->getSectionName()); | 
| 180 |  | if (i != sectionParsers_.end()) { | 
| 181 | < | std::cerr << sp->getSectionName() << " section parser is alway existed" << std::endl; | 
| 182 | < | return; | 
| 181 | > | std::cerr << sp->getSectionName() << " section parser already exists" | 
| 182 | > | << std::endl; | 
| 183 | > | return; | 
| 184 |  | } | 
| 185 |  |  | 
| 186 |  | SectionParserContext context; | 
| 187 |  | if (sectionParsers_.empty()) { | 
| 188 | < | context.priority = beginPriority_; | 
| 188 | > | context.priority = beginPriority_; | 
| 189 |  | } else { | 
| 190 | < | context.priority = sectionParsers_.back().priority + priorityDifference_; | 
| 190 | > | context.priority = sectionParsers_.back().priority + priorityDifference_; | 
| 191 |  | } | 
| 192 |  |  | 
| 193 |  | context.sectionParser = sp; | 
| 197 |  |  | 
| 198 |  | sectionParsers_.push_back(context); | 
| 199 |  |  | 
| 200 | < | } | 
| 200 | > | } | 
| 201 |  |  | 
| 202 | < | void SectionParserManager::insert(SectionParser* sp, int priority) { | 
| 202 | > | void SectionParserManager::insert(SectionParser* sp, int priority) { | 
| 203 |  | SectionParserManager::iterator i; | 
| 204 |  | i = findSectionParser(sp->getSectionName()); | 
| 205 |  | if (i != sectionParsers_.end()) { | 
| 206 | < | std::cerr << sp->getSectionName() << " section parser is alway existed" << std::endl; | 
| 206 | > | std::cerr << sp->getSectionName() << " section parser already exists" | 
| 207 | > | << std::endl; | 
| 208 |  | } | 
| 209 |  |  | 
| 210 |  | SectionParserContext context; | 
| 215 |  | context.isActive = false; | 
| 216 |  |  | 
| 217 |  | if (sectionParsers_.empty()) { | 
| 218 | < | sectionParsers_.push_back(context); | 
| 218 | > | sectionParsers_.push_back(context); | 
| 219 |  | } else { | 
| 220 |  |  | 
| 221 | < | for (i = sectionParsers_.begin(); i != sectionParsers_.end(); ++i) { | 
| 222 | < | if (i->priority == priority) { | 
| 223 | < | std::cerr << "Priority " << priority << " already used" << std::endl; | 
| 224 | < | return; | 
| 225 | < | } else if (i->priority > priority) { | 
| 226 | < | sectionParsers_.insert(i, context); | 
| 227 | < | break; | 
| 228 | < | } | 
| 221 | > | for (i = sectionParsers_.begin(); i != sectionParsers_.end(); ++i) { | 
| 222 | > | if (i->priority == priority) { | 
| 223 | > | std::cerr << "Priority " << priority << " already used" << std::endl; | 
| 224 | > | return; | 
| 225 | > | } else if (i->priority > priority) { | 
| 226 | > | sectionParsers_.insert(i, context); | 
| 227 | > | break; | 
| 228 | > | } | 
| 229 |  |  | 
| 230 | < | } | 
| 230 | > | } | 
| 231 |  | } | 
| 232 |  |  | 
| 233 | < | } | 
| 233 | > | } | 
| 234 |  |  | 
| 235 |  |  | 
| 236 | < | SectionParserManager::iterator SectionParserManager::findSectionParser(const std::string& sectionName) { | 
| 236 | > | SectionParserManager::iterator SectionParserManager::findSectionParser(const std::string& sectionName) { | 
| 237 |  | SectionParserManager::iterator i; | 
| 238 |  | for (i = sectionParsers_.begin(); i != sectionParsers_.end(); ++i) { | 
| 239 | < | if (i->sectionParser->getSectionName() == sectionName) { | 
| 240 | < | break; | 
| 241 | < | } | 
| 239 | > | if (i->sectionParser->getSectionName() == sectionName) { | 
| 240 | > | break; | 
| 241 | > | } | 
| 242 |  | } | 
| 243 |  |  | 
| 244 |  | return i; | 
| 245 | < | } | 
| 245 | > | } | 
| 246 |  |  | 
| 247 |  | } |