SUMO - Simulation of Urban MObility
OptionsCont.cpp
Go to the documentation of this file.
1 /****************************************************************************/
10 // A storage for options (typed value containers)
11 /****************************************************************************/
12 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
13 // Copyright (C) 2001-2016 DLR (http://www.dlr.de/) and contributors
14 /****************************************************************************/
15 //
16 // This file is part of SUMO.
17 // SUMO is free software: you can redistribute it and/or modify
18 // it under the terms of the GNU General Public License as published by
19 // the Free Software Foundation, either version 3 of the License, or
20 // (at your option) any later version.
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 <map>
33 #include <string>
34 #include <exception>
35 #include <algorithm>
36 #include <vector>
37 #include <iostream>
38 #include <cstdlib>
39 #include <cassert>
40 #include <ctime>
41 #include <cstring>
42 #include <cerrno>
43 #include <iterator>
44 #include "Option.h"
45 #include "OptionsCont.h"
52 #include <sstream>
53 
54 #ifdef CHECK_MEMORY_LEAKS
55 #include <foreign/nvwa/debug_new.h>
56 #endif // CHECK_MEMORY_LEAKS
57 
58 
59 // ===========================================================================
60 // static member definitions
61 // ===========================================================================
63 
64 
65 // ===========================================================================
66 // method definitions
67 // ===========================================================================
70  return myOptions;
71 }
72 
73 
76  myCopyrightNotices.push_back("Copyright (C) 2001-2016 DLR and contributors; http://sumo.dlr.de");
77 }
78 
79 
81  clear();
82 }
83 
84 
85 void
86 OptionsCont::doRegister(const std::string& name, Option* v) {
87  assert(v != 0);
88  ItemAddressContType::iterator i = find(myAddresses.begin(), myAddresses.end(), v);
89  if (i == myAddresses.end()) {
90  myAddresses.push_back(v);
91  }
92  if (myValues.find(name) != myValues.end()) {
93  throw ProcessError(name + " is an already used option name.");
94  }
95  myValues[name] = v;
96 }
97 
98 
99 void
100 OptionsCont::doRegister(const std::string& name1, char abbr, Option* v) {
101  doRegister(name1, v);
102  doRegister(convertChar(abbr), v);
103 }
104 
105 
106 void
107 OptionsCont::addSynonyme(const std::string& name1, const std::string& name2, bool isDeprecated) {
108  KnownContType::iterator i1 = myValues.find(name1);
109  KnownContType::iterator i2 = myValues.find(name2);
110  if (i1 == myValues.end() && i2 == myValues.end()) {
111  throw ProcessError("Neither the option '" + name1 + "' nor the option '" + name2 + "' is known yet");
112  }
113  if (i1 != myValues.end() && i2 != myValues.end()) {
114  if ((*i1).second == (*i2).second) {
115  return;
116  }
117  throw ProcessError("Both options '" + name1 + "' and '" + name2 + "' do exist and differ.");
118  }
119  if (i1 == myValues.end() && i2 != myValues.end()) {
120  doRegister(name1, (*i2).second);
121  if (isDeprecated) {
122  myDeprecatedSynonymes[name1] = false;
123  }
124  }
125  if (i1 != myValues.end() && i2 == myValues.end()) {
126  doRegister(name2, (*i1).second);
127  if (isDeprecated) {
128  myDeprecatedSynonymes[name2] = false;
129  }
130  }
131 }
132 
133 
134 void
135 OptionsCont::addXMLDefault(const std::string& name, const std::string& xmlRoot) {
136  myXMLDefaults[xmlRoot] = name;
137 }
138 
139 
140 bool
141 OptionsCont::exists(const std::string& name) const {
142  return myValues.count(name) > 0;
143 }
144 
145 
146 bool
147 OptionsCont::isSet(const std::string& name, bool failOnNonExistant) const {
148  KnownContType::const_iterator i = myValues.find(name);
149  if (i == myValues.end()) {
150  if (failOnNonExistant) {
151  throw ProcessError("Internal request for unknown option '" + name + "'!");
152  } else {
153  return false;
154  }
155  }
156  return (*i).second->isSet();
157 }
158 
159 
160 void
161 OptionsCont::unSet(const std::string& name, bool failOnNonExistant) const {
162  KnownContType::const_iterator i = myValues.find(name);
163  if (i == myValues.end()) {
164  if (failOnNonExistant) {
165  throw ProcessError("Internal request for unknown option '" + name + "'!");
166  } else {
167  return;
168  }
169  }
170  (*i).second->unSet();
171 }
172 
173 
174 bool
175 OptionsCont::isDefault(const std::string& name) const {
176  KnownContType::const_iterator i = myValues.find(name);
177  if (i == myValues.end()) {
178  return false;
179  }
180  return (*i).second->isDefault();
181 }
182 
183 
184 Option*
185 OptionsCont::getSecure(const std::string& name) const {
186  KnownContType::const_iterator k = myValues.find(name);
187  if (k == myValues.end()) {
188  throw ProcessError("No option with the name '" + name + "' exists.");
189  }
190  std::map<std::string, bool>::iterator s = myDeprecatedSynonymes.find(name);
191  if (s != myDeprecatedSynonymes.end() && !s->second) {
192  std::string defaultName;
193  for (std::map<std::string, std::vector<std::string> >::const_iterator i = mySubTopicEntries.begin(); i != mySubTopicEntries.end(); ++i) {
194  for (std::vector<std::string>::const_iterator j = i->second.begin(); j != i->second.end(); ++j) {
195  KnownContType::const_iterator l = myValues.find(*j);
196  if (l != myValues.end() && l->second == k->second) {
197  defaultName = *j;
198  break;
199  }
200  }
201  if (defaultName != "") {
202  break;
203  }
204  }
205  WRITE_WARNING("Please note that '" + name + "' is deprecated.\n Use '" + defaultName + "' instead.");
206  s->second = true;
207  }
208  return k->second;
209 }
210 
211 
212 std::string
213 OptionsCont::getString(const std::string& name) const {
214  Option* o = getSecure(name);
215  return o->getString();
216 }
217 
218 
219 SUMOReal
220 OptionsCont::getFloat(const std::string& name) const {
221  Option* o = getSecure(name);
222  return o->getFloat();
223 }
224 
225 
226 int
227 OptionsCont::getInt(const std::string& name) const {
228  Option* o = getSecure(name);
229  return o->getInt();
230 }
231 
232 
233 bool
234 OptionsCont::getBool(const std::string& name) const {
235  Option* o = getSecure(name);
236  return o->getBool();
237 }
238 
239 
240 const IntVector&
241 OptionsCont::getIntVector(const std::string& name) const {
242  Option* o = getSecure(name);
243  return o->getIntVector();
244 }
245 
246 
247 bool
248 OptionsCont::set(const std::string& name, const std::string& value) {
249  Option* o = getSecure(name);
250  if (!o->isWriteable()) {
251  reportDoubleSetting(name);
252  return false;
253  }
254  try {
255  if (!o->set(value)) {
256  return false;
257  }
258  } catch (ProcessError& e) {
259  WRITE_ERROR("While processing option '" + name + "':\n " + e.what());
260  return false;
261  }
262  return true;
263 }
264 
265 
266 bool
267 OptionsCont::setDefault(const std::string& name, const std::string& value) {
268  if (set(name, value)) {
269  getSecure(name)->resetDefault();
270  return true;
271  }
272  return false;
273 }
274 
275 
276 bool
277 OptionsCont::setByRootElement(const std::string& root, const std::string& value) {
278  if (myXMLDefaults.count(root) > 0) {
279  return set(myXMLDefaults[root], value);
280  }
281  if (myXMLDefaults.count("") > 0) {
282  return set(myXMLDefaults[""], value);
283  }
284  return false;
285 }
286 
287 
288 std::vector<std::string>
289 OptionsCont::getSynonymes(const std::string& name) const {
290  Option* o = getSecure(name);
291  std::vector<std::string> v(0);
292  for (KnownContType::const_iterator i = myValues.begin(); i != myValues.end(); i++) {
293  if ((*i).second == o && name != (*i).first) {
294  v.push_back((*i).first);
295  }
296  }
297  return v;
298 }
299 
300 
301 std::ostream&
302 operator<<(std::ostream& os, const OptionsCont& oc) {
303  std::vector<std::string> done;
304  os << "Options set:" << std::endl;
305  for (OptionsCont::KnownContType::const_iterator i = oc.myValues.begin();
306  i != oc.myValues.end(); i++) {
307  std::vector<std::string>::iterator j = find(done.begin(), done.end(), (*i).first);
308  if (j == done.end()) {
309  std::vector<std::string> synonymes = oc.getSynonymes((*i).first);
310  if (synonymes.size() != 0) {
311  os << (*i).first << " (";
312  for (j = synonymes.begin(); j != synonymes.end(); j++) {
313  if (j != synonymes.begin()) {
314  os << ", ";
315  }
316  os << (*j);
317  }
318  os << ")";
319  } else {
320  os << (*i).first;
321  }
322  if ((*i).second->isSet()) {
323  os << ": " << (*i).second->getValueString() << std::endl;
324  } else {
325  os << ": <INVALID>" << std::endl;
326  }
327  done.push_back((*i).first);
328  copy(synonymes.begin(), synonymes.end(), back_inserter(done));
329  }
330  }
331  return os;
332 }
333 
334 
335 void
336 OptionsCont::relocateFiles(const std::string& configuration) const {
337  for (ItemAddressContType::const_iterator i = myAddresses.begin(); i != myAddresses.end(); i++) {
338  if ((*i)->isFileName() && (*i)->isSet()) {
339  StringTokenizer st((*i)->getString(), ";, ", true);
340  std::string conv;
341  while (st.hasNext()) {
342  if (conv.length() != 0) {
343  conv += ',';
344  }
345  std::string tmp = st.next();
346  if (!FileHelpers::isAbsolute(tmp)) {
347  tmp = FileHelpers::getConfigurationRelative(configuration, tmp);
348  }
349  conv += StringUtils::urlDecode(tmp);
350  }
351  if (conv != (*i)->getString()) {
352  const bool hadDefault = (*i)->isDefault();
353  (*i)->set(conv);
354  if (hadDefault) {
355  (*i)->resetDefault();
356  }
357  }
358  }
359  }
360 }
361 
362 
363 bool
364 OptionsCont::isUsableFileList(const std::string& name) const {
365  Option* o = getSecure(name);
366  // check whether the option is set
367  // return false i not
368  if (!o->isSet()) {
369  return false;
370  }
371  // check whether the list of files is valid
372  bool ok = true;
373  std::vector<std::string> files = getStringVector(name);
374  if (files.size() == 0) {
375  WRITE_ERROR("The file list for '" + name + "' is empty.");
376  ok = false;
377  }
378  for (std::vector<std::string>::const_iterator fileIt = files.begin(); fileIt != files.end(); ++fileIt) {
379  if (!FileHelpers::isReadable(*fileIt)) {
380  if (*fileIt != "") {
381  WRITE_ERROR("File '" + *fileIt + "' is not accessible (" + std::strerror(errno) + ").");
382  ok = false;
383  } else {
384  WRITE_WARNING("Empty file name given; ignoring.");
385  }
386  }
387  }
388  return ok;
389 }
390 
391 
392 bool
393 OptionsCont::checkDependingSuboptions(const std::string& name, const std::string& prefix) const {
394  Option* o = getSecure(name);
395  if (o->isSet()) {
396  return true;
397  }
398  bool ok = true;
399  std::vector<std::string> seenSynonymes;
400  for (KnownContType::const_iterator i = myValues.begin(); i != myValues.end(); i++) {
401  if (std::find(seenSynonymes.begin(), seenSynonymes.end(), (*i).first) != seenSynonymes.end()) {
402  continue;
403  }
404  if ((*i).second->isSet() && !(*i).second->isDefault() && (*i).first.find(prefix) == 0) {
405  WRITE_ERROR("Option '" + (*i).first + "' needs option '" + name + "'.");
406  std::vector<std::string> synonymes = getSynonymes((*i).first);
407  std::copy(synonymes.begin(), synonymes.end(), std::back_inserter(seenSynonymes));
408  ok = false;
409  }
410  }
411  return ok;
412 }
413 
414 
415 void
416 OptionsCont::reportDoubleSetting(const std::string& arg) const {
417  std::vector<std::string> synonymes = getSynonymes(arg);
418  std::ostringstream s;
419  s << "A value for the option '" + arg + "' was already set.\n Possible synonymes: ";
420  for (std::vector<std::string>::iterator i = synonymes.begin(); i != synonymes.end();) {
421  s << (*i);
422  i++;
423  if (i != synonymes.end()) {
424  s << ", ";
425  }
426  }
427  WRITE_ERROR(s.str());
428 }
429 
430 
431 std::string
432 OptionsCont::convertChar(char abbr) const {
433  char buf[2];
434  buf[0] = abbr;
435  buf[1] = 0;
436  std::string s(buf);
437  return s;
438 }
439 
440 
441 bool
442 OptionsCont::isBool(const std::string& name) const {
443  Option* o = getSecure(name);
444  return o->isBool();
445 }
446 
447 
448 void
450  for (ItemAddressContType::iterator i = myAddresses.begin(); i != myAddresses.end(); i++) {
451  (*i)->resetWritable();
452  }
453 }
454 
455 
456 bool
457 OptionsCont::isWriteable(const std::string& name) {
458  Option* o = getSecure(name);
459  return o->isWriteable();
460 }
461 
462 
463 void
465  ItemAddressContType::iterator i;
466  for (i = myAddresses.begin(); i != myAddresses.end(); i++) {
467  delete(*i);
468  }
469  myAddresses.clear();
470  myValues.clear();
471  mySubTopics.clear();
472  mySubTopicEntries.clear();
473 }
474 
475 
476 void
477 OptionsCont::addDescription(const std::string& name,
478  const std::string& subtopic,
479  const std::string& description) {
480  Option* o = getSecure(name);
481  assert(o != 0);
482  assert(find(mySubTopics.begin(), mySubTopics.end(), subtopic) != mySubTopics.end());
483  o->setDescription(description);
484  mySubTopicEntries[subtopic].push_back(name);
485 }
486 
487 
488 void
489 OptionsCont::setApplicationName(const std::string& appName,
490  const std::string& fullName) {
491  myAppName = appName;
492  myFullName = fullName;
493 }
494 
495 
496 void
497 OptionsCont::setApplicationDescription(const std::string& appDesc) {
498  myAppDescription = appDesc;
499 }
500 
501 
502 void
503 OptionsCont::addCallExample(const std::string& example, const std::string& desc) {
504  myCallExamples.push_back(std::make_pair(example, desc));
505 }
506 
507 
508 void
509 OptionsCont::setAdditionalHelpMessage(const std::string& add) {
510  myAdditionalMessage = add;
511 }
512 
513 
514 void
515 OptionsCont::addCopyrightNotice(const std::string& copyrightLine) {
516  myCopyrightNotices.push_back(copyrightLine);
517 }
518 
519 
520 void
522  myCopyrightNotices.clear();
523 }
524 
525 
526 void
527 OptionsCont::addOptionSubTopic(const std::string& topic) {
528  mySubTopics.push_back(topic);
529  mySubTopicEntries[topic] = std::vector<std::string>();
530 }
531 
532 
533 void
534 OptionsCont::splitLines(std::ostream& os, std::string what,
535  size_t offset, size_t nextOffset) {
536  while (what.length() > 0) {
537  if (what.length() > 79 - offset) {
538  size_t splitPos = what.rfind(';', 79 - offset);
539  if (splitPos == std::string::npos) {
540  splitPos = what.rfind(' ', 79 - offset);
541  } else {
542  splitPos++;
543  }
544  if (splitPos != std::string::npos) {
545  os << what.substr(0, splitPos) << std::endl;
546  what = what.substr(splitPos);
547  for (size_t r = 0; r < nextOffset + 1; ++r) {
548  os << ' ';
549  }
550  } else {
551  os << what;
552  what = "";
553  }
554  offset = nextOffset;
555  } else {
556  os << what;
557  what = "";
558  }
559  }
560  os << std::endl;
561 }
562 
563 
564 bool
565 OptionsCont::processMetaOptions(bool missingOptions) {
566  if (missingOptions) {
567  // no options are given
568  std::cout << myFullName << std::endl;
569  std::cout << " Build features: " << HAVE_ENABLED << std::endl;
570  for (std::vector<std::string>::const_iterator it =
571  myCopyrightNotices.begin(); it != myCopyrightNotices.end(); ++it) {
572  std::cout << " " << *it << std::endl;
573  }
574  std::cout << " License GPLv3+: GNU GPL Version 3 or later <http://gnu.org/licenses/gpl.html>\n";
575  std::cout << " Use --help to get the list of options." << std::endl;
576  return true;
577  }
578 
579  // check whether the help shall be printed
580  if (getBool("help")) {
581  std::cout << myFullName << std::endl;
582  for (std::vector<std::string>::const_iterator it =
583  myCopyrightNotices.begin(); it != myCopyrightNotices.end(); ++it) {
584  std::cout << " " << *it << std::endl;
585  }
586  printHelp(std::cout);
587  return true;
588  }
589  // check whether the help shall be printed
590  if (getBool("version")) {
591  std::cout << myFullName << std::endl;
592  std::cout << " Build features: " << HAVE_ENABLED << std::endl;
593  for (std::vector<std::string>::const_iterator it =
594  myCopyrightNotices.begin(); it != myCopyrightNotices.end(); ++it) {
595  std::cout << " " << *it << std::endl;
596  }
597  std::cout << "\n" << myFullName << " is part of SUMO.\n";
598  std::cout << "SUMO is free software: you can redistribute it and/or modify\n";
599  std::cout << "it under the terms of the GNU General Public License as published by\n";
600  std::cout << "the Free Software Foundation, either version 3 of the License, or\n";
601  std::cout << "(at your option) any later version.\n\n";
602  std::cout << "This program is distributed in the hope that it will be useful,\n";
603  std::cout << "but WITHOUT ANY WARRANTY; without even the implied warranty of\n";
604  std::cout << "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n";
605  std::cout << "GNU General Public License for more details.\n\n";
606  std::cout << "You should have received a copy of the GNU General Public License\n";
607  std::cout << "along with this program. If not, see http://www.gnu.org/licenses/gpl.html" << std::endl;
608  return true;
609  }
610  // check whether the settings shall be printed
611  if (exists("print-options") && getBool("print-options")) {
612  std::cout << (*this);
613  }
614  // check whether something has to be done with options
615  // whether the current options shall be saved
616  if (isSet("save-configuration", false)) { // sumo-gui does not register these
617  if (getString("save-configuration") == "-" || getString("save-configuration") == "stdout") {
618  writeConfiguration(std::cout, true, false, getBool("save-commented"));
619  return true;
620  }
621  std::ofstream out(getString("save-configuration").c_str());
622  if (!out.good()) {
623  throw ProcessError("Could not save configuration to '" + getString("save-configuration") + "'");
624  } else {
625  writeConfiguration(out, true, false, getBool("save-commented"));
626  if (getBool("verbose")) {
627  WRITE_MESSAGE("Written configuration to '" + getString("save-configuration") + "'");
628  }
629  return true;
630  }
631  }
632  // whether the template shall be saved
633  if (isSet("save-template", false)) { // sumo-gui does not register these
634  if (getString("save-template") == "-" || getString("save-template") == "stdout") {
635  writeConfiguration(std::cout, false, true, getBool("save-commented"));
636  return true;
637  }
638  std::ofstream out(getString("save-template").c_str());
639  if (!out.good()) {
640  throw ProcessError("Could not save template to '" + getString("save-template") + "'");
641  } else {
642  writeConfiguration(out, false, true, getBool("save-commented"));
643  if (getBool("verbose")) {
644  WRITE_MESSAGE("Written template to '" + getString("save-template") + "'");
645  }
646  return true;
647  }
648  }
649  if (isSet("save-schema", false)) { // sumo-gui does not register these
650  if (getString("save-schema") == "-" || getString("save-schema") == "stdout") {
651  writeSchema(std::cout, getBool("save-commented"));
652  return true;
653  }
654  std::ofstream out(getString("save-schema").c_str());
655  if (!out.good()) {
656  throw ProcessError("Could not save schema to '" + getString("save-schema") + "'");
657  } else {
658  writeSchema(out, getBool("save-commented"));
659  if (getBool("verbose")) {
660  WRITE_MESSAGE("Written schema to '" + getString("save-schema") + "'");
661  }
662  return true;
663  }
664  }
665  return false;
666 }
667 
668 void
669 OptionsCont::printHelp(std::ostream& os) {
670  std::vector<std::string>::const_iterator i, j;
671  // print application description
672  splitLines(os, myAppDescription, 0, 0);
673  os << std::endl;
674  // print usage BNF
675  os << "Usage: " << myAppName << " [OPTION]*" << std::endl;
676  // print additional text if any
677  if (myAdditionalMessage.length() > 0) {
678  os << myAdditionalMessage << std::endl << ' ' << std::endl;
679  }
680  // print the options
681  // check their sizes first
682  // we want to know how large the largest not-too-large-entry will be
683  size_t tooLarge = 40;
684  size_t maxSize = 0;
685  for (i = mySubTopics.begin(); i != mySubTopics.end(); ++i) {
686  const std::vector<std::string>& entries = mySubTopicEntries[*i];
687  for (j = entries.begin(); j != entries.end(); ++j) {
688  Option* o = getSecure(*j);
689  // name, two leading spaces and "--"
690  size_t csize = (*j).length() + 2 + 4;
691  // abbreviation length ("-X, "->4chars) if any
692  std::vector<std::string> synonymes = getSynonymes(*j);
693  if (find_if(synonymes.begin(), synonymes.end(), abbreviation_finder()) != synonymes.end()) {
694  csize += 4;
695  }
696  // the type name
697  if (!o->isBool()) {
698  csize += 1 + o->getTypeName().length();
699  }
700  // divider
701  csize += 2;
702  if (csize < tooLarge && maxSize < csize) {
703  maxSize = csize;
704  }
705  }
706  }
707 
708  for (i = mySubTopics.begin(); i != mySubTopics.end(); ++i) {
709  os << *i << " Options:" << std::endl;
710  const std::vector<std::string>& entries = mySubTopicEntries[*i];
711  for (j = entries.begin(); j != entries.end(); ++j) {
712  // start length computation
713  size_t csize = (*j).length() + 2;
714  Option* o = getSecure(*j);
715  os << " ";
716  // write abbreviation if given
717  std::vector<std::string> synonymes = getSynonymes(*j);
718  std::vector<std::string>::iterator a = find_if(synonymes.begin(), synonymes.end(), abbreviation_finder());
719  if (a != synonymes.end()) {
720  os << '-' << (*a) << ", ";
721  csize += 4;
722  }
723  // write leading '-'/"--"
724  os << "--";
725  csize += 2;
726  // write the name
727  os << *j;
728  // write the type if not a bool option
729  if (!o->isBool()) {
730  os << ' ' << o->getTypeName();
731  csize += 1 + o->getTypeName().length();
732  }
733  csize += 2;
734  // write the description formatting it
735  os << " ";
736  for (size_t r = maxSize; r > csize; --r) {
737  os << ' ';
738  }
739  size_t offset = csize > tooLarge ? csize : maxSize;
740  splitLines(os, o->getDescription(), offset, maxSize);
741  }
742  os << std::endl;
743  }
744  os << std::endl;
745  // print usage examples, calc size first
746  if (myCallExamples.size() != 0) {
747  os << "Examples:" << std::endl;
748  for (std::vector<std::pair<std::string, std::string> >::const_iterator e = myCallExamples.begin(); e != myCallExamples.end(); ++e) {
749  os << " " << myAppName << ' ' << e->first << std::endl;
750  os << " " << e->second << std::endl;
751  }
752  }
753  os << std::endl;
754  os << "Report bugs at <http://sumo.dlr.de/trac/>." << std::endl;
755  os << "Get in contact via <sumo@dlr.de>." << std::endl;
756 }
757 
758 
759 void
760 OptionsCont::writeConfiguration(std::ostream& os, bool filled,
761  bool complete, bool addComments) const {
762  os << "<?xml version=\"1.0\"" << SUMOSAXAttributes::ENCODING << "?>\n\n";
763  os << "<configuration xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"http://sumo.dlr.de/xsd/" << myAppName << "Configuration.xsd\">" << std::endl << std::endl;
764  for (std::vector<std::string>::const_iterator i = mySubTopics.begin(); i != mySubTopics.end(); ++i) {
765  std::string subtopic = *i;
766  if (subtopic == "Configuration" && !complete) {
767  continue;
768  }
769  std::replace(subtopic.begin(), subtopic.end(), ' ', '_');
770  std::transform(subtopic.begin(), subtopic.end(), subtopic.begin(), tolower);
771  const std::vector<std::string>& entries = mySubTopicEntries.find(*i)->second;
772  bool hadOne = false;
773  for (std::vector<std::string>::const_iterator j = entries.begin(); j != entries.end(); ++j) {
774  Option* o = getSecure(*j);
775  bool write = complete || (filled && !o->isDefault());
776  if (!write) {
777  continue;
778  }
779  if (!hadOne) {
780  os << " <" << subtopic << ">" << std::endl;
781  }
782  // add the comment if wished
783  if (addComments) {
784  os << " <!-- " << StringUtils::escapeXML(o->getDescription()) << " -->" << std::endl;
785  }
786  // write the option and the value (if given)
787  os << " <" << *j << " value=\"";
788  if (o->isSet() && (filled || o->isDefault())) {
789  os << o->getValueString();
790  }
791  if (complete) {
792  std::vector<std::string> synonymes = getSynonymes(*j);
793  if (!synonymes.empty()) {
794  os << "\" synonymes=\"";
795  for (std::vector<std::string>::const_iterator s = synonymes.begin(); s != synonymes.end(); ++s) {
796  if (s != synonymes.begin()) {
797  os << " ";
798  }
799  os << (*s);
800  }
801  }
802  os << "\" type=\"" << o->getTypeName();
803  if (!addComments) {
804  os << "\" help=\"" << StringUtils::escapeXML(o->getDescription());
805  }
806  }
807  os << "\"/>" << std::endl;
808  // append an endline if a comment was printed
809  if (addComments) {
810  os << std::endl;
811  }
812  hadOne = true;
813  }
814  if (hadOne) {
815  os << " </" << subtopic << ">" << std::endl << std::endl;
816  }
817  }
818  os << "</configuration>" << std::endl;
819 }
820 
821 
822 void
823 OptionsCont::writeSchema(std::ostream& os, bool /* addComments */) {
824  os << "<?xml version=\"1.0\"" << SUMOSAXAttributes::ENCODING << "?>\n\n";
825  os << "<xsd:schema elementFormDefault=\"qualified\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">\n\n";
826  os << " <xsd:include schemaLocation=\"baseTypes.xsd\"/>\n";
827  os << " <xsd:element name=\"configuration\" type=\"configurationType\"/>\n\n";
828  os << " <xsd:complexType name=\"configurationType\">\n";
829  os << " <xsd:all>\n";
830  for (std::vector<std::string>::const_iterator i = mySubTopics.begin(); i != mySubTopics.end(); ++i) {
831  std::string subtopic = *i;
832  if (subtopic == "Configuration") {
833  continue;
834  }
835  std::replace(subtopic.begin(), subtopic.end(), ' ', '_');
836  std::transform(subtopic.begin(), subtopic.end(), subtopic.begin(), tolower);
837  os << " <xsd:element name=\"" << subtopic << "\" type=\"" << subtopic << "Type\" minOccurs=\"0\"/>\n";
838  }
839  os << " </xsd:all>\n";
840  os << " </xsd:complexType>\n\n";
841  for (std::vector<std::string>::const_iterator i = mySubTopics.begin(); i != mySubTopics.end(); ++i) {
842  std::string subtopic = *i;
843  if (subtopic == "Configuration") {
844  continue;
845  }
846  std::replace(subtopic.begin(), subtopic.end(), ' ', '_');
847  std::transform(subtopic.begin(), subtopic.end(), subtopic.begin(), tolower);
848  os << " <xsd:complexType name=\"" << subtopic << "Type\">\n";
849  os << " <xsd:all>\n";
850  const std::vector<std::string>& entries = mySubTopicEntries[*i];
851  for (std::vector<std::string>::const_iterator j = entries.begin(); j != entries.end(); ++j) {
852  Option* o = getSecure(*j);
853  std::string type = o->getTypeName();
854  std::transform(type.begin(), type.end(), type.begin(), tolower);
855  if (type == "int[]") {
856  type = "intArray";
857  }
858  os << " <xsd:element name=\"" << *j << "\" type=\"" << type << "OptionType\" minOccurs=\"0\"/>\n";
859  }
860  os << " </xsd:all>\n";
861  os << " </xsd:complexType>\n\n";
862  }
863  os << "</xsd:schema>\n";
864 }
865 
866 
867 void
868 OptionsCont::writeXMLHeader(std::ostream& os) {
869  time_t rawtime;
870  char buffer [80];
871 
872  os << "<?xml version=\"1.0\"" << SUMOSAXAttributes::ENCODING << "?>\n\n";
873  time(&rawtime);
874  strftime(buffer, 80, "<!-- generated on %c by ", localtime(&rawtime));
875  os << buffer << myFullName << "\n";
876  writeConfiguration(os, true, false, false);
877  os << "-->\n\n";
878 }
879 
880 
881 std::vector<std::string>
882 OptionsCont::getStringVector(const std::string& name) const {
883  Option* o = getSecure(name);
884  std::string def = o->getString();
885  if (def.find(';') != std::string::npos && !myHaveInformedAboutDeprecatedDivider) {
886  WRITE_WARNING("Please note that using ';' as list separator is deprecated.\n From 1.0 onwards, only ',' will be accepted.");
888  }
889  StringTokenizer st(def, ";,", true);
890  std::vector<std::string> ret = st.getVector();
891  for (std::vector<std::string>::iterator i = ret.begin(); i != ret.end(); ++i) {
892  (*i) = StringUtils::prune(*i);
893  }
894  return ret;
895 }
896 
897 
898 bool
899 OptionsCont::isInStringVector(const std::string& optionName,
900  const std::string& itemName) {
901  if (isSet(optionName)) {
902  std::vector<std::string> values = getStringVector(optionName);
903  return find(values.begin(), values.end(), itemName) != values.end();
904  }
905  return false;
906 }
907 
908 
909 /****************************************************************************/
std::string myAppName
some information on the application
Definition: OptionsCont.h:732
void doRegister(const std::string &name, Option *v)
Adds an option under the given name.
Definition: OptionsCont.cpp:86
std::vector< std::string > getStringVector(const std::string &name) const
Returns the list of string-vector-value of the named option (only for Option_String) ...
std::vector< std::string > getSynonymes(const std::string &name) const
Returns the synonymes of an option name.
std::vector< std::string > mySubTopics
lists of option subtopics and copyright notices
Definition: OptionsCont.h:738
std::string myFullName
Definition: OptionsCont.h:732
static std::string getConfigurationRelative(const std::string &configPath, const std::string &path)
Returns the second path as a relative path to the first file.
Definition: FileHelpers.cpp:86
void resetWritable()
Resets all options to be writeable.
bool isSet() const
returns the information whether this options holds a valid value
Definition: Option.cpp:80
std::string next()
void addCopyrightNotice(const std::string &copyrightLine)
Adds a copyright notice to the help output.
bool isInStringVector(const std::string &optionName, const std::string &itemName)
Returns the named option is a list of string values containing the specified item.
static bool isReadable(std::string path)
Checks whether the given file is readable.
Definition: FileHelpers.cpp:58
bool checkDependingSuboptions(const std::string &name, const std::string &prefix) const
Checks whether an option is set, which has options with a prefix depending on it. ...
virtual bool isBool() const
Returns the information whether the option is a bool option.
Definition: Option.cpp:133
void addCallExample(const std::string &example, const std::string &desc)
Add a call example.
void unSet(const std::string &name, bool failOnNonExistant=true) const
Marks the option as unset.
void reportDoubleSetting(const std::string &arg) const
Reports an error that the option has already been set.
void setApplicationDescription(const std::string &appDesc)
Sets the application description.
void clearCopyrightNotices()
Removes all copyright information.
void printHelp(std::ostream &os)
Prints the help.
const IntVector & getIntVector(const std::string &name) const
Returns the list of integer-value of the named option (only for Option_IntVector) ...
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
static std::string escapeXML(const std::string &orig)
Replaces the standard escapes by their XML entities.
bool myHaveInformedAboutDeprecatedDivider
Information whether a warning a deprecated divider.
Definition: OptionsCont.h:750
virtual bool getBool() const
Returns the stored boolean value.
Definition: Option.cpp:104
std::string convertChar(char abbr) const
Converts an abbreviation into a name.
virtual const IntVector & getIntVector() const
Returns the stored integer vector.
Definition: Option.cpp:110
void setDescription(const std::string &desc)
Sets the description of what this option does.
Definition: Option.cpp:175
virtual const std::string & getTypeName() const
Returns the mml-type name of this option.
Definition: Option.cpp:181
static OptionsCont myOptions
The static options container used.
Definition: OptionsCont.h:717
virtual std::string getString() const
Returns the stored string value.
Definition: Option.cpp:98
SUMOReal getFloat(const std::string &name) const
Returns the SUMOReal-value of the named option (only for Option_Float)
std::map< std::string, std::string > myXMLDefaults
A map from XML root element to option.
Definition: OptionsCont.h:744
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:200
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:69
void resetDefault()
Resets the option to be on its default value.
Definition: Option.cpp:163
void addSynonyme(const std::string &name1, const std::string &name2, bool isDeprecated=false)
Adds a synonyme for an options name (any order)
std::string myAppDescription
Definition: OptionsCont.h:732
ItemAddressContType myAddresses
Definition: OptionsCont.h:726
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
bool setByRootElement(const std::string &name, const std::string &value)
Sets the given value for the option which can handle the given XML root.
std::map< std::string, bool > myDeprecatedSynonymes
A map from deprecated options to a bool indicating whether we warned about deprecation.
Definition: OptionsCont.h:747
void clear()
Removes all information from the container.
void writeXMLHeader(std::ostream &os)
Writes a standard XML header, including the configuration.
bool isBool(const std::string &name) const
Returns the information whether the option is a boolean option.
std::string myAdditionalMessage
Definition: OptionsCont.h:732
std::vector< int > IntVector
Definition of a vector of unsigned ints.
Definition: Option.h:48
const std::string & getDescription() const
Returns the description of what this option does.
Definition: Option.cpp:169
#define HAVE_ENABLED
Definition: config.h:32
bool setDefault(const std::string &name, const std::string &value)
Sets the given value for the named option as new default value.
static bool isAbsolute(const std::string &path)
Returns the information whether the given path is absolute.
void setAdditionalHelpMessage(const std::string &add)
Sets an additional message to be printed at the begin of the help screen.
bool isUsableFileList(const std::string &name) const
Checks whether the named option is usable as a file list (with at least a single file) ...
static const std::string ENCODING
The encoding of parsed strings.
bool processMetaOptions(bool missingOptions)
Checks for help and configuration output, returns whether we should exit.
~OptionsCont()
Destructor.
Definition: OptionsCont.cpp:80
bool isWriteable(const std::string &name)
Returns the information whether the named option may be set.
void addOptionSubTopic(const std::string &topic)
Adds an option subtopic.
bool isWriteable() const
Returns the information whether the option may be set a further time.
Definition: Option.cpp:151
bool isDefault(const std::string &name) const
Returns the information whether the named option has still the default value.
void splitLines(std::ostream &os, std::string what, size_t offset, size_t nextOffset)
Writes the given string &#39;formatted&#39;.
virtual bool set(const std::string &v)=0
Stores the given value.
void writeSchema(std::ostream &os, bool addComments)
Writes the xml schema for the configuration.
std::vector< std::string > getVector()
A class to find abbreviated option names (length=1)
Definition: OptionsCont.h:758
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:206
A class representing a single program option.
Definition: Option.h:79
void relocateFiles(const std::string &configuration) const
Modifies file name options according to the configuration path.
virtual bool isDefault() const
Returns the information whether the option holds the default value.
Definition: Option.cpp:139
virtual int getInt() const
Returns the stored integer value.
Definition: Option.cpp:92
virtual SUMOReal getFloat() const
Returns the stored SUMOReal value.
Definition: Option.cpp:86
static std::string prune(const std::string &str)
Removes trailing and leading whitechars.
Definition: StringUtils.cpp:56
bool set(const std::string &name, const std::string &value)
Sets the given value for the named option.
virtual std::string getValueString() const =0
Returns the string-representation of the value.
void addXMLDefault(const std::string &name, const std::string &xmlRoot="")
Adds an XML root element to handle by default. The special root "" denotes the default handler...
KnownContType myValues
Definition: OptionsCont.h:729
std::map< std::string, std::vector< std::string > > mySubTopicEntries
A map from subtopic to option.
Definition: OptionsCont.h:741
friend std::ostream & operator<<(std::ostream &os, const OptionsCont &oc)
Output operator.
static std::string urlDecode(const std::string &encoded)
A storage for options typed value containers)
Definition: OptionsCont.h:108
Option * getSecure(const std::string &name) const
Returns the named option.
std::vector< std::string > myCopyrightNotices
Definition: OptionsCont.h:738
#define SUMOReal
Definition: config.h:213
void writeConfiguration(std::ostream &os, bool filled, bool complete, bool addComments) const
Writes the configuration.
void addDescription(const std::string &name, const std::string &subtopic, const std::string &description)
Adds a description for an option.
int getInt(const std::string &name) const
Returns the int-value of the named option (only for Option_Integer)
#define WRITE_MESSAGE(msg)
Definition: MsgHandler.h:201
OptionsCont()
Constructor.
Definition: OptionsCont.cpp:74
std::vector< std::pair< std::string, std::string > > myCallExamples
list of call examples
Definition: OptionsCont.h:735
bool exists(const std::string &name) const
Returns the information whether the named option is known.
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
void setApplicationName(const std::string &appName, const std::string &fullName)
Sets the application name.