SUMO - Simulation of Urban MObility
OptionsIO.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
18 // Helper for parsing command line arguments and reading configuration files
19 /****************************************************************************/
20 
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #ifdef _MSC_VER
26 #include <windows_config.h>
27 #else
28 #include <config.h>
29 #endif
30 
31 #include <string>
32 #include <iostream>
33 #include <cstdlib>
34 #include <cstring>
35 #include <xercesc/framework/XMLPScanToken.hpp>
36 #include <xercesc/parsers/SAXParser.hpp>
37 #include <xercesc/sax/HandlerBase.hpp>
38 #include <xercesc/sax/AttributeList.hpp>
39 #include <xercesc/util/PlatformUtils.hpp>
40 #include <xercesc/sax/SAXParseException.hpp>
41 #include <xercesc/sax/SAXException.hpp>
42 #include "OptionsIO.h"
43 #include "OptionsCont.h"
44 #include "OptionsLoader.h"
45 #include "OptionsParser.h"
49 
50 // ===========================================================================
51 // static member definitions
52 // ===========================================================================
53 int OptionsIO::myArgC = 0;
54 char** OptionsIO::myArgV;
55 
56 
57 // ===========================================================================
58 // method definitions
59 // ===========================================================================
60 void
61 OptionsIO::setArgs(int argc, char** argv) {
62  myArgC = argc;
63  myArgV = argv;
64 }
65 
66 
67 void
68 OptionsIO::setArgs(const std::vector<std::string>& args) {
69  char* const app = myArgC > 0 ? myArgV[0] : 0;
70  myArgC = (int)args.size() + 1;
71  char** argv = new char* [myArgC];
72  argv[0] = app;
73  for (int i = 1; i < myArgC; i++) {
74  argv[i] = new char[args[i - 1].size() + 1];
75  std::strcpy(argv[i], args[i - 1].c_str());
76  }
77  myArgV = argv;
78 }
79 
80 
81 void
82 OptionsIO::getOptions(const bool commandLineOnly) {
83  if (myArgC == 2 && myArgV[1][0] != '-') {
84  // special case only one parameter, check who can handle it
85  if (OptionsCont::getOptions().setByRootElement(getRoot(myArgV[1]), myArgV[1])) {
87  return;
88  }
89  }
90  // preparse the options
91  // (maybe another configuration file was chosen)
93  throw ProcessError("Could not parse commandline options.");
94  }
95  if (!commandLineOnly) {
96  // read the configuration when everything's ok
99  // reparse the options
100  // (overwrite the settings from the configuration file)
103  }
104 }
105 
106 
107 void
110  if (!oc.exists("configuration-file") || !oc.isSet("configuration-file")) {
111  return;
112  }
113  std::string path = oc.getString("configuration-file");
114  if (!FileHelpers::isReadable(path)) {
115  throw ProcessError("Could not access configuration '" + oc.getString("configuration-file") + "'.");
116  }
117  PROGRESS_BEGIN_MESSAGE("Loading configuration");
118  // build parser
119  XERCES_CPP_NAMESPACE::SAXParser parser;
120  parser.setValidationScheme(XERCES_CPP_NAMESPACE::SAXParser::Val_Auto);
121  parser.setDoNamespaces(false);
122  parser.setDoSchema(false);
123  // start the parsing
124  OptionsLoader handler;
125  try {
126  parser.setDocumentHandler(&handler);
127  parser.setErrorHandler(&handler);
128  parser.parse(path.c_str());
129  if (handler.errorOccured()) {
130  throw ProcessError("Could not load configuration '" + path + "'.");
131  }
132  } catch (const XERCES_CPP_NAMESPACE::XMLException& e) {
133  throw ProcessError("Could not load configuration '" + path + "':\n " + TplConvert::_2str(e.getMessage()));
134  }
135  oc.relocateFiles(path);
137 }
138 
139 
140 std::string
141 OptionsIO::getRoot(const std::string& filename) {
142  // build parser
143  XERCES_CPP_NAMESPACE::SAXParser parser;
144  // start the parsing
145  OptionsLoader handler;
146  try {
147  parser.setDocumentHandler(&handler);
148  parser.setErrorHandler(&handler);
149  XERCES_CPP_NAMESPACE::XMLPScanToken token;
150  if (!parser.parseFirst(filename.c_str(), token)) {
151  throw ProcessError("Can not read XML-file '" + filename + "'.");
152  }
153  while (parser.parseNext(token) && handler.getItem() == "");
154  if (handler.errorOccured()) {
155  throw ProcessError("Could not load '" + filename + "'.");
156  }
157  } catch (const XERCES_CPP_NAMESPACE::XMLException& e) {
158  throw ProcessError("Could not load '" + filename + "':\n " + TplConvert::_2str(e.getMessage()));
159  }
160  return handler.getItem();
161 }
162 
163 
164 /****************************************************************************/
165 
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:82
static bool isReadable(std::string path)
Checks whether the given file is readable.
Definition: FileHelpers.cpp:53
static char ** myArgV
Definition: OptionsIO.h:112
static void setArgs(int argc, char **argv)
Stores the command line arguments for later parsing.
Definition: OptionsIO.cpp:61
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:64
static bool parse(int argc, char **argv)
Parses the given command line arguments.
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
static void loadConfiguration()
Loads and parses the configuration.
Definition: OptionsIO.cpp:108
static int myArgC
Definition: OptionsIO.h:111
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
bool exists(const std::string &name) const
Returns the information whether the named option is known.
bool errorOccured() const
Returns the information whether an error occured.
#define PROGRESS_BEGIN_MESSAGE(msg)
Definition: MsgHandler.h:201
A SAX-Handler for loading options.
Definition: OptionsLoader.h:53
void relocateFiles(const std::string &configuration) const
Modifies file name options according to the configuration path.
const std::string & getItem() const
Returns the last item read.
A storage for options typed value containers)
Definition: OptionsCont.h:98
static std::string getRoot(const std::string &filename)
Retrieves the XML root element of a supposed configuration or net.
Definition: OptionsIO.cpp:141
#define PROGRESS_DONE_MESSAGE()
Definition: MsgHandler.h:202
static std::string _2str(const int var)
convert int to string
Definition: TplConvert.h:56