config-file.h 4.9 KB

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