|
ginnOS
The reference: libc, the libraries and the kernel
|
libconf: settings files with sections and "key = value" lines. More...
#include "conf.h"#include <ctype.h>#include <errno.h>#include <fcntl.h>#include <pwd.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <sys/stat.h>#include <unistd.h>Macros | |
| #define | NAME_MAX_LEN 64 |
| #define | PATH_LEN 256 |
Enumerations | |
| enum | line_kind_t { LINE_OTHER , LINE_SECTION , LINE_VALUE , LINE_BAD } |
Functions | |
| conf_t * | conf_from_text (const char *system_text, const char *user_text) |
| settings from text in memory (for tests). | |
| conf_t * | conf_load (const char *system_path, const char *user_path) |
| open two files. | |
| conf_t * | conf_open (const char *name) |
| open the settings called name: /etc/ginnos/<name>.conf and ~/.config/ginnos/<name>.conf. | |
| void | conf_free (conf_t *conf) |
| const char * | conf_get (const conf_t *conf, const char *section, const char *key, const char *fallback) |
| a value. | |
| const char * | conf_section (const conf_t *conf, int index) |
| the name of a section, to go through all of them: index 0, 1, ... until NULL. | |
| long | conf_get_int (const conf_t *conf, const char *section, const char *key, long fallback) |
| a whole number: decimal, or hexadecimal with 0x. | |
| bool | conf_get_bool (const conf_t *conf, const char *section, const char *key, bool fallback) |
| true, yes, on, 1 / false, no, off, 0 (any letter case). | |
| uint32_t | conf_get_color (const conf_t *conf, const char *section, const char *key, uint32_t fallback) |
| a colour: #rrggbb or #rrggbbaa. | |
| int | conf_set (conf_t *conf, const char *section, const char *key, const char *value) |
| set a user value. | |
| int | conf_set_int (conf_t *conf, const char *section, const char *key, long value) |
| int | conf_set_bool (conf_t *conf, const char *section, const char *key, bool value) |
| int | conf_set_color (conf_t *conf, const char *section, const char *key, uint32_t argb) |
| int | conf_unset (conf_t *conf, const char *section, const char *key) |
| remove the user's value, so the system value is used again. | |
| char * | conf_user_text (const conf_t *conf) |
| the user's file as text, as conf_save writes it (for tests). | |
| int | conf_save (const conf_t *conf) |
| write the user's file: to a new file, fsync, then rename over the old one, so a crash leaves the old file or the new one. | |
| int | conf_bad_lines (const conf_t *conf) |
| how many bad lines the two files had. | |
libconf: settings files with sections and "key = value" lines.
each file is kept as a list of its lines. a line keeps its text, so a file is written again with its comments, blank lines and bad lines as they were; a set changes or adds only the one line.
| #define NAME_MAX_LEN 64 |
| #define PATH_LEN 256 |
| enum line_kind_t |
| conf_t * conf_from_text | ( | const char * | system_text, |
| const char * | user_text ) |
settings from text in memory (for tests).
conf_save fails on them.
| system_text | the defaults, may be NULL. |
| user_text | the user's values, may be NULL. |
| conf_t * conf_load | ( | const char * | system_path, |
| const char * | user_path ) |
open two files.
user_path is the file conf_save writes.
| system_path | may be NULL (no defaults). |
| user_path | may be NULL (conf_save then fails). |
| conf_t * conf_open | ( | const char * | name | ) |
open the settings called name: /etc/ginnos/<name>.conf and ~/.config/ginnos/<name>.conf.
a file that is not there is empty.
| name | letters, digits, '-' and '_' only. |
| void conf_free | ( | conf_t * | conf | ) |
| const char * conf_get | ( | const conf_t * | conf, |
| const char * | section, | ||
| const char * | key, | ||
| const char * | fallback ) |
a value.
| const char * conf_section | ( | const conf_t * | conf, |
| int | index ) |
the name of a section, to go through all of them: index 0, 1, ... until NULL.
the sections of the system file come first, then the sections that only the user's file has, each in file order and each name one time. only a [name] line makes a section; keys before the first [section] are not one.
| long conf_get_int | ( | const conf_t * | conf, |
| const char * | section, | ||
| const char * | key, | ||
| long | fallback ) |
a whole number: decimal, or hexadecimal with 0x.
true, yes, on, 1 / false, no, off, 0 (any letter case).
| uint32_t conf_get_color | ( | const conf_t * | conf, |
| const char * | section, | ||
| const char * | key, | ||
| uint32_t | fallback ) |
a colour: #rrggbb or #rrggbbaa.
| int conf_set | ( | conf_t * | conf, |
| const char * | section, | ||
| const char * | key, | ||
| const char * | value ) |
set a user value.
the line is changed where it is; a new key goes after the last line of its section, and a new section goes at the end.
| section | a name without '[', ']' or a line break. |
| key | a name without '=', a line break, or a '#' or ';' or '[' at its start. |
| value | text without a line break. |
| int conf_set_int | ( | conf_t * | conf, |
| const char * | section, | ||
| const char * | key, | ||
| long | value ) |
| int conf_unset | ( | conf_t * | conf, |
| const char * | section, | ||
| const char * | key ) |
remove the user's value, so the system value is used again.
| char * conf_user_text | ( | const conf_t * | conf | ) |
the user's file as text, as conf_save writes it (for tests).
| int conf_save | ( | const conf_t * | conf | ) |
write the user's file: to a new file, fsync, then rename over the old one, so a crash leaves the old file or the new one.
the directory is made if it is not there.
| int conf_bad_lines | ( | const conf_t * | conf | ) |
how many bad lines the two files had.