Fork me on GitHub
Loading...
Searching...
No Matches
Macros | Functions
config.c File Reference

Configuration files parsing. More...

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <libgen.h>
#include <libconfig.h>
#include "config.h"
#include "debug.h"
#include "utils.h"
Include dependency graph for config.c:

Macros

#define JANUS_CONFIG_INDENT   4
 

Functions

janus_configjanus_config_parse (const char *config_file)
 Method to parse an INI configuration file.
 
janus_configjanus_config_create (const char *name)
 Method to create a new, empty, configuration.
 
janus_config_itemjanus_config_item_create (const char *name, const char *value)
 Method to create a new janus_config_item instance from name and value.
 
janus_config_categoryjanus_config_category_create (const char *name)
 Method to create a new janus_config_category instance.
 
janus_config_arrayjanus_config_array_create (const char *name)
 Method to create a new janus_config_array instance.
 
void janus_config_container_destroy (janus_config_container *container)
 Helper method to quickly destroy an item, category or array.
 
janus_config_containerjanus_config_get (janus_config *config, janus_config_container *parent, janus_config_type type, const char *name)
 Helper method to quickly get an item, category, or array.
 
janus_config_containerjanus_config_get_create (janus_config *config, janus_config_container *parent, janus_config_type type, const char *name)
 Same as janus_config_get, but creates the element if it doesn't exist.
 
janus_config_containerjanus_config_search (janus_config *config,...)
 Helper method to quickly lookup an item, category, or array.
 
int janus_config_add (janus_config *config, janus_config_container *container, janus_config_container *item)
 Add an item/category/array instance to a category or array.
 
int janus_config_remove (janus_config *config, janus_config_container *container, const char *name)
 Remove an existing item with the specific name from a category/array.
 
GList * janus_config_get_items (janus_config *config, janus_config_container *parent)
 Helper method to return the list of plain items, either in root or from a parent.
 
GList * janus_config_get_categories (janus_config *config, janus_config_container *parent)
 Helper method to return the list of categories, either in root or from a parent.
 
GList * janus_config_get_arrays (janus_config *config, janus_config_container *parent)
 Helper method to return the list of arrays, either in root or from a parent.
 
void janus_config_print (janus_config *config)
 Helper method to print a configuration on the standard output.
 
void janus_config_print_as (janus_config *config, int level)
 Helper method to print a configuration on the standard output using a different logging level than LOG_VERB.
 
gboolean janus_config_save (janus_config *config, const char *folder, const char *filename)
 Helper method to save a configuration to a file.
 
void janus_config_destroy (janus_config *config)
 Destroy a configuration container instance.
 

Detailed Description

Configuration files parsing.

Author
Lorenzo Miniero loren.nosp@m.zo@m.nosp@m.eetec.nosp@m.ho.c.nosp@m.om

Implementation of a parser of INI and libconfig configuration files.

Core

Macro Definition Documentation

◆ JANUS_CONFIG_INDENT

#define JANUS_CONFIG_INDENT   4

Function Documentation

◆ janus_config_add()

int janus_config_add ( janus_config config,
janus_config_container parent,
janus_config_container item 
)

Add an item/category/array instance to a category or array.

Note
If adding to a category and the item/category/array already exists, it is replaced; it is appended if the target is an array instead, where duplicates are accepted.
Parameters
[in]configThe configuration instance
[in]parentThe category or array to add the item to, if any
[in]itemThe item/category/array to add
Returns
0 if successful, a negative integer otherwise

◆ janus_config_array_create()

janus_config_array * janus_config_array_create ( const char *  name)

Method to create a new janus_config_array instance.

Parameters
[in]nameName to give to the array
Returns
A valid janus_config_array instance if successful, NULL otherwise

◆ janus_config_category_create()

janus_config_category * janus_config_category_create ( const char *  name)

Method to create a new janus_config_category instance.

Parameters
[in]nameName to give to the category
Returns
A pointer to a valid janus_config_category instance if successful, NULL otherwise

◆ janus_config_container_destroy()

void janus_config_container_destroy ( janus_config_container container)

Helper method to quickly destroy an item, category or array.

Note
This method also destroys anything it contains, if it's a category or array, but will not unlink the object from its parent: this is up to the caller
Parameters
[in]containerThe item/category/array to destroy

◆ janus_config_create()

janus_config * janus_config_create ( const char *  name)

Method to create a new, empty, configuration.

Parameters
[in]nameName to give to the configuration
Returns
A pointer to a valid janus_config instance if successful, NULL otherwise

◆ janus_config_destroy()

void janus_config_destroy ( janus_config config)

Destroy a configuration container instance.

Parameters
[in]configThe configuration to destroy

◆ janus_config_get()

