66 bool preprocess(std::istream& myStream,
const std::string& filename,
int startingLine, ostream& os) {
67 std::set<std::string> defineSet;
68 std::stack<bool> ifStates;
71 return doPreprocess(myStream, filename, startingLine, os, defineSet, ifStates);
75 bool doPreprocess(std::istream& myStream,
const std::string& filename,
int startingLine, ostream& os, std::set<std::string>& defineSet, std::stack<bool>& ifStates) {
90 int lineNo = startingLine;
91 os <<
"#line " << lineNo <<
" \"" << filename <<
"\"\n";
92 const int bufferSize = 8192;
93 char buffer[bufferSize];
94 while(myStream.getline(buffer, bufferSize)) {
96 std::string line = Utils::trimLeftCopy(buffer);
97 if (!line.empty() && line[0] ==
'#') {
99 std::vector<std::string> tokens = tokenizer.
getAllTokens();
100 if (tokens.empty()) {
103 if (tokens[0] ==
"endif") {
105 if (ifStates.empty()) {
106 std::cout <<
"Error in preprocessing: endif \n";
110 }
else if (tokens.size() == 2) {
111 if (tokens[0] ==
"include") {
113 std::string includeFilename = tokens[1];
114 includeFilename = includeFilename.substr(1, includeFilename.length() -2);
115 std::ifstream includeStream(includeFilename.c_str());
116 if (!includeStream.is_open()) {
117 std::stringstream ss;
118 ss <<
"Can not open " << includeFilename <<
" for preprocessing\n";
122 bool ret = subPreprocessor.doPreprocess(includeStream, includeFilename, 1, os, defineSet, ifStates);
124 std::cout <<
"Error in preprocessing\n";
127 os <<
"#line " << lineNo <<
" \"" << filename <<
"\"\n";
128 }
else if (tokens[0] ==
"define") {
129 defineSet.insert(tokens[1]);
131 }
else if (tokens[0] ==
"undef") {
132 defineSet.erase(tokens[1]);
134 }
else if (tokens[0] ==
"ifdef") {
135 if (defineSet.find(tokens[1]) != defineSet.end() ) {
138 ifStates.push(
false);
141 }
else if (tokens[0] ==
"ifndef") {
142 if (defineSet.find(tokens[1]) == defineSet.end() ) {
145 ifStates.push(
false);
149 std::cout << tokens[0] <<
" is not supported (yet)." << std::endl;
156 }
else if (ifStates.top()){
157 os << buffer << std::endl;