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 file.
 
char * get_config_section_value (CONFIG_FILE *cfg_file, char *section_name, size_t section_position, char *entry, size_t entry_position)
 Function to parse sections and get entries values.
 
size_t get_nb_config_file_sections (CONFIG_FILE *cfg_file, char *section_name)
 Get the number of config file with section_name.
 
size_t get_nb_config_file_sections_entries (CONFIG_FILE *cfg_file, char *section_name, size_t section_position, char *entry)
 Get the number of config file with section_name.
 
CONFIG_FILEload_config_file (char *filename, int *errors)
 load a config file
 
int write_config_file (CONFIG_FILE *cfg_file, char *filename)
 write a config file
 

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 37 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 31 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 70 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:93
#define LOG_ERR
error conditions
Definition n_log.h:57

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

Examples
ex_configfile.c.

Definition at line 56 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 28 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 26 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 488 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 
)

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

References __n_assert, _nstr, CONFIG_FILE_SECTION::entries, free_nstr, ht_get_ptr(), list_foreach, LOG_DEBUG, 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,
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 0

Definition at line 380 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,
char *  section_name,
size_t  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 0

Definition at line 406 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 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 46 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_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(), 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 a config file

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

Definition at line 335 of file n_config_file.c.

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