config-file.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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_save(config_t config);
  40. EXPORT void config_close(config_t config);
  41. EXPORT size_t config_num_sections(config_t config);
  42. EXPORT const char *config_get_section(config_t config, size_t idx);
  43. EXPORT void config_set_string(config_t config, const char *section,
  44. const char *name, const char *value);
  45. EXPORT void config_set_int(config_t config, const char *section,
  46. const char *name, int64_t value);
  47. EXPORT void config_set_uint(config_t config, const char *section,
  48. const char *name, uint64_t value);
  49. EXPORT void config_set_bool(config_t config, const char *section,
  50. const char *name, bool value);
  51. EXPORT void config_set_double(config_t config, const char *section,
  52. const char *name, double value);
  53. EXPORT const char *config_get_string(config_t config, const char *section,
  54. const char *name);
  55. EXPORT int64_t config_get_int(config_t config, const char *section,
  56. const char *name);
  57. EXPORT uint64_t config_get_uint(config_t config, const char *section,
  58. const char *name);
  59. EXPORT bool config_get_bool(config_t config, const char *section,
  60. const char *name);
  61. EXPORT double config_get_double(config_t config, const char *section,
  62. const char *name);
  63. /*
  64. * DEFAULT VALUES
  65. *
  66. * The following functions are used to set what values will return if they do
  67. * not exist. Call these functions *once* for each known value before reading
  68. * any of them anywhere else.
  69. *
  70. * These do *not* actually set any values, they only set what values will be
  71. * returned for config_get_* if the specified variable does not exist.
  72. *
  73. * You can initialize the defaults programmitically using config_set_default_*
  74. * functions (recommended for most cases), or you can initialize it via a file
  75. * with config_open_defaults.
  76. */
  77. EXPORT int config_open_defaults(config_t config, const char *file);
  78. EXPORT void config_set_default_string(config_t config, const char *section,
  79. const char *name, const char *value);
  80. EXPORT void config_set_default_int(config_t config, const char *section,
  81. const char *name, int64_t value);
  82. EXPORT void config_set_default_uint(config_t config, const char *section,
  83. const char *name, uint64_t value);
  84. EXPORT void config_set_default_bool(config_t config, const char *section,
  85. const char *name, bool value);
  86. EXPORT void config_set_default_double(config_t config, const char *section,
  87. const char *name, double value);
  88. /* These functions allow you to get the current default values rather than get
  89. * the actual values. Probably almost never really needed */
  90. EXPORT const char *config_get_default_string(config_t config,
  91. const char *section, const char *name);
  92. EXPORT int64_t config_get_default_int(config_t config, const char *section,
  93. const char *name);
  94. EXPORT uint64_t config_get_default_uint(config_t config, const char *section,
  95. const char *name);
  96. EXPORT bool config_get_default_bool(config_t config, const char *section,
  97. const char *name);
  98. EXPORT double config_get_default_double(config_t config, const char *section,
  99. const char *name);
  100. EXPORT bool config_has_user_value(config_t config, const char *section,
  101. const char *name);
  102. EXPORT bool config_has_default_value(config_t config, const char *section,
  103. const char *name);
  104. #ifdef __cplusplus
  105. }
  106. #endif