Define simple configuration on INI files.
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

ini-config.h 4.6 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. #ifndef INI_CONFIG_H
  2. #define INI_CONFIG_H
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <stdint.h>
  6. #include <stdbool.h>
  7. #ifdef __cplusplus
  8. extern "C" {
  9. #endif
  10. /**
  11. * Struct for transformer functions used for properly loading and saving the config item's value.
  12. */
  13. typedef struct {
  14. /**
  15. * Function that formats the value from memory to a value that is write-friendly to the config file.
  16. */
  17. void* serialize;
  18. /**
  19. * Function that formats the value from file to a value that is read-friendly to memory.
  20. */
  21. void* deserialize;
  22. } INI_ConfigTransformer; // TODO: should we unify this with INI_ConfigType?
  23. struct INI_ConfigItem;
  24. /**
  25. * Function for loading a config item value from file to memory.
  26. */
  27. typedef void INI_ConfigTypeLoad(struct INI_ConfigItem*, const char*, void*);
  28. /**
  29. * Result enum for saving config items.
  30. */
  31. typedef enum {
  32. /**
  33. * Result returned when saving a config item was not successful.
  34. */
  35. INI_CONFIG_SAVE_ITEM_ERROR = -1,
  36. /**
  37. * Result returned when saving a config item was successful.
  38. */
  39. INI_CONFIG_SAVE_ITEM_OK,
  40. } INI_ConfigSaveItemResult;
  41. /**
  42. * Function for saving a config item value from memory to file.
  43. */
  44. typedef INI_ConfigSaveItemResult INI_ConfigTypeSave(struct INI_ConfigItem*, const char*, void*);
  45. /**
  46. * Function for retrieving a config item value from the command-line to memory.
  47. */
  48. typedef void INI_ConfigTypeOverride(struct INI_ConfigItem*, uint8_t, const char* []);
  49. /**
  50. * Struct for the config item type.
  51. */
  52. typedef struct {
  53. /**
  54. * Size of the corresponding in-memory value of the config item.
  55. */
  56. size_t size;
  57. /**
  58. * Load function.
  59. * @see INI_ConfigTypeLoad
  60. */
  61. INI_ConfigTypeLoad* load;
  62. /**
  63. * Save function.
  64. * @see INI_ConfigTypeSave
  65. */
  66. INI_ConfigTypeSave* save;
  67. /**
  68. * Override function.
  69. * @see INI_ConfigTypeOverride
  70. */
  71. INI_ConfigTypeOverride* override;
  72. } INI_ConfigType;
  73. /**
  74. * Struct for the config item, which occuptes a single key in a specific section of the config INI file.
  75. */
  76. typedef struct INI_ConfigItem {
  77. /**
  78. * Type of the config item.
  79. */
  80. INI_ConfigType type;
  81. /**
  82. * Section where this config item can be found.
  83. */
  84. const char* section;
  85. /**
  86. * Key where this config item value is serialized and stored.
  87. */
  88. const char* key;
  89. /**
  90. * Command-line option for overriding this config item's value.
  91. */
  92. const char* cmdline_option; // TODO: should we extract commandline parsing logic?
  93. /**
  94. * Default value of the config item, when the value could not be read from the config file.
  95. */
  96. const void* default_value;
  97. /**
  98. * Validator function for the config item's value.
  99. */
  100. void* validator;
  101. /**
  102. * Transformer functions.
  103. * @see INI_ConfigTransformer
  104. */
  105. INI_ConfigTransformer transformer;
  106. /**
  107. * The memory address where the config item value will reside. This property should allow storing the amount of butes
  108. * specified under `type.size`.
  109. */
  110. void* dest;
  111. } INI_ConfigItem;
  112. /**
  113. * Retrieves the value from a command-line option.
  114. * @return The string value from the command-line option.
  115. */
  116. const char* INI_ConfigGetCommandlineOption(uint8_t, const char*[], const char*);
  117. /**
  118. * Result enum for initializing config items.
  119. */
  120. typedef enum {
  121. /**
  122. * Result returned when initializing all config items was not successful.
  123. */
  124. INI_CONFIG_INITIALIZE_RESULT_ERROR = -1,
  125. /**
  126. * Result returned when initializing all config items was successful.
  127. */
  128. INI_CONFIG_INITIALIZE_RESULT_OK,
  129. /**
  130. * Result returned when initializing some config items was successful.
  131. */
  132. INI_CONFIG_INITIALIZE_RESULT_WARNING
  133. } INI_ConfigInitializeResult;
  134. /**
  135. * Syncs the config file to memory. This includes the loading of the items from the config file.
  136. * @return Result of the initialization.
  137. */
  138. INI_ConfigInitializeResult INI_ConfigInitialize(INI_ConfigItem[], const char*, uint8_t, const char*[]);
  139. typedef int32_t INI_ConfigSaveResult;
  140. /**
  141. * Saves the contents of the memory to the config file.
  142. * @return
  143. */
  144. INI_ConfigSaveResult INI_ConfigSave(INI_ConfigItem[], const char*);
  145. #ifdef __cplusplus
  146. };
  147. #endif
  148. #define INI_CONFIG_DECLARE_TYPE(ID) \
  149. INI_ConfigTypeLoad INI_ConfigLoad##ID; \
  150. INI_ConfigTypeSave INI_ConfigSave##ID; \
  151. INI_ConfigTypeOverride INI_ConfigOverride##ID
  152. /**
  153. * Null transformer.
  154. */
  155. #define INI_CONFIG_TRANSFORMER_NONE (INI_ConfigTransformer) { \
  156. .serialize = NULL, \
  157. .deserialize = NULL, \
  158. }
  159. /**
  160. * Null item. Use this to signify the end of the config item list.
  161. */
  162. #define INI_CONFIG_ITEM_NULL (INI_ConfigItem) { \
  163. (INI_ConfigType) { \
  164. .size = 0, \
  165. .load = NULL, \
  166. .save = NULL, \
  167. .override = NULL, \
  168. }, \
  169. NULL, \
  170. NULL, \
  171. NULL, \
  172. NULL, \
  173. NULL, \
  174. INI_CONFIG_TRANSFORMER_NONE, \
  175. NULL, \
  176. }
  177. #endif