From 3f80b27c435f967ab1d7626380ea61cbf9bcab71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 26 Oct 2008 14:11:21 +0000 Subject: [PATCH] preprocessor: quick fix to make the TestPreprocessor::test1 work --- preprocessor.cpp | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/preprocessor.cpp b/preprocessor.cpp index 5df013a6a..e74b1366b 100644 --- a/preprocessor.cpp +++ b/preprocessor.cpp @@ -19,8 +19,11 @@ #include "preprocessor.h" +#include #include +static std::string getcode(const std::string &filedata, std::string cfg); + void preprocess(std::istream &istr, std::map &result) { std::ostringstream ostr; @@ -28,6 +31,40 @@ void preprocess(std::istream &istr, std::map &result) while ( getline(istr, line) ) ostr << line << "\n"; + result.clear(); + result[""] = getcode( ostr.str(), "" ); + result["WIN32"] = getcode( ostr.str(), "WIN32" ); } +static std::string getcode(const std::string &filedata, std::string cfg) +{ + std::ostringstream ret; + + std::list matching_ifdef; + + std::istringstream istr(filedata); + std::string line; + while ( getline(istr, line) ) + { + if ( line.find("#ifdef ") == 0 ) + matching_ifdef.push_back( !cfg.empty() && line.find(cfg) != std::string::npos ); + + else if ( line.find("#else") == 0) + matching_ifdef.back() = ! matching_ifdef.back(); + + else if ( line.find("#endif") == 0 ) + matching_ifdef.pop_back(); + + if ( !matching_ifdef.empty() && !matching_ifdef.back() ) + line = ""; + + if ( line.find("#") == 0 ) + line = ""; + + ret << line << "\n"; + } + + return ret.str(); +} +