Eclipse 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-2020 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials are made available under the
5 // terms of the Eclipse Public License 2.0 which is available at
6 // https://www.eclipse.org/legal/epl-2.0/
7 // This Source Code may also be made available under the following Secondary
8 // Licenses when the conditions for such availability set forth in the Eclipse
9 // Public License 2.0 are satisfied: GNU General Public License, version 2
10 // or later which is available at
11 // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 /****************************************************************************/
19 // Helper for parsing command line arguments and reading configuration files
20 /****************************************************************************/
21 #include <config.h>
22 
23 #include <string>
24 #include <iostream>
25 #include <cstdlib>
26 #include <cstring>
27 #include <xercesc/framework/XMLPScanToken.hpp>
28 #include <xercesc/parsers/SAXParser.hpp>
29 #include <xercesc/sax/HandlerBase.hpp>
30 #include <xercesc/sax/AttributeList.hpp>
31 #include <xercesc/util/PlatformUtils.hpp>
32 #include <xercesc/sax/SAXParseException.hpp>
33 #include <xercesc/sax/SAXException.hpp>
34 #include "OptionsIO.h"
35 #include "OptionsCont.h"
36 #include "OptionsLoader.h"
37 #include "OptionsParser.h"
41 #ifdef HAVE_ZLIB
42 #include <foreign/zstr/zstr.hpp>
43 #endif
45 
46 
47 // ===========================================================================
48 // static member definitions
49 // ===========================================================================
50 int OptionsIO::myArgC = 0;
51 char** OptionsIO::myArgV;
52 
53 
54 // ===========================================================================
55 // method definitions
56 // ===========================================================================
57 void
58 OptionsIO::setArgs(int argc, char** argv) {
59  myArgC = argc;
60  myArgV = argv;
61 }
62 
63 
64 void
65 OptionsIO::setArgs(const std::vector<std::string>& args) {
66  char* const app = myArgC > 0 ? myArgV[0] : nullptr;
67  myArgC = (int)args.size() + 1;
68  char** argv = new char* [myArgC];
69  argv[0] = app;
70  for (int i = 1; i < myArgC; i++) {
71  argv[i] = new char[args[i - 1].size() + 1];
72  std::strcpy(argv[i], args[i - 1].c_str());
73  }
74  myArgV = argv;
75 }
76 
77 
78 void
79 OptionsIO::getOptions(const bool commandLineOnly) {
80  if (myArgC == 2 && myArgV[1][0] != '-') {
81  // special case only one parameter, check who can handle it
82  if (OptionsCont::getOptions().setByRootElement(getRoot(myArgV[1]), myArgV[1])) {
83  if (!commandLineOnly) {
85  }
86  return;
87  }
88  }
89  // preparse the options
90  // (maybe another configuration file was chosen)
92  throw ProcessError("Could not parse commandline options.");
93  }
94  if (!commandLineOnly || OptionsCont::getOptions().isSet("save-configuration", false)) {
95  // read the configuration when everything's ok
97  }
98 }
99 
100 
101 void
104  if (!oc.exists("configuration-file") || !oc.isSet("configuration-file")) {
105  return;
106  }
107  const std::string path = oc.getString("configuration-file");
108  if (!FileHelpers::isReadable(path)) {
109  throw ProcessError("Could not access configuration '" + oc.getString("configuration-file") + "'.");
110  }
111  PROGRESS_BEGIN_MESSAGE("Loading configuration");
112  oc.resetWritable();
113  // build parser
114  XERCES_CPP_NAMESPACE::SAXParser parser;
115  parser.setValidationScheme(XERCES_CPP_NAMESPACE::SAXParser::Val_Auto);
116  parser.setDoNamespaces(false);
117  parser.setDoSchema(false);
118  // start the parsing
119  OptionsLoader handler;
120  try {
121  parser.setDocumentHandler(&handler);
122  parser.setErrorHandler(&handler);
123  parser.parse(path.c_str());
124  if (handler.errorOccurred()) {
125  throw ProcessError("Could not load configuration '" + path + "'.");
126  }
127  } catch (const XERCES_CPP_NAMESPACE::XMLException& e) {
128  throw ProcessError("Could not load configuration '" + path + "':\n " + StringUtils::transcode(e.getMessage()));
129  }
130  oc.relocateFiles(path);
131  if (myArgC > 2) {
132  // reparse the options (overwrite the settings from the configuration file)
133  oc.resetWritable();
135  }
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 (!FileHelpers::isReadable(filename) || FileHelpers::isDirectory(filename)) {
151  throw ProcessError("Could not open '" + filename + "'.");
152  }
153 #ifdef HAVE_ZLIB
154  zstr::ifstream istream(filename.c_str(), std::fstream::in | std::fstream::binary);
155  IStreamInputSource inputStream(istream);
156  const bool result = parser.parseFirst(inputStream, token);
157 #else
158  const bool result = parser.parseFirst(filename.c_str(), token);
159 #endif
160  if (!result) {
161  throw ProcessError("Can not read XML-file '" + filename + "'.");
162  }
163  while (parser.parseNext(token) && handler.getItem() == "");
164  if (handler.errorOccurred()) {
165  throw ProcessError("Could not load '" + filename + "'.");
166  }
167  } catch (const XERCES_CPP_NAMESPACE::XMLException& e) {
168  throw ProcessError("Could not load '" + filename + "':\n " + StringUtils::transcode(e.getMessage()));
169  }
170  return handler.getItem();
171 }
172 
173 
174 /****************************************************************************/
#define PROGRESS_DONE_MESSAGE()
Definition: MsgHandler.h:280
#define PROGRESS_BEGIN_MESSAGE(msg)
Definition: MsgHandler.h:279
static bool isReadable(std::string path)
Checks whether the given file is readable.
Definition: FileHelpers.cpp:48
static bool isDirectory(std::string path)
Checks whether the given file is a directory.
Definition: FileHelpers.cpp:62
Xerces InputSource reading from arbitrary std::istream.
A storage for options typed value containers)
Definition: OptionsCont.h:89
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
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.
void resetWritable()
Resets all options to be writeable.
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:58
void relocateFiles(const std::string &configuration) const
Modifies file name options according to the configuration path.
static void loadConfiguration()
Loads and parses the configuration.
Definition: OptionsIO.cpp:102
static void setArgs(int argc, char **argv)
Stores the command line arguments for later parsing.
Definition: OptionsIO.cpp:58
static void getOptions(const bool commandLineOnly=false)
Parses the command line arguments and loads the configuration.
Definition: OptionsIO.cpp:79
static std::string getRoot(const std::string &filename)
Retrieves the XML root element of a supposed configuration or net.
Definition: OptionsIO.cpp:141
static int myArgC
Definition: OptionsIO.h:101
static char ** myArgV
Definition: OptionsIO.h:102
A SAX-Handler for loading options.
Definition: OptionsLoader.h:44
const std::string & getItem() const
Returns the last item read.
bool errorOccurred() const
Returns the information whether an error occurred.
static bool parse(int argc, char **argv)
Parses the given command line arguments.
static std::string transcode(const XMLCh *const data)
converts a 0-terminated XMLCh* array (usually UTF-16, stemming from Xerces) into std::string in UTF-8
Definition: StringUtils.h:133