~ubuntu-branches/ubuntu/precise/frogatto/precise

« back to all changes in this revision

Viewing changes to src/preprocessor.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Dmitry E. Oboukhov
  • Date: 2010-07-21 16:21:45 UTC
  • Revision ID: james.westby@ubuntu.com-20100721162145-zid0u93fm3xz73gh
Tags: upstream-1.0+dfsg1
ImportĀ upstreamĀ versionĀ 1.0+dfsg1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <algorithm>
 
2
#include <iostream>
 
3
#include <fstream>
 
4
#include <sstream>
 
5
#include <string>
 
6
 
 
7
#include "preprocessor.hpp"
 
8
#include "filesystem.hpp"
 
9
std::string preprocess(const std::string& input){
 
10
        std::string output_string;
 
11
 
 
12
        std::string::const_iterator i = input.begin();
 
13
        
 
14
        while(i != input.end()){
 
15
                //std::cerr << "test";
 
16
                if(*i == '@'){
 
17
 
 
18
 
 
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;
 
23
 
 
24
                                        i += IncludeString.size(); //skip past the directive - we've tested that it exists
 
25
                                        
 
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;
 
30
                                        }
 
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;
 
34
                                        }
 
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;
 
40
                                        }
 
41
                                        
 
42
                                        
 
43
                                        filename_string = std::string(i, endQuote);
 
44
                                        
 
45
                                        i = endQuote + 1;
 
46
                                        
 
47
                                                                                                                        
 
48
                                        output_string += preprocess(sys::read_file(filename_string));
 
49
                        }
 
50
                } else {
 
51
                        //nothing special to process, just copy the chars across
 
52
                        output_string.push_back(*i);
 
53
                }
 
54
                ++i;
 
55
        }
 
56
 
 
57
        return output_string;
 
58
}
 
59
 
 
60
 
 
61
 
 
62
 
 
63
#ifdef BUILD_PREPROCESSOR_TOOL
 
64
 
 
65
extern "C" int main(int argc, char** argv)
 
66
{
 
67
 
 
68
 
 
69
        for(int i = 1; i < argc; ++i) {
 
70
                std::ifstream file(argv[i], std::ios_base::binary);
 
71
                std::stringstream ss;
 
72
                ss << file.rdbuf();
 
73
                std::cout << preprocess(ss.str());
 
74
        }
 
75
 
 
76
}
 
77
#endif