Lely core libraries 2.3.4
config.c File Reference

This file is part of the utilities library; it contains the implementation of the configuration functions. More...

#include "util.h"
#include <lely/util/cmp.h>
#include <lely/util/config.h>
#include <lely/util/errnum.h>
#include <lely/util/rbtree.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
Include dependency graph for config.c:

Go to the source code of this file.

Data Structures

struct  __config
 A configuration struct. More...
 
struct  config_section
 A section in a configuration struct. More...
 
struct  config_entry
 An entry in a configuration section. More...
 

Functions

config_tconfig_create (int flags)
 Creates a new configuration struct with an unnamed empty root section.
 
void config_destroy (config_t *config)
 Destroys a configuration struct.
 
size_t config_get_sections (const config_t *config, size_t n, const char **sections)
 Retrieves a list of section names from a configuration struct.
 
size_t config_get_keys (const config_t *config, const char *section, size_t n, const char **keys)
 Retrieves a list of key names from a section in a configuration struct.
 
const char * config_get (const config_t *config, const char *section, const char *key)
 Retrieves a key from a configuration struct.
 
const char * config_set (config_t *config, const char *section, const char *key, const char *value)
 Sets a key in or removes a key from a configuration struct.
 
void config_foreach (const config_t *config, config_foreach_func_t *func, void *data)
 Invokes a function for each key in a configuration struct.
 

Detailed Description

This file is part of the utilities library; it contains the implementation of the configuration functions.

See also
lely/util/config.h
Author
J. S. Seldenthuis jseld.nosp@m.enth.nosp@m.uis@l.nosp@m.ely..nosp@m.com

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Definition in file config.c.

Function Documentation

◆ config_create()

config_t * config_create ( int  flags)

Creates a new configuration struct with an unnamed empty root section.

Parameters
flagseither 0 or CONFIG_CASE.
Returns
a pointer to a new configuration struct, or NULL on error. In the latter case, the error number can be obtained with get_errc().
See also
config_destroy()

Definition at line 115 of file config.c.

◆ config_destroy()

void config_destroy ( config_t config)

Destroys a configuration struct.

See also
config_create()

Definition at line 140 of file config.c.

◆ config_get_sections()

size_t config_get_sections ( const config_t config,
size_t  n,
const char **  sections 
)

Retrieves a list of section names from a configuration struct.

Parameters
configa pointer to a configuration struct.
nthe maximum number of sections to return.
sectionsan array of at least n pointers (can be NULL). On success, *sections contains the pointers to the section names.
Returns
the total number of sections in the configuration struct (which may may be different from n).
See also
config_get_keys()

Definition at line 149 of file config.c.

◆ config_get_keys()

size_t config_get_keys ( const config_t config,
const char *  section,
size_t  n,
const char **  keys 
)

Retrieves a list of key names from a section in a configuration struct.

Parameters
configa pointer to a configuration struct.
sectiona pointer to the name of the section. If section is NULL or "", the root section is used instead.
nthe maximum number of sections to return.
keysan array of at least n pointers (can be NULL). On success, *keys contains the pointers to the key names.
Returns
the total number of keys in the section (which may may be different from n).
See also
config_get_sections()

Definition at line 166 of file config.c.

◆ config_get()

const char * config_get ( const config_t config,
const char *  section,
const char *  key 
)

Retrieves a key from a configuration struct.

Parameters
configa pointer to a configuration struct.
sectiona pointer to the name of the section. If section is NULL or "", the root section is used instead.
keya pointer to the name of the key.
Returns
a pointer to the value of the key, or NULL if not found.

Definition at line 193 of file config.c.

◆ config_set()

const char * config_set ( config_t config,
const char *  section,
const char *  key,
const char *  value 
)

Sets a key in or removes a key from a configuration struct.

Parameters
configa pointer to a configuration struct.
sectiona pointer to the name of the section. If section is NULL or "", the root section is used instead.
keya pointer to the name of the key. This name is duplicated on success, so the caller need not guarantee the lifetime of the string.
valuea pointer to the new value of the key. If value is NULL, the key is removed. Like key, the string is duplicated on success.
Returns
a pointer to the value (which will differ from value due to duplication), or NULL on error or when the key is deleted. In case of an error, the error number can be obtained with get_errc().

Definition at line 218 of file config.c.

◆ config_foreach()

void config_foreach ( const config_t config,
config_foreach_func_t func,
void *  data 
)

Invokes a function for each key in a configuration struct.

Parameters
configa pointer to a configuration struct.
funca pointer to the function to be invoked.
dataa pointer to user-specified data (can be NULL). data is passed as the last parameter to func.

Definition at line 238 of file config.c.