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-2019 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 /****************************************************************************/
15 // Helper for parsing command line arguments and reading configuration files
16 /****************************************************************************/
17 
18 
19 // ===========================================================================
20 // included modules
21 // ===========================================================================
22 #include <config.h>
23 
24 #include <string>
25 #include <iostream>
26 #include <cstdlib>
27 #include <cstring>
28 #include <xercesc/framework/XMLPScanToken.hpp>
29 #include <xercesc/parsers/SAXParser.hpp>
30 #include <xercesc/sax/HandlerBase.hpp>
31 #include <xercesc/sax/AttributeList.hpp>
32 #include <xercesc/util/PlatformUtils.hpp>
33 #include <xercesc/sax/SAXParseException.hpp>
34 #include <xercesc/sax/SAXException.hpp>
35 #include "OptionsIO.h"
36 #include "OptionsCont.h"
37 #include "OptionsLoader.h"
38 #include "OptionsParser.h"
42 
43 // ===========================================================================
44 // static member definitions
45 // ===========================================================================
46 int OptionsIO::myArgC = 0;
47 char** OptionsIO::myArgV;
48 
49 
50 // ===========================================================================
51 // method definitions
52 // ===========================================================================
53 void
54 OptionsIO::setArgs(int argc, char** argv) {
55  myArgC = argc;
56  myArgV = argv;
57 }
58 
59 
60 void
61 OptionsIO::setArgs(const std::vector<std::string>& args) {
62  char* const app = myArgC > 0 ? myArgV[0] : nullptr;
63  myArgC = (int)args.size() + 1;
64  char** argv = new char* [myArgC];
65  argv[0] = app;
66  for (int i = 1; i < myArgC; i++) {
67  argv[i] = new char[args[i - 1].size() + 1];
68  std::strcpy(argv[i], args[i - 1].c_str());
69  }
70  myArgV = argv;
71 }
72 
73 
74 void
75 OptionsIO::getOptions(const bool commandLineOnly) {
76  if (myArgC == 2 && myArgV[1][0] != '-') {
77  // special case only one parameter, check who can handle it
78  if (OptionsCont::getOptions().setByRootElement(getRoot(myArgV[1]), myArgV[1])) {
80  return;
81  }
82  }
83  // preparse the options
84  // (maybe another configuration file was chosen)
86  throw ProcessError("Could not parse commandline options.");
87  }
88  if (!commandLineOnly) {
89  // read the configuration when everything's ok
92  // reparse the options
93  // (overwrite the settings from the configuration file)
96  }
97 }
98 
99 
100 void
103  if (!oc.exists("configuration-file") || !oc.isSet("configuration-file")) {
104  return;
105  }
106  std::string path = oc.getString("configuration-file");
107  if (!FileHelpers::isReadable(path)) {
108  throw ProcessError("Could not access configuration '" + oc.getString("configuration-file") + "'.");
109  }
110  PROGRESS_BEGIN_MESSAGE("Loading configuration");
111  // build parser
112  XERCES_CPP_NAMESPACE::SAXParser parser;
113  parser.setValidationScheme(XERCES_CPP_NAMESPACE::SAXParser::Val_Auto);
114  parser.setDoNamespaces(false);
115  parser.setDoSchema(false);
116  // start the parsing
117  OptionsLoader handler;
118  try {
119  parser.setDocumentHandler(&handler);
120  parser.setErrorHandler(&handler);
121  parser.parse(path.c_str());
122  if (handler.errorOccurred()) {
123  throw ProcessError("Could not load configuration '" + path + "'.");
124  }
125  } catch (const XERCES_CPP_NAMESPACE::XMLException& e) {
126  throw ProcessError("Could not load configuration '" + path + "':\n " + StringUtils::transcode(e.getMessage()));
127  }
128  oc.relocateFiles(path);
130 }
131 
132 
133 std::string
134 OptionsIO::getRoot(const std::string& filename) {
135  // build parser
136  XERCES_CPP_NAMESPACE::SAXParser parser;
137  // start the parsing
138  OptionsLoader handler;
139  try {
140  parser.setDocumentHandler(&handler);
141  parser.setErrorHandler(&handler);
142  XERCES_CPP_NAMESPACE::XMLPScanToken token;
143  if (!parser.parseFirst(filename.c_str(), token)) {
144  throw ProcessError("Can not read XML-file '" + filename + "'.");
145  }
146  while (parser.parseNext(token) && handler.getItem() == "");
147  if (handler.errorOccurred()) {
148  throw ProcessError("Could not load '" + filename + "'.");
149  }
150  } catch (const XERCES_CPP_NAMESPACE::XMLException& e) {
151  throw ProcessError("Could not load '" + filename + "':\n " + StringUtils::transcode(e.getMessage()));
152  }
153  return handler.getItem();
154 }
155 
156 
157 /****************************************************************************/
158 
OptionsCont::isSet
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
Definition: OptionsCont.cpp:135
OptionsCont::relocateFiles
void relocateFiles(const std::string &configuration) const
Modifies file name options according to the configuration path.
Definition: OptionsCont.cpp:335
OptionsCont.h
OptionsCont::resetWritable
void resetWritable()
Resets all options to be writeable.
Definition: OptionsCont.cpp:441
MsgHandler.h
OptionsCont::getString
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
Definition: OptionsCont.cpp:201
FileHelpers.h
OptionsCont::exists
bool exists(const std::string &name) const
Returns the information whether the named option is known.
Definition: OptionsCont.cpp:129
OptionsCont::getOptions
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:57
OptionsParser::parse
static bool parse(int argc, char **argv)
Parses the given command line arguments.
Definition: OptionsParser.cpp:38
OptionsLoader.h
OptionsParser.h
OptionsLoader::errorOccurred
bool errorOccurred() const
Returns the information whether an error occurred.
Definition: OptionsLoader.cpp:153
OptionsLoader::getItem
const std::string & getItem() const
Returns the last item read.
Definition: OptionsLoader.h:120
ProcessError
Definition: UtilExceptions.h:39
OptionsCont
A storage for options typed value containers)
Definition: OptionsCont.h:89
OptionsIO::myArgV
static char ** myArgV
Definition: OptionsIO.h:105
OptionsIO::myArgC
static int myArgC
Definition: OptionsIO.h:104
OptionsIO::getOptions
static void getOptions(const bool commandLineOnly=false)
Parses the command line arguments and loads the configuration.
Definition: OptionsIO.cpp:75
StringUtils.h
StringUtils::transcode
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:136
OptionsIO::loadConfiguration
static void loadConfiguration()
Loads and parses the configuration.
Definition: OptionsIO.cpp:101
PROGRESS_BEGIN_MESSAGE
#define PROGRESS_BEGIN_MESSAGE(msg)
Definition: MsgHandler.h:278
OptionsLoader
A SAX-Handler for loading options.
Definition: OptionsLoader.h:46
PROGRESS_DONE_MESSAGE
#define PROGRESS_DONE_MESSAGE()
Definition: MsgHandler.h:279
OptionsIO::setArgs
static void setArgs(int argc, char **argv)
Stores the command line arguments for later parsing.
Definition: OptionsIO.cpp:54
OptionsIO::getRoot
static std::string getRoot(const std::string &filename)
Retrieves the XML root element of a supposed configuration or net.
Definition: OptionsIO.cpp:134
FileHelpers::isReadable
static bool isReadable(std::string path)
Checks whether the given file is readable.
Definition: FileHelpers.cpp:49
config.h
OptionsIO.h