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