SUMO - Simulation of Urban MObility
|
00001 /****************************************************************************/ 00008 // Helper for parsing command line arguments and reading configuration files 00009 /****************************************************************************/ 00010 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/ 00011 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors 00012 /****************************************************************************/ 00013 // 00014 // This file is part of SUMO. 00015 // SUMO is free software: you can redistribute it and/or modify 00016 // it under the terms of the GNU General Public License as published by 00017 // the Free Software Foundation, either version 3 of the License, or 00018 // (at your option) any later version. 00019 // 00020 /****************************************************************************/ 00021 00022 00023 // =========================================================================== 00024 // included modules 00025 // =========================================================================== 00026 #ifdef _MSC_VER 00027 #include <windows_config.h> 00028 #else 00029 #include <config.h> 00030 #endif 00031 00032 #include <string> 00033 #include <iostream> 00034 #include <xercesc/parsers/SAXParser.hpp> 00035 #include <xercesc/sax/HandlerBase.hpp> 00036 #include <xercesc/sax/AttributeList.hpp> 00037 #include <xercesc/util/PlatformUtils.hpp> 00038 #include <xercesc/sax/SAXParseException.hpp> 00039 #include <xercesc/sax/SAXException.hpp> 00040 #include <cstdlib> 00041 #include "OptionsIO.h" 00042 #include "OptionsCont.h" 00043 #include "OptionsLoader.h" 00044 #include "OptionsParser.h" 00045 #include <utils/common/FileHelpers.h> 00046 #include <utils/common/MsgHandler.h> 00047 #include <utils/common/TplConvert.h> 00048 00049 #ifdef CHECK_MEMORY_LEAKS 00050 #include <foreign/nvwa/debug_new.h> 00051 #endif // CHECK_MEMORY_LEAKS 00052 00053 // =========================================================================== 00054 // static member definitions 00055 // =========================================================================== 00056 int OptionsIO::myArgC; 00057 char** OptionsIO::myArgV; 00058 00059 00060 // =========================================================================== 00061 // method definitions 00062 // =========================================================================== 00063 void 00064 OptionsIO::getOptions(bool loadConfig, int argc, char** argv) { 00065 // preparse the options 00066 // (maybe another configuration file was chosen) 00067 if (argc > 0) { 00068 myArgC = argc; 00069 myArgV = argv; 00070 } 00071 if (!OptionsParser::parse(myArgC, myArgV)) { 00072 throw ProcessError("Could not parse commandline options."); 00073 } 00074 // check whether to use the command line parameters only 00075 if (!loadConfig) { 00076 return; 00077 } 00078 // read the configuration when everything's ok 00079 OptionsCont::getOptions().resetWritable(); 00080 loadConfiguration(); 00081 // reparse the options 00082 // (overwrite the settings from the configuration file) 00083 OptionsCont::getOptions().resetWritable(); 00084 OptionsParser::parse(myArgC, myArgV); 00085 } 00086 00087 00088 void 00089 OptionsIO::loadConfiguration() { 00090 OptionsCont& oc = OptionsCont::getOptions(); 00091 if (!oc.exists("configuration-file") || !oc.isSet("configuration-file")) { 00092 return; 00093 } 00094 std::string path = oc.getString("configuration-file"); 00095 if (!FileHelpers::exists(path)) { 00096 throw ProcessError("Could not find configuration '" + oc.getString("configuration-file") + "'."); 00097 } 00098 PROGRESS_BEGIN_MESSAGE("Loading configuration"); 00099 // build parser 00100 SAXParser parser; 00101 parser.setValidationScheme(SAXParser::Val_Auto); 00102 parser.setDoNamespaces(false); 00103 parser.setDoSchema(false); 00104 // start the parsing 00105 OptionsLoader handler; 00106 try { 00107 parser.setDocumentHandler(&handler); 00108 parser.setErrorHandler(&handler); 00109 parser.parse(path.c_str()); 00110 if (handler.errorOccured()) { 00111 throw ProcessError("Could not load configuration '" + path + "'."); 00112 } 00113 } catch (const XMLException& e) { 00114 throw ProcessError("Could not load configuration '" + path + "':\n " + TplConvert<XMLCh>::_2str(e.getMessage())); 00115 } 00116 oc.relocateFiles(path); 00117 PROGRESS_DONE_MESSAGE(); 00118 } 00119 00120 00121 00122 /****************************************************************************/ 00123