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