From ea9b2a33a10baafc10db1cf430262879712cb4c4 Mon Sep 17 00:00:00 2001 From: gimmedonutnow Date: Mon, 14 Aug 2006 22:35:14 +0000 Subject: [PATCH] - added a way to have options like '-h, --help' by having the user of the class specify a function that is called when such options are seen --- src/drivers/common/configSys.cpp | 70 +++++++++++++++++++++++++++----- src/drivers/common/configSys.h | 17 +++++--- 2 files changed, 70 insertions(+), 17 deletions(-) diff --git a/src/drivers/common/configSys.cpp b/src/drivers/common/configSys.cpp index a40f12b8..aff9b045 100644 --- a/src/drivers/common/configSys.cpp +++ b/src/drivers/common/configSys.cpp @@ -36,6 +36,9 @@ Config::_addOption(char shortArg, case(STRING): _strOptMap[name] = ""; break; + case(FUNCTION): + _fnOptMap[name] = NULL; + break; default: break; } @@ -55,13 +58,12 @@ int Config::addOption(char shortArg, const std::string &longArg, const std::string &name, - int type, int defaultValue) { int error; // add the option to the config system - error = _addOption(shortArg, longArg, name, type); + error = _addOption(shortArg, longArg, name, INTEGER); if(error) { return error; } @@ -85,13 +87,12 @@ int Config::addOption(char shortArg, const std::string &longArg, const std::string &name, - int type, const std::string &defaultValue) { int error; // add the option to the config system - error = _addOption(shortArg, longArg, name, type); + error = _addOption(shortArg, longArg, name, STRING); if(error) { return error; } @@ -105,6 +106,30 @@ Config::addOption(char shortArg, return 0; } +int +Config::addOption(char shortArg, + const std::string &longArg, + const std::string &name, + void (*defaultFn)(void)) +{ + int error; + + // add the option to the config system + error = _addOption(shortArg, longArg, name, FUNCTION); + if(error) { + return error; + } + + // set the option to the default value + error = setOption(name, defaultFn); + if(error) { + return error; + } + + return 0; +} + + /** * Sets the specified option to the given integer value. */ @@ -145,6 +170,27 @@ Config::setOption(const std::string &name, return 0; } +/** + * Sets the specified option to the given function. + */ +int +Config::setOption(const std::string &name, + void (*value)(void)) +{ + std::map::iterator opt_i; + + // confirm that the option exists + opt_i = _fnOptMap.find(name); + if(opt_i == _fnOptMap.end()) { + return -1; + } + + // set the option + opt_i->second = value; + return 0; +} + + int Config::getOption(const std::string &name, std::string *value) @@ -196,7 +242,6 @@ Config::getOption(const std::string &name, return 0; } - /** * Parses the command line arguments. Short args are of the form -f * , long args are of the form --foo . Returns < 0 on error, @@ -210,6 +255,7 @@ Config::_parseArgs(int argc, std::map::iterator long_i, str_i; std::map::iterator short_i; std::map::iterator int_i; + std::map::iterator fn_i; std::string arg, opt, value; for(int i = 1; i < argc; i++) { @@ -246,6 +292,13 @@ Config::_parseArgs(int argc, opt = short_i->second; } + // check if its a function, and if so, call the function + fn_i = _fnOptMap.find(opt); + if(fn_i != _fnOptMap.end()) { + (*(fn_i->second))(); + continue; + } + // make sure we've got a value if(i + 1 >= argc) { // XXX missing value @@ -288,12 +341,7 @@ Config::parse(int argc, } // parse the arguments - error = _parseArgs(argc, argv); - if(error) { - return error; - } - - return 0; + return _parseArgs(argc, argv); } diff --git a/src/drivers/common/configSys.h b/src/drivers/common/configSys.h index da72c959..798f90d9 100644 --- a/src/drivers/common/configSys.h +++ b/src/drivers/common/configSys.h @@ -8,8 +8,9 @@ class Config { private: std::string _dir; - std::map _strOptMap; - std::map _intOptMap; + std::map _strOptMap; + std::map _intOptMap; + std::map _fnOptMap; std::map _shortArgMap; std::map _longArgMap; @@ -20,8 +21,9 @@ private: int _parseArgs(int, char **); public: - const static int STRING = 1; - const static int INTEGER = 2; + const static int STRING = 1; + const static int INTEGER = 2; + const static int FUNCTION = 3; public: Config(std::string d) : _dir(d) { } @@ -32,15 +34,18 @@ public: * parse(). */ int addOption(char, const std::string &, - const std::string &, int, int); + const std::string &, int); int addOption(char, const std::string &, - const std::string &, int, const std::string &); + const std::string &, const std::string &); + int addOption(char, const std::string &, + const std::string &, void (*)(void)); /** * Sets a configuration option. Can be called at any time. */ int setOption(const std::string &, int); int setOption(const std::string &, const std::string &); + int setOption(const std::string &, void (*)(void)); int getOption(const std::string &, std::string *); int getOption(const std::string &, const char **);