From b1250e17507045e876f7cfa4d7e7a2242aee6c02 Mon Sep 17 00:00:00 2001 From: Reijo Tomperi Date: Sun, 18 Jan 2009 17:13:05 +0000 Subject: [PATCH] Some work for the include support (commented out for now) --- src/preprocessor.cpp | 49 ++++++++++++++++++++++++++++++++++++++++++++ src/preprocessor.h | 14 +++++++++++++ 2 files changed, 63 insertions(+) diff --git a/src/preprocessor.cpp b/src/preprocessor.cpp index 570572006..208ec86d7 100644 --- a/src/preprocessor.cpp +++ b/src/preprocessor.cpp @@ -257,6 +257,9 @@ void Preprocessor::preprocess(std::istream &istr, std::string &processedFile, st processedFile = replaceIfDefined(processedFile); +// TODO, uncomment below when handling of includes is implemented well enough +// processedFile = handleIncludes(processedFile); + // Get all possible configurations.. resultConfigurations = getcfgs(processedFile); } @@ -443,8 +446,54 @@ std::string Preprocessor::getcode(const std::string &filedata, std::string cfg) return expandMacros(ret.str()); } +std::string Preprocessor::getHeaderFileName(const std::string &str) +{ + std::string result; + std::string::size_type i = str.find("\""); + if (i == std::string::npos) + return result; + for (i = i + 1; i < str.length(); ++i) + { + if (str[i] == '"') + break; + result.append(1, str[i]); + } + + return result; +} + +std::string Preprocessor::handleIncludes(std::string code) +{ + std::istringstream istr(code); + std::string line; + std::string::size_type pos = 0; + while ((pos = code.find("#include", pos)) != std::string::npos) + { + // Accept only includes that are at the start of a line + // TODO, multi line strings can contain includes that break this + if (pos > 0 && code[pos-1] != '\n') + continue; + + std::string::size_type end = code.find("\n", pos); + std::string filename = code.substr(pos, end - pos); + + // Remove #include clause + code.erase(pos, end - pos); + + filename = getHeaderFileName(filename); + if (filename.length() == 0) + continue; + + // filename contains now a file name e.g. "menu.h" + + // TODO, add the file here, except do not mess up with the line numbers. + // So me must tokenize, before we can append. + } + + return code; +} class Macro { diff --git a/src/preprocessor.h b/src/preprocessor.h index 8de2495d0..47491ace6 100644 --- a/src/preprocessor.h +++ b/src/preprocessor.h @@ -90,6 +90,20 @@ private: static bool match_cfg_def(std::string cfg, const std::string &def); static std::string expandMacros(std::string code); + + /** + * Search includes from code and append code from the included + * file + */ + static std::string handleIncludes(std::string code); + + /** + * Returns the string between double quote characters. + * @param str e.g. '#include "menu.h"' + * @return e.g. 'menu.h' or empty string if double quotes were + * not found. + */ + static std::string getHeaderFileName(const std::string &str); }; //---------------------------------------------------------------------------