SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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.dlr.de/
11 // Copyright (C) 2001-2016 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/framework/XMLPScanToken.hpp>
35 #include <xercesc/parsers/SAXParser.hpp>
36 #include <xercesc/sax/HandlerBase.hpp>
37 #include <xercesc/sax/AttributeList.hpp>
38 #include <xercesc/util/PlatformUtils.hpp>
39 #include <xercesc/sax/SAXParseException.hpp>
40 #include <xercesc/sax/SAXException.hpp>
41 #include <cstdlib>
42 #include "OptionsIO.h"
43 #include "OptionsCont.h"
44 #include "OptionsLoader.h"
45 #include "OptionsParser.h"
49 
50 #ifdef CHECK_MEMORY_LEAKS
51 #include <foreign/nvwa/debug_new.h>
52 #endif // CHECK_MEMORY_LEAKS
53 
54 // ===========================================================================
55 // static member definitions
56 // ===========================================================================
58 char** OptionsIO::myArgV;
59 
60 
61 // ===========================================================================
62 // method definitions
63 // ===========================================================================
64 void
65 OptionsIO::setArgs(int argc, char** argv) {
66  myArgC = argc;
67  myArgV = argv;
68 }
69 
70 
71 void
72 OptionsIO::getOptions(const bool commandLineOnly) {
73  if (myArgC == 2 && myArgV[1][0] != '-') {
74  // special case only one parameter, check who can handle it
75  if (OptionsCont::getOptions().setByRootElement(getRoot(myArgV[1]), myArgV[1])) {
77  return;
78  }
79  }
80  // preparse the options
81  // (maybe another configuration file was chosen)
83  throw ProcessError("Could not parse commandline options.");
84  }
85  if (!commandLineOnly) {
86  // read the configuration when everything's ok
89  // reparse the options
90  // (overwrite the settings from the configuration file)
93  }
94 }
95 
96 
97 void
100  if (!oc.exists("configuration-file") || !oc.isSet("configuration-file")) {
101  return;
102  }
103  std::string path = oc.getString("configuration-file");
104  if (!FileHelpers::isReadable(path)) {
105  throw ProcessError("Could not access configuration '" + oc.getString("configuration-file") + "'.");
106  }
107  PROGRESS_BEGIN_MESSAGE("Loading configuration");
108  // build parser
109  XERCES_CPP_NAMESPACE::SAXParser parser;
110  parser.setValidationScheme(XERCES_CPP_NAMESPACE::SAXParser::Val_Auto);
111  parser.setDoNamespaces(false);
112  parser.setDoSchema(false);
113  // start the parsing
114  OptionsLoader handler;
115  try {
116  parser.setDocumentHandler(&handler);
117  parser.setErrorHandler(&handler);
118  parser.parse(path.c_str());
119  if (handler.errorOccured()) {
120  throw ProcessError("Could not load configuration '" + path + "'.");
121  }
122  } catch (const XERCES_CPP_NAMESPACE::XMLException& e) {
123  throw ProcessError("Could not load configuration '" + path + "':\n " + TplConvert::_2str(e.getMessage()));
124  }
125  oc.relocateFiles(path);
127 }
128 
129 
130 std::string
131 OptionsIO::getRoot(const std::string& filename) {
132  // build parser
133  XERCES_CPP_NAMESPACE::SAXParser parser;
134  // start the parsing
135  OptionsLoader handler;
136  try {
137  parser.setDocumentHandler(&handler);
138  parser.setErrorHandler(&handler);
139  XERCES_CPP_NAMESPACE::XMLPScanToken token;
140  if (!parser.parseFirst(filename.c_str(), token)) {
141  throw ProcessError("Can not read XML-file '" + filename + "'.");
142  }
143  while (parser.parseNext(token) && handler.getItem() == "");
144  if (handler.errorOccured()) {
145  throw ProcessError("Could not load '" + filename + "'.");
146  }
147  } catch (const XERCES_CPP_NAMESPACE::XMLException& e) {
148  throw ProcessError("Could not load '" + filename + "':\n " + TplConvert::_2str(e.getMessage()));
149  }
150  return handler.getItem();
151 }
152 
153 
154 /****************************************************************************/
155 
void resetWritable()
Resets all options to be writeable.
static void getOptions(const bool commandLineOnly=false)
Parses the command line arguments and loads the configuration.
Definition: OptionsIO.cpp:72
static bool isReadable(std::string path)
Checks whether the given file is readable.
Definition: FileHelpers.cpp:58
const std::string & getItem() const
Returns the last item read.
static char ** myArgV
Definition: OptionsIO.h:99
static void setArgs(int argc, char **argv)
Stores the command line arguments for later parsing.
Definition: OptionsIO.cpp:65
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:98
static int myArgC
Definition: OptionsIO.h:98
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
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:99
static std::string getRoot(const std::string &filename)
Retrieves the XML root element of a supposed configuration or net.
Definition: OptionsIO.cpp:131
#define PROGRESS_DONE_MESSAGE()
Definition: MsgHandler.h:203
static std::string _2str(const int var)
convert int to string
Definition: TplConvert.h:57
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.