Define simple configuration on INI files.
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

178 rader
4.2 KiB

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