ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE-4/src/mdParser/SimplePreprocessor.hpp
Revision: 2661
Committed: Thu Mar 23 14:59:14 2006 UTC (18 years, 5 months ago) by tim
File size: 6583 byte(s)
Log Message:
Throw exception if the file can not be opened for preprocessing

File Contents

# User Rev Content
1 tim 2469 /*
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    
42     #ifndef MDPARSER_SIMPLEPREPROCESSOR_HPP
43     #define MDPARSER_SIMPLEPREPROCESSOR_HPP
44     #include <iostream>
45     #include <set>
46     #include <fstream>
47 tim 2661 #include <sstream>
48 tim 2469 #include "utils/StringTokenizer.hpp"
49     #include "utils/Trim.hpp"
50 tim 2661 #include "utils/OOPSEException.hpp"
51    
52 tim 2469 /**
53     * @class SimplePreprocessor
54     * @brief A simple preprocessor.
55     * @note only support #include #ifdef, #ifndef, #endif, #define and #undef, c-like multiple line
56     * comment is not support, macro substitude is not support.
57     */
58     namespace oopse {
59     class SimplePreprocessor {
60     public:
61     bool preprocess(const std::string& filename, ostream& os) {
62     std::set<std::string> defineSet;
63     std::stack<bool> ifStates;
64    
65     ifStates.push(true);
66     return doPreprocess(filename, os, defineSet, ifStates);
67     }
68    
69     private:
70     bool doPreprocess(const std::string& filename, ostream& os, std::set<std::string>& defineSet, std::stack<bool>& ifStates) {
71     std::ifstream input(filename.c_str());
72 tim 2661 if (!input.is_open()) {
73     std::stringstream ss;
74     ss << "Can not open " << filename << " for preprocessing\n";
75     throw OOPSEException(ss.str());
76     }
77 tim 2469 int lineNo =1;
78     os << "#line " << lineNo << " \"" << filename << "\"\n";
79     while(input.getline(buffer, bufferSize)) {
80     ++lineNo;
81     std::string line = trimLeftCopy(buffer);
82     if (!line.empty() && line[0] == '#') {
83     StringTokenizer tokenizer(line.substr(1, line.length()));
84     std::vector<std::string> tokens = tokenizer.getAllTokens();
85     if (tokens.size() < 1 ) {
86     return false;
87     }
88     std::string keyword = tokens[0];
89     if (tokens[0] == "endif") {
90     ifStates.pop();
91     if (ifStates.empty()) {
92     std::cout << "Error in preprocessing: endif \n";
93     return false;
94     }
95     os << std::endl;
96     } else if (tokens.size() == 2) {
97     if (tokens[0] == "include") {
98     SimplePreprocessor subPreprocessor;
99     std::string includeFilename = tokens[1];
100     includeFilename = includeFilename.substr(1, includeFilename.length() -2);
101     bool ret = subPreprocessor.doPreprocess(includeFilename, os, defineSet, ifStates);
102     if (!ret) {
103     std::cout << "Error in preprocessing\n";
104     return false;
105     }
106     os << "#line " << lineNo << " \"" << filename << "\"\n";
107     } else if (tokens[0] == "define") {
108     defineSet.insert(tokens[1]);
109     os << std::endl;
110     } else if (tokens[0] == "undef") {
111     defineSet.erase(tokens[1]);
112     os << std::endl;
113     } else if (tokens[0] == "ifdef") {
114     if (defineSet.find(tokens[1]) != defineSet.end() ) {
115     ifStates.push(true);
116     } else {
117     ifStates.push(false);
118     }
119     os << std::endl;
120     } else if (tokens[0] == "ifndef") {
121     if (defineSet.find(tokens[1]) == defineSet.end() ) {
122     ifStates.push(true);
123     } else {
124     ifStates.push(false);
125     }
126     os << std::endl;
127     } else {
128     std::cout << tokens[0] << " is not support" << std::endl;
129     return false;
130     }
131     }else {
132     return false;
133     }
134    
135     }else if (ifStates.top()){
136     os << buffer << std::endl;
137     }
138    
139     }
140    
141     return true;
142     }
143     private:
144    
145     const static int bufferSize = 1024;
146     char buffer[bufferSize];
147     };
148    
149     }
150     #endif