Nilorea Library
C utilities for networking, threading, graphics
ex_configfile.c

Nilorea Library config api test.

Nilorea Library config api test

Author
Mickael Castagnier
Version
1.0
Date
2018-26-10
int main( int argc, char *argv[] )
{
if( argc < 2 )
{
n_log( LOG_ERR, "Not enough arguments. Use ex_configfile file" );
exit( 1 );
}
int errors = 0 ;
CONFIG_FILE *config = load_config_file( argv[ 1 ], &errors );
if( !config )
{
n_log( LOG_ERR, "Unable to load config file from %s", argv[ 1 ] );
exit( 1 );
}
if( errors != 0 )
{
n_log( LOG_ERR, "There were %d errors in %s. Check the logs !", errors, argv[ 1 ] );
}
/* default section, only one should be allocated. Let's test it ! */
int nb = get_nb_config_file_sections( config, "__DEFAULT__" );
for( int it = 0 ; it < nb ; it++ )
{
n_log( LOG_INFO, "[DEFAULT]" );
char *value = NULL ;
value = get_config_section_value( config, "__DEFAULT__", it, "check_interval", 0 );
n_log( LOG_INFO, "check_interval:%s", (value!=NULL)?value:"NULL" );
value = get_config_section_value( config, "__DEFAULT__", it, "refresh_interval", 0 );
n_log( LOG_INFO, "refresh_interval:%s", (value!=NULL)?value:"NULL" );
value = get_config_section_value( config, "__DEFAULT__", it, "cache_file", 0 );
n_log( LOG_INFO, "cache_file:%s", (value!=NULL)?value:"NULL" );
value = get_config_section_value( config, "__DEFAULT__", it, "cache_file_swp", 0 );
n_log( LOG_INFO, "cache_file_swp:%s", (value!=NULL)?value:"NULL" );
value = get_config_section_value( config, "__DEFAULT__", it, "cache_refresh_interval", 0 );
n_log( LOG_INFO, "cache_refresh_interval:%s", (value!=NULL)?value:"NULL" );
}
/* get a loaded config value */
nb = get_nb_config_file_sections( config, "SECTION" );
n_log( LOG_INFO, "%d SECTION sections", nb );
for( int it = 0 ; it < nb ; it++ )
{
n_log( LOG_INFO, "[SECTION]" );
char *value = NULL ;
value = get_config_section_value( config, "SECTION", it, "check_interval", 0 );
n_log( LOG_INFO, "check_interval:%s", (value!=NULL)?value:"NULL" );
value = get_config_section_value( config, "SECTION", it, "refresh_interval", 0 );
n_log( LOG_INFO, "refresh_interval:%s", (value!=NULL)?value:"NULL" );
value = get_config_section_value( config, "SECTION", it, "cache_file", 0 );
n_log( LOG_INFO, "cache_file:%s", (value!=NULL)?value:"NULL" );
value = get_config_section_value( config, "SECTION", it, "cache_file_swp", 0 );
n_log( LOG_INFO, "cache_file_swp:%s", (value!=NULL)?value:"NULL" );
value = get_config_section_value( config, "SECTION", it, "cache_refresh_interval", 0 );
n_log( LOG_INFO, "cache_refresh_interval:%s", (value!=NULL)?value:"NULL" );
}
/* get a loaded config value */
nb = get_nb_config_file_sections( config, "SECTION_FILE" );
n_log( LOG_INFO, "%d SECTION_FILE sections", nb );
for( int it = 0 ; it < nb ; it++ )
{
n_log( LOG_INFO, "[SECTION_FILE]" );
char *value = NULL ;
value = get_config_section_value( config, "SECTION_FILE", it, "file_id", 0 );
n_log( LOG_INFO, "file_id:%s", (value!=NULL)?value:"NULL" );
value = get_config_section_value( config, "SECTION_FILE", it, "file", 0 );
n_log( LOG_INFO, "file:%s", (value!=NULL)?value:"NULL" );
value = get_config_section_value( config, "SECTION_FILE", it, "check_interval", 0 );
n_log( LOG_INFO, "check_interval:%s", (value!=NULL)?value:"NULL" );
value = get_config_section_value( config, "SECTION_FILE", it, "command", 0 );
n_log( LOG_INFO, "command:%s", (value!=NULL)?value:"NULL" );
value = get_config_section_value( config, "SECTION_FILE", it, "command_timeout", 0 );
n_log( LOG_INFO, "command_timeout:%s", (value!=NULL)?value:"NULL" );
}
/* get a loaded config value */
nb = get_nb_config_file_sections( config, "SECTION_FILE_MULTICMD" );
n_log( LOG_INFO, "%d SECTION_FILE_MULTICMD sections", nb );
for( int it = 0 ; it < nb ; it++ )
{
n_log( LOG_INFO, "[SECTION_FILE_MULTICMD]" );
char *value = NULL ;
value = get_config_section_value( config, "SECTION_FILE_MULTICMD", it, "file_id", 0 );
n_log( LOG_INFO, "file_id:%s", (value!=NULL)?value:"NULL" );
value = get_config_section_value( config, "SECTION_FILE_MULTICMD", it, "file", 0 );
n_log( LOG_INFO, "file:%s", (value!=NULL)?value:"NULL" );
value = get_config_section_value( config, "SECTION_FILE_MULTICMD", it, "check_interval", 0 );
n_log( LOG_INFO, "check_interval:%s", (value!=NULL)?value:"NULL" );
value = get_config_section_value( config, "SECTION_FILE_MULTICMD", it, "command_timeout", 0 );
n_log( LOG_INFO, "command_timeout:%s", (value!=NULL)?value:"NULL" );
int nb_cmd = get_nb_config_file_sections_entries( config, "SECTION_FILE_MULTICMD", it, "command" );
for( int it1 = 0 ; it1 < nb_cmd ; it1 ++ )
{
value = get_config_section_value( config, "SECTION_FILE_MULTICMD", it, "command", it1 );
n_log( LOG_INFO, "command:%s", (value!=NULL)?value:"NULL" );
}
}
n_log( LOG_INFO, "\n\nCONFIG_FILE foreach !!" );
char *section_name = NULL, *key=NULL, *val = NULL ;
config_foreach( config, section_name, key, val )
{
n_log( LOG_INFO, "[%s]%s:%s", section_name, key, val );
}
destroy_config_file( &config );
exit( 0 );
}
CONFIG_FILE * load_config_file(char *filename, int *errors)
load a config file
Definition: n_config_file.c:51
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.
#define config_endfor
Foreach elements of CONFIG_FILE macro END.
Definition: n_config_file.h:80
int destroy_config_file(CONFIG_FILE **cfg_file)
Destroy a loaded config file.
#define config_foreach(__config, __section_name, __key, __val)
Foreach elements of CONFIG_FILE macro, i.e config_foreach( config , section , key ,...
Definition: n_config_file.h:60
int get_nb_config_file_sections(CONFIG_FILE *cfg_file, char *section_name)
Get the number of config file with section_name.
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.
Structure of a config file.
Definition: n_config_file.h:41
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition: n_log.h:74
#define LOG_DEBUG
debug-level messages
Definition: n_log.h:66
#define LOG_ERR
error conditions
Definition: n_log.h:58
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL )
Definition: n_log.c:97
#define LOG_INFO
informational
Definition: n_log.h:64
Config file reading and writing.