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 bool
237 OptionsCont::set(const std::string& name, const std::string& value) {
238  Option* o = getSecure(name);
239  if (!o->isWriteable()) {
240  reportDoubleSetting(name);
241  return false;
242  }
243  try {
244  if (!o->set(value)) {
245  return false;
246  }
247  } catch (ProcessError& e) {
248  WRITE_ERROR("While processing option '" + name + "':\n " + e.what());
249  return false;
250  }
251  return true;
252 }
253 
254 
255 bool
256 OptionsCont::setDefault(const std::string& name, const std::string& value) {
257  if (set(name, value)) {
258  getSecure(name)->resetDefault();
259  return true;
260  }
261  return false;
262 }
263 
264 
265 bool
266 OptionsCont::setByRootElement(const std::string& root, const std::string& value) {
267  if (myXMLDefaults.count(root) > 0) {
268  return set(myXMLDefaults[root], value);
269  }
270  if (myXMLDefaults.count("") > 0) {
271  return set(myXMLDefaults[""], value);
272  }
273  return false;
274 }
275 
276 
277 std::vector<std::string>
278 OptionsCont::getSynonymes(const std::string& name) const {
279  Option* o = getSecure(name);
280  std::vector<std::string> v(0);
281  for (KnownContType::const_iterator i = myValues.begin(); i != myValues.end(); i++) {
282  if ((*i).second == o && name != (*i).first) {
283  v.push_back((*i).first);
284  }
285  }
286  return v;
287 }
288 
289 
290 const std::string&
291 OptionsCont::getDescription(const std::string& name) const {
292  return getSecure(name)->getDescription();
293 }
294 
295 
296 std::ostream&
297 operator<<(std::ostream& os, const OptionsCont& oc) {
298  std::vector<std::string> done;
299  os << "Options set:" << std::endl;
300  for (OptionsCont::KnownContType::const_iterator i = oc.myValues.begin();
301  i != oc.myValues.end(); i++) {
302  std::vector<std::string>::iterator j = find(done.begin(), done.end(), (*i).first);
303  if (j == done.end()) {
304  std::vector<std::string> synonymes = oc.getSynonymes((*i).first);
305  if (synonymes.size() != 0) {
306  os << (*i).first << " (";
307  for (j = synonymes.begin(); j != synonymes.end(); j++) {
308  if (j != synonymes.begin()) {
309  os << ", ";
310  }
311  os << (*j);
312  }
313  os << ")";
314  } else {
315  os << (*i).first;
316  }
317  if ((*i).second->isSet()) {
318  os << ": " << (*i).second->getValueString() << std::endl;
319  } else {
320  os << ": <INVALID>" << std::endl;
321  }
322  done.push_back((*i).first);
323  copy(synonymes.begin(), synonymes.end(), back_inserter(done));
324  }
325  }
326  return os;
327 }
328 
329 
330 void
331 OptionsCont::relocateFiles(const std::string& configuration) const {
332  for (Option* const option : myAddresses) {
333  if (option->isFileName() && option->isSet()) {
334  std::vector<std::string> fileList = StringTokenizer(option->getString(), ",").getVector();
335  for (std::string& f : fileList) {
336  // Pruning is necessary because filenames may be separated by ', ' in the configuration file
338  }
339  const std::string conv = joinToString(fileList, ',');
340  if (conv != option->getString()) {
341  const bool hadDefault = option->isDefault();
342  option->set(conv);
343  if (hadDefault) {
344  option->resetDefault();
345  }
346  }
347  }
348  }
349 }
350 
351 
352 bool
353 OptionsCont::isUsableFileList(const std::string& name) const {
354  Option* o = getSecure(name);
355  // check whether the option is set
356  // return false i not
357  if (!o->isSet()) {
358  return false;
359  }
360  // check whether the list of files is valid
361  bool ok = true;
362  std::vector<std::string> files = getStringVector(name);
363  if (files.size() == 0) {
364  WRITE_ERROR("The file list for '" + name + "' is empty.");
365  ok = false;
366  }
367  for (std::vector<std::string>::const_iterator fileIt = files.begin(); fileIt != files.end(); ++fileIt) {
368  if (!FileHelpers::isReadable(*fileIt)) {
369  if (*fileIt != "") {
370  WRITE_ERROR("File '" + *fileIt + "' is not accessible (" + std::strerror(errno) + ").");
371  ok = false;
372  } else {
373  WRITE_WARNING("Empty file name given; ignoring.");
374  }
375  }
376  }
377  return ok;
378 }
379 
380 
381 bool
382 OptionsCont::checkDependingSuboptions(const std::string& name, const std::string& prefix) const {
383  Option* o = getSecure(name);
384  if (o->isSet()) {
385  return true;
386  }
387  bool ok = true;
388  std::vector<std::string> seenSynonymes;
389  for (KnownContType::const_iterator i = myValues.begin(); i != myValues.end(); i++) {
390  if (std::find(seenSynonymes.begin(), seenSynonymes.end(), (*i).first) != seenSynonymes.end()) {
391  continue;
392  }
393  if ((*i).second->isSet() && !(*i).second->isDefault() && (*i).first.find(prefix) == 0) {
394  WRITE_ERROR("Option '" + (*i).first + "' needs option '" + name + "'.");
395  std::vector<std::string> synonymes = getSynonymes((*i).first);
396  std::copy(synonymes.begin(), synonymes.end(), std::back_inserter(seenSynonymes));
397  ok = false;
398  }
399  }
400  return ok;
401 }
402 
403 
404 void
405 OptionsCont::reportDoubleSetting(const std::string& arg) const {
406  std::vector<std::string> synonymes = getSynonymes(arg);
407  std::ostringstream s;
408  s << "A value for the option '" + arg + "' was already set.\n Possible synonymes: ";
409  for (std::vector<std::string>::iterator i = synonymes.begin(); i != synonymes.end();) {
410  s << (*i);
411  i++;
412  if (i != synonymes.end()) {
413  s << ", ";
414  }
415  }
416  WRITE_ERROR(s.str());
417 }
418 
419 
420 std::string
421 OptionsCont::convertChar(char abbr) const {
422  char buf[2];
423  buf[0] = abbr;
424  buf[1] = 0;
425  std::string s(buf);
426  return s;
427 }
428 
429 
430 bool
431 OptionsCont::isBool(const std::string& name) const {
432  Option* o = getSecure(name);
433  return o->isBool();
434 }
435 
436 
437 void
439  for (ItemAddressContType::iterator i = myAddresses.begin(); i != myAddresses.end(); i++) {
440  (*i)->resetWritable();
441  }
442 }
443 
444 
445 bool
446 OptionsCont::isWriteable(const std::string& name) {
447  Option* o = getSecure(name);
448  return o->isWriteable();
449 }
450 
451 
452 void
454  ItemAddressContType::iterator i;
455  for (i = myAddresses.begin(); i != myAddresses.end(); i++) {
456  delete(*i);
457  }
458  myAddresses.clear();
459  myValues.clear();
460  mySubTopics.clear();
461  mySubTopicEntries.clear();
462 }
463 
464 
465 void
466 OptionsCont::addDescription(const std::string& name,
467  const std::string& subtopic,
468  const std::string& description) {
469  Option* o = getSecure(name);
470  assert(o != 0);
471  assert(find(mySubTopics.begin(), mySubTopics.end(), subtopic) != mySubTopics.end());
472  o->setDescription(description);
473  mySubTopicEntries[subtopic].push_back(name);
474 }
475 
476 
477 void
478 OptionsCont::setApplicationName(const std::string& appName,
479  const std::string& fullName) {
480  myAppName = appName;
481  myFullName = fullName;
482 }
483 
484 
485 void
486 OptionsCont::setApplicationDescription(const std::string& appDesc) {
487  myAppDescription = appDesc;
488 }
489 
490 
491 void
492 OptionsCont::addCallExample(const std::string& example, const std::string& desc) {
493  myCallExamples.push_back(std::make_pair(example, desc));
494 }
495 
496 
497 void
498 OptionsCont::setAdditionalHelpMessage(const std::string& add) {
499  myAdditionalMessage = add;
500 }
501 
502 
503 void
504 OptionsCont::addCopyrightNotice(const std::string& copyrightLine) {
505  myCopyrightNotices.push_back(copyrightLine);
506 }
507 
508 
509 void
511  myCopyrightNotices.clear();
512 }
513 
514 
515 void
516 OptionsCont::addOptionSubTopic(const std::string& topic) {
517  mySubTopics.push_back(topic);
518  mySubTopicEntries[topic] = std::vector<std::string>();
519 }
520 
521 
522 void
523 OptionsCont::splitLines(std::ostream& os, std::string what,
524  int offset, int nextOffset) {
525  while (what.length() > 0) {
526  if ((int)what.length() > 79 - offset) {
527  std::string::size_type splitPos = what.rfind(';', 79 - offset);
528  if (splitPos == std::string::npos) {
529  splitPos = what.rfind(' ', 79 - offset);
530  } else {
531  splitPos++;
532  }
533  if (splitPos != std::string::npos) {
534  os << what.substr(0, splitPos) << std::endl;
535  what = what.substr(splitPos);
536  for (int r = 0; r < nextOffset + 1; ++r) {
537  os << ' ';
538  }
539  } else {
540  os << what;
541  what = "";
542  }
543  offset = nextOffset;
544  } else {
545  os << what;
546  what = "";
547  }
548  }
549  os << std::endl;
550 }
551 
552 
553 bool
554 OptionsCont::processMetaOptions(bool missingOptions) {
555  if (missingOptions) {
556  // no options are given
557  std::cout << myFullName << std::endl;
558  std::cout << " Build features: " << HAVE_ENABLED << std::endl;
559  for (std::vector<std::string>::const_iterator it =
560  myCopyrightNotices.begin(); it != myCopyrightNotices.end(); ++it) {
561  std::cout << " " << *it << std::endl;
562  }
563  std::cout << " License EPL-2.0: Eclipse Public License Version 2 <https://eclipse.org/legal/epl-v20.html>\n";
564  std::cout << " Use --help to get the list of options." << std::endl;
565  return true;
566  }
567 
568  myWriteLicense = getBool("write-license");
569  // check whether the help shall be printed
570  if (getBool("help")) {
571  std::cout << myFullName << std::endl;
572  for (std::vector<std::string>::const_iterator it =
573  myCopyrightNotices.begin(); it != myCopyrightNotices.end(); ++it) {
574  std::cout << " " << *it << std::endl;
575  }
576  printHelp(std::cout);
577  return true;
578  }
579  // check whether the help shall be printed
580  if (getBool("version")) {
581  std::cout << myFullName << std::endl;
582  std::cout << " Build features: " << HAVE_ENABLED << std::endl;
583  for (std::vector<std::string>::const_iterator it =
584  myCopyrightNotices.begin(); it != myCopyrightNotices.end(); ++it) {
585  std::cout << " " << *it << std::endl;
586  }
587  std::cout << "\n" << myFullName << " is part of SUMO.\n";
588  std::cout << "This program and the accompanying materials\n";
589  std::cout << "are made available under the terms of the Eclipse Public License v2.0\n";
590  std::cout << "which accompanies this distribution, and is available at\n";
591  std::cout << "http://www.eclipse.org/legal/epl-v20.html\n";
592  std::cout << "SPDX-License-Identifier: EPL-2.0" << std::endl;
593  return true;
594  }
595  // check whether the settings shall be printed
596  if (exists("print-options") && getBool("print-options")) {
597  std::cout << (*this);
598  }
599  // check whether something has to be done with options
600  // whether the current options shall be saved
601  if (isSet("save-configuration", false)) { // sumo-gui does not register these
602  if (getString("save-configuration") == "-" || getString("save-configuration") == "stdout") {
603  writeConfiguration(std::cout, true, false, getBool("save-commented"));
604  return true;
605  }
606  std::ofstream out(getString("save-configuration").c_str());
607  if (!out.good()) {
608  throw ProcessError("Could not save configuration to '" + getString("save-configuration") + "'");
609  } else {
610  writeConfiguration(out, true, false, getBool("save-commented"));
611  if (getBool("verbose")) {
612  WRITE_MESSAGE("Written configuration to '" + getString("save-configuration") + "'");
613  }
614  return true;
615  }
616  }
617  // whether the template shall be saved
618  if (isSet("save-template", false)) { // sumo-gui does not register these
619  if (getString("save-template") == "-" || getString("save-template") == "stdout") {
620  writeConfiguration(std::cout, false, true, getBool("save-commented"));
621  return true;
622  }
623  std::ofstream out(getString("save-template").c_str());
624  if (!out.good()) {
625  throw ProcessError("Could not save template to '" + getString("save-template") + "'");
626  } else {
627  writeConfiguration(out, false, true, getBool("save-commented"));
628  if (getBool("verbose")) {
629  WRITE_MESSAGE("Written template to '" + getString("save-template") + "'");
630  }
631  return true;
632  }
633  }
634  if (isSet("save-schema", false)) { // sumo-gui does not register these
635  if (getString("save-schema") == "-" || getString("save-schema") == "stdout") {
636  writeSchema(std::cout);
637  return true;
638  }
639  std::ofstream out(getString("save-schema").c_str());
640  if (!out.good()) {
641  throw ProcessError("Could not save schema to '" + getString("save-schema") + "'");
642  } else {
643  writeSchema(out);
644  if (getBool("verbose")) {
645  WRITE_MESSAGE("Written schema to '" + getString("save-schema") + "'");
646  }
647  return true;
648  }
649  }
650  return false;
651 }
652 
653 void
654 OptionsCont::printHelp(std::ostream& os) {
655  std::vector<std::string>::const_iterator i, j;
656  // print application description
657  splitLines(os, myAppDescription, 0, 0);
658  os << std::endl;
659  // print usage BNF
660  os << "Usage: " << myAppName << " [OPTION]*" << std::endl;
661  // print additional text if any
662  if (myAdditionalMessage.length() > 0) {
663  os << myAdditionalMessage << std::endl << ' ' << std::endl;
664  }
665  // print the options
666  // check their sizes first
667  // we want to know how large the largest not-too-large-entry will be
668  int tooLarge = 40;
669  int maxSize = 0;
670  for (i = mySubTopics.begin(); i != mySubTopics.end(); ++i) {
671  const std::vector<std::string>& entries = mySubTopicEntries[*i];
672  for (j = entries.begin(); j != entries.end(); ++j) {
673  Option* o = getSecure(*j);
674  // name, two leading spaces and "--"
675  int csize = (int)j->length() + 2 + 4;
676  // abbreviation length ("-X, "->4chars) if any
677  const std::vector<std::string> synonymes = getSynonymes(*j);
678  for (std::vector<std::string>::const_iterator s = synonymes.begin(); s != synonymes.end(); ++s) {
679  if (s->length() == 1 && myDeprecatedSynonymes.count(*s) == 0) {
680  csize += 4;
681  break;
682  }
683  }
684  // the type name
685  if (!o->isBool()) {
686  csize += 1 + (int)o->getTypeName().length();
687  }
688  // divider
689  csize += 2;
690  if (csize < tooLarge && maxSize < csize) {
691  maxSize = csize;
692  }
693  }
694  }
695 
696  for (i = mySubTopics.begin(); i != mySubTopics.end(); ++i) {
697  os << *i << " Options:" << std::endl;
698  const std::vector<std::string>& entries = mySubTopicEntries[*i];
699  for (j = entries.begin(); j != entries.end(); ++j) {
700  // start length computation
701  int csize = (int)j->length() + 2;
702  Option* o = getSecure(*j);
703  os << " ";
704  // write abbreviation if given
705  std::vector<std::string> synonymes = getSynonymes(*j);
706  for (std::vector<std::string>::const_iterator s = synonymes.begin(); s != synonymes.end(); ++s) {
707  if (s->length() == 1 && myDeprecatedSynonymes.count(*s) == 0) {
708  os << '-' << *s << ", ";
709  csize += 4;
710  break;
711  }
712  }
713  // write leading '-'/"--"
714  os << "--";
715  csize += 2;
716  // write the name
717  os << *j;
718  // write the type if not a bool option
719  if (!o->isBool()) {
720  os << ' ' << o->getTypeName();
721  csize += 1 + (int)o->getTypeName().length();
722  }
723  csize += 2;
724  // write the description formatting it
725  os << " ";
726  for (int r = maxSize; r > csize; --r) {
727  os << ' ';
728  }
729  int offset = csize > tooLarge ? csize : maxSize;
730  splitLines(os, o->getDescription(), offset, maxSize);
731  }
732  os << std::endl;
733  }
734  os << std::endl;
735  // print usage examples, calc size first
736  if (myCallExamples.size() != 0) {
737  os << "Examples:" << std::endl;
738  for (std::vector<std::pair<std::string, std::string> >::const_iterator e = myCallExamples.begin(); e != myCallExamples.end(); ++e) {
739  os << " " << myAppName << ' ' << e->first << std::endl;
740  os << " " << e->second << std::endl;
741  }
742  }
743  os << std::endl;
744  os << "Report bugs at <https://github.com/eclipse/sumo/issues>." << std::endl;
745  os << "Get in contact via <sumo@dlr.de>." << std::endl;
746 }
747 
748 
749 void
750 OptionsCont::writeConfiguration(std::ostream& os, const bool filled,
751  const bool complete, const bool addComments,
752  const bool inComment) const {
753  if (!inComment) {
754  writeXMLHeader(os, false);
755  }
756  os << "<configuration xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"http://sumo.dlr.de/xsd/";
757  if (myAppName == "sumo-gui") {
758  os << "sumo";
759  } else if (myAppName == "netedit") {
760  os << "netconvert";
761  } else {
762  os << myAppName;
763  }
764  os << "Configuration.xsd\">" << std::endl << std::endl;
765  for (std::vector<std::string>::const_iterator i = mySubTopics.begin(); i != mySubTopics.end(); ++i) {
766  std::string subtopic = *i;
767  if (subtopic == "Configuration" && !complete) {
768  continue;
769  }
770  std::replace(subtopic.begin(), subtopic.end(), ' ', '_');
771  std::transform(subtopic.begin(), subtopic.end(), subtopic.begin(), tolower);
772  const std::vector<std::string>& entries = mySubTopicEntries.find(*i)->second;
773  bool hadOne = false;
774  for (std::vector<std::string>::const_iterator j = entries.begin(); j != entries.end(); ++j) {
775  Option* o = getSecure(*j);
776  bool write = complete || (filled && !o->isDefault());
777  if (!write) {
778  continue;
779  }
780  if (!hadOne) {
781  os << " <" << subtopic << ">" << std::endl;
782  }
783  // add the comment if wished
784  if (addComments) {
785  os << " <!-- " << StringUtils::escapeXML(o->getDescription(), inComment) << " -->" << std::endl;
786  }
787  // write the option and the value (if given)
788  os << " <" << *j << " value=\"";
789  if (o->isSet() && (filled || o->isDefault())) {
790  os << StringUtils::escapeXML(o->getValueString(), inComment);
791  }
792  if (complete) {
793  std::vector<std::string> synonymes = getSynonymes(*j);
794  if (!synonymes.empty()) {
795  os << "\" synonymes=\"";
796  for (std::vector<std::string>::const_iterator s = synonymes.begin(); s != synonymes.end(); ++s) {
797  if (s != synonymes.begin()) {
798  os << " ";
799  }
800  os << (*s);
801  }
802  }
803  os << "\" type=\"" << o->getTypeName();
804  if (!addComments) {
805  os << "\" help=\"" << StringUtils::escapeXML(o->getDescription());
806  }
807  }
808  os << "\"/>" << std::endl;
809  // append an endline if a comment was printed
810  if (addComments) {
811  os << std::endl;
812  }
813  hadOne = true;
814  }
815  if (hadOne) {
816  os << " </" << subtopic << ">" << std::endl << std::endl;
817  }
818  }
819  os << "</configuration>" << std::endl;
820 }
821 
822 
823 void
824 OptionsCont::writeSchema(std::ostream& os) {
825  writeXMLHeader(os, false);
826  os << "<xsd:schema elementFormDefault=\"qualified\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">\n\n";
827  os << " <xsd:include schemaLocation=\"baseTypes.xsd\"/>\n";
828  os << " <xsd:element name=\"configuration\" type=\"configurationType\"/>\n\n";
829  os << " <xsd:complexType name=\"configurationType\">\n";
830  os << " <xsd:all>\n";
831  for (std::vector<std::string>::const_iterator i = mySubTopics.begin(); i != mySubTopics.end(); ++i) {
832  std::string subtopic = *i;
833  if (subtopic == "Configuration") {
834  continue;
835  }
836  std::replace(subtopic.begin(), subtopic.end(), ' ', '_');
837  std::transform(subtopic.begin(), subtopic.end(), subtopic.begin(), tolower);
838  os << " <xsd:element name=\"" << subtopic << "\" type=\"" << subtopic << "TopicType\" minOccurs=\"0\"/>\n";
839  }
840  os << " </xsd:all>\n";
841  os << " </xsd:complexType>\n\n";
842  for (std::vector<std::string>::const_iterator i = mySubTopics.begin(); i != mySubTopics.end(); ++i) {
843  std::string subtopic = *i;
844  if (subtopic == "Configuration") {
845  continue;
846  }
847  std::replace(subtopic.begin(), subtopic.end(), ' ', '_');
848  std::transform(subtopic.begin(), subtopic.end(), subtopic.begin(), tolower);
849  os << " <xsd:complexType name=\"" << subtopic << "TopicType\">\n";
850  os << " <xsd:all>\n";
851  const std::vector<std::string>& entries = mySubTopicEntries[*i];
852  for (std::vector<std::string>::const_iterator j = entries.begin(); j != entries.end(); ++j) {
853  Option* o = getSecure(*j);
854  std::string type = o->getTypeName();
855  std::transform(type.begin(), type.end(), type.begin(), tolower);
856  if (type == "int[]") {
857  type = "intArray";
858  }
859  os << " <xsd:element name=\"" << *j << "\" type=\"" << type << "OptionType\" minOccurs=\"0\"/>\n";
860  }
861  os << " </xsd:all>\n";
862  os << " </xsd:complexType>\n\n";
863  }
864  os << "</xsd:schema>\n";
865 }
866 
867 
868 void
869 OptionsCont::writeXMLHeader(std::ostream& os, const bool includeConfig) const {
870  time_t rawtime;
871  char buffer [80];
872 
873  os << "<?xml version=\"1.0\"" << SUMOSAXAttributes::ENCODING << "?>\n\n";
874  time(&rawtime);
875  strftime(buffer, 80, "<!-- generated on %c by ", localtime(&rawtime));
876  os << buffer << myFullName << "\n";
877  if (myWriteLicense) {
878  os << "This data file and the accompanying materials\n";
879  os << "are made available under the terms of the Eclipse Public License v2.0\n";
880  os << "which accompanies this distribution, and is available at\n";
881  os << "http://www.eclipse.org/legal/epl-v20.html\n";
882  os << "SPDX-License-Identifier: EPL-2.0\n";
883  }
884  if (includeConfig) {
885  writeConfiguration(os, true, false, false, true);
886  }
887  os << "-->\n\n";
888 }
889 
890 
891 std::vector<std::string>
892 OptionsCont::getStringVector(const std::string& name) const {
893  Option* o = getSecure(name);
894  std::string def = o->getString();
895  if (def.find(';') != std::string::npos && !myHaveInformedAboutDeprecatedDivider) {
896  WRITE_WARNING("Please note that using ';' as list separator is deprecated.\n From 1.0 onwards, only ',' will be accepted.");
898  }
899  StringTokenizer st(def, ";,", true);
900  std::vector<std::string> ret = st.getVector();
901  for (std::vector<std::string>::iterator i = ret.begin(); i != ret.end(); ++i) {
902  (*i) = StringUtils::prune(*i);
903  }
904  return ret;
905 }
906 
907 
908 bool
909 OptionsCont::isInStringVector(const std::string& optionName,
910  const std::string& itemName) {
911  if (isSet(optionName)) {
912  std::vector<std::string> values = getStringVector(optionName);
913  return find(values.begin(), values.end(), itemName) != values.end();
914  }
915  return false;
916 }
917 
918 
919 /****************************************************************************/
virtual double getFloat() const
Returns the stored double value.
Definition: Option.cpp:75
std::string myAppName
some information on the application
Definition: OptionsCont.h:725
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:731
std::string myFullName
Definition: OptionsCont.h:725
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)
void resetWritable()
Resets all options to be writeable.
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:743
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:164
static OptionsCont myOptions
The static options container used.
Definition: OptionsCont.h:710
bool isWriteable() const
Returns the information whether the option may be set a further time.
Definition: Option.cpp:140
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:737
virtual int getInt() const
Returns the stored integer value.
Definition: Option.cpp:81
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:242
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:152
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:725
ItemAddressContType myAddresses
Definition: OptionsCont.h:719
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:740
void clear()
Removes all information from the container.
std::string myAdditionalMessage
Definition: OptionsCont.h:725
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:122
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:248
A class representing a single program option.
Definition: Option.h:72
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:746
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:45
virtual bool isDefault() const
Returns the information whether the option holds the default value.
Definition: Option.cpp:128
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:158
KnownContType myValues
Definition: OptionsCont.h:722
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:734
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:170
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:731
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:243
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:230
std::vector< std::pair< std::string, std::string > > myCallExamples
list of call examples
Definition: OptionsCont.h:728
void setApplicationName(const std::string &appName, const std::string &fullName)
Sets the application name.