7
#include "preprocessor.hpp"
8
#include "filesystem.hpp"
9
std::string preprocess(const std::string& input){
10
std::string output_string;
12
std::string::const_iterator i = input.begin();
14
while(i != input.end()){
15
//std::cerr << "test";
19
// process pre-processing directive here. See what comes after the '@' and do something appropriate
20
static const std::string IncludeString = "@include";
21
if(input.end() - i > IncludeString.size() && std::equal(IncludeString.begin(), IncludeString.end(), i)) {
22
std::string filename_string;
24
i += IncludeString.size(); //skip past the directive - we've tested that it exists
26
//test for an argument to @include - e.g. "filename.cfg". First the open quote:
27
std::string::const_iterator quote = std::find(i, input.end(), '"');
28
if(quote == input.end()) {
29
std::cerr << "we didn't find a opening quote. Syntax error." << std::endl;
31
if(std::count_if(i, quote, isspace) != quote - i) {
32
// # of whitespaces != number of intervening chars => something else was present. Syntax Error.
33
std::cerr << "# of whitespaces != number of intervening chars." << std::endl;
35
i = quote + 1; //we've found a quote, advance past it
36
//now the closing quote, and use it to find what's inbetween:
37
std::string::const_iterator endQuote = std::find(i, input.end(), '"');
38
if(endQuote == input.end()) {
39
std::cerr << "we didn't find a closing quote. Syntax error." << std::endl;
43
filename_string = std::string(i, endQuote);
48
output_string += preprocess(sys::read_file(filename_string));
51
//nothing special to process, just copy the chars across
52
output_string.push_back(*i);
63
#ifdef BUILD_PREPROCESSOR_TOOL
65
extern "C" int main(int argc, char** argv)
69
for(int i = 1; i < argc; ++i) {
70
std::ifstream file(argv[i], std::ios_base::binary);
73
std::cout << preprocess(ss.str());