| 1 | /****************************************************************************** |
| 2 | * |
| 3 | * LDC2 source code |
| 4 | * |
| 5 | * $Date: 2007/03/26 01:00:38 $ |
| 6 | * $Author: hachti $ |
| 7 | * |
| 8 | * $Log: configuration_manager.hh,v $ |
| 9 | * Revision 2.0 2007/03/26 01:00:38 hachti |
| 10 | * *** empty log message *** |
| 11 | * |
| 12 | * |
| 13 | ******************************************************************************/ |
| 14 | |
| 15 | #ifndef ARGUMENT_READER_H |
| 16 | #define ARGUMENT_READER_H |
| 17 | |
| 18 | #include <vector> |
| 19 | #include <string> |
| 20 | |
| 21 | using namespace std; |
| 22 | |
| 23 | /*! |
| 24 | *\brief Hachti's even more wonderful configuration manager. |
| 25 | * |
| 26 | * This class is designed to do all the work with the parameters passed to |
| 27 | * the main() routine of a program.\n |
| 28 | * Additionally, it is able to interpret configuration files. |
| 29 | * Usage:\n |
| 30 | * - Create an instance. |
| 31 | * - Add Parameters/switches with add_option_*(). |
| 32 | * - Add Arguments with add_argument(). |
| 33 | * - Call read_args() with your main routine's arguments. |
| 34 | * The vector returned by read_args() contains all error |
| 35 | * messages. When it's empty, everything is fine! |
| 36 | * - Call get_help() if you want nice formatted help output. |
| 37 | * (You want!!!) |
| 38 | * Sould be easy to use.... |
| 39 | */ |
| 40 | class configuration_manager{ |
| 41 | |
| 42 | protected: // Types |
| 43 | /*! |
| 44 | *\brief Container for an option switch |
| 45 | */ |
| 46 | class opt_switch_t{ |
| 47 | public: |
| 48 | opt_switch_t (const string & shortname, |
| 49 | const string & longname, |
| 50 | const string & description, |
| 51 | int * status, |
| 52 | const bool & allow_commandline=true, |
| 53 | const bool & allow_config_file=true); |
| 54 | string shortname; |
| 55 | string longname; |
| 56 | int * status; |
| 57 | string * target; |
| 58 | string description; |
| 59 | string placeholder; |
| 60 | bool allow_conffile; |
| 61 | bool allow_cmdline; |
| 62 | }; |
| 63 | |
| 64 | /*! |
| 65 | *\brief Container for an option value |
| 66 | */ |
| 67 | class opt_value_t{ |
| 68 | public: |
| 69 | opt_value_t (const string & shortname, |
| 70 | const string & longname, |
| 71 | const string & description, |
| 72 | int * status, |
| 73 | string * target, |
| 74 | const string & placeholder=string("<string>"), |
| 75 | const bool & allow_commandline=true, |
| 76 | const bool & allow_config_file=true); |
| 77 | string shortname; |
| 78 | string longname; |
| 79 | int * status; |
| 80 | string * target; |
| 81 | string description; |
| 82 | string placeholder; |
| 83 | bool allow_conffile; |
| 84 | bool allow_cmdline; |
| 85 | }; |
| 86 | |
| 87 | /*! |
| 88 | *\brief Container for a commandline argument |
| 89 | */ |
| 90 | class cmd_arg_t{ |
| 91 | public: |
| 92 | cmd_arg_t (const string & placeholder, |
| 93 | const string & description, |
| 94 | int * status, |
| 95 | string * target); |
| 96 | int * status; |
| 97 | string * target; |
| 98 | string description; |
| 99 | string placeholder; |
| 100 | }; |
| 101 | |
| 102 | public: // Methods |
| 103 | |
| 104 | configuration_manager(string name); |
| 105 | |
| 106 | void add_option_value (const string & shortname, |
| 107 | const string & longname, |
| 108 | const string & description, |
| 109 | int *status, |
| 110 | string *target=NULL, |
| 111 | const string & placeholder=string("<string>"), |
| 112 | const bool & allow_commandline=true, |
| 113 | const bool & allow_config_file=true |
| 114 | ); |
| 115 | |
| 116 | void add_option_switch (const string & shortname, |
| 117 | const string & longname, |
| 118 | const string & description, |
| 119 | int *status, |
| 120 | const bool & allow_commandline=true, |
| 121 | const bool & allow_config_file=true |
| 122 | ); |
| 123 | |
| 124 | void add_argument (const string & description, |
| 125 | int *status, |
| 126 | string *target=NULL, |
| 127 | const string & placeholder=string("<string>") |
| 128 | ); |
| 129 | |
| 130 | vector<string> read_args(int argc, char ** args); |
| 131 | vector<string> read_file(string filename); |
| 132 | |
| 133 | void get_help (vector<string> & target); |
| 134 | vector<string> get_help(); |
| 135 | |
| 136 | protected: // members |
| 137 | vector<opt_switch_t>option_switches; |
| 138 | vector<opt_value_t>option_values; |
| 139 | vector<cmd_arg_t>cmd_args; |
| 140 | string app_name; |
| 141 | |
| 142 | protected: // methods |
| 143 | string analyse_string(const string & line); |
| 144 | bool analyse_bool(const string & data); |
| 145 | bool analyse_bool_false(const string & data); |
| 146 | }; // class configuration_manager |
| 147 | |
| 148 | |
| 149 | #endif |
| 150 | |