aboutsummaryrefslogtreecommitdiffstats
path: root/lib/libconfig.h++
blob: dfc7939bf62e983aca62da24fd8342210b28e6bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19pre { line-height: 125%; margin: 0; }
td.linenos pre { color: #000000; background-color: #f0f0f0; padding: 0 5px 0 5px; }
span.linenos { color: #000000; background-color: #f0f0f0; padding: 0 5px 0 5px; }
td.linenos pre.special { color: #000000; background-color: #ffffc0; padding: 0 5px 0 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding: 0 5px 0 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
# Sweden - Vätö
[CHANNEL]
	DELIVERY_SYSTEM = DVBT
	FREQUENCY = 666000000
	BANDWIDTH_HZ = 8000000
1 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
/* ----------------------------------------------------------------------------
   libconfig - A library for processing structured configuration files
   Copyright (C) 2005-2010  Mark A Lindner

   This file is part of libconfig.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public License
   as published by the Free Software Foundation; either version 2.1 of
   the License, or (at your option) any later version.

   This library 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
   Lesser General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with this library; if not, see
   <http://www.gnu.org/licenses/>.
   ----------------------------------------------------------------------------
*/

#ifndef __libconfig_hpp
#define __libconfig_hpp

#include <stdio.h>
#include <exception>
#include <string>

#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
#if defined(LIBCONFIGXX_STATIC)
#define LIBCONFIGXX_API
#elif defined(LIBCONFIGXX_EXPORTS)
#define LIBCONFIGXX_API __declspec(dllexport)
#else /* ! LIBCONFIGXX_EXPORTS */
#define LIBCONFIGXX_API __declspec(dllimport)
#endif /* LIBCONFIGXX_STATIC */
#else /* ! WIN32 */
#define LIBCONFIGXX_API
#endif /* WIN32 */

#define LIBCONFIGXX_VER_MAJOR    1
#define LIBCONFIGXX_VER_MINOR    4
#define LIBCONFIGXX_VER_REVISION 8

struct config_t; // fwd decl
struct config_setting_t; // fwd decl

namespace libconfig {

class LIBCONFIGXX_API ConfigException : public std::exception { };

class Setting; // fwd decl

class LIBCONFIGXX_API SettingException : public ConfigException
{
  friend class Config;

  public:

  SettingException(const SettingException &other);
  SettingException& operator=(const SettingException &other);

  virtual ~SettingException() throw();

  const char *getPath() const;

  virtual const char *what() const throw();

  protected:

  SettingException(const Setting &setting);
  SettingException(const Setting &setting, int idx);
  SettingException(const Setting &setting, const char *name);
  SettingException(const char *path);

  private:

  char *_path;
};

class LIBCONFIGXX_API SettingTypeException : public SettingException
{
  friend class Config;
  friend class Setting;

  public:

  const char *what() const throw();

  private:

  SettingTypeException(const Setting &setting);
  SettingTypeException(const Setting &setting, int idx);
  SettingTypeException(const Setting &setting, const char *name);
};

class LIBCONFIGXX_API SettingNotFoundException : public SettingException
{
  friend class Config;
  friend class Setting;

  public:

  const char *what() const throw();

  private:

  SettingNotFoundException(const Setting &setting, int idx);
  SettingNotFoundException(const Setting &setting, const char *name);
  SettingNotFoundException(const char *path);
};

class LIBCONFIGXX_API SettingNameException : public SettingException
{
  friend class Config;
  friend class Setting;

  public:

  const char *what() const throw();

  private:

  SettingNameException(const Setting &setting, const char *name);
};

class LIBCONFIGXX_API FileIOException : public ConfigException
{
  public:

  const char *what() const throw();
};

class LIBCONFIGXX_API ParseException : public ConfigException
{
  friend class Config;

  public:

  ParseException(const ParseException &other);

  virtual ~ParseException() throw();

  inline const char *getFile() const throw()
  { return(_file); }

  inline int getLine() const throw()
  { return(_line); }

  inline const char *getError() const throw()
  { return(_error); }

  const char *what() const throw();

  private:

  ParseException(const char *file, int line, const char *error);

  const char *_file;
  int _line;
  const char *_error;
};

class LIBCONFIGXX_API Setting
{
  friend class Config;

  public:

  enum Type
  {
    TypeNone = 0,
    // scalar types
    TypeInt,
    TypeInt64,
    TypeFloat,
    TypeString,
    TypeBoolean,
    // aggregate types
    TypeGroup,
    TypeArray,
    TypeList
  };

  enum Format
  {
    FormatDefault = 0,
    FormatHex = 1
  };

  private:

  config_setting_t *_setting;
  Type _type;
  Format _format;

  Setting(config_setting_t *setting);

  void assertType(Type type) const
    throw(SettingTypeException);
  static Setting & wrapSetting(config_setting_t *setting);

  Setting(const Setting& other); // not supported
  Setting& operator=(const Setting& other); // not supported

  public:

  virtual ~Setting() throw();

  inline Type getType() const throw() { return(_type); }

  inline Format getFormat() const throw() { return(_format); }
  void setFormat(Format format) throw();

  operator bool() const throw(SettingTypeException);
  operator int() const throw(SettingTypeException);
  operator unsigned int() const throw(SettingTypeException);
  operator long() const throw(SettingTypeException);
  operator unsigned long() const throw(SettingTypeException);
  operator long long() const throw(SettingTypeException);
  operator unsigned long long() const throw(SettingTypeException);
  operator double() const throw(SettingTypeException);
  operator float() const throw(SettingTypeException);
  operator const char *() const throw(SettingTypeException);
  operator std::string() const throw(SettingTypeException);

  inline const char *c_str() const throw(SettingTypeException)
  { return operator const char*(); }

  Setting & operator=(bool value) throw(SettingTypeException);
  Setting & operator=(int value) throw(SettingTypeException);
  Setting & operator=(long value) throw(SettingTypeException);
  Setting & operator=(const long long &value) throw(SettingTypeException);
  Setting & operator=(const double &value) throw(SettingTypeException);
  Setting & operator=(float value) throw(SettingTypeException);
  Setting & operator=(const char *value) throw(SettingTypeException);
  Setting & operator=(const std::string &value) throw(SettingTypeException);

  Setting & operator[](const char *key) const
    throw(SettingTypeException, SettingNotFoundException);

  inline Setting & operator[](const std::string &key) const
    throw(SettingTypeException, SettingNotFoundException)
  { return(operator[](key.c_str())); }

  Setting & operator[](int index) const
    throw(SettingTypeException, SettingNotFoundException);

  bool lookupValue(const char *name, bool &value) const throw();
  bool lookupValue(const char *name, int &value) const throw();
  bool lookupValue(const char *name, unsigned int &value) const throw();
  bool lookupValue(const char *name, long long &value) const throw();
  bool lookupValue(const char *name, unsigned long long &value)
    const throw();
  bool lookupValue(const char *name, double &value) const throw();
  bool lookupValue(const char *name, float &value) const throw();
  bool lookupValue(const char *name, const char *&value) const throw();
  bool lookupValue(const char *name, std::string &value) const throw();

  inline bool lookupValue(const std::string &name, bool &value)
    const throw()
  { return(lookupValue(name.c_str(), value)); }

  inline bool lookupValue(const std::string &name, int &value)
    const throw()
  { return(lookupValue(name.c_str(), value)); }

  inline bool lookupValue(const std::string &name, unsigned int &value)
    const throw()
  { return(lookupValue(name.c_str(), value)); }

  inline bool lookupValue(const std::string &name, long long &value)
    const throw()
  { return(lookupValue(name.c_str(), value)); }

  inline bool lookupValue(const std::string &name,
                          unsigned long long &value) const throw()
  { return(lookupValue(name.c_str(), value)); }

  inline bool lookupValue(const std::string &name, double &value) const
    throw()
  { return(lookupValue(name.c_str(), value)); }

  inline bool lookupValue(const std::string &name, float &value) const
    throw()
  { return(lookupValue(name.c_str(), value)); }

  inline bool lookupValue(const std::string &name, const char *&value) const
    throw()
  { return(lookupValue(name.c_str(), value)); }

  inline bool lookupValue(const std::string &name, std::string &value) const
    throw()
  { return(lookupValue(name.c_str(), value)); }

  void remove(const char *name)
    throw(SettingTypeException, SettingNotFoundException);

  inline void remove(const std::string & name)
    throw(SettingTypeException, SettingNotFoundException)
  { remove(name.c_str()); }

  void remove(unsigned int idx)
    throw(SettingTypeException, SettingNotFoundException);

  inline Setting & add(const std::string & name, Type type)
    throw(SettingNameException, SettingTypeException)
  { return(add(name.c_str(), type)); }

  Setting & add(const char *name, Type type)
    throw(SettingNameException, SettingTypeException);

  Setting & add(Type type) throw(SettingTypeException);

  inline bool exists(const std::string &name) const throw()
  { return(exists(name.c_str())); }

  bool exists(const char *name) const throw();

  int getLength() const throw();
  const char *getName() const throw();
  std::string getPath() const;
  int getIndex() const throw();

  const Setting & getParent() const throw(SettingNotFoundException);
  Setting & getParent() throw(SettingNotFoundException);

  bool isRoot() const throw();

  inline bool isGroup() const throw()
  { return(_type == TypeGroup); }

  inline bool isArray() const throw()
  { return(_type == TypeArray); }

  inline bool isList() const throw()
  { return(_type == TypeList); }

  inline bool isAggregate() const throw()
  { return(_type >= TypeGroup); }

  inline bool isScalar() const throw()
  { return((_type > TypeNone) && (_type < TypeGroup)); }

  inline bool isNumber() const throw()
  { return((_type == TypeInt) || (_type == TypeInt64)
           || (_type == TypeFloat)); }

  unsigned int getSourceLine() const throw();
  const char *getSourceFile() const throw();
};

class LIBCONFIGXX_API Config
{
  private:

  config_t *_config;

  static void ConfigDestructor(void *arg);
  Config(const Config& other); // not supported
  Config& operator=(const Config& other); // not supported

  public:

  Config();
  virtual ~Config();

  void setAutoConvert(bool flag);
  bool getAutoConvert() const;

  void setDefaultFormat(Setting::Format format);
  inline Setting::Format getDefaultFormat() const
  { return(_defaultFormat); }

  void setTabWidth(unsigned short width) throw();
  unsigned short getTabWidth() const throw();

  void setIncludeDir(const char *includeDir) throw();
  const char *getIncludeDir() const throw();

  void read(FILE *stream) throw(ParseException);
  void write(FILE *stream) const;

  void readString(const char *str) throw(ParseException);
  inline void readString(const std::string &str) throw(ParseException)
  { return(readString(str.c_str())); }

  void readFile(const char *filename) throw(FileIOException, ParseException);
  void writeFile(const char *filename) throw(FileIOException);

  inline Setting & lookup(const std::string &path) const
    throw(SettingNotFoundException)
  { return(lookup(path.c_str())); }

  Setting & lookup(const char *path) const throw(SettingNotFoundException);

  inline bool exists(const std::string & path) const throw()
  { return(exists(path.c_str())); }

  bool exists(const char *path) const throw();

  bool lookupValue(const char *path, bool &value) const throw();
  bool lookupValue(const char *path, int &value) const throw();
  bool lookupValue(const char *path, unsigned int &value) const throw();
  bool lookupValue(const char *path, long long &value) const throw();
  bool lookupValue(const char *path, unsigned long long &value)
    const throw();
  bool lookupValue(const char *path, double &value) const throw();
  bool lookupValue(const char *path, float &value) const throw();
  bool lookupValue(const char *path, const char *&value) const throw();
  bool lookupValue(const char *path, std::string &value) const throw();

  inline bool lookupValue(const std::string &path, bool &value) const throw()
  { return(lookupValue(path.c_str(), value)); }

  inline bool lookupValue(const std::string &path, int &value) const throw()
  { return(lookupValue(path.c_str(), value)); }

  inline bool lookupValue(const std::string &path, unsigned int &value)
    const throw()
  { return(lookupValue(path.c_str(), value)); }

  inline bool lookupValue(const std::string &path, long long &value)
    const throw()
  { return(lookupValue(path.c_str(), value)); }

  inline bool lookupValue(const std::string &path,
                          unsigned long long &value) const throw()
  { return(lookupValue(path.c_str(), value)); }

  inline bool lookupValue(const std::string &path, double &value)
    const throw()
  { return(lookupValue(path.c_str(), value)); }

  inline bool lookupValue(const std::string &path, float &value)
    const throw()
  { return(lookupValue(path.c_str(), value)); }

  inline bool lookupValue(const std::string &path, const char *&value)
    const throw()
  { return(lookupValue(path.c_str(), value)); }

  inline bool lookupValue(const std::string &path, std::string &value)
    const throw()
  { return(lookupValue(path.c_str(), value)); }

  Setting & getRoot() const;

  private:

  Setting::Format _defaultFormat;

  void handleError() const;
};

} // namespace libconfig

#endif // __libconfig_hpp