ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/branches/development/src/mdParser/SimplePreprocessor.hpp
Revision: 1665
Committed: Tue Nov 22 20:38:56 2011 UTC (13 years, 5 months ago) by gezelter
File size: 7632 byte(s)
Log Message:
updated copyright notices

File Contents

# User Rev Content
1 tim 770 /*
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 gezelter 1390 * 1. Redistributions of source code must retain the above copyright
10 tim 770 * notice, this list of conditions and the following disclaimer.
11     *
12 gezelter 1390 * 2. Redistributions in binary form must reproduce the above copyright
13 tim 770 * notice, this list of conditions and the following disclaimer in the
14     * documentation and/or other materials provided with the
15     * distribution.
16     *
17     * This software is provided "AS IS," without a warranty of any
18     * kind. All express or implied conditions, representations and
19     * warranties, including any implied warranty of merchantability,
20     * fitness for a particular purpose or non-infringement, are hereby
21     * excluded. The University of Notre Dame and its licensors shall not
22     * be liable for any damages suffered by licensee as a result of
23     * using, modifying or distributing the software or its
24     * derivatives. In no event will the University of Notre Dame or its
25     * licensors be liable for any lost revenue, profit or data, or for
26     * direct, indirect, special, consequential, incidental or punitive
27     * damages, however caused and regardless of the theory of liability,
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 gezelter 1390 *
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, 24107 (2008).
39 gezelter 1665 * [4] Kuang & Gezelter, J. Chem. Phys. 133, 164101 (2010).
40     * [5] Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011).
41 tim 770 */
42    
43     #ifndef MDPARSER_SIMPLEPREPROCESSOR_HPP
44     #define MDPARSER_SIMPLEPREPROCESSOR_HPP
45     #include <iostream>
46     #include <set>
47     #include <fstream>
48 tim 912 #include <sstream>
49 tim 770 #include "utils/StringTokenizer.hpp"
50     #include "utils/Trim.hpp"
51 gezelter 1390 #include "utils/OpenMDException.hpp"
52 chuckv 924 #include "utils/simError.h"
53 tim 912
54 chuckv 924
55 tim 770 /**
56     * @class SimplePreprocessor
57     * @brief A simple preprocessor.
58 gezelter 938 * @note only supports #include #ifdef, #ifndef, #endif, #define and #undef, c-like multiple line
59     * comment is not supported, macro substitute is not supported.
60 tim 770 */
61 gezelter 1390 namespace OpenMD {
62 tim 770 class SimplePreprocessor {
63     public:
64 tim 1024 bool preprocess(std::istream& myStream, const std::string& filename, int startingLine, ostream& os) {
65 tim 770 std::set<std::string> defineSet;
66     std::stack<bool> ifStates;
67    
68     ifStates.push(true);
69 tim 1024 return doPreprocess(myStream, filename, startingLine, os, defineSet, ifStates);
70 tim 770 }
71    
72     private:
73 tim 1024 bool doPreprocess(std::istream& myStream, const std::string& filename, int startingLine, ostream& os, std::set<std::string>& defineSet, std::stack<bool>& ifStates) {
74     //std::ifstream input(filename.c_str());
75     //if (!input.is_open()) {
76     // std::stringstream ss;
77     // ss << "Can not open " << filename << " for preprocessing\n";
78     //
79     // sprintf(painCave.errMsg,
80     // "Can not open (%s) for processing. \n"
81     // "\tPlease check md file name syntax.\n", filename.c_str());
82     //
83     // painCave.isFatal = 1;
84     // simError();
85     //
86 gezelter 1390 // throw OpenMDException(ss.str());
87 tim 1024 //}
88     int lineNo = startingLine;
89 tim 770 os << "#line " << lineNo << " \"" << filename << "\"\n";
90 tim 1024 const int bufferSize = 1024;
91     char buffer[bufferSize];
92     while(myStream.getline(buffer, bufferSize)) {
93 tim 770 ++lineNo;
94     std::string line = trimLeftCopy(buffer);
95     if (!line.empty() && line[0] == '#') {
96     StringTokenizer tokenizer(line.substr(1, line.length()));
97     std::vector<std::string> tokens = tokenizer.getAllTokens();
98     if (tokens.size() < 1 ) {
99     return false;
100     }
101     std::string keyword = tokens[0];
102     if (tokens[0] == "endif") {
103     ifStates.pop();
104     if (ifStates.empty()) {
105     std::cout << "Error in preprocessing: endif \n";
106     return false;
107     }
108     os << std::endl;
109     } else if (tokens.size() == 2) {
110     if (tokens[0] == "include") {
111     SimplePreprocessor subPreprocessor;
112     std::string includeFilename = tokens[1];
113     includeFilename = includeFilename.substr(1, includeFilename.length() -2);
114 tim 1024 std::ifstream includeStream(includeFilename.c_str());
115     if (!includeStream.is_open()) {
116     std::stringstream ss;
117 chuckv 1245 ss << "Can not open " << includeFilename << " for preprocessing\n";
118 gezelter 1390 throw OpenMDException(ss.str());
119 tim 1024 }
120    
121     bool ret = subPreprocessor.doPreprocess(includeStream, includeFilename, 1, os, defineSet, ifStates);
122 tim 770 if (!ret) {
123     std::cout << "Error in preprocessing\n";
124     return false;
125     }
126     os << "#line " << lineNo << " \"" << filename << "\"\n";
127     } else if (tokens[0] == "define") {
128     defineSet.insert(tokens[1]);
129     os << std::endl;
130     } else if (tokens[0] == "undef") {
131     defineSet.erase(tokens[1]);
132     os << std::endl;
133     } else if (tokens[0] == "ifdef") {
134     if (defineSet.find(tokens[1]) != defineSet.end() ) {
135     ifStates.push(true);
136     } else {
137     ifStates.push(false);
138     }
139     os << std::endl;
140     } else if (tokens[0] == "ifndef") {
141     if (defineSet.find(tokens[1]) == defineSet.end() ) {
142     ifStates.push(true);
143     } else {
144     ifStates.push(false);
145     }
146     os << std::endl;
147     } else {
148 gezelter 938 std::cout << tokens[0] << " is not supported (yet)." << std::endl;
149 tim 770 return false;
150     }
151     }else {
152     return false;
153     }
154    
155     }else if (ifStates.top()){
156     os << buffer << std::endl;
157     }
158    
159     }
160    
161     return true;
162     }
163     private:
164    
165     };
166    
167     }
168     #endif

Properties

Name Value
svn:keywords Author Id Revision Date