config-file.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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, enum config_open_type open_type);
  38. EXPORT int config_open_string(config_t **config, const char *str);
  39. EXPORT int config_save(config_t *config);
  40. EXPORT int config_save_safe(config_t *config, const char *temp_ext, const char *backup_ext);
  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, const char *name, const char *value);
  45. EXPORT void config_set_int(config_t *config, const char *section, const char *name, int64_t value);
  46. EXPORT void config_set_uint(config_t *config, const char *section, const char *name, uint64_t value);
  47. EXPORT void config_set_bool(config_t *config, const char *section, const char *name, bool value);
  48. EXPORT void config_set_double(config_t *config, const char *section, const char *name, double value);
  49. EXPORT const char *config_get_string(config_t *config, const char *section, const char *name);
  50. EXPORT int64_t config_get_int(config_t *config, const char *section, const char *name);
  51. EXPORT uint64_t config_get_uint(config_t *config, const char *section, const char *name);
  52. EXPORT bool config_get_bool(config_t *config, const char *section, const char *name);
  53. EXPORT double config_get_double(config_t *config, const char *section, const char *name);
  54. EXPORT bool config_remove_value(config_t *config, const char *section, const char *name);
  55. /*
  56. * DEFAULT VALUES
  57. *
  58. * The following functions are used to set what values will return if they do
  59. * not exist. Call these functions *once* for each known value before reading
  60. * any of them anywhere else.
  61. *
  62. * These do *not* actually set any values, they only set what values will be
  63. * returned for config_get_* if the specified variable does not exist.
  64. *
  65. * You can initialize the defaults programmatically using config_set_default_*
  66. * functions (recommended for most cases), or you can initialize it via a file
  67. * with config_open_defaults.
  68. */
  69. EXPORT int config_open_defaults(config_t *config, const char *file);
  70. EXPORT void config_set_default_string(config_t *config, const char *section, const char *name, const char *value);
  71. EXPORT void config_set_default_int(config_t *config, const char *section, const char *name, int64_t value);
  72. EXPORT void config_set_default_uint(config_t *config, const char *section, const char *name, uint64_t value);
  73. EXPORT void config_set_default_bool(config_t *config, const char *section, const char *name, bool value);
  74. EXPORT void config_set_default_double(config_t *config, const char *section, const char *name, double value);
  75. /* These functions allow you to get the current default values rather than get
  76. * the actual values. Probably almost never really needed */
  77. EXPORT const char *config_get_default_string(config_t *config, const char *section, const char *name);
  78. EXPORT int64_t config_get_default_int(config_t *config, const char *section, const char *name);
  79. EXPORT uint64_t config_get_default_uint(config_t *config, const char *section, const char *name);
  80. EXPORT bool config_get_default_bool(config_t *config, const char *section, const char *name);
  81. EXPORT double config_get_default_double(config_t *config, const char *section, const char *name);
  82. EXPORT bool config_has_user_value(config_t *config, const char *section, const char *name);
  83. EXPORT bool config_has_default_value(config_t *config, const char *section, const char *name);
  84. #ifdef __cplusplus
  85. }
  86. #endif