Nilorea Library
C utilities for networking, threading, graphics
CONFIGURATIONS: classic config file loading/saving

Data Structures

struct  CONFIG_FILE
 Structure of a config file. More...
 
struct  CONFIG_FILE_SECTION
 Structure of a config section. More...
 

Macros

#define config_endfor
 Foreach elements of CONFIG_FILE macro END. More...
 
#define config_foreach(__config, __section_name, __key, __val)
 Foreach elements of CONFIG_FILE macro, i.e config_foreach( config , section , key , val ); config_endfor;. More...
 
#define CONFIG_SECTION_HASH_TABLE_LEN   16
 size of the hash table of config sections entries More...
 
#define MAX_CONFIG_LINE_LEN   1024
 maximum length of a single config line More...
 

Functions

int destroy_config_file (CONFIG_FILE **cfg_file)
 Destroy a loaded config file. More...
 
char * get_config_section_value (CONFIG_FILE *cfg_file, char *section_name, int section_position, char *entry, int entry_position)
 Function to parse sections and get entries values. More...
 
int get_nb_config_file_sections (CONFIG_FILE *cfg_file, char *section_name)
 Get the number of config file with section_name. More...
 
int get_nb_config_file_sections_entries (CONFIG_FILE *cfg_file, char *section_name, int section_position, char *entry)
 Get the number of config file with section_name. More...
 
CONFIG_FILEload_config_file (char *filename, int *errors)
 load a config file More...
 
int write_config_file (CONFIG_FILE *cfg_file, char *filename)
 write a config file More...
 

Detailed Description


Data Structure Documentation

◆ CONFIG_FILE

struct CONFIG_FILE

Structure of a config file.

Examples
ex_configfile.c, and ex_fluid_config.c.

Definition at line 40 of file n_config_file.h.

+ Collaboration diagram for CONFIG_FILE:
Data Fields
char * filename
LIST * sections

◆ CONFIG_FILE_SECTION

struct CONFIG_FILE_SECTION

Structure of a config section.

Definition at line 32 of file n_config_file.h.

+ Collaboration diagram for CONFIG_FILE_SECTION:
Data Fields
HASH_TABLE * entries
char * section_name

Macro Definition Documentation

◆ config_endfor

#define config_endfor
Value:
} \
} \
} \
}

Foreach elements of CONFIG_FILE macro END.

Will cause errors if ommitted

Examples
ex_configfile.c.

Definition at line 80 of file n_config_file.h.

◆ config_foreach

#define config_foreach (   __config,
  __section_name,
  __key,
  __val 
)
Value:
if( !__config || !__config -> sections ) \
{ \
n_log( LOG_ERR , "No config file for %s" , #__config ); \
} \
else \
{ \
list_foreach( listnode , __config -> sections ) \
{ \
CONFIG_FILE_SECTION *__section = (CONFIG_FILE_SECTION *)listnode -> ptr ; \
__section_name = __section -> section_name ; \
ht_foreach( entry , __section -> entries ) \
{ \
HASH_NODE *htnode = (HASH_NODE *)entry -> ptr ; \
if( htnode && htnode -> data . ptr ) \
{ \
__key = htnode -> key ; \
__val = htnode -> data . ptr ;
Structure of a config section.
Definition: n_config_file.h:33
structure of a hash table node
Definition: n_hash.h:83
#define LOG_ERR
error conditions
Definition: n_log.h:58

Foreach elements of CONFIG_FILE macro, i.e config_foreach( config , section , key , val ); config_endfor;.

Examples
ex_configfile.c.

Definition at line 60 of file n_config_file.h.

◆ CONFIG_SECTION_HASH_TABLE_LEN

#define CONFIG_SECTION_HASH_TABLE_LEN   16

size of the hash table of config sections entries

Definition at line 29 of file n_config_file.h.

◆ MAX_CONFIG_LINE_LEN

#define MAX_CONFIG_LINE_LEN   1024

maximum length of a single config line

Definition at line 27 of file n_config_file.h.

Function Documentation

◆ destroy_config_file()

int destroy_config_file ( CONFIG_FILE **  cfg_file)

Destroy a loaded config file.

Parameters
cfg_filepointer to a loaded config file to destroy
Returns
TRUE or FALSE

Definition at line 540 of file n_config_file.c.

References __n_assert, Free, and list_destroy().

+ Here is the call graph for this function:

◆ get_config_section_value()

char * get_config_section_value ( CONFIG_FILE cfg_file,
char *  section_name,
int  section_position,
char *  entry,
int  entry_position 
)

Function to parse sections and get entries values.

Parameters
cfg_filename of config file
section_namename of section
section_positionsection number
entryentry name
entry_positionentry number
Returns
the value of the named entry in section[ position ]

Definition at line 490 of file n_config_file.c.

References __n_assert, _nstr, free_nstr, ht_get_ptr(), list_foreach, LOG_DEBUG, n_log, and nstrprintf.

+ Here is the call graph for this function:

◆ get_nb_config_file_sections()

int get_nb_config_file_sections ( CONFIG_FILE cfg_file,
char *  section_name 
)

Get the number of config file with section_name.

Parameters
cfg_fileConfig file to process
section_namename of sections to search , or NULL to have a count of all available sections
Returns
The number of named sections or a negative value

Definition at line 408 of file n_config_file.c.

References __n_assert, and list_foreach.

◆ get_nb_config_file_sections_entries()

int get_nb_config_file_sections_entries ( CONFIG_FILE cfg_file,
char *  section_name,
int  section_position,
char *  entry 
)

Get the number of config file with section_name.

Parameters
cfg_fileConfig file to process
section_namename of sections to search
section_positionposition of the section if there are multiples same name sections
entryname of entry to retrieve
Returns
The number of named sections entries or a negative value

Definition at line 442 of file n_config_file.c.

References __n_assert, _nstr, free_nstr, ht_get_ptr(), list_foreach, and nstrprintf.

+ Here is the call graph for this function:

◆ load_config_file()

CONFIG_FILE * load_config_file ( char *  filename,
int *  errors 
)

load a config file

Parameters
filenameFilename of the config file
errorspointer to an int which will represent the number of errors encountered when reading config file
Returns
NULL or a loaded config file

Definition at line 51 of file n_config_file.c.

References __n_assert, _nstr, _str, CONFIG_SECTION_HASH_TABLE_LEN, destroy_config_file_section(), Free, free_no_null(), free_nstr, free_split_result(), ht_get_ptr(), ht_put_ptr(), list_push(), LOG_DEBUG, LOG_ERR, Malloc, MAX_CONFIG_LINE_LEN, n_log, new_generic_list(), new_ht(), npcre_delete(), npcre_match(), npcre_new(), nstrprintf, skipu(), split_count(), str_replace(), and trim_nocopy().

+ Here is the call graph for this function:

◆ write_config_file()

int write_config_file ( CONFIG_FILE cfg_file,
char *  filename 
)

write a config file

Parameters
filenamedestination filename
cfg_filesource config file to write
Returns
TRUE or FALSE

Definition at line 368 of file n_config_file.c.

References __n_assert, config_endfor, config_foreach, FreeNoLog, LOG_ERR, and n_log.