Nilorea Library
C utilities for networking, threading, graphics
ex_configfile.c
1
8
9int main( int argc, char *argv[] )
10{
12 if( argc < 2 )
13 {
14 n_log( LOG_ERR, "Not enough arguments. Use ex_configfile file" );
15 exit( 1 );
16 }
17 int errors = 0 ;
18 CONFIG_FILE *config = load_config_file( argv[ 1 ], &errors );
19 if( !config )
20 {
21 n_log( LOG_ERR, "Unable to load config file from %s", argv[ 1 ] );
22 exit( 1 );
23 }
24 if( errors != 0 )
25 {
26 n_log( LOG_ERR, "There were %d errors in %s. Check the logs !", errors, argv[ 1 ] );
27
28 }
29 /* default section, only one should be allocated. Let's test it ! */
30 int nb = get_nb_config_file_sections( config, "__DEFAULT__" );
31 for( int it = 0 ; it < nb ; it++ )
32 {
33 n_log( LOG_INFO, "[DEFAULT]" );
34 char *value = NULL ;
35 value = get_config_section_value( config, "__DEFAULT__", it, "check_interval", 0 );
36 n_log( LOG_INFO, "check_interval:%s", (value!=NULL)?value:"NULL" );
37 value = get_config_section_value( config, "__DEFAULT__", it, "refresh_interval", 0 );
38 n_log( LOG_INFO, "refresh_interval:%s", (value!=NULL)?value:"NULL" );
39 value = get_config_section_value( config, "__DEFAULT__", it, "cache_file", 0 );
40 n_log( LOG_INFO, "cache_file:%s", (value!=NULL)?value:"NULL" );
41 value = get_config_section_value( config, "__DEFAULT__", it, "cache_file_swp", 0 );
42 n_log( LOG_INFO, "cache_file_swp:%s", (value!=NULL)?value:"NULL" );
43 value = get_config_section_value( config, "__DEFAULT__", it, "cache_refresh_interval", 0 );
44 n_log( LOG_INFO, "cache_refresh_interval:%s", (value!=NULL)?value:"NULL" );
45 }
46 /* get a loaded config value */
47 nb = get_nb_config_file_sections( config, "SECTION" );
48 n_log( LOG_INFO, "%d SECTION sections", nb );
49 for( int it = 0 ; it < nb ; it++ )
50 {
51 n_log( LOG_INFO, "[SECTION]" );
52 char *value = NULL ;
53 value = get_config_section_value( config, "SECTION", it, "check_interval", 0 );
54 n_log( LOG_INFO, "check_interval:%s", (value!=NULL)?value:"NULL" );
55 value = get_config_section_value( config, "SECTION", it, "refresh_interval", 0 );
56 n_log( LOG_INFO, "refresh_interval:%s", (value!=NULL)?value:"NULL" );
57 value = get_config_section_value( config, "SECTION", it, "cache_file", 0 );
58 n_log( LOG_INFO, "cache_file:%s", (value!=NULL)?value:"NULL" );
59 value = get_config_section_value( config, "SECTION", it, "cache_file_swp", 0 );
60 n_log( LOG_INFO, "cache_file_swp:%s", (value!=NULL)?value:"NULL" );
61 value = get_config_section_value( config, "SECTION", it, "cache_refresh_interval", 0 );
62 n_log( LOG_INFO, "cache_refresh_interval:%s", (value!=NULL)?value:"NULL" );
63 }
64 /* get a loaded config value */
65 nb = get_nb_config_file_sections( config, "SECTION_FILE" );
66 n_log( LOG_INFO, "%d SECTION_FILE sections", nb );
67 for( int it = 0 ; it < nb ; it++ )
68 {
69 n_log( LOG_INFO, "[SECTION_FILE]" );
70 char *value = NULL ;
71 value = get_config_section_value( config, "SECTION_FILE", it, "file_id", 0 );
72 n_log( LOG_INFO, "file_id:%s", (value!=NULL)?value:"NULL" );
73 value = get_config_section_value( config, "SECTION_FILE", it, "file", 0 );
74 n_log( LOG_INFO, "file:%s", (value!=NULL)?value:"NULL" );
75 value = get_config_section_value( config, "SECTION_FILE", it, "check_interval", 0 );
76 n_log( LOG_INFO, "check_interval:%s", (value!=NULL)?value:"NULL" );
77 value = get_config_section_value( config, "SECTION_FILE", it, "command", 0 );
78 n_log( LOG_INFO, "command:%s", (value!=NULL)?value:"NULL" );
79 value = get_config_section_value( config, "SECTION_FILE", it, "command_timeout", 0 );
80 n_log( LOG_INFO, "command_timeout:%s", (value!=NULL)?value:"NULL" );
81 }
82 /* get a loaded config value */
83 nb = get_nb_config_file_sections( config, "SECTION_FILE_MULTICMD" );
84 n_log( LOG_INFO, "%d SECTION_FILE_MULTICMD sections", nb );
85 for( int it = 0 ; it < nb ; it++ )
86 {
87 n_log( LOG_INFO, "[SECTION_FILE_MULTICMD]" );
88 char *value = NULL ;
89 value = get_config_section_value( config, "SECTION_FILE_MULTICMD", it, "file_id", 0 );
90 n_log( LOG_INFO, "file_id:%s", (value!=NULL)?value:"NULL" );
91 value = get_config_section_value( config, "SECTION_FILE_MULTICMD", it, "file", 0 );
92 n_log( LOG_INFO, "file:%s", (value!=NULL)?value:"NULL" );
93 value = get_config_section_value( config, "SECTION_FILE_MULTICMD", it, "check_interval", 0 );
94 n_log( LOG_INFO, "check_interval:%s", (value!=NULL)?value:"NULL" );
95 value = get_config_section_value( config, "SECTION_FILE_MULTICMD", it, "command_timeout", 0 );
96 n_log( LOG_INFO, "command_timeout:%s", (value!=NULL)?value:"NULL" );
97 int nb_cmd = get_nb_config_file_sections_entries( config, "SECTION_FILE_MULTICMD", it, "command" );
98 for( int it1 = 0 ; it1 < nb_cmd ; it1 ++ )
99 {
100 value = get_config_section_value( config, "SECTION_FILE_MULTICMD", it, "command", it1 );
101 n_log( LOG_INFO, "command:%s", (value!=NULL)?value:"NULL" );
102 }
103 }
104 n_log( LOG_INFO, "\n\nCONFIG_FILE foreach !!" );
105
106 char *section_name = NULL, *key=NULL, *val = NULL ;
107 config_foreach( config, section_name, key, val )
108 {
109 n_log( LOG_INFO, "[%s]%s:%s", section_name, key, val );
110 }
112
113 destroy_config_file( &config );
114 exit( 0 );
115}
116
117
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.