preprocessor: quick fix to make the TestPreprocessor::test1 work

This commit is contained in:
Daniel Marjamäki 2008-10-26 14:11:21 +00:00
parent f792bf4a0d
commit 3f80b27c43
1 changed files with 37 additions and 0 deletions

View File

@ -19,8 +19,11 @@
#include "preprocessor.h"
#include <list>
#include <sstream>
static std::string getcode(const std::string &filedata, std::string cfg);
void preprocess(std::istream &istr, std::map<std::string, std::string> &result)
{
std::ostringstream ostr;
@ -28,6 +31,40 @@ void preprocess(std::istream &istr, std::map<std::string, std::string> &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<bool> 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();
}