config-file.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright (c) 2013 Hugh Bailey <[email protected]>
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #pragma once
  17. #include "c99defs.h"
  18. /*
  19. * Generic ini-style config file functions
  20. *
  21. * NOTE: It is highly recommended to use the default value functions (bottom of
  22. * the file) before reading any variables from config files.
  23. */
  24. #ifdef __cplusplus
  25. extern "C" {
  26. #endif
  27. struct config_data;
  28. typedef struct config_data config_t;
  29. #define CONFIG_SUCCESS 0
  30. #define CONFIG_FILENOTFOUND -1
  31. #define CONFIG_ERROR -2
  32. enum config_open_type {
  33. CONFIG_OPEN_EXISTING,
  34. CONFIG_OPEN_ALWAYS,
  35. };
  36. EXPORT config_t *config_create(const char *file);
  37. EXPORT int config_open(config_t **config, const char *file,
  38. enum config_open_type open_type);
  39. EXPORT int config_open_string(config_t **config, const char *str);
  40. EXPORT int config_save(config_t *config);
  41. EXPORT void config_close(config_t *config);
  42. EXPORT size_t config_num_sections(config_t *config);
  43. EXPORT const char *config_get_section(config_t *config, size_t idx);
  44. EXPORT void config_set_string(config_t *config, const char *section,
  45. const char *name, const char *value);
  46. EXPORT void config_set_int(config_t *config, const char *section,
  47. const char *name, int64_t value);
  48. EXPORT void config_set_uint(config_t *config, const char *section,
  49. const char *name, uint64_t value);
  50. EXPORT void config_set_bool(config_t *config, const char *section,
  51. const char *name, bool value);
  52. EXPORT void config_set_double(config_t *config, const char *section,
  53. const char *name, double value);
  54. EXPORT const char *config_get_string(const config_t *config,
  55. const char *section, const char *name);
  56. EXPORT int64_t config_get_int(const config_t *config, const char *section,
  57. const char *name);
  58. EXPORT uint64_t config_get_uint(const config_t *config, const char *section,
  59. const char *name);
  60. EXPORT bool config_get_bool(const config_t *config, const char *section,
  61. const char *name);
  62. EXPORT double config_get_double(const config_t *config, const char *section,
  63. const char *name);
  64. /*
  65. * DEFAULT VALUES
  66. *
  67. * The following functions are used to set what values will return if they do
  68. * not exist. Call these functions *once* for each known value before reading
  69. * any of them anywhere else.
  70. *
  71. * These do *not* actually set any values, they only set what values will be
  72. * returned for config_get_* if the specified variable does not exist.
  73. *
  74. * You can initialize the defaults programmitically using config_set_default_*
  75. * functions (recommended for most cases), or you can initialize it via a file
  76. * with config_open_defaults.
  77. */
  78. EXPORT int config_open_defaults(config_t *config, const char *file);
  79. EXPORT void config_set_default_string(config_t *config, const char *section,
  80. const char *name, const char *value);
  81. EXPORT void config_set_default_int(config_t *config, const char *section,
  82. const char *name, int64_t value);
  83. EXPORT void config_set_default_uint(config_t *config, const char *section,
  84. const char *name, uint64_t value);
  85. EXPORT void config_set_default_bool(config_t *config, const char *section,
  86. const char *name, bool value);
  87. EXPORT void config_set_default_double(config_t *config, const char *section,
  88. const char *name, double value);
  89. /* These functions allow you to get the current default values rather than get
  90. * the actual values. Probably almost never really needed */
  91. EXPORT const char *config_get_default_string(const config_t *config,
  92. const char *section, const char *name);
  93. EXPORT int64_t config_get_default_int(const config_t *config,
  94. const char *section, const char *name);
  95. EXPORT uint64_t config_get_default_uint(const config_t *config,
  96. const char *section, const char *name);
  97. EXPORT bool config_get_default_bool(const config_t *config,
  98. const char *section, const char *name);
  99. EXPORT double config_get_default_double(const config_t *config,
  100. const char *section, const char *name);
  101. EXPORT bool config_has_user_value(const config_t *config,
  102. const char *section, const char *name);
  103. EXPORT bool config_has_default_value(const config_t *config,
  104. const char *section, const char *name);
  105. #ifdef __cplusplus
  106. }
  107. #endif