--- trunk/src/mdParser/SimplePreprocessor.hpp 2005/12/02 15:38:03 770 +++ trunk/src/mdParser/SimplePreprocessor.hpp 2006/08/30 18:42:29 1024 @@ -44,32 +44,51 @@ #include #include #include +#include #include "utils/StringTokenizer.hpp" #include "utils/Trim.hpp" +#include "utils/OOPSEException.hpp" +#include "utils/simError.h" + + /** * @class SimplePreprocessor * @brief A simple preprocessor. - * @note only support #include #ifdef, #ifndef, #endif, #define and #undef, c-like multiple line - * comment is not support, macro substitude is not support. + * @note only supports #include #ifdef, #ifndef, #endif, #define and #undef, c-like multiple line + * comment is not supported, macro substitute is not supported. */ namespace oopse { class SimplePreprocessor { public: - bool preprocess(const std::string& filename, ostream& os) { + bool preprocess(std::istream& myStream, const std::string& filename, int startingLine, ostream& os) { std::set defineSet; std::stack ifStates; ifStates.push(true); - return doPreprocess(filename, os, defineSet, ifStates); + return doPreprocess(myStream, filename, startingLine, os, defineSet, ifStates); } private: - bool doPreprocess(const std::string& filename, ostream& os, std::set& defineSet, std::stack& ifStates) { - std::ifstream input(filename.c_str()); - - int lineNo =1; + bool doPreprocess(std::istream& myStream, const std::string& filename, int startingLine, ostream& os, std::set& defineSet, std::stack& ifStates) { + //std::ifstream input(filename.c_str()); + //if (!input.is_open()) { + // std::stringstream ss; + // ss << "Can not open " << filename << " for preprocessing\n"; + // + // sprintf(painCave.errMsg, + // "Can not open (%s) for processing. \n" + // "\tPlease check md file name syntax.\n", filename.c_str()); + // + // painCave.isFatal = 1; + // simError(); + // + // throw OOPSEException(ss.str()); + //} + int lineNo = startingLine; os << "#line " << lineNo << " \"" << filename << "\"\n"; - while(input.getline(buffer, bufferSize)) { + const int bufferSize = 1024; + char buffer[bufferSize]; + while(myStream.getline(buffer, bufferSize)) { ++lineNo; std::string line = trimLeftCopy(buffer); if (!line.empty() && line[0] == '#') { @@ -91,7 +110,14 @@ class SimplePreprocessor { SimplePreprocessor subPreprocessor; std::string includeFilename = tokens[1]; includeFilename = includeFilename.substr(1, includeFilename.length() -2); - bool ret = subPreprocessor.doPreprocess(includeFilename, os, defineSet, ifStates); + std::ifstream includeStream(includeFilename.c_str()); + if (!includeStream.is_open()) { + std::stringstream ss; + ss << "Can not open " << filename << " for preprocessing\n"; + throw OOPSEException(ss.str()); + } + + bool ret = subPreprocessor.doPreprocess(includeStream, includeFilename, 1, os, defineSet, ifStates); if (!ret) { std::cout << "Error in preprocessing\n"; return false; @@ -118,7 +144,7 @@ class SimplePreprocessor { } os << std::endl; } else { - std::cout << tokens[0] << " is not support" << std::endl; + std::cout << tokens[0] << " is not supported (yet)." << std::endl; return false; } }else { @@ -135,8 +161,6 @@ class SimplePreprocessor { } private: - const static int bufferSize = 1024; - char buffer[bufferSize]; }; }