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