Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "tools/schema/sourceparser.hpp"
00024
00025 #include "filesystem.hpp"
00026
00027 #include <iostream>
00028 #include <fstream>
00029 #include <string>
00030
00031 #include <string.h>
00032
00033
00034 std::string version = "0.6.0";
00035
00036 using namespace schema_validation;
00037
00038
00039
00040
00041
00042
00043
00044
00045 int main(int argc, char *argv[]){
00046 std::string input_dir = "";
00047 std::string output_file = "./data/gui/schema.cfg";
00048 std::string regex_file = "./utils/regex_list.txt";
00049 bool expand = false;
00050 for (int arg = 1; arg != argc; ++arg) {
00051 const std::string val(argv[arg]);
00052 if (val.empty()) {
00053 continue;
00054 }
00055 else if ((val == "--src-dir" || val == "-s") && arg+1 != argc) {
00056 input_dir = argv[++arg];
00057 }
00058 else if ((val == "--output" || val == "-o") && arg+1 != argc) {
00059 output_file = argv[++arg];
00060 }
00061 else if ((val == "--regex" || val == "-r") ) {
00062 if (arg+1 != argc){
00063 regex_file = argv[++arg];
00064 }
00065 std::fstream f;
00066 f.open(regex_file.c_str(),std::ios::out|std::ios::trunc);
00067 if (f.fail()){
00068 return 4;
00069 }
00070 test_regex(f);
00071 f.close();
00072 return 0;
00073 }
00074 else if (val == "--help" || val == "-h") {
00075 std::cout << "usage: " << argv[0]
00076 << " [-erhV] [-s <input_dir>] [-o <output_file>]\n"
00077 << " -r, --regex <regex_file>\t"
00078 << "List of used regexes.\n"
00079 << " -e, --expand \t"
00080 << "Expands all tags due to their super-tags."
00081 << "Useful to debug schema markup.\n"
00082 << " -h, --help\t\t\t"
00083 << "Shows this usage message.\n"
00084 << " -s, --src-dir <input_dir>\t"
00085 <<"Select the input directory.\n"
00086 << " -o, --output <output_file>\t"
00087 <<"Select the output file.\n"
00088 << " -V, --version\t\t\t"
00089 << "Version of tool\n";
00090 return 0;
00091 } else if (val == "--version" || val == "-V") {
00092 std::cout << "Battle for Wesnoth schema generator tool, version "
00093 << version << "\n";
00094 return 0;
00095 } else if (val == "--expand" || val == "-e") {
00096 expand = true;
00097 }
00098 }
00099 if(input_dir.empty()){
00100 std::cout << "No input was selected. Processing \"./src\"\n";
00101 input_dir = "./src";
00102 }
00103
00104 if (! file_exists(input_dir)){
00105 return 2;
00106 }
00107
00108 std::vector<std::string> files;
00109 std::vector<std::string> dirs;
00110
00111 if (is_directory(input_dir)){
00112 get_files_in_dir(input_dir, &files, &dirs, ENTIRE_FILE_PATH);
00113
00114 if (files.empty() && dirs.empty()){
00115 std::cout << "Some problem with input directory "
00116 << input_dir << "\n"
00117 << "No files found\n";
00118 return 2;
00119 }
00120
00121 while (!dirs.empty()){
00122 std::string temp_dir = dirs.back();
00123 dirs.pop_back();
00124 get_files_in_dir(temp_dir, &files, &dirs, ENTIRE_FILE_PATH);
00125 }
00126 }else{
00127 files.push_back(input_dir);
00128 }
00129 class_source_parser parser;
00130 parser.set_output(output_file);
00131 std::vector<std::string>::iterator i = files.begin();
00132 unsigned int counter = 0;
00133
00134 for (;i != files.end(); ++i){
00135 bool ok = false;
00136 if (file_name((*i)).find(".cpp")!=std::string::npos){
00137 ok = true;
00138 } else
00139 if (file_name((*i)).find(".hpp")!=std::string::npos){
00140 ok = true;
00141 } else
00142 if (file_name((*i)).find(".schema")!=std::string::npos){
00143 ok = true;
00144 }
00145 if (ok){
00146 ++counter;
00147 parser.set_input((*i));
00148 parser.parse_source();
00149 }
00150 }
00151 std::cout << "Processed " << counter << " files in " << input_dir << "\n";
00152
00153
00154 if (! parser.see_errors().is_empty()) {
00155
00156
00157
00158
00159 std::cout << "There are some errors\n";
00160 parser.see_errors().print_errors(std::cout);
00161
00162 char c;
00163 std::cout << "Continue with errors? Y/N ";
00164 while (true) {
00165 std::cin.get(c);
00166 const char *r = strchr("yYnN",c);
00167 if (r == NULL){
00168 std::cout << "Please, choose your answer " << std::endl;
00169 continue;
00170 }
00171 if (c == 'n' || c=='N'){
00172 std::cout << "I'll take that as a NO" << std::endl;
00173 return 1;
00174 }
00175 if (c == 'y' || c=='Y'){
00176 std::cout << "I'll take that as a Yes" << std::endl;
00177 break;
00178 }
00179 }
00180 }
00181 if (expand) {
00182 parser.expand();
00183 }
00184
00185 if ( ! parser.save_schema()){
00186 return 4;
00187 }
00188 std::cout << "Schema written to "<< output_file << "\n";
00189 return 0;
00190 }