SUMO - Simulation of Urban MObility
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.dlr.de/
11 // Copyright (C) 2001-2015 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::setArgs(int argc, char** argv) {
65  myArgC = argc;
66  myArgV = argv;
67 }
68 
69 
70 void
72  // preparse the options
73  // (maybe another configuration file was chosen)
75  throw ProcessError("Could not parse commandline options.");
76  }
77  // read the configuration when everything's ok
80  // reparse the options
81  // (overwrite the settings from the configuration file)
84 }
85 
86 
87 void
90  if (!oc.exists("configuration-file") || !oc.isSet("configuration-file")) {
91  return;
92  }
93  std::string path = oc.getString("configuration-file");
94  if (!FileHelpers::isReadable(path)) {
95  throw ProcessError("Could not access configuration '" + oc.getString("configuration-file") + "'.");
96  }
97  PROGRESS_BEGIN_MESSAGE("Loading configuration");
98  // build parser
99  XERCES_CPP_NAMESPACE::SAXParser parser;
100  parser.setValidationScheme(XERCES_CPP_NAMESPACE::SAXParser::Val_Auto);
101  parser.setDoNamespaces(false);
102  parser.setDoSchema(false);
103  // start the parsing
104  OptionsLoader handler;
105  try {
106  parser.setDocumentHandler(&handler);
107  parser.setErrorHandler(&handler);
108  parser.parse(path.c_str());
109  if (handler.errorOccured()) {
110  throw ProcessError("Could not load configuration '" + path + "'.");
111  }
112  } catch (const XERCES_CPP_NAMESPACE::XMLException& e) {
113  throw ProcessError("Could not load configuration '" + path + "':\n " + TplConvert::_2str(e.getMessage()));
114  }
115  oc.relocateFiles(path);
117 }
118 
119 
120 
121 /****************************************************************************/
122 
void resetWritable()
Resets all options to be writeable.
static bool isReadable(std::string path)
Checks whether the given file is readable.
Definition: FileHelpers.cpp:58
static char ** myArgV
Definition: OptionsIO.h:89
static void setArgs(int argc, char **argv)
Stores the command line arguments for later parsing.
Definition: OptionsIO.cpp:64
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:69
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:88
static std::string _2str(const E *const data)
Definition: TplConvert.h:56
static int myArgC
Definition: OptionsIO.h:88
bool errorOccured() const
Returns the information whether an error occured.
#define PROGRESS_BEGIN_MESSAGE(msg)
Definition: MsgHandler.h:202
A SAX-Handler for loading options.
Definition: OptionsLoader.h:54
static void getOptions()
Parses the command line arguments and loads the configuration.
Definition: OptionsIO.cpp:71
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:203
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.