fix cr/lfs and remove trailing whitespaces...
[supertux.git] / tools / miniswig / main.cpp
1 #include <config.h>
2
3 #include <iostream>
4 #include <fstream>
5 #include <vector>
6 #include <string>
7 #include "tree.hpp"
8 #include "globals.hpp"
9 #include "create_wrapper.hpp"
10 #include "create_docu.hpp"
11
12 extern int yyparse();
13 extern int yylex();
14
15 CompilationUnit* unit = 0;
16 std::istream* input = 0;
17 std::string inputfile;
18 std::string selected_namespace;
19 std::string modulename = "wrapper";
20
21 void usage()
22 {
23     std::cout << "Usage: miniswig --input FILE --output-cpp FILE --output-hpp FILE [--module NAME] [--select-namespace NAME]\n";
24     std::cout << "\n";
25 }
26
27 int main(int argc, char** argv)
28 {
29     std::string outputcpp;
30     std::string outputhpp;
31     std::string output_doc;
32     for(int i = 0; i < argc; ++i) {
33         if(strcmp(argv[i], "--module") == 0) {
34             if(i+1 >= argc) {
35                 std::cerr << "Need to specify a module name.\n";
36                 usage();
37                 return 1;
38             }
39             modulename = argv[++i];
40         } else if(strcmp(argv[i], "--input") == 0) {
41             if(i+1 >= argc) {
42                 std::cerr << "Need to specify input file name.\n";
43                 usage();
44                 return 1;
45             }
46             inputfile = argv[++i];
47         } else if(strcmp(argv[i], "--output-cpp") == 0) {
48             if(i+1 >= argc) {
49                 std::cerr << "Need to specifiy output cpp name.\n";
50                 usage();
51                 return 1;
52             }
53             outputcpp = argv[++i];
54         } else if(strcmp(argv[i], "--output-hpp") == 0) {
55             if(i+1 >= argc) {
56                 std::cerr << "Need to specify output hpp name.\n";
57                 usage();
58                 return 1;
59             }
60             outputhpp = argv[++i];
61         } else if(strcmp(argv[i], "--select-namespace") == 0) {
62             if(i+1 >= argc) {
63                 std::cerr << "Need to specify a namespace.\n";
64                 usage();
65                 return 1;
66             }
67             selected_namespace = argv[++i];
68         } else if(strcmp(argv[i], "--output-doc") == 0) {
69           if(i+1 >= argc) {
70             std::cerr << "Need to specify document xml file.\n";
71             usage();
72             return 1;
73           }
74           output_doc = argv[++i];
75         } else if(argv[i][0] == '-') {
76             std::cerr << "Unknown option '" << argv[i] << "'.\n";
77             usage();
78             return 1;
79         } else {
80         }
81     }
82     if( inputfile == "" || (
83             (outputcpp == "" || outputhpp == "") && output_doc == "")) {
84         std::cerr << "Not all options specified.\n";
85         usage();
86         return 1;
87     }
88
89     try {
90         input = new std::ifstream(inputfile.c_str());
91         if(!input->good()) {
92             std::cerr << "Couldn't open file '" << input << "' for reading.\n";
93             return 1;
94         }
95         current_file = inputfile;
96         unit = new CompilationUnit();
97         Namespace* std_namespace = new Namespace();
98         std_namespace->name = "std";
99         std_namespace->types.push_back(new StringType());
100         unit->namespaces.push_back(std_namespace);
101         unit->types.push_back(new HSQUIRRELVMType());
102         unit->types.push_back(new SQIntegerType());
103
104         yyparse();
105
106         Namespace* ns = unit;
107         if(selected_namespace != "") {
108             ns = ns->findNamespace(selected_namespace);
109         }
110
111         if(outputcpp != "") {
112             std::ofstream cppout(outputcpp.c_str());
113             if(!cppout.good()) {
114                 std::cerr << "Couldn't open file '"
115                           << outputcpp << "' for writing.\n";
116                 return 1;
117             }
118             std::ofstream hppout(outputhpp.c_str());
119             if(!hppout.good()) {
120                 std::cerr << "Couldn't open file '" << outputhpp
121                           << "' for writing.\n";
122                 return 1;
123             }
124
125             WrapperCreator creator(cppout, hppout);
126             creator.create_wrapper(ns);
127         }
128
129         if(output_doc != "") {
130             std::ofstream dout(output_doc.c_str());
131             if(!dout.good()) {
132                 std::cerr << "Couldn't open file '"
133                     << output_doc << "' for writing.\n";
134                 return 1;
135             }
136             DocuCreator creator(dout);
137             creator.create_docu(ns);
138         }
139     } catch(std::exception& e) {
140         std::cerr << "Exception: " << e.what() << "\n";
141         return 1;
142     }
143
144     return 0;
145 }