reference-libobs-util-config-file.rst 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. Config Files
  2. ============
  3. The configuration file functions are a simple implementation of the INI
  4. file format, with the addition of default values.
  5. .. code:: cpp
  6. #include <util/config-file.h>
  7. .. type:: config_t
  8. Config File Functions
  9. ---------------------
  10. .. function:: config_t *config_create(const char *file)
  11. Creates a new configuration object and associates it with the
  12. specified file name.
  13. :param file: Path to the new configuration file
  14. :return: A new configuration file object
  15. ----------------------
  16. .. function:: int config_open(config_t **config, const char *file, enum config_open_type open_type)
  17. Opens a configuration file.
  18. :param config: Pointer that receives a pointer to a new configuration
  19. file object (if successful)
  20. :param file: Path to the configuration file
  21. :param open_type: Can be one of the following values:
  22. - CONFIG_OPEN_EXISTING - Fail if the file doesn't
  23. exist.
  24. - CONFIG_OPEN_ALWAYS - Try to open the file. If
  25. the file doesn't exist, create it.
  26. :return: Can return the following values:
  27. - CONFIG_SUCCESS - Successful
  28. - CONFIG_FILENOTFOUND - File not found
  29. - CONFIG_ERROR - Generic error
  30. ----------------------
  31. .. function:: int config_open_string(config_t **config, const char *str)
  32. Opens configuration data via a string rather than a file.
  33. :param config: Pointer that receives a pointer to a new configuration
  34. file object (if successful)
  35. :param str: Configuration string
  36. :return: Can return the following values:
  37. - CONFIG_SUCCESS - Successful
  38. - CONFIG_FILENOTFOUND - File not found
  39. - CONFIG_ERROR - Generic error
  40. ----------------------
  41. .. function:: int config_save(config_t *config)
  42. Saves configuration data to a file (if associated with a file).
  43. :param config: Configuration object
  44. :return: Can return the following values:
  45. - CONFIG_SUCCESS - Successful
  46. - CONFIG_FILENOTFOUND - File not found
  47. - CONFIG_ERROR - Generic error
  48. ----------------------
  49. .. function:: int config_save_safe(config_t *config, const char *temp_ext, const char *backup_ext)
  50. Saves configuration data and minimizes overwrite corruption risk.
  51. Saves the file with the file name
  52. :param config: Configuration object
  53. :param temp_ext: Temporary extension for the new file
  54. :param backup_ext: Backup extension for the old file. Can be *NULL*
  55. if no backup is desired.
  56. :return: Can return the following values:
  57. - CONFIG_SUCCESS - Successful
  58. - CONFIG_FILENOTFOUND - File not found
  59. - CONFIG_ERROR - Generic error
  60. ----------------------
  61. .. function:: void config_close(config_t *config)
  62. Closes the configuration object.
  63. :param config: Configuration object
  64. ----------------------
  65. .. function:: size_t config_num_sections(config_t *config)
  66. Returns the number of sections.
  67. :param config: Configuration object
  68. :return: Number of configuration sections
  69. ----------------------
  70. .. function:: const char *config_get_section(config_t *config, size_t idx)
  71. Returns a section name based upon its index.
  72. :param config: Configuration object
  73. :param idx: Index of the section
  74. :return: The section's name
  75. Set/Get Functions
  76. -----------------
  77. .. function:: void config_set_string(config_t *config, const char *section, const char *name, const char *value)
  78. Sets a string value.
  79. :param config: Configuration object
  80. :param section: The section of the value
  81. :param name: The value name
  82. :param value: The string value
  83. ----------------------
  84. .. function:: void config_set_int(config_t *config, const char *section, const char *name, int64_t value)
  85. Sets an integer value.
  86. :param config: Configuration object
  87. :param section: The section of the value
  88. :param name: The value name
  89. :param value: The integer value
  90. ----------------------
  91. .. function:: void config_set_uint(config_t *config, const char *section, const char *name, uint64_t value)
  92. Sets an unsigned integer value.
  93. :param config: Configuration object
  94. :param section: The section of the value
  95. :param name: The value name
  96. :param value: The unsigned integer value
  97. ----------------------
  98. .. function:: void config_set_bool(config_t *config, const char *section, const char *name, bool value)
  99. Sets a boolean value.
  100. :param config: Configuration object
  101. :param section: The section of the value
  102. :param name: The value name
  103. :param value: The boolean value
  104. ----------------------
  105. .. function:: void config_set_double(config_t *config, const char *section, const char *name, double value)
  106. Sets a floating point value.
  107. :param config: Configuration object
  108. :param section: The section of the value
  109. :param name: The value name
  110. :param value: The floating point value
  111. ----------------------
  112. .. function:: const char *config_get_string(config_t *config, const char *section, const char *name)
  113. Gets a string value. If the value is not set, it will use the
  114. default value. If there is no default value, it will return *NULL*.
  115. :param config: Configuration object
  116. :param section: The section of the value
  117. :param name: The value name
  118. :return: The string value
  119. ----------------------
  120. .. function:: int64_t config_get_int(config_t *config, const char *section, const char *name)
  121. Gets an integer value. If the value is not set, it will use the
  122. default value. If there is no default value, it will return 0.
  123. :param config: Configuration object
  124. :param section: The section of the value
  125. :param name: The value name
  126. :return: The integer value
  127. ----------------------
  128. .. function:: uint64_t config_get_uint(config_t *config, const char *section, const char *name)
  129. Gets an unsigned integer value. If the value is not set, it will use
  130. the default value. If there is no default value, it will return 0.
  131. :param config: Configuration object
  132. :param section: The section of the value
  133. :param name: The value name
  134. :return: The unsigned integer value
  135. ----------------------
  136. .. function:: bool config_get_bool(config_t *config, const char *section, const char *name)
  137. Gets a boolean value. If the value is not set, it will use the
  138. default value. If there is no default value, it will return false.
  139. :param config: Configuration object
  140. :param section: The section of the value
  141. :param name: The value name
  142. :return: The boolean value
  143. ----------------------
  144. .. function:: double config_get_double(config_t *config, const char *section, const char *name)
  145. Gets a floating point value. If the value is not set, it will use
  146. the default value. If there is no default value, it will return 0.0.
  147. :param config: Configuration object
  148. :param section: The section of the value
  149. :param name: The value name
  150. :return: The floating point value
  151. ----------------------
  152. .. function:: bool config_remove_value(config_t *config, const char *section, const char *name)
  153. Removes a value. Does not remove the default value if any.
  154. :param config: Configuration object
  155. :param section: The section of the value
  156. :param name: The value name
  157. Default Value Functions
  158. -----------------------
  159. .. function:: int config_open_defaults(config_t *config, const char *file)
  160. Opens a file and uses it for default values.
  161. :param config: Configuration object
  162. :param file: The file to open for default values
  163. ----------------------
  164. .. function:: void config_set_default_string(config_t *config, const char *section, const char *name, const char *value)
  165. Sets a default string value.
  166. :param config: Configuration object
  167. :param section: The section of the value
  168. :param name: The value name
  169. :param value: The string value
  170. ----------------------
  171. .. function:: void config_set_default_int(config_t *config, const char *section, const char *name, int64_t value)
  172. Sets a default integer value.
  173. :param config: Configuration object
  174. :param section: The section of the value
  175. :param name: The value name
  176. :param value: The integer value
  177. ----------------------
  178. .. function:: void config_set_default_uint(config_t *config, const char *section, const char *name, uint64_t value)
  179. Sets a default unsigned integer value.
  180. :param config: Configuration object
  181. :param section: The section of the value
  182. :param name: The value name
  183. :param value: The unsigned integer value
  184. ----------------------
  185. .. function:: void config_set_default_bool(config_t *config, const char *section, const char *name, bool value)
  186. Sets a default boolean value.
  187. :param config: Configuration object
  188. :param section: The section of the value
  189. :param name: The value name
  190. :param value: The boolean value
  191. ----------------------
  192. .. function:: void config_set_default_double(config_t *config, const char *section, const char *name, double value)
  193. Sets a default floating point value.
  194. :param config: Configuration object
  195. :param section: The section of the value
  196. :param name: The value name
  197. :param value: The floating point value
  198. ----------------------
  199. .. function:: const char *config_get_default_string(config_t *config, const char *section, const char *name)
  200. Gets a default string value. If there is no default value, it will return *NULL*.
  201. :param config: Configuration object
  202. :param section: The section of the value
  203. :param name: The value name
  204. :return: The default string value
  205. ----------------------
  206. .. function:: int64_t config_get_default_int(config_t *config, const char *section, const char *name)
  207. Gets a default integer value. If there is no default value, it will return 0.
  208. :param config: Configuration object
  209. :param section: The section of the value
  210. :param name: The value name
  211. :return: The integer value
  212. ----------------------
  213. .. function:: uint64_t config_get_default_uint(config_t *config, const char *section, const char *name)
  214. Gets a default unsigned integer value. If there is no default value, it will return 0.
  215. :param config: Configuration object
  216. :param section: The section of the value
  217. :param name: The value name
  218. :return: The unsigned integer value
  219. ----------------------
  220. .. function:: bool config_get_default_bool(config_t *config, const char *section, const char *name)
  221. Gets a default boolean value. If there is no default value, it will return false.
  222. :param config: Configuration object
  223. :param section: The section of the value
  224. :param name: The value name
  225. :return: The boolean value
  226. ----------------------
  227. .. function:: double config_get_default_double(config_t *config, const char *section, const char *name)
  228. Gets a default floating point value. If there is no default value, it will return 0.0.
  229. :param config: Configuration object
  230. :param section: The section of the value
  231. :param name: The value name
  232. :return: The floating point value
  233. -----------------------
  234. .. function:: bool config_has_user_value(config_t *config, const char *section, const char *name)
  235. Returns whether a value is user-set (true) or default/none (false).
  236. :param config: Configuration object
  237. :param section: The section of the value
  238. :param name: The value name
  239. :return: Whether a user value exists
  240. -----------------------
  241. .. function:: bool config_has_default_value(config_t *config, const char *section, const char *name)
  242. Returns whether a value has a default set.
  243. :param config: Configuration object
  244. :param section: The section of the value
  245. :param name: The value name
  246. :return: Whether a user value exists