PatchworkOS  966e257
A non-POSIX operating system.
Loading...
Searching...
No Matches
config.h
Go to the documentation of this file.
1#ifndef CONFIG_CONFIG_H
2#define CONFIG_CONFIG_H 1
3
4#include <errno.h>
5#include <stdbool.h>
6#include <stdint.h>
7
8#if defined(__cplusplus)
9extern "C"
10{
11#endif
12
13/**
14 * @brief System configuration files.
15 * @defgroup libpatchwork_config Config files
16 * @ingroup libpatchwork
17 *
18 * Patchwork uses a `/cfg` folder for all system configuration files. These files are simple INI style text files that
19 * store key-value pairs in sections.
20 *
21 * @todo The current system is rather simplistic and in the future it might, if i can be bothered, be worth implementing
22 * a database like configuration system.
23 *
24 * @{
25 */
26
27/**
28 * @brief Opaque configuration structure.
29 * @struct config_t
30 */
31typedef struct config config_t;
32
33/**
34 * @brief Configuration array structure.
35 * @struct config_array_t
36 */
37typedef struct config_array
38{
39 char** items;
42
43/**
44 * @brief Open a configuration file.
45 *
46 * All configuration files have this full path: `/cfg/<prefix>-<name>.cfg`.
47 *
48 * The goal is that each system or application uses its own prefix to avoid name collisions.
49 *
50 * @param prefix The prefix of the configuration file, for example `theme` for theme related settings.
51 * @param name The name of the configuration file, for example `colors` for color related settings.
52 * @return On success, the opened configuration file. On failure, returns `NULL`.
53 */
54config_t* config_open(const char* prefix, const char* name);
55
56/**
57 * @brief Close a configuration file.
58 *
59 * @param config The configuration file to close.
60 */
61void config_close(config_t* config);
62
63/**
64 * @brief Get a value as a string by index from a configuration file.
65 *
66 * @param config The configuration file.
67 * @param section The section to get the value from, case insensitive.
68 * @param index The index of the value to get.
69 * @param fallback A default value to return if the index is out of bounds.
70 * @param outValue The output value, will be set to `fallback` if the index is out of bounds.
71 * @param outKey The output key, will be set to `fallback` if the index is out of bounds.
72 */
73void config_get(config_t* config, const char* section, uint64_t index, const char* fallback, const char** outValue,
74 const char** outKey);
75
76/**
77 * @brief Get a string value from a configuration file.
78 *
79 * The value returned is owned by the configuration system and should not be freed or modified.
80 *
81 * @param config The configuration file.
82 * @param section The section to get the value from, case insensitive.
83 * @param key The key to get the value for, case insensitive.
84 * @param fallback A default value to return if the key is not found.
85 * @return The string value if found, or `fallback` otherwise.
86 */
87const char* config_get_string(config_t* config, const char* section, const char* key, const char* fallback);
88
89/**
90 * @brief Get an integer value from a configuration file.
91 *
92 * @param config The configuration file.
93 * @param section The section to get the value from, case insensitive.
94 * @param key The key to get the value for, case insensitive.
95 * @param fallback A default value to return if the key is not found or cannot be parsed as an integer.
96 * @return The integer value if found and parsed, or `fallback` otherwise.
97 */
98int64_t config_get_int(config_t* config, const char* section, const char* key, int64_t fallback);
99
100/**
101 * @brief Get a boolean value from a configuration file.
102 *
103 * Recognized "true" values (case-insensitive): "true", "yes", "on", "1".
104 * Recognized "false" values (case-insensitive): "false", "no", "off", "0".
105 *
106 * @param config The configuration file.
107 * @param section The section to get the value from, case insensitive.
108 * @param key The key to get the value for, case insensitive.
109 * @param fallback A default value to return if not found or unrecognized.
110 * @return The boolean value if found and parsed, or `fallback` otherwise.
111 */
112bool config_get_bool(config_t* config, const char* section, const char* key, bool fallback);
113
114/**
115 * @brief Get an array of strings from a configuration file.
116 *
117 * Parses the string value into an array of strings, split by commas and with the whitespace trimmed.
118 *
119 * @param config The configuration file.
120 * @param section The section to get the value from, case insensitive.
121 * @param key The key to get the value for, case insensitive.
122 * @return The configuration array or an empty array, will only be `NULL` on memory allocation failure.
123 */
124config_array_t* config_get_array(config_t* config, const char* section, const char* key);
125
126/**
127 * @brief Free a configuration array.
128 *
129 * @param array The configuration array to free.
130 */
132
133/** @} */
134
135#if defined(__cplusplus)
136}
137#endif
138
139#endif
config_array_t * config_get_array(config_t *config, const char *section, const char *key)
Get an array of strings from a configuration file.
Definition config.c:335
config_t * config_open(const char *prefix, const char *name)
Open a configuration file.
Definition config.c:93
int64_t config_get_int(config_t *config, const char *section, const char *key, int64_t fallback)
Get an integer value from a configuration file.
Definition config.c:285
const char * config_get_string(config_t *config, const char *section, const char *key, const char *fallback)
Get a string value from a configuration file.
Definition config.c:263
void config_array_free(config_array_t *array)
Free a configuration array.
Definition config.c:423
bool config_get_bool(config_t *config, const char *section, const char *key, bool fallback)
Get a boolean value from a configuration file.
Definition config.c:308
void config_close(config_t *config)
Close a configuration file.
Definition config.c:202
void config_get(config_t *config, const char *section, uint64_t index, const char *fallback, const char **outValue, const char **outKey)
Get a value as a string by index from a configuration file.
Definition config.c:228
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
__INT64_TYPE__ int64_t
Definition stdint.h:16
Configuration array structure.
Definition config.h:38
char ** items
Definition config.h:39
uint64_t length
Definition config.h:40
Opaque configuration structure.
Definition config.c:28