janus_config_container * janus_config_get ( janus_config config,
janus_config_container parent,
janus_config_type  type,
const char *  name 
)

Helper method to quickly get an item, category, or array.

Note
If the parent container is NULL, the lookup is done at the root. If something is found but type doesn't match (name is an array but we're looking for a category), NULL is returned.
Parameters
[in]configThe configuration instance
[in]parentThe parent container (category or array), if any
[in]typeThe type of container to look for
[in]nameThe name of the item/category/array to look for
Returns
A pointer to a valid janus_config_container instance if successful, NULL otherwise

◆ janus_config_get_arrays()

GList * janus_config_get_arrays ( janus_config config,
janus_config_container parent 
)

Helper method to return the list of arrays, either in root or from a parent.

Note
The method returns a new GList: it's up to the caller to free it. The values of the list data must NOT be freed, though, as it's just linked from the configuration.
Parameters
[in]configThe configuration instance
[in]parentThe parent container (category or array), if any
Returns
A pointer to the categories GLib linked list of arrays if successful, NULL otherwise

◆ janus_config_get_categories()

GList * janus_config_get_categories ( janus_config config,
janus_config_container parent 
)

Helper method to return the list of categories, either in root or from a parent.

Note
The method returns a new GList: it's up to the caller to free it. The values of the list data must NOT be freed, though, as it's just linked from the configuration.
Parameters
[in]configThe configuration instance
[in]parentThe parent container (category or array), if any
Returns
A pointer to the categories GLib linked list of categories if successful, NULL otherwise

◆ janus_config_get_create()

janus_config_container * janus_config_get_create ( janus_config config,
janus_config_container parent,
janus_config_type  type,
const char *  name 
)

Same as janus_config_get, but creates the element if it doesn't exist.

Note
Nothing is created if type is janus_config_type_any.
Parameters
[in]configThe configuration instance
[in]parentThe parent container (category or array), if any
[in]typeThe type of container to look for
[in]nameThe name of the item/category/array to look for
Returns
A pointer to a valid janus_config_container instance if successful, NULL otherwise

◆ janus_config_get_items()

GList * janus_config_get_items ( janus_config config,
janus_config_container parent 
)

Helper method to return the list of plain items, either in root or from a parent.

Note
The method returns a new GList: it's up to the caller to free it. The values of the list data must NOT be freed, though, as it's just linked from the configuration.
Parameters
[in]configThe configuration instance
[in]parentThe parent container (category or array), if any
Returns
A pointer to the categories GLib linked list of items if successful, NULL otherwise

◆ janus_config_item_create()

janus_config_item * janus_config_item_create ( const char *  name,
const char *  value 
)

Method to create a new janus_config_item instance from name and value.

Parameters
[in]nameName to give to the item
[in]valueValue of the item (optional)
Returns
A valid janus_config_item instance if successful, NULL otherwise

◆ janus_config_parse()

janus_config * janus_config_parse ( const char *  config_file)

Method to parse an INI configuration file.

Parameters
[in]config_filePath to the configuration file
Returns
A pointer to a valid janus_config instance if successful, NULL otherwise

◆ janus_config_print()

void janus_config_print ( janus_config config)

Helper method to print a configuration on the standard output.

Note
This prints with LOG_VERB: if you need the configuration to be visible at a different debugging level, use janus_config_print_as instead
Parameters
[in]configThe configuration to print

◆ janus_config_print_as()

void janus_config_print_as ( janus_config config,
int  level 
)

Helper method to print a configuration on the standard output using a different logging level than LOG_VERB.

Parameters
[in]configThe configuration to print
[in]levelThe debugging level to use

◆ janus_config_remove()

int janus_config_remove ( janus_config config,
janus_config_container parent,
const char *  name 
)

Remove an existing item with the specific name from a category/array.

Parameters
[in]configThe configuration instance
[in]parentThe category/array to remove the item from, if any
[in]nameThe name of the item/category/array to remove
Returns
0 if successful, a negative integer otherwise

◆ janus_config_save()

gboolean janus_config_save ( janus_config config,
const char *  folder,
const char *  filename 
)

Helper method to save a configuration to a file.

Parameters
[in]configThe configuration to sav
[in]folderThe folder the file should be saved to
[in]filenameThe file name, extension included (should be .jcfg, or .cfg for legacy INI files)
Returns
0 if successful, a negative integer otherwise

◆ janus_config_search()

janus_config_container * janus_config_search ( janus_config config,
  ... 
)

Helper method to quickly lookup an item, category, or array.

Note
If something is found but type doesn't match (name is an array but we're looking for a category), NULL is returned.
Parameters
[in]configThe configuration instance
Returns
A pointer to a valid janus_config_container instance if successful, NULL otherwise