qLibc
qconfig.c File Reference

INI-style configuration file parser. More...

Go to the source code of this file.

Macros

#define _INCLUDE_DIRECTIVE   "@INCLUDE "

Functions

qlisttbl_t * qconfig_parse_file (qlisttbl_t *tbl, const char *filepath, char sepchar)
 Load and parse a configuration file.
qlisttbl_t * qconfig_parse_str (qlisttbl_t *tbl, const char *str, char sepchar)
 Parse a configuration string.

Detailed Description

INI-style configuration file parser.

Definition in file qconfig.c.

Macro Definition Documentation

◆ _INCLUDE_DIRECTIVE

#define _INCLUDE_DIRECTIVE   "@INCLUDE "

Definition at line 47 of file qconfig.c.

Function Documentation

◆ qconfig_parse_file()

qlisttbl_t * qconfig_parse_file ( qlisttbl_t * tbl,
const char * filepath,
char sepchar )

Load and parse a configuration file.

Parameters
tblqlisttbl_t pointer. If NULL, a new table is created.
filepathpath to the configuration file.
sepcharseparator used to split keys and values.
Returns
qlisttbl_t pointer on success, or NULL if the file cannot be loaded.
# This is the "config.conf" file.
# A line that starts with # is a comment.
@INCLUDE config.def => include the "config.def" file.
prefix=/tmp => set a fixed value. "prefix" is the key.
log=${prefix}/log => use the value of the previously defined key "prefix".
user=${%USER} => use an environment variable.
host=${!/bin/hostname -s} => run an external command and use its output.
id=${user}@${host}
# Enter the "system" section.
[system] => the key "system." with value "system" is inserted.
ostype=${%OSTYPE} => "system.ostype" is the key for this entry.
machtype=${%MACHTYPE} => "system.machtype" is the key for this entry.
# Enter the "daemon" section.
[daemon]
port=1234
name=${user}_${host}_${system.ostype}_${system.machtype}
# Leave the section and go back to the root.
[]
rev=822
# This is "config.def" file.
prefix = /usr/local
bin = ${prefix}/bin
log = ${prefix}/log
user = unknown
host = unknown
qlisttbl_t *tbl = qconfig_parse_file(NULL, "config.conf", '=', true);
tbl->debug(tbl, stdout);
[Output]
bin=/usr/local/bin? (15)
prefix=/tmp? (5)
log=/tmp/log? (9)
user=seungyoung.kim? (9)
host=eng22? (6)
id=seungyoung.kim@eng22? (15)
system.=system? (7)
system.ostype=linux? (6)
system.machtype=x86_64? (7)
daemon.=daemon? (7)
daemon.port=1234? (5)
daemon.name=seungyoung.kim_eng22_linux_x86_64? (28)
rev=822? (4)
qlisttbl_t * qconfig_parse_file(qlisttbl_t *tbl, const char *filepath, char sepchar)
Load and parse a configuration file.
Definition qconfig.c:125

Definition at line 125 of file qconfig.c.

◆ qconfig_parse_str()

qlisttbl_t * qconfig_parse_str ( qlisttbl_t * tbl,
const char * str,
char sepchar )

Parse a configuration string.

Parameters
tblqlisttbl_t pointer. If NULL, a new table is created.
strstring that contains key/value pairs.
sepcharseparator used to split keys and values.
Returns
qlisttbl_t pointer on success, or NULL on failure.
See also
qconfig_parse_file
qlisttbl_t *tbl;
tbl = qconfig_parse_str(NULL, "key = value\nhello = world", '=');
qlisttbl_t * qconfig_parse_str(qlisttbl_t *tbl, const char *str, char sepchar)
Parse a configuration string.
Definition qconfig.c:214

Definition at line 214 of file qconfig.c.