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-2015 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-2015 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 bool
135 OptionsCont::exists(const std::string& name) const {
136  return myValues.count(name) > 0;
137 }
138 
139 
140 bool
141 OptionsCont::isSet(const std::string& name, bool failOnNonExistant) const {
142  KnownContType::const_iterator i = myValues.find(name);
143  if (i == myValues.end()) {
144  if (failOnNonExistant) {
145  throw ProcessError("Internal request for unknown option '" + name + "'!");
146  } else {
147  return false;
148  }
149  }
150  return (*i).second->isSet();
151 }
152 
153 
154 void
155 OptionsCont::unSet(const std::string& name, bool failOnNonExistant) const {
156  KnownContType::const_iterator i = myValues.find(name);
157  if (i == myValues.end()) {
158  if (failOnNonExistant) {
159  throw ProcessError("Internal request for unknown option '" + name + "'!");
160  } else {
161  return;
162  }
163  }
164  (*i).second->unSet();
165 }
166 
167 
168 bool
169 OptionsCont::isDefault(const std::string& name) const {
170  KnownContType::const_iterator i = myValues.find(name);
171  if (i == myValues.end()) {
172  return false;
173  }
174  return (*i).second->isDefault();
175 }
176 
177 
178 Option*
179 OptionsCont::getSecure(const std::string& name) const {
180  KnownContType::const_iterator k = myValues.find(name);
181  if (k == myValues.end()) {
182  throw ProcessError("No option with the name '" + name + "' exists.");
183  }
184  std::map<std::string, bool>::iterator s = myDeprecatedSynonymes.find(name);
185  if (s != myDeprecatedSynonymes.end() && !s->second) {
186  std::string defaultName;
187  for (std::map<std::string, std::vector<std::string> >::const_iterator i = mySubTopicEntries.begin(); i != mySubTopicEntries.end(); ++i) {
188  for (std::vector<std::string>::const_iterator j = i->second.begin(); j != i->second.end(); ++j) {
189  KnownContType::const_iterator l = myValues.find(*j);
190  if (l != myValues.end() && l->second == k->second) {
191  defaultName = *j;
192  break;
193  }
194  }
195  if (defaultName != "") {
196  break;
197  }
198  }
199  WRITE_WARNING("Please note that '" + name + "' is deprecated.\n Use '" + defaultName + "' instead.");
200  s->second = true;
201  }
202  return k->second;
203 }
204 
205 
206 std::string
207 OptionsCont::getString(const std::string& name) const {
208  Option* o = getSecure(name);
209  return o->getString();
210 }
211 
212 
213 SUMOReal
214 OptionsCont::getFloat(const std::string& name) const {
215  Option* o = getSecure(name);
216  return o->getFloat();
217 }
218 
219 
220 int
221 OptionsCont::getInt(const std::string& name) const {
222  Option* o = getSecure(name);
223  return o->getInt();
224 }
225 
226 
227 bool
228 OptionsCont::getBool(const std::string& name) const {
229  Option* o = getSecure(name);
230  return o->getBool();
231 }
232 
233 
234 const IntVector&
235 OptionsCont::getIntVector(const std::string& name) const {
236  Option* o = getSecure(name);
237  return o->getIntVector();
238 }
239 
240 
241 bool
242 OptionsCont::set(const std::string& name, const std::string& value) {
243  Option* o = getSecure(name);
244  if (!o->isWriteable()) {
245  reportDoubleSetting(name);
246  return false;
247  }
248  try {
249  if (!o->set(value)) {
250  return false;
251  }
252  } catch (ProcessError& e) {
253  WRITE_ERROR("While processing option '" + name + "':\n " + e.what());
254  return false;
255  }
256  return true;
257 }
258 
259 
260 std::vector<std::string>
261 OptionsCont::getSynonymes(const std::string& name) const {
262  Option* o = getSecure(name);
263  std::vector<std::string> v(0);
264  for (KnownContType::const_iterator i = myValues.begin(); i != myValues.end(); i++) {
265  if ((*i).second == o && name != (*i).first) {
266  v.push_back((*i).first);
267  }
268  }
269  return v;
270 }
271 
272 
273 std::ostream&
274 operator<<(std::ostream& os, const OptionsCont& oc) {
275  std::vector<std::string> done;
276  os << "Options set:" << std::endl;
277  for (OptionsCont::KnownContType::const_iterator i = oc.myValues.begin();
278  i != oc.myValues.end(); i++) {
279  std::vector<std::string>::iterator j = find(done.begin(), done.end(), (*i).first);
280  if (j == done.end()) {
281  std::vector<std::string> synonymes = oc.getSynonymes((*i).first);
282  if (synonymes.size() != 0) {
283  os << (*i).first << " (";
284  for (j = synonymes.begin(); j != synonymes.end(); j++) {
285  if (j != synonymes.begin()) {
286  os << ", ";
287  }
288  os << (*j);
289  }
290  os << ")";
291  } else {
292  os << (*i).first;
293  }
294  if ((*i).second->isSet()) {
295  os << ": " << (*i).second->getValueString() << std::endl;
296  } else {
297  os << ": <INVALID>" << std::endl;
298  }
299  done.push_back((*i).first);
300  copy(synonymes.begin(), synonymes.end(), back_inserter(done));
301  }
302  }
303  return os;
304 }
305 
306 
307 void
308 OptionsCont::relocateFiles(const std::string& configuration) const {
309  for (ItemAddressContType::const_iterator i = myAddresses.begin(); i != myAddresses.end(); i++) {
310  if ((*i)->isFileName() && (*i)->isSet()) {
311  StringTokenizer st((*i)->getString(), ";, ", true);
312  std::string conv;
313  while (st.hasNext()) {
314  if (conv.length() != 0) {
315  conv += ',';
316  }
317  std::string tmp = st.next();
318  if (!FileHelpers::isAbsolute(tmp)) {
319  tmp = FileHelpers::getConfigurationRelative(configuration, tmp);
320  }
321  conv += StringUtils::urlDecode(tmp);
322  }
323  if (conv != (*i)->getString()) {
324  (*i)->set(conv);
325  }
326  }
327  }
328 }
329 
330 
331 bool
332 OptionsCont::isUsableFileList(const std::string& name) const {
333  Option* o = getSecure(name);
334  // check whether the option is set
335  // return false i not
336  if (!o->isSet()) {
337  return false;
338  }
339  // check whether the list of files is valid
340  bool ok = true;
341  std::vector<std::string> files = getStringVector(name);
342  if (files.size() == 0) {
343  WRITE_ERROR("The file list for '" + name + "' is empty.");
344  ok = false;
345  }
346  for (std::vector<std::string>::const_iterator fileIt = files.begin(); fileIt != files.end(); ++fileIt) {
347  if (!FileHelpers::isReadable(*fileIt)) {
348  if (*fileIt != "") {
349  WRITE_ERROR("File '" + *fileIt + "' is not accessible (" + std::strerror(errno) + ").");
350  ok = false;
351  } else {
352  WRITE_WARNING("Empty file name given; ignoring.");
353  }
354  }
355  }
356  return ok;
357 }
358 
359 
360 bool
361 OptionsCont::checkDependingSuboptions(const std::string& name, const std::string& prefix) const {
362  Option* o = getSecure(name);
363  if (o->isSet()) {
364  return true;
365  }
366  bool ok = true;
367  std::vector<std::string> seenSynonymes;
368  for (KnownContType::const_iterator i = myValues.begin(); i != myValues.end(); i++) {
369  if (std::find(seenSynonymes.begin(), seenSynonymes.end(), (*i).first) != seenSynonymes.end()) {
370  continue;
371  }
372  if ((*i).second->isSet() && !(*i).second->isDefault() && (*i).first.find(prefix) == 0) {
373  WRITE_ERROR("Option '" + (*i).first + "' needs option '" + name + "'.");
374  std::vector<std::string> synonymes = getSynonymes((*i).first);
375  std::copy(synonymes.begin(), synonymes.end(), std::back_inserter(seenSynonymes));
376  ok = false;
377  }
378  }
379  return ok;
380 }
381 
382 
383 void
384 OptionsCont::reportDoubleSetting(const std::string& arg) const {
385  std::vector<std::string> synonymes = getSynonymes(arg);
386  std::ostringstream s;
387  s << "A value for the option '" + arg + "' was already set.\n Possible synonymes: ";
388  for (std::vector<std::string>::iterator i = synonymes.begin(); i != synonymes.end();) {
389  s << (*i);
390  i++;
391  if (i != synonymes.end()) {
392  s << ", ";
393  }
394  }
395  WRITE_ERROR(s.str());
396 }
397 
398 
399 std::string
400 OptionsCont::convertChar(char abbr) const {
401  char buf[2];
402  buf[0] = abbr;
403  buf[1] = 0;
404  std::string s(buf);
405  return s;
406 }
407 
408 
409 bool
410 OptionsCont::isBool(const std::string& name) const {
411  Option* o = getSecure(name);
412  return o->isBool();
413 }
414 
415 
416 void
418  for (ItemAddressContType::iterator i = myAddresses.begin(); i != myAddresses.end(); i++) {
419  (*i)->resetWritable();
420  }
421 }
422 
423 
424 bool
425 OptionsCont::isWriteable(const std::string& name) {
426  Option* o = getSecure(name);
427  return o->isWriteable();
428 }
429 
430 
431 void
433  ItemAddressContType::iterator i;
434  for (i = myAddresses.begin(); i != myAddresses.end(); i++) {
435  delete(*i);
436  }
437  myAddresses.clear();
438  myValues.clear();
439  mySubTopics.clear();
440  mySubTopicEntries.clear();
441 }
442 
443 
444 void
445 OptionsCont::addDescription(const std::string& name,
446  const std::string& subtopic,
447  const std::string& description) {
448  Option* o = getSecure(name);
449  assert(o != 0);
450  assert(find(mySubTopics.begin(), mySubTopics.end(), subtopic) != mySubTopics.end());
451  o->setDescription(description);
452  mySubTopicEntries[subtopic].push_back(name);
453 }
454 
455 
456 void
457 OptionsCont::setApplicationName(const std::string& appName,
458  const std::string& fullName) {
459  myAppName = appName;
460  myFullName = fullName;
461 }
462 
463 
464 void
465 OptionsCont::setApplicationDescription(const std::string& appDesc) {
466  myAppDescription = appDesc;
467 }
468 
469 
470 void
471 OptionsCont::addCallExample(const std::string& example, const std::string& desc) {
472  myCallExamples.push_back(std::make_pair(example, desc));
473 }
474 
475 
476 void
477 OptionsCont::setAdditionalHelpMessage(const std::string& add) {
478  myAdditionalMessage = add;
479 }
480 
481 
482 void
483 OptionsCont::addCopyrightNotice(const std::string& copyrightLine) {
484  myCopyrightNotices.push_back(copyrightLine);
485 }
486 
487 
488 void
490  myCopyrightNotices.clear();
491 }
492 
493 
494 void
495 OptionsCont::addOptionSubTopic(const std::string& topic) {
496  mySubTopics.push_back(topic);
497  mySubTopicEntries[topic] = std::vector<std::string>();
498 }
499 
500 
501 void
502 OptionsCont::splitLines(std::ostream& os, std::string what,
503  size_t offset, size_t nextOffset) {
504  while (what.length() > 0) {
505  if (what.length() > 79 - offset) {
506  size_t splitPos = what.rfind(';', 79 - offset);
507  if (splitPos == std::string::npos) {
508  splitPos = what.rfind(' ', 79 - offset);
509  } else {
510  splitPos++;
511  }
512  if (splitPos != std::string::npos) {
513  os << what.substr(0, splitPos) << std::endl;
514  what = what.substr(splitPos);
515  for (size_t r = 0; r < nextOffset + 1; ++r) {
516  os << ' ';
517  }
518  } else {
519  os << what;
520  what = "";
521  }
522  offset = nextOffset;
523  } else {
524  os << what;
525  what = "";
526  }
527  }
528  os << std::endl;
529 }
530 
531 
532 bool
533 OptionsCont::processMetaOptions(bool missingOptions) {
534  if (missingOptions) {
535  // no options are given
536  std::cout << myFullName << std::endl;
537  for (std::vector<std::string>::const_iterator it =
538  myCopyrightNotices.begin(); it != myCopyrightNotices.end(); ++it) {
539  std::cout << " " << *it << std::endl;
540  }
541  std::cout << " License GPLv3+: GNU GPL Version 3 or later <http://gnu.org/licenses/gpl.html>\n";
542  std::cout << " Use --help to get the list of options." << std::endl;
543  return true;
544  }
545 
546  // check whether the help shall be printed
547  if (getBool("help")) {
548  std::cout << myFullName << std::endl;
549  for (std::vector<std::string>::const_iterator it =
550  myCopyrightNotices.begin(); it != myCopyrightNotices.end(); ++it) {
551  std::cout << " " << *it << std::endl;
552  }
553  printHelp(std::cout);
554  return true;
555  }
556  // check whether the help shall be printed
557  if (getBool("version")) {
558  std::cout << myFullName << 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 << "\n" << myFullName << " is part of SUMO.\n";
564  std::cout << "SUMO is free software: you can redistribute it and/or modify\n";
565  std::cout << "it under the terms of the GNU General Public License as published by\n";
566  std::cout << "the Free Software Foundation, either version 3 of the License, or\n";
567  std::cout << "(at your option) any later version.\n\n";
568  std::cout << "This program is distributed in the hope that it will be useful,\n";
569  std::cout << "but WITHOUT ANY WARRANTY; without even the implied warranty of\n";
570  std::cout << "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n";
571  std::cout << "GNU General Public License for more details.\n\n";
572  std::cout << "You should have received a copy of the GNU General Public License\n";
573  std::cout << "along with this program. If not, see http://www.gnu.org/licenses/gpl.html" << std::endl;
574  return true;
575  }
576  // check whether the settings shall be printed
577  if (exists("print-options") && getBool("print-options")) {
578  std::cout << (*this);
579  }
580  // check whether something has to be done with options
581  // whether the current options shall be saved
582  if (isSet("save-configuration", false)) { // sumo-gui does not register these
583  if (getString("save-configuration") == "-" || getString("save-configuration") == "stdout") {
584  writeConfiguration(std::cout, true, false, getBool("save-commented"));
585  return true;
586  }
587  std::ofstream out(getString("save-configuration").c_str());
588  if (!out.good()) {
589  throw ProcessError("Could not save configuration to '" + getString("save-configuration") + "'");
590  } else {
591  writeConfiguration(out, true, false, getBool("save-commented"));
592  if (getBool("verbose")) {
593  WRITE_MESSAGE("Written configuration to '" + getString("save-configuration") + "'");
594  }
595  return true;
596  }
597  }
598  // whether the template shall be saved
599  if (isSet("save-template", false)) { // sumo-gui does not register these
600  if (getString("save-template") == "-" || getString("save-template") == "stdout") {
601  writeConfiguration(std::cout, false, true, getBool("save-commented"));
602  return true;
603  }
604  std::ofstream out(getString("save-template").c_str());
605  if (!out.good()) {
606  throw ProcessError("Could not save template to '" + getString("save-template") + "'");
607  } else {
608  writeConfiguration(out, false, true, getBool("save-commented"));
609  if (getBool("verbose")) {
610  WRITE_MESSAGE("Written template to '" + getString("save-template") + "'");
611  }
612  return true;
613  }
614  }
615  if (isSet("save-schema", false)) { // sumo-gui does not register these
616  if (getString("save-schema") == "-" || getString("save-schema") == "stdout") {
617  writeSchema(std::cout, getBool("save-commented"));
618  return true;
619  }
620  std::ofstream out(getString("save-schema").c_str());
621  if (!out.good()) {
622  throw ProcessError("Could not save schema to '" + getString("save-schema") + "'");
623  } else {
624  writeSchema(out, getBool("save-commented"));
625  if (getBool("verbose")) {
626  WRITE_MESSAGE("Written schema to '" + getString("save-schema") + "'");
627  }
628  return true;
629  }
630  }
631  return false;
632 }
633 
634 void
635 OptionsCont::printHelp(std::ostream& os) {
636  std::vector<std::string>::const_iterator i, j;
637  // print application description
638  splitLines(os, myAppDescription, 0, 0);
639  os << std::endl;
640  // print usage BNF
641  os << "Usage: " << myAppName << " [OPTION]*" << std::endl;
642  // print additional text if any
643  if (myAdditionalMessage.length() > 0) {
644  os << myAdditionalMessage << std::endl << ' ' << std::endl;
645  }
646  // print the options
647  // check their sizes first
648  // we want to know how large the largest not-too-large-entry will be
649  size_t tooLarge = 40;
650  size_t maxSize = 0;
651  for (i = mySubTopics.begin(); i != mySubTopics.end(); ++i) {
652  const std::vector<std::string>& entries = mySubTopicEntries[*i];
653  for (j = entries.begin(); j != entries.end(); ++j) {
654  Option* o = getSecure(*j);
655  // name, two leading spaces and "--"
656  size_t csize = (*j).length() + 2 + 4;
657  // abbreviation length ("-X, "->4chars) if any
658  std::vector<std::string> synonymes = getSynonymes(*j);
659  if (find_if(synonymes.begin(), synonymes.end(), abbreviation_finder()) != synonymes.end()) {
660  csize += 4;
661  }
662  // the type name
663  if (!o->isBool()) {
664  csize += 1 + o->getTypeName().length();
665  }
666  // divider
667  csize += 2;
668  if (csize < tooLarge && maxSize < csize) {
669  maxSize = csize;
670  }
671  }
672  }
673 
674  for (i = mySubTopics.begin(); i != mySubTopics.end(); ++i) {
675  os << *i << " Options:" << std::endl;
676  const std::vector<std::string>& entries = mySubTopicEntries[*i];
677  for (j = entries.begin(); j != entries.end(); ++j) {
678  // start length computation
679  size_t csize = (*j).length() + 2;
680  Option* o = getSecure(*j);
681  os << " ";
682  // write abbreviation if given
683  std::vector<std::string> synonymes = getSynonymes(*j);
684  std::vector<std::string>::iterator a = find_if(synonymes.begin(), synonymes.end(), abbreviation_finder());
685  if (a != synonymes.end()) {
686  os << '-' << (*a) << ", ";
687  csize += 4;
688  }
689  // write leading '-'/"--"
690  os << "--";
691  csize += 2;
692  // write the name
693  os << *j;
694  // write the type if not a bool option
695  if (!o->isBool()) {
696  os << ' ' << o->getTypeName();
697  csize += 1 + o->getTypeName().length();
698  }
699  csize += 2;
700  // write the description formatting it
701  os << " ";
702  for (size_t r = maxSize; r > csize; --r) {
703  os << ' ';
704  }
705  size_t offset = csize > tooLarge ? csize : maxSize;
706  splitLines(os, o->getDescription(), offset, maxSize);
707  }
708  os << std::endl;
709  }
710  os << std::endl;
711  // print usage examples, calc size first
712  if (myCallExamples.size() != 0) {
713  os << "Examples:" << std::endl;
714  for (std::vector<std::pair<std::string, std::string> >::const_iterator e = myCallExamples.begin(); e != myCallExamples.end(); ++e) {
715  os << " " << myAppName << ' ' << e->first << std::endl;
716  os << " " << e->second << std::endl;
717  }
718  }
719  os << std::endl;
720  os << "Report bugs at <http://sumo.dlr.de/trac/>." << std::endl;
721  os << "Get in contact via <sumo@dlr.de>." << std::endl;
722 }
723 
724 
725 void
726 OptionsCont::writeConfiguration(std::ostream& os, bool filled,
727  bool complete, bool addComments) const {
728  os << "<?xml version=\"1.0\"" << SUMOSAXAttributes::ENCODING << "?>\n\n";
729  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;
730  for (std::vector<std::string>::const_iterator i = mySubTopics.begin(); i != mySubTopics.end(); ++i) {
731  std::string subtopic = *i;
732  if (subtopic == "Configuration" && !complete) {
733  continue;
734  }
735  std::replace(subtopic.begin(), subtopic.end(), ' ', '_');
736  std::transform(subtopic.begin(), subtopic.end(), subtopic.begin(), tolower);
737  const std::vector<std::string>& entries = mySubTopicEntries.find(*i)->second;
738  bool hadOne = false;
739  for (std::vector<std::string>::const_iterator j = entries.begin(); j != entries.end(); ++j) {
740  Option* o = getSecure(*j);
741  bool write = complete || (filled && !o->isDefault());
742  if (!write) {
743  continue;
744  }
745  if (!hadOne) {
746  os << " <" << subtopic << ">" << std::endl;
747  }
748  // add the comment if wished
749  if (addComments) {
750  os << " <!-- " << StringUtils::escapeXML(o->getDescription()) << " -->" << std::endl;
751  }
752  // write the option and the value (if given)
753  os << " <" << *j << " value=\"";
754  if (o->isSet() && (filled || o->isDefault())) {
755  os << o->getValueString();
756  }
757  if (complete) {
758  std::vector<std::string> synonymes = getSynonymes(*j);
759  if (!synonymes.empty()) {
760  os << "\" synonymes=\"";
761  for (std::vector<std::string>::const_iterator s = synonymes.begin(); s != synonymes.end(); ++s) {
762  if (s != synonymes.begin()) {
763  os << " ";
764  }
765  os << (*s);
766  }
767  }
768  os << "\" type=\"" << o->getTypeName();
769  if (!addComments) {
770  os << "\" help=\"" << StringUtils::escapeXML(o->getDescription());
771  }
772  }
773  os << "\"/>" << std::endl;
774  // append an endline if a comment was printed
775  if (addComments) {
776  os << std::endl;
777  }
778  hadOne = true;
779  }
780  if (hadOne) {
781  os << " </" << subtopic << ">" << std::endl << std::endl;
782  }
783  }
784  os << "</configuration>" << std::endl;
785 }
786 
787 
788 void
789 OptionsCont::writeSchema(std::ostream& os, bool /* addComments */) {
790  os << "<?xml version=\"1.0\"" << SUMOSAXAttributes::ENCODING << "?>\n\n";
791  os << "<xsd:schema elementFormDefault=\"qualified\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">\n\n";
792  os << " <xsd:include schemaLocation=\"baseTypes.xsd\"/>\n";
793  os << " <xsd:element name=\"configuration\" type=\"configurationType\"/>\n\n";
794  os << " <xsd:complexType name=\"configurationType\">\n";
795  os << " <xsd:all>\n";
796  for (std::vector<std::string>::const_iterator i = mySubTopics.begin(); i != mySubTopics.end(); ++i) {
797  std::string subtopic = *i;
798  if (subtopic == "Configuration") {
799  continue;
800  }
801  std::replace(subtopic.begin(), subtopic.end(), ' ', '_');
802  std::transform(subtopic.begin(), subtopic.end(), subtopic.begin(), tolower);
803  os << " <xsd:element name=\"" << subtopic << "\" type=\"" << subtopic << "Type\" minOccurs=\"0\"/>\n";
804  }
805  os << " </xsd:all>\n";
806  os << " </xsd:complexType>\n\n";
807  for (std::vector<std::string>::const_iterator i = mySubTopics.begin(); i != mySubTopics.end(); ++i) {
808  std::string subtopic = *i;
809  if (subtopic == "Configuration") {
810  continue;
811  }
812  std::replace(subtopic.begin(), subtopic.end(), ' ', '_');
813  std::transform(subtopic.begin(), subtopic.end(), subtopic.begin(), tolower);
814  os << " <xsd:complexType name=\"" << subtopic << "Type\">\n";
815  os << " <xsd:all>\n";
816  const std::vector<std::string>& entries = mySubTopicEntries[*i];
817  for (std::vector<std::string>::const_iterator j = entries.begin(); j != entries.end(); ++j) {
818  Option* o = getSecure(*j);
819  std::string type = o->getTypeName();
820  std::transform(type.begin(), type.end(), type.begin(), tolower);
821  if (type == "int[]") {
822  type = "intArray";
823  }
824  os << " <xsd:element name=\"" << *j << "\" type=\"" << type << "OptionType\" minOccurs=\"0\"/>\n";
825  }
826  os << " </xsd:all>\n";
827  os << " </xsd:complexType>\n\n";
828  }
829  os << "</xsd:schema>\n";
830 }
831 
832 
833 void
834 OptionsCont::writeXMLHeader(std::ostream& os) {
835  time_t rawtime;
836  char buffer [80];
837 
838  os << "<?xml version=\"1.0\"" << SUMOSAXAttributes::ENCODING << "?>\n\n";
839  time(&rawtime);
840  strftime(buffer, 80, "<!-- generated on %c by ", localtime(&rawtime));
841  os << buffer << myFullName << "\n";
842  writeConfiguration(os, true, false, false);
843  os << "-->\n\n";
844 }
845 
846 
847 std::vector<std::string>
848 OptionsCont::getStringVector(const std::string& name) const {
849  Option* o = getSecure(name);
850  std::string def = o->getString();
851  if (def.find(';') != std::string::npos && !myHaveInformedAboutDeprecatedDivider) {
852  WRITE_WARNING("Please note that using ';' as list separator is deprecated.\n From 1.0 onwards, only ',' will be accepted.");
854  }
855  StringTokenizer st(def, ";,", true);
856  std::vector<std::string> ret = st.getVector();
857  for (std::vector<std::string>::iterator i = ret.begin(); i != ret.end(); ++i) {
858  (*i) = StringUtils::prune(*i);
859  }
860  return ret;
861 }
862 
863 
864 bool
865 OptionsCont::isInStringVector(const std::string& optionName,
866  const std::string& itemName) {
867  if (isSet(optionName)) {
868  std::vector<std::string> values = getStringVector(optionName);
869  return find(values.begin(), values.end(), itemName) != values.end();
870  }
871  return false;
872 }
873 
874 
875 /****************************************************************************/
std::string myAppName
some information on the application
Definition: OptionsCont.h:689
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:695
std::string myFullName
Definition: OptionsCont.h:689
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:704
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:169
virtual const std::string & getTypeName() const
Returns the mml-type name of this option.
Definition: Option.cpp:175
static OptionsCont myOptions
The static options container used.
Definition: OptionsCont.h:674
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)
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:200
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:69
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:689
ItemAddressContType myAddresses
Definition: OptionsCont.h:683
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
std::map< std::string, bool > myDeprecatedSynonymes
A map from deprecated options to a bool indicating whether we warned about deprecation.
Definition: OptionsCont.h:701
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:689
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:163
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 (used for non-bool options)
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:712
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:205
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.
KnownContType myValues
Definition: OptionsCont.h:686
std::map< std::string, std::vector< std::string > > mySubTopicEntries
A map from subtopic to option.
Definition: OptionsCont.h:698
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:695
#define SUMOReal
Definition: config.h:214
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:692
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.