The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
conf.h File Reference

settings files: sections of "key = value" lines. More...

#include <stdbool.h>
#include <stdint.h>

Go to the source code of this file.

Macros

#define CONF_SYSTEM_DIR   "/etc/ginnos"
#define CONF_USER_DIR   ".config/ginnos"

Typedefs

typedef struct conf conf_t

Functions

conf_t * conf_open (const char *name)
 open the settings called name: /etc/ginnos/<name>.conf and ~/.config/ginnos/<name>.conf.
conf_t * conf_load (const char *system_path, const char *user_path)
 open two files.
conf_t * conf_from_text (const char *system_text, const char *user_text)
 settings from text in memory (for tests).
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.
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.
char * conf_user_text (const conf_t *conf)
 the user's file as text, as conf_save writes it (for tests).
int conf_bad_lines (const conf_t *conf)
 how many bad lines the two files had.

Detailed Description

settings files: sections of "key = value" lines.

a settings file looks like this:

# the look of the desktop
[theme]
mode = dark
accent = #3b82f6
font_size = 13
  • a line that starts with '#' or ';' is a comment. there are no comments after a value, so "#3b82f6" is a value.
  • keys before the first [section] are in the section "".
  • spaces around the key and the value are removed. a value in double quotes keeps its spaces (" x "); the quotes are removed.
  • if a key is in a section two times, the last one is used.
  • a bad line (no '=', no key, a [section without ']') is kept as it is when the file is written again, and gives a warning on stderr.

a conf_t has two layers: the system defaults (/etc/ginnos/<name>.conf, read only) and the user's values (~/.config/ginnos/<name>.conf). a get looks at the user's values first. a set changes only the user's values, and conf_save writes only the user's file. comments, blank lines and the order of the lines stay as they were.

Macro Definition Documentation

◆ CONF_SYSTEM_DIR

#define CONF_SYSTEM_DIR   "/etc/ginnos"

◆ CONF_USER_DIR

#define CONF_USER_DIR   ".config/ginnos"

Typedef Documentation

◆ conf_t

typedef struct conf conf_t

Function Documentation

◆ conf_open()

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.

Parameters
nameletters, digits, '-' and '_' only.
Returns
the settings, or NULL (a bad name, or no memory).

◆ conf_load()

conf_t * conf_load ( const char * system_path,
const char * user_path )

open two files.

user_path is the file conf_save writes.

Parameters
system_pathmay be NULL (no defaults).
user_pathmay be NULL (conf_save then fails).

◆ conf_from_text()

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.

Parameters
system_textthe defaults, may be NULL.
user_textthe user's values, may be NULL.

◆ conf_free()

void conf_free ( conf_t * conf)

◆ conf_get()

const char * conf_get ( const conf_t * conf,
const char * section,
const char * key,
const char * fallback )

a value.

Returns
the user's value, else the system value, else fallback. the string is valid until the next set, unset or free.

◆ conf_section()

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.

Returns
the name, or NULL when index is past the last section. the string is valid until the next set, unset or free.

◆ conf_get_int()

long conf_get_int ( const conf_t * conf,
const char * section,
const char * key,
long fallback )

a whole number: decimal, or hexadecimal with 0x.

Returns
fallback if the key is not there or is not a number.

◆ conf_get_bool()

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).

Returns
fallback if the key is not there or is not one of these.

◆ conf_get_color()

uint32_t conf_get_color ( const conf_t * conf,
const char * section,
const char * key,
uint32_t fallback )

a colour: #rrggbb or #rrggbbaa.

Returns
0xAARRGGBB, not premultiplied (gfx_rgba makes a gfx colour of it); alpha is 0xFF for #rrggbb. fallback if the key is not there or is not a colour.

◆ conf_set()

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.

Parameters
sectiona name without '[', ']' or a line break.
keya name without '=', a line break, or a '#' or ';' or '[' at its start.
valuetext without a line break.
Returns
0, or -1 (a bad name or value, or no memory).

◆ conf_set_int()

int conf_set_int ( conf_t * conf,
const char * section,
const char * key,
long value )

◆ conf_set_bool()

int conf_set_bool ( conf_t * conf,
const char * section,
const char * key,
bool value )

◆ conf_set_color()

int conf_set_color ( conf_t * conf,
const char * section,
const char * key,
uint32_t argb )

◆ conf_unset()

int conf_unset ( conf_t * conf,
const char * section,
const char * key )

remove the user's value, so the system value is used again.

Returns
0 (also when there was no user value), or -1 (bad arguments).

◆ conf_save()

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.

Returns
0, or -1 with errno set.

◆ conf_user_text()

char * conf_user_text ( const conf_t * conf)

the user's file as text, as conf_save writes it (for tests).

Returns
a string to free, or NULL (no memory).

◆ conf_bad_lines()

int conf_bad_lines ( const conf_t * conf)

how many bad lines the two files had.