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-2018 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
16 // Helper for parsing command line arguments and reading configuration files
17 /****************************************************************************/
18 
19 
20 // ===========================================================================
21 // included modules
22 // ===========================================================================
23 #include <config.h>
24 
25 #include <string>
26 #include <iostream>
27 #include <cstdlib>
28 #include <cstring>
29 #include <xercesc/framework/XMLPScanToken.hpp>
30 #include <xercesc/parsers/SAXParser.hpp>
31 #include <xercesc/sax/HandlerBase.hpp>
32 #include <xercesc/sax/AttributeList.hpp>
33 #include <xercesc/util/PlatformUtils.hpp>
34 #include <xercesc/sax/SAXParseException.hpp>
35 #include <xercesc/sax/SAXException.hpp>
36 #include "OptionsIO.h"
37 #include "OptionsCont.h"
38 #include "OptionsLoader.h"
39 #include "OptionsParser.h"
43 
44 // ===========================================================================
45 // static member definitions
46 // ===========================================================================
47 int OptionsIO::myArgC = 0;
48 char** OptionsIO::myArgV;
49 
50 
51 // ===========================================================================
52 // method definitions
53 // ===========================================================================
54 void
55 OptionsIO::setArgs(int argc, char** argv) {
56  myArgC = argc;
57  myArgV = argv;
58 }
59 
60 
61 void
62 OptionsIO::setArgs(const std::vector<std::string>& args) {
63  char* const app = myArgC > 0 ? myArgV[0] : nullptr;
64  myArgC = (int)args.size() + 1;
65  char** argv = new char* [myArgC];
66  argv[0] = app;
67  for (int i = 1; i < myArgC; i++) {
68  argv[i] = new char[args[i - 1].size() + 1];
69  std::strcpy(argv[i], args[i - 1].c_str());
70  }
71  myArgV = argv;
72 }
73 
74 
75 void
76 OptionsIO::getOptions(const bool commandLineOnly) {
77  if (myArgC == 2 && myArgV[1][0] != '-') {
78  // special case only one parameter, check who can handle it
79  if (OptionsCont::getOptions().setByRootElement(getRoot(myArgV[1]), myArgV[1])) {
81  return;
82  }
83  }
84  // preparse the options
85  // (maybe another configuration file was chosen)
87  throw ProcessError("Could not parse commandline options.");
88  }
89  if (!commandLineOnly) {
90  // read the configuration when everything's ok
93  // reparse the options
94  // (overwrite the settings from the configuration file)
97  }
98 }
99 
100 
101 void
104  if (!oc.exists("configuration-file") || !oc.isSet("configuration-file")) {
105  return;
106  }
107  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  // build parser
113  XERCES_CPP_NAMESPACE::SAXParser parser;
114  parser.setValidationScheme(XERCES_CPP_NAMESPACE::SAXParser::Val_Auto);
115  parser.setDoNamespaces(false);
116  parser.setDoSchema(false);
117  // start the parsing
118  OptionsLoader handler;
119  try {
120  parser.setDocumentHandler(&handler);
121  parser.setErrorHandler(&handler);
122  parser.parse(path.c_str());
123  if (handler.errorOccurred()) {
124  throw ProcessError("Could not load configuration '" + path + "'.");
125  }
126  } catch (const XERCES_CPP_NAMESPACE::XMLException& e) {
127  throw ProcessError("Could not load configuration '" + path + "':\n " + StringUtils::transcode(e.getMessage()));
128  }
129  oc.relocateFiles(path);
131 }
132 
133 
134 std::string
135 OptionsIO::getRoot(const std::string& filename) {
136  // build parser
137  XERCES_CPP_NAMESPACE::SAXParser parser;
138  // start the parsing
139  OptionsLoader handler;
140  try {
141  parser.setDocumentHandler(&handler);
142  parser.setErrorHandler(&handler);
143  XERCES_CPP_NAMESPACE::XMLPScanToken token;
144  if (!parser.parseFirst(filename.c_str(), token)) {
145  throw ProcessError("Can not read XML-file '" + filename + "'.");
146  }
147  while (parser.parseNext(token) && handler.getItem() == "");
148  if (handler.errorOccurred()) {
149  throw ProcessError("Could not load '" + filename + "'.");
150  }
151  } catch (const XERCES_CPP_NAMESPACE::XMLException& e) {
152  throw ProcessError("Could not load '" + filename + "':\n " + StringUtils::transcode(e.getMessage()));
153  }
154  return handler.getItem();
155 }
156 
157 
158 /****************************************************************************/
159 
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:76
static bool isReadable(std::string path)
Checks whether the given file is readable.
Definition: FileHelpers.cpp:47
static char ** myArgV
Definition: OptionsIO.h:106
static void setArgs(int argc, char **argv)
Stores the command line arguments for later parsing.
Definition: OptionsIO.cpp:55
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:58
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
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:102
static int myArgC
Definition: OptionsIO.h:105
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.
#define PROGRESS_BEGIN_MESSAGE(msg)
Definition: MsgHandler.h:243
A SAX-Handler for loading options.
Definition: OptionsLoader.h:47
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:92
bool errorOccurred() const
Returns the information whether an error occurred.
static std::string getRoot(const std::string &filename)
Retrieves the XML root element of a supposed configuration or net.
Definition: OptionsIO.cpp:135
#define PROGRESS_DONE_MESSAGE()
Definition: MsgHandler.h:244