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