| 1 | // -------------------------------------------------------------------------- |
|---|
| 2 | // |
|---|
| 3 | // File |
|---|
| 4 | // Name: Configuration |
|---|
| 5 | // Purpose: Reading configuration files |
|---|
| 6 | // Created: 2003/07/23 |
|---|
| 7 | // |
|---|
| 8 | // -------------------------------------------------------------------------- |
|---|
| 9 | |
|---|
| 10 | #ifndef CONFIGURATION__H |
|---|
| 11 | #define CONFIGURATION__H |
|---|
| 12 | |
|---|
| 13 | #include <map> |
|---|
| 14 | #include <list> |
|---|
| 15 | #include <vector> |
|---|
| 16 | #include <string> |
|---|
| 17 | #include <memory> |
|---|
| 18 | |
|---|
| 19 | // For defining tests |
|---|
| 20 | enum |
|---|
| 21 | { |
|---|
| 22 | ConfigTest_LastEntry = 1, |
|---|
| 23 | ConfigTest_Exists = 2, |
|---|
| 24 | ConfigTest_IsInt = 4, |
|---|
| 25 | ConfigTest_IsUint32 = 8, |
|---|
| 26 | ConfigTest_MultiValueAllowed = 16, |
|---|
| 27 | ConfigTest_IsBool = 32 |
|---|
| 28 | }; |
|---|
| 29 | |
|---|
| 30 | class ConfigurationVerifyKey |
|---|
| 31 | { |
|---|
| 32 | public: |
|---|
| 33 | typedef enum |
|---|
| 34 | { |
|---|
| 35 | NoDefaultValue = 1 |
|---|
| 36 | } NoDefaultValue_t; |
|---|
| 37 | |
|---|
| 38 | ConfigurationVerifyKey(std::string name, int flags, |
|---|
| 39 | void *testFunction = NULL); |
|---|
| 40 | // to allow passing ConfigurationVerifyKey::NoDefaultValue |
|---|
| 41 | // for default ListenAddresses |
|---|
| 42 | ConfigurationVerifyKey(std::string name, int flags, |
|---|
| 43 | NoDefaultValue_t t, void *testFunction = NULL); |
|---|
| 44 | ConfigurationVerifyKey(std::string name, int flags, |
|---|
| 45 | std::string defaultValue, void *testFunction = NULL); |
|---|
| 46 | ConfigurationVerifyKey(std::string name, int flags, |
|---|
| 47 | const char* defaultValue, void *testFunction = NULL); |
|---|
| 48 | ConfigurationVerifyKey(std::string name, int flags, |
|---|
| 49 | int defaultValue, void *testFunction = NULL); |
|---|
| 50 | ConfigurationVerifyKey(std::string name, int flags, |
|---|
| 51 | bool defaultValue, void *testFunction = NULL); |
|---|
| 52 | const std::string& Name() const { return mName; } |
|---|
| 53 | const std::string& DefaultValue() const { return mDefaultValue; } |
|---|
| 54 | const bool HasDefaultValue() const { return mHasDefaultValue; } |
|---|
| 55 | const int Flags() const { return mFlags; } |
|---|
| 56 | const void* TestFunction() const { return mTestFunction; } |
|---|
| 57 | ConfigurationVerifyKey(const ConfigurationVerifyKey& rToCopy); |
|---|
| 58 | |
|---|
| 59 | private: |
|---|
| 60 | ConfigurationVerifyKey& operator=(const ConfigurationVerifyKey& |
|---|
| 61 | noAssign); |
|---|
| 62 | |
|---|
| 63 | std::string mName; // "*" for all other keys (not implemented yet) |
|---|
| 64 | std::string mDefaultValue; // default for when it's not present |
|---|
| 65 | bool mHasDefaultValue; |
|---|
| 66 | int mFlags; |
|---|
| 67 | void *mTestFunction; // set to zero for now, will implement later |
|---|
| 68 | }; |
|---|
| 69 | |
|---|
| 70 | class ConfigurationVerify |
|---|
| 71 | { |
|---|
| 72 | public: |
|---|
| 73 | std::string mName; // "*" for all other sub config names |
|---|
| 74 | const ConfigurationVerify *mpSubConfigurations; |
|---|
| 75 | const ConfigurationVerifyKey *mpKeys; |
|---|
| 76 | int Tests; |
|---|
| 77 | void *TestFunction; // set to zero for now, will implement later |
|---|
| 78 | }; |
|---|
| 79 | |
|---|
| 80 | class FdGetLine; |
|---|
| 81 | |
|---|
| 82 | // -------------------------------------------------------------------------- |
|---|
| 83 | // |
|---|
| 84 | // Class |
|---|
| 85 | // Name: Configuration |
|---|
| 86 | // Purpose: Loading, checking, and representing configuration files |
|---|
| 87 | // Created: 2003/07/23 |
|---|
| 88 | // |
|---|
| 89 | // -------------------------------------------------------------------------- |
|---|
| 90 | class Configuration |
|---|
| 91 | { |
|---|
| 92 | public: |
|---|
| 93 | Configuration(const std::string &rName); |
|---|
| 94 | Configuration(const Configuration &rToCopy); |
|---|
| 95 | ~Configuration(); |
|---|
| 96 | |
|---|
| 97 | enum |
|---|
| 98 | { |
|---|
| 99 | // The character to separate multi-values |
|---|
| 100 | MultiValueSeparator = '\x01' |
|---|
| 101 | }; |
|---|
| 102 | |
|---|
| 103 | static std::auto_ptr<Configuration> LoadAndVerify( |
|---|
| 104 | const std::string& rFilename, |
|---|
| 105 | const ConfigurationVerify *pVerify, |
|---|
| 106 | std::string &rErrorMsg); |
|---|
| 107 | |
|---|
| 108 | static std::auto_ptr<Configuration> Load( |
|---|
| 109 | const std::string& rFilename, |
|---|
| 110 | std::string &rErrorMsg) |
|---|
| 111 | { return LoadAndVerify(rFilename, 0, rErrorMsg); } |
|---|
| 112 | |
|---|
| 113 | bool KeyExists(const std::string& rKeyName) const; |
|---|
| 114 | const std::string &GetKeyValue(const std::string& rKeyName) const; |
|---|
| 115 | int GetKeyValueInt(const std::string& rKeyName) const; |
|---|
| 116 | uint32_t GetKeyValueUint32(const std::string& rKeyName) const; |
|---|
| 117 | bool GetKeyValueBool(const std::string& rKeyName) const; |
|---|
| 118 | std::vector<std::string> GetKeyNames() const; |
|---|
| 119 | |
|---|
| 120 | bool SubConfigurationExists(const std::string& rSubName) const; |
|---|
| 121 | const Configuration &GetSubConfiguration(const std::string& rSubName) const; |
|---|
| 122 | Configuration &GetSubConfigurationEditable(const std::string& rSubName); |
|---|
| 123 | std::vector<std::string> GetSubConfigurationNames() const; |
|---|
| 124 | |
|---|
| 125 | void AddKeyValue(const std::string& rKey, const std::string& rValue); |
|---|
| 126 | void AddSubConfig(const std::string& rName, const Configuration& rSubConfig); |
|---|
| 127 | |
|---|
| 128 | bool Verify(const ConfigurationVerify &rVerify, std::string &rErrorMsg) |
|---|
| 129 | { |
|---|
| 130 | return Verify(rVerify, std::string(), rErrorMsg); |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | private: |
|---|
| 134 | std::string mName; |
|---|
| 135 | // Order of keys not preserved |
|---|
| 136 | std::map<std::string, std::string> mKeys; |
|---|
| 137 | // Order of sub blocks preserved |
|---|
| 138 | typedef std::list<std::pair<std::string, Configuration> > SubConfigListType; |
|---|
| 139 | SubConfigListType mSubConfigurations; |
|---|
| 140 | |
|---|
| 141 | static bool LoadInto(Configuration &rConfig, FdGetLine &rGetLine, std::string &rErrorMsg, bool RootLevel); |
|---|
| 142 | bool Verify(const ConfigurationVerify &rVerify, const std::string &rLevel, |
|---|
| 143 | std::string &rErrorMsg); |
|---|
| 144 | }; |
|---|
| 145 | |
|---|
| 146 | #endif // CONFIGURATION__H |
|---|
| 147 | |
|---|