| 1 | /* $Id$ $URL$ */ |
|---|
| 2 | #ifndef RCONFIG_DOT_AITCH |
|---|
| 3 | #define RCONFIG_DOT_AITCH |
|---|
| 4 | /**\file rconfig.h |
|---|
| 5 | * \ingroup coreheaders |
|---|
| 6 | * Declarations of the configuration variables go here, within the RConfig |
|---|
| 7 | * namespace. |
|---|
| 8 | * \author Trent Apted <tapted@it.usyd.edu.au> |
|---|
| 9 | * $Revision$ |
|---|
| 10 | * $Date$ |
|---|
| 11 | */ |
|---|
| 12 | |
|---|
| 13 | #include <stdlib.h> |
|---|
| 14 | #include <string> |
|---|
| 15 | #include <vector> |
|---|
| 16 | |
|---|
| 17 | /** The readable configuration parser handle */ |
|---|
| 18 | class RCParser { |
|---|
| 19 | int argc; ///< main()'s argc |
|---|
| 20 | char **argv; ///< main()'s argv |
|---|
| 21 | char **envv; ///< main()'s envv, if present |
|---|
| 22 | public: |
|---|
| 23 | /** Construct the RCParser, with arguments from main() */ |
|---|
| 24 | RCParser(int argc, char** argv, char** envv = 0); |
|---|
| 25 | /** Get the argc, from main() */ |
|---|
| 26 | int getArgc() const; |
|---|
| 27 | /** Get argv[], from main() */ |
|---|
| 28 | char* const * getArgv() const; |
|---|
| 29 | /** get envv[], from main(), NULL if not present */ |
|---|
| 30 | char* const * getEnvv() const; |
|---|
| 31 | }; |
|---|
| 32 | |
|---|
| 33 | /** |
|---|
| 34 | * Configuration variables are kept in the RConfig namespace. |
|---|
| 35 | */ |
|---|
| 36 | |
|---|
| 37 | namespace RConfig { |
|---|
| 38 | |
|---|
| 39 | /** |
|---|
| 40 | * Parse a readable configuration file |
|---|
| 41 | */ |
|---|
| 42 | int rcfile_parse(const char* file = "sharepicrc"); |
|---|
| 43 | |
|---|
| 44 | /** |
|---|
| 45 | * Parse a line of configuration |
|---|
| 46 | */ |
|---|
| 47 | bool rcfile_parseline(const std::string& line); |
|---|
| 48 | |
|---|
| 49 | /** |
|---|
| 50 | * Get the names of stored variables |
|---|
| 51 | */ |
|---|
| 52 | void rcfile_getnames(std::vector<std::string> & names); |
|---|
| 53 | |
|---|
| 54 | /** |
|---|
| 55 | * Get the string value of a stored variable if stored |
|---|
| 56 | */ |
|---|
| 57 | bool rcfile_getvalue(std::string& val, const std::string& name); |
|---|
| 58 | |
|---|
| 59 | //@{ |
|---|
| 60 | /** |
|---|
| 61 | * Tell the RCFile parser to look for config vars of \a name to store in \a ref |
|---|
| 62 | */ |
|---|
| 63 | void rcfile_tell(const char* name, bool &ref, const char* context = 0); |
|---|
| 64 | void rcfile_tell(const char* name, int &ref, const char* context = 0); |
|---|
| 65 | void rcfile_tell(const char* name, float &ref, const char* context = 0); |
|---|
| 66 | void rcfile_tell(const char* name, double &ref, const char* context = 0); |
|---|
| 67 | void rcfile_tell(const char* name, unsigned &ref, const char* context = 0); |
|---|
| 68 | void rcfile_tell(const char* name, std::string &ref, const char* context = 0); |
|---|
| 69 | void rcfile_tellstr(const char* name, std::string &ref, const char* defval = 0, const char* context = 0); |
|---|
| 70 | //void rcfile_tell(const char* name, std::vector<bool> &ref); |
|---|
| 71 | void rcfile_tell(const char* name, std::vector<int> &ref, const char* context = 0); |
|---|
| 72 | void rcfile_tell(const char* name, std::vector<float> &ref, const char* context = 0); |
|---|
| 73 | void rcfile_tell(const char* name, std::vector<double> &ref, const char* context = 0); |
|---|
| 74 | void rcfile_tell(const char* name, std::vector<unsigned> &ref, const char* context = 0); |
|---|
| 75 | void rcfile_tell(const char* name, std::vector<std::string> &ref, const char* context = 0); |
|---|
| 76 | //@} |
|---|
| 77 | |
|---|
| 78 | template<class T, class V> |
|---|
| 79 | T rcfile_initwrap(const char* name, T& ref, const V& initval, const char* context = 0) { |
|---|
| 80 | ref = initval; //this only works for primitive types |
|---|
| 81 | rcfile_tell(name, ref, context); |
|---|
| 82 | return initval; |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | template<class V> |
|---|
| 86 | std::string rcfile_initwrap(const char* name, std::string& ref, const V& initval, const char* context = 0) { |
|---|
| 87 | rcfile_tellstr(name, ref, initval, context); |
|---|
| 88 | return initval; |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | /**\internal RConfig Initialiser */ |
|---|
| 92 | struct Init {static int count; Init(); ~Init();}; |
|---|
| 93 | } |
|---|
| 94 | namespace { |
|---|
| 95 | /**\internal Guarantee initialisation regardless of linking order */ |
|---|
| 96 | RConfig::Init rconfig_init__; |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | /** If the variable has the same name as the config parameter, you can use this macro */ |
|---|
| 100 | #define RCFILE_TELLMACRO(_var) RConfig::rcfile_tell( #_var, _var, __FILE__ ) |
|---|
| 101 | #define RCFILE_CONFIGVAR(_var, _default) _var = RConfig::rcfile_initwrap(#_var, _var, _default, __FILE__) |
|---|
| 102 | |
|---|
| 103 | #endif |
|---|