[245] | 1 | /* ---------------------------------------------------------------------------- |
---|
| 2 | libconfig - A library for processing structured configuration files |
---|
[691] | 3 | Copyright (C) 2005-2009 Mark A Lindner |
---|
| 4 | |
---|
[245] | 5 | This file is part of libconfig. |
---|
[691] | 6 | |
---|
[245] | 7 | This library is free software; you can redistribute it and/or |
---|
| 8 | modify it under the terms of the GNU Lesser General Public License |
---|
| 9 | as published by the Free Software Foundation; either version 2.1 of |
---|
| 10 | the License, or (at your option) any later version. |
---|
[691] | 11 | |
---|
[245] | 12 | This library is distributed in the hope that it will be useful, but |
---|
| 13 | WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
| 15 | Lesser General Public License for more details. |
---|
[691] | 16 | |
---|
[245] | 17 | You should have received a copy of the GNU Library General Public |
---|
| 18 | License along with this library; if not, see |
---|
| 19 | <http://www.gnu.org/licenses/>. |
---|
| 20 | ---------------------------------------------------------------------------- |
---|
| 21 | */ |
---|
| 22 | |
---|
| 23 | #ifndef __libconfig_hpp |
---|
| 24 | #define __libconfig_hpp |
---|
| 25 | |
---|
| 26 | #include <stdio.h> |
---|
| 27 | #include <string> |
---|
| 28 | #include <map> |
---|
| 29 | |
---|
[691] | 30 | |
---|
| 31 | #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) |
---|
| 32 | #if defined(LIBCONFIGXX_STATIC) |
---|
| 33 | #define LIBCONFIGXX_API |
---|
| 34 | #elif defined(LIBCONFIGXX_EXPORTS) |
---|
| 35 | #define LIBCONFIGXX_API __declspec(dllexport) |
---|
| 36 | #else /* ! LIBCONFIGXX_EXPORTS */ |
---|
| 37 | #define LIBCONFIGXX_API __declspec(dllimport) |
---|
| 38 | #endif /* LIBCONFIGXX_STATIC */ |
---|
| 39 | #else /* ! WIN32 */ |
---|
| 40 | #define LIBCONFIGXX_API |
---|
| 41 | #endif /* WIN32 */ |
---|
| 42 | |
---|
| 43 | |
---|
| 44 | #define LIBCONFIGXX_VER_MAJOR 1 |
---|
| 45 | #define LIBCONFIGXX_VER_MINOR 4 |
---|
| 46 | #define LIBCONFIGXX_VER_REVISION 0 |
---|
| 47 | |
---|
| 48 | struct config_t; // fwd decl |
---|
| 49 | struct config_setting_t; // fwd decl |
---|
| 50 | |
---|
| 51 | namespace libconfig { |
---|
| 52 | |
---|
| 53 | class LIBCONFIGXX_API ConfigException : public std::exception { }; |
---|
| 54 | |
---|
| 55 | class Setting; // fwd decl |
---|
| 56 | |
---|
| 57 | class LIBCONFIGXX_API SettingException : public ConfigException |
---|
[245] | 58 | { |
---|
[691] | 59 | friend class Config; |
---|
[245] | 60 | |
---|
[691] | 61 | public: |
---|
[245] | 62 | |
---|
[691] | 63 | SettingException(const SettingException &other); |
---|
| 64 | SettingException& operator=(const SettingException &other); |
---|
[245] | 65 | |
---|
[691] | 66 | virtual ~SettingException() throw(); |
---|
[245] | 67 | |
---|
[691] | 68 | const char *getPath() const; |
---|
[245] | 69 | |
---|
[691] | 70 | virtual const char *what() const throw(); |
---|
[245] | 71 | |
---|
[691] | 72 | protected: |
---|
[245] | 73 | |
---|
[691] | 74 | SettingException(const Setting &setting); |
---|
| 75 | SettingException(const Setting &setting, int idx); |
---|
| 76 | SettingException(const Setting &setting, const char *name); |
---|
| 77 | SettingException(const char *path); |
---|
[245] | 78 | |
---|
[691] | 79 | private: |
---|
[245] | 80 | |
---|
[691] | 81 | char *_path; |
---|
| 82 | }; |
---|
[245] | 83 | |
---|
[691] | 84 | class LIBCONFIGXX_API SettingTypeException : public SettingException |
---|
| 85 | { |
---|
| 86 | friend class Config; |
---|
| 87 | friend class Setting; |
---|
[245] | 88 | |
---|
[691] | 89 | public: |
---|
[245] | 90 | |
---|
[691] | 91 | const char *what() const throw(); |
---|
[245] | 92 | |
---|
[691] | 93 | private: |
---|
[245] | 94 | |
---|
[691] | 95 | SettingTypeException(const Setting &setting); |
---|
| 96 | SettingTypeException(const Setting &setting, int idx); |
---|
| 97 | SettingTypeException(const Setting &setting, const char *name); |
---|
| 98 | }; |
---|
[245] | 99 | |
---|
[691] | 100 | class LIBCONFIGXX_API SettingNotFoundException : public SettingException |
---|
| 101 | { |
---|
| 102 | friend class Config; |
---|
| 103 | friend class Setting; |
---|
[245] | 104 | |
---|
[691] | 105 | public: |
---|
[245] | 106 | |
---|
[691] | 107 | const char *what() const throw(); |
---|
[245] | 108 | |
---|
[691] | 109 | private: |
---|
[245] | 110 | |
---|
[691] | 111 | SettingNotFoundException(const Setting &setting, int idx); |
---|
| 112 | SettingNotFoundException(const Setting &setting, const char *name); |
---|
| 113 | SettingNotFoundException(const char *path); |
---|
| 114 | }; |
---|
[245] | 115 | |
---|
[691] | 116 | class LIBCONFIGXX_API SettingNameException : public SettingException |
---|
| 117 | { |
---|
| 118 | friend class Config; |
---|
| 119 | friend class Setting; |
---|
| 120 | |
---|
| 121 | public: |
---|
| 122 | |
---|
| 123 | const char *what() const throw(); |
---|
| 124 | |
---|
| 125 | private: |
---|
| 126 | |
---|
| 127 | SettingNameException(const Setting &setting, const char *name); |
---|
| 128 | }; |
---|
| 129 | |
---|
| 130 | class LIBCONFIGXX_API FileIOException : public ConfigException |
---|
| 131 | { |
---|
| 132 | public: |
---|
| 133 | |
---|
| 134 | const char *what() const throw(); |
---|
| 135 | }; |
---|
| 136 | |
---|
| 137 | class LIBCONFIGXX_API ParseException : public ConfigException |
---|
| 138 | { |
---|
| 139 | friend class Config; |
---|
| 140 | |
---|
| 141 | public: |
---|
| 142 | |
---|
| 143 | virtual ~ParseException() throw(); |
---|
| 144 | |
---|
| 145 | inline const char *getFile() const throw() |
---|
| 146 | { return(_file); } |
---|
| 147 | |
---|
| 148 | inline int getLine() const throw() |
---|
| 149 | { return(_line); } |
---|
| 150 | |
---|
| 151 | inline const char *getError() const throw() |
---|
| 152 | { return(_error); } |
---|
| 153 | |
---|
| 154 | const char *what() const throw(); |
---|
| 155 | |
---|
| 156 | private: |
---|
| 157 | |
---|
| 158 | ParseException(const char *file, int line, const char *error); |
---|
| 159 | |
---|
| 160 | const char *_file; |
---|
| 161 | int _line; |
---|
| 162 | const char *_error; |
---|
| 163 | }; |
---|
| 164 | |
---|
| 165 | class LIBCONFIGXX_API Setting |
---|
| 166 | { |
---|
| 167 | friend class Config; |
---|
| 168 | |
---|
| 169 | public: |
---|
| 170 | |
---|
| 171 | enum Type |
---|
[245] | 172 | { |
---|
[691] | 173 | TypeNone = 0, |
---|
| 174 | // scalar types |
---|
| 175 | TypeInt, |
---|
| 176 | TypeInt64, |
---|
| 177 | TypeFloat, |
---|
| 178 | TypeString, |
---|
| 179 | TypeBoolean, |
---|
| 180 | // aggregate types |
---|
| 181 | TypeGroup, |
---|
| 182 | TypeArray, |
---|
| 183 | TypeList |
---|
[245] | 184 | }; |
---|
| 185 | |
---|
[691] | 186 | enum Format |
---|
[245] | 187 | { |
---|
[691] | 188 | FormatDefault = 0, |
---|
| 189 | FormatHex = 1 |
---|
| 190 | }; |
---|
[245] | 191 | |
---|
[691] | 192 | private: |
---|
[245] | 193 | |
---|
[691] | 194 | config_setting_t *_setting; |
---|
| 195 | Type _type; |
---|
| 196 | Format _format; |
---|
[245] | 197 | |
---|
[691] | 198 | Setting(config_setting_t *setting); |
---|
[245] | 199 | |
---|
[691] | 200 | void assertType(Type type) const |
---|
| 201 | throw(SettingTypeException); |
---|
| 202 | static Setting & wrapSetting(config_setting_t *setting); |
---|
[245] | 203 | |
---|
[691] | 204 | Setting(const Setting& other); // not supported |
---|
| 205 | Setting& operator=(const Setting& other); // not supported |
---|
[245] | 206 | |
---|
[691] | 207 | public: |
---|
[245] | 208 | |
---|
[691] | 209 | virtual ~Setting() throw(); |
---|
[245] | 210 | |
---|
[691] | 211 | inline Type getType() const throw() { return(_type); } |
---|
[245] | 212 | |
---|
[691] | 213 | inline Format getFormat() const throw() { return(_format); } |
---|
| 214 | void setFormat(Format format) throw(); |
---|
[245] | 215 | |
---|
[691] | 216 | operator bool() const throw(SettingTypeException); |
---|
| 217 | operator int() const throw(SettingTypeException); |
---|
| 218 | operator unsigned int() const throw(SettingTypeException); |
---|
| 219 | operator long() const throw(SettingTypeException); |
---|
| 220 | operator unsigned long() const throw(SettingTypeException); |
---|
| 221 | operator long long() const throw(SettingTypeException); |
---|
| 222 | operator unsigned long long() const throw(SettingTypeException); |
---|
| 223 | operator double() const throw(SettingTypeException); |
---|
| 224 | operator float() const throw(SettingTypeException); |
---|
| 225 | operator const char *() const throw(SettingTypeException); |
---|
| 226 | operator std::string() const throw(SettingTypeException); |
---|
[245] | 227 | |
---|
[691] | 228 | Setting & operator=(bool value) throw(SettingTypeException); |
---|
| 229 | Setting & operator=(int value) throw(SettingTypeException); |
---|
| 230 | Setting & operator=(long value) throw(SettingTypeException); |
---|
| 231 | Setting & operator=(const long long &value) throw(SettingTypeException); |
---|
| 232 | Setting & operator=(const double &value) throw(SettingTypeException); |
---|
| 233 | Setting & operator=(float value) throw(SettingTypeException); |
---|
| 234 | Setting & operator=(const char *value) throw(SettingTypeException); |
---|
| 235 | Setting & operator=(const std::string &value) throw(SettingTypeException); |
---|
[245] | 236 | |
---|
[691] | 237 | Setting & operator[](const char * key) const |
---|
| 238 | throw(SettingTypeException, SettingNotFoundException); |
---|
[245] | 239 | |
---|
[691] | 240 | inline Setting & operator[](const std::string & key) const |
---|
| 241 | throw(SettingTypeException, SettingNotFoundException) |
---|
| 242 | { return(operator[](key.c_str())); } |
---|
[245] | 243 | |
---|
[691] | 244 | Setting & operator[](int index) const |
---|
| 245 | throw(SettingTypeException, SettingNotFoundException); |
---|
[245] | 246 | |
---|
[691] | 247 | bool lookupValue(const char *name, bool &value) const throw(); |
---|
| 248 | bool lookupValue(const char *name, int &value) const throw(); |
---|
| 249 | bool lookupValue(const char *name, unsigned int &value) const throw(); |
---|
| 250 | bool lookupValue(const char *name, long long &value) const throw(); |
---|
| 251 | bool lookupValue(const char *name, unsigned long long &value) |
---|
| 252 | const throw(); |
---|
| 253 | bool lookupValue(const char *name, double &value) const throw(); |
---|
| 254 | bool lookupValue(const char *name, float &value) const throw(); |
---|
| 255 | bool lookupValue(const char *name, const char *&value) const throw(); |
---|
| 256 | bool lookupValue(const char *name, std::string &value) const throw(); |
---|
[245] | 257 | |
---|
[691] | 258 | inline bool lookupValue(const std::string &name, bool &value) |
---|
| 259 | const throw() |
---|
| 260 | { return(lookupValue(name.c_str(), value)); } |
---|
[245] | 261 | |
---|
[691] | 262 | inline bool lookupValue(const std::string &name, int &value) |
---|
| 263 | const throw() |
---|
| 264 | { return(lookupValue(name.c_str(), value)); } |
---|
[245] | 265 | |
---|
[691] | 266 | inline bool lookupValue(const std::string &name, unsigned int &value) |
---|
| 267 | const throw() |
---|
| 268 | { return(lookupValue(name.c_str(), value)); } |
---|
[245] | 269 | |
---|
[691] | 270 | inline bool lookupValue(const std::string &name, long long &value) |
---|
| 271 | const throw() |
---|
| 272 | { return(lookupValue(name.c_str(), value)); } |
---|
[245] | 273 | |
---|
[691] | 274 | inline bool lookupValue(const std::string &name, |
---|
| 275 | unsigned long long &value) const throw() |
---|
| 276 | { return(lookupValue(name.c_str(), value)); } |
---|
[245] | 277 | |
---|
[691] | 278 | inline bool lookupValue(const std::string &name, double &value) const |
---|
| 279 | throw() |
---|
| 280 | { return(lookupValue(name.c_str(), value)); } |
---|
[245] | 281 | |
---|
[691] | 282 | inline bool lookupValue(const std::string &name, float &value) const |
---|
| 283 | throw() |
---|
| 284 | { return(lookupValue(name.c_str(), value)); } |
---|
[245] | 285 | |
---|
[691] | 286 | inline bool lookupValue(const std::string &name, const char *&value) const |
---|
| 287 | throw() |
---|
| 288 | { return(lookupValue(name.c_str(), value)); } |
---|
[245] | 289 | |
---|
[691] | 290 | inline bool lookupValue(const std::string &name, std::string &value) const |
---|
| 291 | throw() |
---|
| 292 | { return(lookupValue(name.c_str(), value)); } |
---|
[245] | 293 | |
---|
[691] | 294 | void remove(const char *name) |
---|
| 295 | throw(SettingTypeException, SettingNotFoundException); |
---|
[245] | 296 | |
---|
[691] | 297 | inline void remove(const std::string & name) |
---|
| 298 | throw(SettingTypeException, SettingNotFoundException) |
---|
| 299 | { remove(name.c_str()); } |
---|
[245] | 300 | |
---|
[691] | 301 | void remove(unsigned int idx) |
---|
| 302 | throw(SettingTypeException, SettingNotFoundException); |
---|
[245] | 303 | |
---|
[691] | 304 | inline Setting & add(const std::string & name, Type type) |
---|
| 305 | throw(SettingNameException, SettingTypeException) |
---|
| 306 | { return(add(name.c_str(), type)); } |
---|
[245] | 307 | |
---|
[691] | 308 | Setting & add(const char *name, Type type) |
---|
| 309 | throw(SettingNameException, SettingTypeException); |
---|
[245] | 310 | |
---|
[691] | 311 | Setting & add(Type type) throw(SettingTypeException); |
---|
[245] | 312 | |
---|
[691] | 313 | inline bool exists(const std::string & name) const throw() |
---|
| 314 | { return(exists(name.c_str())); } |
---|
[245] | 315 | |
---|
[691] | 316 | bool exists(const char *name) const throw(); |
---|
[245] | 317 | |
---|
[691] | 318 | int getLength() const throw(); |
---|
| 319 | const char *getName() const throw(); |
---|
| 320 | std::string getPath() const; |
---|
| 321 | int getIndex() const throw(); |
---|
[245] | 322 | |
---|
[691] | 323 | const Setting & getParent() const throw(SettingNotFoundException); |
---|
| 324 | Setting & getParent() throw(SettingNotFoundException); |
---|
[245] | 325 | |
---|
[691] | 326 | bool isRoot() const throw(); |
---|
[245] | 327 | |
---|
[691] | 328 | inline bool isGroup() const throw() |
---|
| 329 | { return(_type == TypeGroup); } |
---|
[245] | 330 | |
---|
[691] | 331 | inline bool isArray() const throw() |
---|
| 332 | { return(_type == TypeArray); } |
---|
[245] | 333 | |
---|
[691] | 334 | inline bool isList() const throw() |
---|
| 335 | { return(_type == TypeList); } |
---|
[245] | 336 | |
---|
[691] | 337 | inline bool isAggregate() const throw() |
---|
| 338 | { return(_type >= TypeGroup); } |
---|
[245] | 339 | |
---|
[691] | 340 | inline bool isScalar() const throw() |
---|
| 341 | { return((_type > TypeNone) && (_type < TypeGroup)); } |
---|
[245] | 342 | |
---|
[691] | 343 | inline bool isNumber() const throw() |
---|
| 344 | { return((_type == TypeInt) || (_type == TypeInt64) |
---|
| 345 | || (_type == TypeFloat)); } |
---|
[246] | 346 | |
---|
[691] | 347 | unsigned int getSourceLine() const throw(); |
---|
| 348 | const char *getSourceFile() const throw(); |
---|
| 349 | }; |
---|
| 350 | |
---|
| 351 | class LIBCONFIGXX_API Config |
---|
[246] | 352 | { |
---|
[691] | 353 | private: |
---|
[246] | 354 | |
---|
[691] | 355 | config_t *_config; |
---|
[245] | 356 | |
---|
[691] | 357 | static void ConfigDestructor(void *arg); |
---|
| 358 | Config(const Config& other); // not supported |
---|
| 359 | Config& operator=(const Config& other); // not supported |
---|
[245] | 360 | |
---|
[691] | 361 | public: |
---|
[245] | 362 | |
---|
[691] | 363 | Config(); |
---|
| 364 | virtual ~Config(); |
---|
[245] | 365 | |
---|
[691] | 366 | void setAutoConvert(bool flag); |
---|
| 367 | bool getAutoConvert() const; |
---|
[245] | 368 | |
---|
[691] | 369 | void setTabWidth(unsigned short width) throw(); |
---|
| 370 | unsigned short getTabWidth() const throw(); |
---|
[245] | 371 | |
---|
[691] | 372 | void read(FILE *stream) throw(ParseException); |
---|
| 373 | void write(FILE *stream) const; |
---|
[245] | 374 | |
---|
[691] | 375 | void readString(const char *str) throw(ParseException); |
---|
| 376 | inline void readString(const std::string &str) throw(ParseException) |
---|
| 377 | { return(readString(str.c_str())); } |
---|
[245] | 378 | |
---|
[691] | 379 | void readFile(const char *filename) throw(FileIOException, ParseException); |
---|
| 380 | void writeFile(const char *filename) throw(FileIOException); |
---|
[245] | 381 | |
---|
[691] | 382 | inline Setting & lookup(const std::string &path) const |
---|
| 383 | throw(SettingNotFoundException) |
---|
| 384 | { return(lookup(path.c_str())); } |
---|
[245] | 385 | |
---|
[691] | 386 | Setting & lookup(const char *path) const throw(SettingNotFoundException); |
---|
[245] | 387 | |
---|
[691] | 388 | inline bool exists(const std::string & path) const throw() |
---|
| 389 | { return(exists(path.c_str())); } |
---|
[245] | 390 | |
---|
[691] | 391 | bool exists(const char *path) const throw(); |
---|
[245] | 392 | |
---|
[691] | 393 | bool lookupValue(const char *path, bool &value) const throw(); |
---|
| 394 | bool lookupValue(const char *path, int &value) const throw(); |
---|
| 395 | bool lookupValue(const char *path, unsigned int &value) const throw(); |
---|
| 396 | bool lookupValue(const char *path, long long &value) const throw(); |
---|
| 397 | bool lookupValue(const char *path, unsigned long long &value) |
---|
| 398 | const throw(); |
---|
| 399 | bool lookupValue(const char *path, double &value) const throw(); |
---|
| 400 | bool lookupValue(const char *path, float &value) const throw(); |
---|
| 401 | bool lookupValue(const char *path, const char *&value) const throw(); |
---|
| 402 | bool lookupValue(const char *path, std::string &value) const throw(); |
---|
[245] | 403 | |
---|
[691] | 404 | inline bool lookupValue(const std::string &path, bool &value) const throw() |
---|
| 405 | { return(lookupValue(path.c_str(), value)); } |
---|
[245] | 406 | |
---|
[691] | 407 | inline bool lookupValue(const std::string &path, int &value) const throw() |
---|
| 408 | { return(lookupValue(path.c_str(), value)); } |
---|
[245] | 409 | |
---|
[691] | 410 | inline bool lookupValue(const std::string &path, unsigned int &value) |
---|
| 411 | const throw() |
---|
| 412 | { return(lookupValue(path.c_str(), value)); } |
---|
[245] | 413 | |
---|
[691] | 414 | inline bool lookupValue(const std::string &path, long long &value) |
---|
| 415 | const throw() |
---|
| 416 | { return(lookupValue(path.c_str(), value)); } |
---|
[245] | 417 | |
---|
[691] | 418 | inline bool lookupValue(const std::string &path, |
---|
| 419 | unsigned long long &value) const throw() |
---|
| 420 | { return(lookupValue(path.c_str(), value)); } |
---|
[245] | 421 | |
---|
[691] | 422 | inline bool lookupValue(const std::string &path, double &value) |
---|
| 423 | const throw() |
---|
| 424 | { return(lookupValue(path.c_str(), value)); } |
---|
| 425 | |
---|
| 426 | inline bool lookupValue(const std::string &path, float &value) |
---|
| 427 | const throw() |
---|
| 428 | { return(lookupValue(path.c_str(), value)); } |
---|
| 429 | |
---|
| 430 | inline bool lookupValue(const std::string &path, const char *&value) |
---|
| 431 | const throw() |
---|
| 432 | { return(lookupValue(path.c_str(), value)); } |
---|
| 433 | |
---|
| 434 | inline bool lookupValue(const std::string &path, std::string &value) |
---|
| 435 | const throw() |
---|
| 436 | { return(lookupValue(path.c_str(), value)); } |
---|
| 437 | |
---|
| 438 | Setting & getRoot() const; |
---|
| 439 | |
---|
| 440 | private: |
---|
| 441 | |
---|
| 442 | void handleError() const; |
---|
| 443 | }; |
---|
| 444 | |
---|
| 445 | }; // namespace libconfig |
---|
| 446 | |
---|
[245] | 447 | #endif // __libconfig_hpp |
---|