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

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.

Detailed Description

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.

Macro Definition Documentation

◆ NAME_MAX_LEN

#define NAME_MAX_LEN   64

◆ PATH_LEN

#define PATH_LEN   256

Enumeration Type Documentation

◆ line_kind_t

Enumerator
LINE_OTHER 
LINE_SECTION 
LINE_VALUE 
LINE_BAD 

Function Documentation

◆ 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_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_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_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_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_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_bad_lines()

int conf_bad_lines ( const conf_t * conf)

how many bad lines the two files had.