tools/schema/schema_generator.cpp

Go to the documentation of this file.
00001 /* $Id: schema_generator.cpp 52533 2012-01-07 02:35:17Z shadowmaster $ */
00002 /*
00003   Copyright (C) 2011 - 2012 by Sytyi Nick <nsytyi@gmail.com>
00004   Part of the Battle for Wesnoth Project http://www.wesnoth.org/
00005 
00006   This program is free software; you can redistribute it and/or modify
00007   it under the terms of the GNU General Public License as published by
00008   the Free Software Foundation; either version 2 of the License, or
00009   (at your option) any later version.
00010   This program is distributed in the hope that it will be useful,
00011   but WITHOUT ANY WARRANTY.
00012 
00013   See the COPYING file for more details.
00014 */
00015 
00016 /**
00017  * @file
00018  * This file parses the input parameters, prepares a list of files to be parsed
00019  * and calls parser for each of them.
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  * Parses the command line.
00039  * @retval 0 Everything's OK!
00040  * @retval 1 Errors found. User decided to exit.
00041  * @retval 2 No input files found. Please, check your input directory.
00042  * @retval 3 Output file for schema cannot be created.
00043  * @retval 4 Output file for regex list cannot be created.
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         /** Getting full list of files recursively.*/
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     // check errors
00153 
00154     if (! parser.see_errors().is_empty()) {
00155         /**
00156      * Let the user decide whether error are great or just misprints.
00157      * If any error found, waits for a Yy or Nn to continue or not.
00158      */
00159         std::cout << "There are some errors\n";
00160         parser.see_errors().print_errors(std::cout);
00161         // Take user response
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     // save schema information
00185     if ( ! parser.save_schema()){
00186         return 4;
00187     }
00188     std::cout << "Schema written to "<< output_file << "\n";
00189     return 0;
00190 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Fri May 25 2012 01:03:12 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs