SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
OptionsIO.cpp
Go to the documentation of this file.
1 /****************************************************************************/
8 // Helper for parsing command line arguments and reading configuration files
9 /****************************************************************************/
10 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
11 // Copyright (C) 2001-2013 DLR (http://www.dlr.de/) and contributors
12 /****************************************************************************/
13 //
14 // This file is part of SUMO.
15 // SUMO is free software: you can redistribute it and/or modify
16 // it under the terms of the GNU General Public License as published by
17 // the Free Software Foundation, either version 3 of the License, or
18 // (at your option) any later version.
19 //
20 /****************************************************************************/
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #ifdef _MSC_VER
27 #include <windows_config.h>
28 #else
29 #include <config.h>
30 #endif
31 
32 #include <string>
33 #include <iostream>
34 #include <xercesc/parsers/SAXParser.hpp>
35 #include <xercesc/sax/HandlerBase.hpp>
36 #include <xercesc/sax/AttributeList.hpp>
37 #include <xercesc/util/PlatformUtils.hpp>
38 #include <xercesc/sax/SAXParseException.hpp>
39 #include <xercesc/sax/SAXException.hpp>
40 #include <cstdlib>
41 #include "OptionsIO.h"
42 #include "OptionsCont.h"
43 #include "OptionsLoader.h"
44 #include "OptionsParser.h"
48 
49 #ifdef CHECK_MEMORY_LEAKS
50 #include <foreign/nvwa/debug_new.h>
51 #endif // CHECK_MEMORY_LEAKS
52 
53 // ===========================================================================
54 // static member definitions
55 // ===========================================================================
57 char** OptionsIO::myArgV;
58 
59 
60 // ===========================================================================
61 // method definitions
62 // ===========================================================================
63 void
64 OptionsIO::getOptions(bool loadConfig, int argc, char** argv) {
65  // preparse the options
66  // (maybe another configuration file was chosen)
67  if (argc > 0) {
68  myArgC = argc;
69  myArgV = argv;
70  }
72  throw ProcessError("Could not parse commandline options.");
73  }
74  // check whether to use the command line parameters only
75  if (!loadConfig) {
76  return;
77  }
78  // read the configuration when everything's ok
81  // reparse the options
82  // (overwrite the settings from the configuration file)
85 }
86 
87 
88 void
91  if (!oc.exists("configuration-file") || !oc.isSet("configuration-file")) {
92  return;
93  }
94  std::string path = oc.getString("configuration-file");
95  if (!FileHelpers::exists(path)) {
96  throw ProcessError("Could not find configuration '" + oc.getString("configuration-file") + "'.");
97  }
98  PROGRESS_BEGIN_MESSAGE("Loading configuration");
99  // build parser
100  XERCES_CPP_NAMESPACE::SAXParser parser;
101  parser.setValidationScheme(XERCES_CPP_NAMESPACE::SAXParser::Val_Auto);
102  parser.setDoNamespaces(false);
103  parser.setDoSchema(false);
104  // start the parsing
105  OptionsLoader handler;
106  try {
107  parser.setDocumentHandler(&handler);
108  parser.setErrorHandler(&handler);
109  parser.parse(path.c_str());
110  if (handler.errorOccured()) {
111  throw ProcessError("Could not load configuration '" + path + "'.");
112  }
113  } catch (const XERCES_CPP_NAMESPACE::XMLException& e) {
114  throw ProcessError("Could not load configuration '" + path + "':\n " + TplConvert::_2str(e.getMessage()));
115  }
116  oc.relocateFiles(path);
118 }
119 
120 
121 
122 /****************************************************************************/
123 
void resetWritable()
Resets all options to be writeable.
static void getOptions(bool loadConfig, int argc=0, char **argv=0)
Parses the command line arguments and loads the configuration optionally.
Definition: OptionsIO.cpp:64
static char ** myArgV
Definition: OptionsIO.h:87
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:67
static bool parse(int argc, char **argv)
Parses the given command line arguments.
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
static void loadConfiguration()
Loads and parses the configuration.
Definition: OptionsIO.cpp:89
static std::string _2str(const E *const data)
Definition: TplConvert.h:56
static int myArgC
Definition: OptionsIO.h:86
static bool exists(std::string path)
Checks whether the given file exists.
Definition: FileHelpers.cpp:57
bool errorOccured() const
Returns the information whether an error occured.
#define PROGRESS_BEGIN_MESSAGE(msg)
Definition: MsgHandler.h:198
A SAX-Handler for loading options.
Definition: OptionsLoader.h:54
void relocateFiles(const std::string &configuration) const
Modifies file name options according to the configuration path.
A storage for options typed value containers)
Definition: OptionsCont.h:108
#define PROGRESS_DONE_MESSAGE()
Definition: MsgHandler.h:199
bool exists(const std::string &name) const
Returns the information whether the named option is known.
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.