bool OptionsCont::isUsableFileList(const std::string &name) const throw(InvalidArgument) { Option *o = getSecure(name); // check whether the option is set // return false i not if (!o->isSet()) { return false; } // check whether the list of files is valid bool ok = true; std::vector<std::string> files = getStringVector(name); if (files.size()==0) { MsgHandler::getErrorInstance()->inform("The file list for '" + name + "' is empty."); ok = false; } for (std::vector<std::string>::const_iterator fileIt=files.begin(); fileIt!=files.end(); ++fileIt) { if (!FileHelpers::exists(*fileIt)) { if (*fileIt!="") { MsgHandler::getErrorInstance()->inform("File '" + *fileIt + "' does not exist."); ok = false; } else { MsgHandler::getWarningInstance()->inform("Empty file name given; ignoring."); } } } return ok; }
bool OptionsCont::isUsableFileList(const std::string& name) const { Option* o = getSecure(name); // check whether the option is set // return false i not if (!o->isSet()) { return false; } // check whether the list of files is valid bool ok = true; std::vector<std::string> files = getStringVector(name); if (files.size() == 0) { WRITE_ERROR("The file list for '" + name + "' is empty."); ok = false; } for (std::vector<std::string>::const_iterator fileIt = files.begin(); fileIt != files.end(); ++fileIt) { if (!FileHelpers::isReadable(*fileIt)) { if (*fileIt != "") { WRITE_ERROR("File '" + *fileIt + "' is not accessible (" + std::strerror(errno) + ")."); ok = false; } else { WRITE_WARNING("Empty file name given; ignoring."); } } } return ok; }
void OptionsCont::writeConfiguration(std::ostream &os, bool filled, bool complete, bool addComments) throw() { std::vector<std::string>::const_iterator i, j; os << "<configuration>" << std::endl << std::endl; for (i=mySubTopics.begin(); i!=mySubTopics.end(); ++i) { std::string subtopic = *i; if (subtopic=="Configuration") { continue; } for (size_t k=0; k<subtopic.length(); ++k) { if (subtopic[k]==' ') { subtopic[k] = '_'; } if (subtopic[k]>='A'&&subtopic[k]<='Z') { subtopic[k] = subtopic[k] - 'A' + 'a'; } } const std::vector<std::string> &entries = mySubTopicEntries[*i]; bool hadOne = false; for (j=entries.begin(); j!=entries.end(); ++j) { Option *o = getSecure(*j); bool write = complete || (filled&&!o->isDefault()); if (!write) { continue; } if (!hadOne) { os << " <" << subtopic << ">" << std::endl; } // add the comment if wished if (addComments) { os << " <!-- " << o->getDescription() << " -->" << std::endl; } // write the option and the value (if given) os << " <" << *j << " value=\""; if (o->isSet()) { os << o->getValueString(); } os << "\"/>" << std::endl; // append an endline if a comment was printed if (addComments) { os << std::endl; } hadOne = true; } if (hadOne) { os << " </" << subtopic << ">" << std::endl << std::endl; } } os << "</configuration>" << std::endl; }
bool OptionsCont::checkDependingSuboptions(const std::string &name, const std::string &prefix) const throw(InvalidArgument) { Option *o = getSecure(name); if (o->isSet()) { return true; } bool ok = true; for (KnownContType::const_iterator i=myValues.begin(); i!=myValues.end(); i++) { if ((*i).second->isSet() && !(*i).second->isDefault() && (*i).first.find(prefix) == 0) { MsgHandler::getErrorInstance()->inform("Option '" + (*i).first + "' needs option '" + name + "'."); ok = false; } } return ok; }
bool OptionsCont::checkDependingSuboptions(const std::string& name, const std::string& prefix) const { Option* o = getSecure(name); if (o->isSet()) { return true; } bool ok = true; std::vector<std::string> seenSynonymes; for (KnownContType::const_iterator i = myValues.begin(); i != myValues.end(); i++) { if (std::find(seenSynonymes.begin(), seenSynonymes.end(), (*i).first) != seenSynonymes.end()) { continue; } if ((*i).second->isSet() && !(*i).second->isDefault() && (*i).first.find(prefix) == 0) { WRITE_ERROR("Option '" + (*i).first + "' needs option '" + name + "'."); std::vector<std::string> synonymes = getSynonymes((*i).first); std::copy(synonymes.begin(), synonymes.end(), std::back_inserter(seenSynonymes)); ok = false; } } return ok; }
int OptionsList::parse(int &aArgc, const char **aArgv) { sort(); // This assumes that the first option (app name) has already been removed. int order = 0, count = 0; program_ = "agent"; Option *opt; const char **argp = aArgv; const char *cp; while (aArgc > 0) { if (**argp == '-') { cp = (*argp) + 1; bool next = false; while (*cp != 0 && !next) { if (find(cp, opt)) { count++; if (opt->hasArgument()) { getArg(argp, aArgc, opt, cp); next = true; } else { if (opt->type_ != Option::eBoolean) { cerr << "Bad argument definition: " << opt->getName() << endl; } else if (opt->isSet_) { cerr << "Option " << opt->getName() << " is already specified" << endl; usage(); } else { *(opt->boolPtr_) = true; } cp += strlen(opt->getName()); } opt->isSet_ = true; } else { cerr << "Bad argument:" << *argp << endl; usage(); } } } else { if (find(order, opt)) { if (!opt->setValue(*argp)) usage(); count++; } else if (find(-1, opt)) { if (!opt->setValue(*argp)) usage(); count++; } order++; } argp++; aArgc--; } for (list<Option>::iterator iter = begin(); iter != end(); iter++) { opt = &(*iter); if (opt->isRequired() && !opt->isSet()) { if (opt->getName() != NULL) cerr << "Required option -" << opt->getName() << " is not specified" << endl; else cerr << "Required option <" << opt->getArgDesc() << "> is not specified" << endl; usage(); } } return count; }
void OptionsCont::writeConfiguration(std::ostream& os, bool filled, bool complete, bool addComments) const { os << "<?xml version=\"1.0\"" << SUMOSAXAttributes::ENCODING << "?>\n\n"; 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; for (std::vector<std::string>::const_iterator i = mySubTopics.begin(); i != mySubTopics.end(); ++i) { std::string subtopic = *i; if (subtopic == "Configuration" && !complete) { continue; } std::replace(subtopic.begin(), subtopic.end(), ' ', '_'); std::transform(subtopic.begin(), subtopic.end(), subtopic.begin(), tolower); const std::vector<std::string>& entries = mySubTopicEntries.find(*i)->second; bool hadOne = false; for (std::vector<std::string>::const_iterator j = entries.begin(); j != entries.end(); ++j) { Option* o = getSecure(*j); bool write = complete || (filled && !o->isDefault()); if (!write) { continue; } if (!hadOne) { os << " <" << subtopic << ">" << std::endl; } // add the comment if wished if (addComments) { os << " <!-- " << StringUtils::escapeXML(o->getDescription()) << " -->" << std::endl; } // write the option and the value (if given) os << " <" << *j << " value=\""; if (o->isSet() && (filled || o->isDefault())) { os << o->getValueString(); } if (complete) { std::vector<std::string> synonymes = getSynonymes(*j); if (!synonymes.empty()) { os << "\" synonymes=\""; for (std::vector<std::string>::const_iterator s = synonymes.begin(); s != synonymes.end(); ++s) { if (s != synonymes.begin()) { os << " "; } os << (*s); } } os << "\" type=\"" << o->getTypeName(); if (!addComments) { os << "\" help=\"" << StringUtils::escapeXML(o->getDescription()); } } os << "\"/>" << std::endl; // append an endline if a comment was printed if (addComments) { os << std::endl; } hadOne = true; } if (hadOne) { os << " </" << subtopic << ">" << std::endl << std::endl; } } os << "</configuration>" << std::endl; }