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