Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
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.
 
#define config_foreach(__config, __section_name, __key, __val)
 Foreach elements of CONFIG_FILE macro, i.e config_foreach( config , section , key , val ); config_endfor;.
 
#define CONFIG_SECTION_HASH_TABLE_LEN   16
 size of the hash table of config sections entries
 
#define MAX_CONFIG_LINE_LEN   1024
 maximum length of a single config line
 

Functions

int destroy_config_file (CONFIG_FILE **cfg_file)
 destroy a loaded config
 
char * get_config_section_value (CONFIG_FILE *cfg_file, char *section_name, size_t section_position, char *entry, size_t entry_position)
 get a loaded config value
 
size_t get_nb_config_file_sections (CONFIG_FILE *cfg_file, const char *section_name)
 get the number of section for section name
 
size_t get_nb_config_file_sections_entries (CONFIG_FILE *cfg_file, const char *section_name, size_t section_position, char *entry)
 get the number of entries for section_name/entry
 
CONFIG_FILEload_config_file (char *filename, int *errors)
 load a config from a file
 
int write_config_file (CONFIG_FILE *cfg_file, char *filename)
 write file from config
 

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 57 of file n_config_file.h.

+ Collaboration diagram for CONFIG_FILE:
Data Fields
char * filename path and name of the config file
LIST * sections list of CONFIG_FILE_SECTION

◆ CONFIG_FILE_SECTION

struct CONFIG_FILE_SECTION

Structure of a config section.

Definition at line 49 of file n_config_file.h.

+ Collaboration diagram for CONFIG_FILE_SECTION:
Data Fields
HASH_TABLE * entries hash table of key-value entries
char * section_name name of the config section

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 92 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.
structure of a hash table node
Definition n_hash.h:111
#define LOG_ERR
error conditions
Definition n_log.h:75

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

Examples
ex_configfile.c.

Definition at line 78 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 46 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 44 of file n_config_file.h.

Function Documentation

◆ destroy_config_file()

int destroy_config_file ( CONFIG_FILE **  cfg_file)

destroy a loaded config

destroy a loaded config

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

Definition at line 528 of file n_config_file.c.

References __n_assert, Free, and list_destroy().

Referenced by load_app_state(), and main().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ get_config_section_value()

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

get a loaded config value

get a loaded config value

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 481 of file n_config_file.c.

References __n_assert, _nstr, CONFIG_FILE_SECTION::entries, free_nstr, ht_get_ptr(), list_foreach, LOG_DEBUG, LOG_ERR, n_log, LIST::nb_items, nstrprintf, CONFIG_FILE_SECTION::section_name, and CONFIG_FILE::sections.

Referenced by load_app_state(), and main().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ get_nb_config_file_sections()

size_t get_nb_config_file_sections ( CONFIG_FILE cfg_file,
const char *  section_name 
)

get the number of section for section name

get the number of section for 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 0

Definition at line 415 of file n_config_file.c.

References __n_assert, list_foreach, CONFIG_FILE_SECTION::section_name, and CONFIG_FILE::sections.

Referenced by load_app_state(), and main().

+ Here is the caller graph for this function:

◆ get_nb_config_file_sections_entries()

size_t get_nb_config_file_sections_entries ( CONFIG_FILE cfg_file,
const char *  section_name,
size_t  section_position,
char *  entry 
)

get the number of entries for section_name/entry

get the number of entries for section_name/entry

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 0

Definition at line 441 of file n_config_file.c.

References __n_assert, _nstr, CONFIG_FILE_SECTION::entries, free_nstr, ht_get_ptr(), list_foreach, nstrprintf, CONFIG_FILE_SECTION::section_name, and CONFIG_FILE::sections.

Referenced by main().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ load_config_file()

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

load a config from a file

load a config from a 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 64 of file n_config_file.c.

References __n_assert, _nstr, _str, CONFIG_SECTION_HASH_TABLE_LEN, destroy_config_file_section(), CONFIG_FILE_SECTION::entries, CONFIG_FILE::filename, Free, free_no_null(), free_nstr, free_split_result(), ht_get_ptr(), ht_put_ptr(), list_destroy(), list_push(), LOG_DEBUG, LOG_ERR, Malloc, N_PCRE::match_list, MAX_CONFIG_LINE_LEN, MAX_LIST_ITEMS, n_log, new_generic_list(), new_ht(), npcre_delete(), npcre_match_capture(), npcre_new(), nstrprintf, CONFIG_FILE_SECTION::section_name, CONFIG_FILE::sections, skipu(), split_count(), str_replace(), and trim_nocopy().

Referenced by load_app_state(), and main().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ write_config_file()

int write_config_file ( CONFIG_FILE cfg_file,
char *  filename 
)

write file from config

write file from config

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

Definition at line 369 of file n_config_file.c.

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

Referenced by main().

+ Here is the caller graph for this function: