From: James Turner Date: Thu, 24 Mar 2016 15:05:03 +0000 (+0000) Subject: New options setting/clearing helpers. X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=88bfaca2b3d4034321ac786f06b074e5590735ee;p=flightgear.git New options setting/clearing helpers. --- diff --git a/src/Main/options.cxx b/src/Main/options.cxx index bd1a91e07..6ae8d9a62 100644 --- a/src/Main/options.cxx +++ b/src/Main/options.cxx @@ -1769,6 +1769,22 @@ public: return it; // not found } + + OptionValueVec::iterator findValue(const string& key) + { + OptionValueVec::iterator it = values.begin(); + for (; it != values.end(); ++it) { + if (!it->desc) { + continue; // ignore markers + } + + if (it->desc->option == key) { + return it; + } + } // of set values iteration + + return it; // not found + } OptionDesc* findOption(const string& key) const { @@ -2202,7 +2218,35 @@ int Options::addOption(const string &key, const string &value) p->values.push_back(OptionValue(desc, value)); return FG_OPTIONS_OK; } - + +int Options::setOption(const string &key, const string &value) +{ + OptionDesc* desc = p->findOption(key); + if (!desc) { + flightgear::modalMessageBox("Unknown option", "Unknown command-line option: " + key); + return FG_OPTIONS_ERROR; + } + + if (!(desc->type & OPTION_MULTI)) { + OptionValueVec::const_iterator it = p->findValue(key); + if (it != p->values.end()) { + // remove existing valye + p->values.erase(it); + } + } + + p->values.push_back(OptionValue(desc, value)); + return FG_OPTIONS_OK; +} + +void Options::clearOption(const std::string& key) +{ + OptionValueVec::iterator it = p->findValue(key); + for (; it != p->values.end(); it = p->findValue(key)) { + p->values.erase(it); + } +} + bool Options::isOptionSet(const string &key) const { OptionValueVec::const_iterator it = p->findValue(key); diff --git a/src/Main/options.hxx b/src/Main/options.hxx index f6f23b0d4..4f618213b 100644 --- a/src/Main/options.hxx +++ b/src/Main/options.hxx @@ -101,7 +101,14 @@ public: * This can be used to inject option values, eg based upon environment variables */ int addOption(const std::string& key, const std::string& value); - + + /** + * set an option, overwriting any existing value which might be set + */ + int setOption(const std::string& key, const std::string& value); + + void clearOption(const std::string& key); + /** * apply option values to the simulation state * (set properties, etc).