ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE-3.0/src/io/SectionParserManager.cpp
(Generate patch)

Comparing trunk/OOPSE-3.0/src/io/SectionParserManager.cpp (file contents):
Revision 1930 by gezelter, Wed Jan 12 22:41:40 2005 UTC vs.
Revision 2204 by gezelter, Fri Apr 15 22:04:00 2005 UTC

# Line 1 | Line 1
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
# Line 39 | Line 39
39   * such damages.
40   */
41   #include <algorithm>
42 + #include <stack>
43   #include "io/SectionParserManager.hpp"
44   #include "utils/Trim.hpp"
45 + #include "utils/simError.h"
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;
66      char buffer[bufferSize];
67      int lineNo = 0;
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 comment
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] == '/' && 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();
83  
84 <                if (keyword != "begin") {
85 <                    continue;
86 <                }
84 >          if (keyword == "begin") {
85 >            std::string section = tokenizer.nextToken();
86 >            sectionNameStack.push(section);
87  
88 <                std::string section = tokenizer.nextToken();
89 <
90 <                i = std::find_if(sectionParsers_.begin(), sectionParsers_.end(), SameSectionParserFunctor(section));
91 <                if (i == sectionParsers_.end()){
92 <                    //can not find corresponding section parser
93 <                    std::cerr << "Can not find corresponding section parser for section: " << section << std::endl;
94 <                } else {
95 <                    i->isActive = true;
96 <                    i->lineNo = lineNo;
97 <                    i->offset = input.tellg();
98 <                }
99 <                
100 <            }
101 <        }
102 <
88 >            i = std::find_if(sectionParsers_.begin(), sectionParsers_.end(), SameSectionParserFunctor(section));
89 >            if (i == sectionParsers_.end()){
90 >              sprintf(painCave.errMsg, "SectionParserManager Error: Can not find corresponding section parser for %s\n",
91 >                      section.c_str());
92 >              painCave.isFatal = 1;
93 >              simError();                        
94 >            } else {
95 >              if (i->isActive) {
96 >                sprintf(painCave.errMsg, "SectionParserManager Error:find multiple %s section\n",
97 >                        section.c_str());
98 >                painCave.isFatal = 1;
99 >                simError();                        
100 >              } else {                        
101 >                i->isActive = true;
102 >                i->lineNo = lineNo;
103 >                i->offset = input.tellg();
104 >              }
105 >            }
106 >          } else if (keyword == "end") {
107 >            std::string section = tokenizer.nextToken();
108 >            if (sectionNameStack.top() == section) {
109 >              sectionNameStack.pop();
110 >            } else {
111 >              sprintf(painCave.errMsg, "SectionParserManager Error: begin %s and end %s does not match at line %d\n",
112 >                      sectionNameStack.top().c_str(), section.c_str(), lineNo);
113 >              painCave.isFatal = 1;
114 >              simError();
115 >            }
116 >                    
117 >          } else {
118 >            continue;
119 >          }
120 >        }
121 >      }
122          
123      }
124  
125 +    if (!sectionNameStack.empty()) {
126 +      sprintf(painCave.errMsg, "SectionParserManager Error: stack is not empty\n");
127 +      painCave.isFatal = 1;
128 +      simError();
129 +    }
130 +
131      //invoke parser
132      for (i = sectionParsers_.begin(); i != sectionParsers_.end(); ++i) {
133 <        if (i->isActive) {
134 <            //C++ standard does not guarantee seekg  reset EOF, in that case, seekg will fail
135 <            //It is always a good idea to call clear() before seek
136 <            input.clear();            
137 <            input.seekg(i->offset);
138 <            (i->sectionParser)->parse(input, ff, i->lineNo);
139 <        }
133 >      if (i->isActive) {
134 >        //C++ standard does not guarantee seekg  reset EOF, in that case, seekg will fail
135 >        //It is always a good idea to call clear() before seek
136 >        input.clear();            
137 >        input.seekg(i->offset);
138 >        (i->sectionParser)->parse(input, ff, i->lineNo);
139 >      }
140      }
141      
142 < }
142 >  }
143  
144 < void SectionParserManager::push_front(SectionParser* sp) {
144 >  void SectionParserManager::push_front(SectionParser* sp) {
145      SectionParserManager::iterator i;
146      i = findSectionParser(sp->getSectionName());
147      if (i != sectionParsers_.end()) {
148 <        std::cerr << sp->getSectionName() << " section parser is alway existed" << std::endl;
149 <        return;
148 >      std::cerr << sp->getSectionName() << " section parser is alway existed" << std::endl;
149 >      return;
150      }
151      
152      SectionParserContext context;
153      
154      if (sectionParsers_.empty()) {
155 <        context.priority = beginPriority_;
155 >      context.priority = beginPriority_;
156      } else {
157 <        context.priority = sectionParsers_.front().priority - priorityDifference_;
157 >      context.priority = sectionParsers_.front().priority - priorityDifference_;
158      }
159  
160      context.sectionParser = sp;
# Line 135 | Line 163 | void SectionParserManager::push_front(SectionParser* s
163      context.isActive = false;
164  
165      sectionParsers_.push_front(context);
166 < }
166 >  }
167  
168 < void SectionParserManager::push_back(SectionParser* sp) {
168 >  void SectionParserManager::push_back(SectionParser* sp) {
169      SectionParserManager::iterator i;
170      i = findSectionParser(sp->getSectionName());
171      if (i != sectionParsers_.end()) {
172 <        std::cerr << sp->getSectionName() << " section parser is alway existed" << std::endl;
173 <        return;
172 >      std::cerr << sp->getSectionName() << " section parser is alway existed" << std::endl;
173 >      return;
174      }
175  
176      SectionParserContext context;    
177      if (sectionParsers_.empty()) {
178 <        context.priority = beginPriority_;
178 >      context.priority = beginPriority_;
179      } else {
180 <        context.priority = sectionParsers_.back().priority + priorityDifference_;
180 >      context.priority = sectionParsers_.back().priority + priorityDifference_;
181      }
182  
183      context.sectionParser = sp;
# Line 159 | Line 187 | void SectionParserManager::push_back(SectionParser* sp
187  
188      sectionParsers_.push_back(context);
189  
190 < }
190 >  }
191  
192 < void SectionParserManager::insert(SectionParser* sp, int priority) {
192 >  void SectionParserManager::insert(SectionParser* sp, int priority) {
193      SectionParserManager::iterator i;
194      i = findSectionParser(sp->getSectionName());
195      if (i != sectionParsers_.end()) {
196 <        std::cerr << sp->getSectionName() << " section parser is alway existed" << std::endl;
196 >      std::cerr << sp->getSectionName() << " section parser is alway existed" << std::endl;
197      }
198  
199      SectionParserContext context;    
# Line 176 | Line 204 | void SectionParserManager::insert(SectionParser* sp, i
204      context.isActive = false;
205  
206      if (sectionParsers_.empty()) {
207 <        sectionParsers_.push_back(context);
207 >      sectionParsers_.push_back(context);
208      } else {
209  
210 <        for (i = sectionParsers_.begin(); i != sectionParsers_.end(); ++i) {
211 <            if (i->priority == priority) {
212 <                 std::cerr << "Priority " << priority << " already used" << std::endl;
213 <                 return;
214 <            } else if (i->priority > priority) {
215 <                sectionParsers_.insert(i, context);
216 <                break;
217 <            }
210 >      for (i = sectionParsers_.begin(); i != sectionParsers_.end(); ++i) {
211 >        if (i->priority == priority) {
212 >          std::cerr << "Priority " << priority << " already used" << std::endl;
213 >          return;
214 >        } else if (i->priority > priority) {
215 >          sectionParsers_.insert(i, context);
216 >          break;
217 >        }
218              
219 <        }
219 >      }
220      }
221  
222 < }
222 >  }
223  
224  
225 < SectionParserManager::iterator SectionParserManager::findSectionParser(const std::string& sectionName) {
225 >  SectionParserManager::iterator SectionParserManager::findSectionParser(const std::string& sectionName) {
226      SectionParserManager::iterator i;
227      for (i = sectionParsers_.begin(); i != sectionParsers_.end(); ++i) {
228 <        if (i->sectionParser->getSectionName() == sectionName) {
229 <            break;
230 <        }
228 >      if (i->sectionParser->getSectionName() == sectionName) {
229 >        break;
230 >      }
231      }
232  
233      return i;
234 < }
234 >  }
235  
236   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines