ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE-4/src/io/SectionParserManager.cpp
Revision: 1930
Committed: Wed Jan 12 22:41:40 2005 UTC (19 years, 5 months ago) by gezelter
File size: 7143 byte(s)
Log Message:
merging new_design branch into OOPSE-2.0

File Contents

# User Rev Content
1 gezelter 1930 /*
2     * Copyright (c) 2005 The University of Notre Dame. All Rights Reserved.
3     *
4     * The University of Notre Dame grants you ("Licensee") a
5     * non-exclusive, royalty free, license to use, modify and
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
19     * notice, this list of conditions and the following disclaimer.
20     *
21     * 3. Redistributions in binary form must reproduce the above copyright
22     * notice, this list of conditions and the following disclaimer in the
23     * documentation and/or other materials provided with the
24     * distribution.
25     *
26     * This software is provided "AS IS," without a warranty of any
27     * kind. All express or implied conditions, representations and
28     * warranties, including any implied warranty of merchantability,
29     * fitness for a particular purpose or non-infringement, are hereby
30     * excluded. The University of Notre Dame and its licensors shall not
31     * be liable for any damages suffered by licensee as a result of
32     * using, modifying or distributing the software or its
33     * derivatives. In no event will the University of Notre Dame or its
34     * licensors be liable for any lost revenue, profit or data, or for
35     * direct, indirect, special, consequential, incidental or punitive
36     * damages, however caused and regardless of the theory of liability,
37     * arising out of the use of or inability to use software, even if the
38     * University of Notre Dame has been advised of the possibility of
39     * such damages.
40     */
41     #include <algorithm>
42     #include "io/SectionParserManager.hpp"
43     #include "utils/Trim.hpp"
44    
45     namespace oopse {
46    
47     SectionParserManager::~SectionParserManager() {
48     SectionParserManager::iterator i;
49     for (i = sectionParsers_.begin(); i != sectionParsers_.end(); ++i) {
50     delete (i->sectionParser);
51     }
52     sectionParsers_.clear();
53     }
54    
55     void SectionParserManager::parse(std::istream& input, ForceField& ff) {
56    
57     //reset active flags
58     SectionParserManager::iterator i;
59     for (i = sectionParsers_.begin(); i != sectionParsers_.end(); ++i) {
60     i->isActive = false;
61     }
62    
63     const int bufferSize = 65535;
64     char buffer[bufferSize];
65     int lineNo = 0;
66     //scan through the input stream and find section names
67     while(input.getline(buffer, bufferSize)) {
68     ++lineNo;
69    
70     std::string line = trimLeftCopy(buffer);
71     //a line begins with "//" is comment
72     if ( line.empty() || (line.size() >= 2 && line[0] == '/' && line[1] == '/')) {
73     continue;
74     } else {
75     StringTokenizer tokenizer(line);
76     if (tokenizer.countTokens() < 2) {
77     continue;
78     } else {
79     std::string keyword = tokenizer.nextToken();
80    
81     if (keyword != "begin") {
82     continue;
83     }
84    
85     std::string section = tokenizer.nextToken();
86    
87     i = std::find_if(sectionParsers_.begin(), sectionParsers_.end(), SameSectionParserFunctor(section));
88     if (i == sectionParsers_.end()){
89     //can not find corresponding section parser
90     std::cerr << "Can not find corresponding section parser for section: " << section << std::endl;
91     } else {
92     i->isActive = true;
93     i->lineNo = lineNo;
94     i->offset = input.tellg();
95     }
96    
97     }
98     }
99    
100    
101     }
102    
103     //invoke parser
104     for (i = sectionParsers_.begin(); i != sectionParsers_.end(); ++i) {
105     if (i->isActive) {
106     //C++ standard does not guarantee seekg reset EOF, in that case, seekg will fail
107     //It is always a good idea to call clear() before seek
108     input.clear();
109     input.seekg(i->offset);
110     (i->sectionParser)->parse(input, ff, i->lineNo);
111     }
112     }
113    
114     }
115    
116     void SectionParserManager::push_front(SectionParser* sp) {
117     SectionParserManager::iterator i;
118     i = findSectionParser(sp->getSectionName());
119     if (i != sectionParsers_.end()) {
120     std::cerr << sp->getSectionName() << " section parser is alway existed" << std::endl;
121     return;
122     }
123    
124     SectionParserContext context;
125    
126     if (sectionParsers_.empty()) {
127     context.priority = beginPriority_;
128     } else {
129     context.priority = sectionParsers_.front().priority - priorityDifference_;
130     }
131    
132     context.sectionParser = sp;
133     context.lineNo = 0;
134     context.offset = 0;
135     context.isActive = false;
136    
137     sectionParsers_.push_front(context);
138     }
139    
140     void SectionParserManager::push_back(SectionParser* sp) {
141     SectionParserManager::iterator i;
142     i = findSectionParser(sp->getSectionName());
143     if (i != sectionParsers_.end()) {
144     std::cerr << sp->getSectionName() << " section parser is alway existed" << std::endl;
145     return;
146     }
147    
148     SectionParserContext context;
149     if (sectionParsers_.empty()) {
150     context.priority = beginPriority_;
151     } else {
152     context.priority = sectionParsers_.back().priority + priorityDifference_;
153     }
154    
155     context.sectionParser = sp;
156     context.lineNo = 0;
157     context.offset = 0;
158     context.isActive = false;
159    
160     sectionParsers_.push_back(context);
161    
162     }
163    
164     void SectionParserManager::insert(SectionParser* sp, int priority) {
165     SectionParserManager::iterator i;
166     i = findSectionParser(sp->getSectionName());
167     if (i != sectionParsers_.end()) {
168     std::cerr << sp->getSectionName() << " section parser is alway existed" << std::endl;
169     }
170    
171     SectionParserContext context;
172     context.priority = priority;
173     context.sectionParser = sp;
174     context.lineNo = 0;
175     context.offset = 0;
176     context.isActive = false;
177    
178     if (sectionParsers_.empty()) {
179     sectionParsers_.push_back(context);
180     } else {
181    
182     for (i = sectionParsers_.begin(); i != sectionParsers_.end(); ++i) {
183     if (i->priority == priority) {
184     std::cerr << "Priority " << priority << " already used" << std::endl;
185     return;
186     } else if (i->priority > priority) {
187     sectionParsers_.insert(i, context);
188     break;
189     }
190    
191     }
192     }
193    
194     }
195    
196    
197     SectionParserManager::iterator SectionParserManager::findSectionParser(const std::string& sectionName) {
198     SectionParserManager::iterator i;
199     for (i = sectionParsers_.begin(); i != sectionParsers_.end(); ++i) {
200     if (i->sectionParser->getSectionName() == sectionName) {
201     break;
202     }
203     }
204    
205     return i;
206     }
207    
208     }