/* * Copyright (C) Roland Jax 2012-2014 * * This file is part of ebusd. * * ebusd is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * ebusd is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with ebusd. If not, see http://www.gnu.org/licenses/. */ #ifndef LIBUTILS_APPL_H_ #define LIBUTILS_APPL_H_ #include #include #include #include /** \file appl.h */ using namespace std; /** the available data types. */ enum DataType { dt_none, //!< default for __text_only__ dt_bool, //!< boolean dt_hex, //!< hex integer dt_int, //!< dec integer dt_long, //!< long dt_float, //!< float dt_string //!< string }; /** option types. */ enum OptionType { ot_none, //!< no option type is needed ot_optional, //!< a value is optional ot_mandatory //!< a value is mandatory }; /** structure for defining application options */ typedef struct { const char* name; //!< long option name const char* shortname; //!< short option name const char* description; //!< description for this option DataType datatype; //!< data type for this option OptionType optiontype; //!< indicates whether an option takes an argument } opt_t; /** * union for option values */ union OptVal { /** boolean */ bool b; /** integer */ int i; /** long */ long l; /** float */ float f; /** string */ const char* c; /** * clear memory */ OptVal() { memset(this, 0, sizeof(OptVal)); } /** * create boolean type * @param _b the boolean */ OptVal(bool _b) : b(_b) {} /** * create integer type * @param _i the integer */ OptVal(int _i) : i(_i) {} /** * create long type * @param _l the long */ OptVal(long _l) : l(_l) {} /** * create float type * @param _f the float */ OptVal(float _f) : f(_f) {} /** * create string type * @param _c the string */ OptVal(const char* _c) : c(_c) {} }; /** * class for all kinds of application parameters. */ class Appl { public: /** * create an instance and return the reference. * @param command string for help page. * @param argument string for help page. * @return the reference to instance. */ static Appl& Instance(const char* command="", const char* argument=""); /** * destructor. */ ~Appl(); /** * save application version string. * @param version string. */ void setVersion(const char* version) { m_version = version; } /** * create new entry of application option only for help page. * @param text string to print. */ void addText(const char* text); /** * create new entry of application option. * @param name the long name. * @param shortname optional short name. * @param optval value of option. * @param datatype data type of option value. * @param optiontype type of given option. * @param description hint text for help page. */ void addOption(const char* name, const char* shortname, OptVal optval, DataType datatype, OptionType optiontype, const char* description); /** * returns the value of the interested option. * @param name the interested option. * @return casted value. */ template T getOptVal(const char* name) { ov_it = m_optvals.find(name); return (reinterpret_cast(ov_it->second)); } /** * parse application arguments. * @param argc the number of options. * @param argv the given options. * @return false if the arguments are invalid or a help/error message was printed and the process should exit. */ bool parseArgs(int argc, char* argv[]); /** * returns the number of saved commands and arguments. * @return number of commands and arguments. */ int numArgs() const { return m_arguments.size(); } /** * returns the string of an interested argument. * @param num number of interested argument. * @return the argument string. */ string getArg(const int num) const { return m_arguments[num]; } /** * returns the string of given command. * @return the command string. */ string getCommand() const { return m_command; } /** * Get whether a command string is missing. * @return true if a command string is missing. */ bool missingCommand() const { return (m_command.size() == 0 ? true : false); } private: /** * private construtor. * @param command string for help page. * @param argument string for help page. */ Appl(const char* command, const char* argument) : m_version(NULL), m_withCommand(command), m_withArgument(argument) {} /** * private copy construtor. * @param reference to an instance. */ Appl(const Appl&); /** * private = operator. * @param reference to an instance. * @return reference to instance. */ Appl& operator=(const Appl&); /** application options */ vector m_opts; /** application options iterator */ vector::const_iterator o_it; /** map option - value */ map m_optvals; /** map option - value iterator */ map::iterator ov_it; /** given arguments */ vector m_argv; /** application version string */ const char* m_version; /** command string for help page */ const char* m_withCommand; /** argument string for help page */ const char* m_withArgument; /** command (argument 0 = command) string */ string m_command; /** arguments (argument >= 1) string */ vector m_arguments; /** * checks the passed parameter if this is a valid option. * @param option to check. * @param value to save. * @return true if parameter is a valid option. */ bool checkOption(const string& option, const string& value); /** * save the passed value to option. * @param option name. * @param value to save. * @param datatype of given option. */ void setOptVal(const char* option, const string value, DataType datatype); /** * print application version. * @return false. */ bool printVersion(); /** * print help page. * @return false. */ bool printHelp(); /** * print used option settings. * @return false. */ bool printSettings(); }; #endif // LIBUTILS_APPL_H_