SUMO - Simulation of Urban MObility
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-2017 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 <cstdlib>
35 #include <cstring>
36 #include <xercesc/framework/XMLPScanToken.hpp>
37 #include <xercesc/parsers/SAXParser.hpp>
38 #include <xercesc/sax/HandlerBase.hpp>
39 #include <xercesc/sax/AttributeList.hpp>
40 #include <xercesc/util/PlatformUtils.hpp>
41 #include <xercesc/sax/SAXParseException.hpp>
42 #include <xercesc/sax/SAXException.hpp>
43 #include "OptionsIO.h"
44 #include "OptionsCont.h"
45 #include "OptionsLoader.h"
46 #include "OptionsParser.h"
50 
51 // ===========================================================================
52 // static member definitions
53 // ===========================================================================
55 char** OptionsIO::myArgV;
56 
57 
58 // ===========================================================================
59 // method definitions
60 // ===========================================================================
61 void
62 OptionsIO::setArgs(int argc, char** argv) {
63  myArgC = argc;
64  myArgV = argv;
65 }
66 
67 
68 void
69 OptionsIO::setArgs(const std::vector<std::string>& args) {
70  myArgC = (int)args.size() + 1;
71  char** argv = new char* [myArgC];
72  argv[0] = myArgV[0];
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:54
static char ** myArgV
Definition: OptionsIO.h:113
static void setArgs(int argc, char **argv)
Stores the command line arguments for later parsing.
Definition: OptionsIO.cpp:62
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:65
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:112
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: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.
const std::string & getItem() const
Returns the last item read.
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:141
#define PROGRESS_DONE_MESSAGE()
Definition: MsgHandler.h:203
static std::string _2str(const int var)
convert int to string
Definition: TplConvert.h:57