Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_configfile.c
Go to the documentation of this file.
1
10
11int main(int argc, char* argv[]) {
13 if (argc < 2) {
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 n_log(LOG_ERR, "Unable to load config file from %s", argv[1]);
21 exit(1);
22 }
23 if (errors != 0) {
24 n_log(LOG_ERR, "There were %d errors in %s. Check the logs !", errors, argv[1]);
25 }
26 /* default section, only one should be allocated. Let's test it ! */
27 int nb = get_nb_config_file_sections(config, "__DEFAULT__");
28 for (int it = 0; it < nb; it++) {
29 n_log(LOG_INFO, "[DEFAULT]");
30 char* value = NULL;
31 value = get_config_section_value(config, "__DEFAULT__", it, "check_interval", 0);
32 n_log(LOG_INFO, "check_interval:%s", (value != NULL) ? value : "NULL");
33 value = get_config_section_value(config, "__DEFAULT__", it, "refresh_interval", 0);
34 n_log(LOG_INFO, "refresh_interval:%s", (value != NULL) ? value : "NULL");
35 value = get_config_section_value(config, "__DEFAULT__", it, "cache_file", 0);
36 n_log(LOG_INFO, "cache_file:%s", (value != NULL) ? value : "NULL");
37 value = get_config_section_value(config, "__DEFAULT__", it, "cache_file_swp", 0);
38 n_log(LOG_INFO, "cache_file_swp:%s", (value != NULL) ? value : "NULL");
39 value = get_config_section_value(config, "__DEFAULT__", it, "cache_refresh_interval", 0);
40 n_log(LOG_INFO, "cache_refresh_interval:%s", (value != NULL) ? value : "NULL");
41 }
42 /* get a loaded config value */
43 nb = get_nb_config_file_sections(config, "SECTION");
44 n_log(LOG_INFO, "%d SECTION sections", nb);
45 for (int it = 0; it < nb; it++) {
46 n_log(LOG_INFO, "[SECTION]");
47 char* value = NULL;
48 value = get_config_section_value(config, "SECTION", it, "check_interval", 0);
49 n_log(LOG_INFO, "check_interval:%s", (value != NULL) ? value : "NULL");
50 value = get_config_section_value(config, "SECTION", it, "refresh_interval", 0);
51 n_log(LOG_INFO, "refresh_interval:%s", (value != NULL) ? value : "NULL");
52 value = get_config_section_value(config, "SECTION", it, "cache_file", 0);
53 n_log(LOG_INFO, "cache_file:%s", (value != NULL) ? value : "NULL");
54 value = get_config_section_value(config, "SECTION", it, "cache_file_swp", 0);
55 n_log(LOG_INFO, "cache_file_swp:%s", (value != NULL) ? value : "NULL");
56 value = get_config_section_value(config, "SECTION", it, "cache_refresh_interval", 0);
57 n_log(LOG_INFO, "cache_refresh_interval:%s", (value != NULL) ? value : "NULL");
58 }
59 /* get a loaded config value */
60 nb = get_nb_config_file_sections(config, "SECTION_FILE");
61 n_log(LOG_INFO, "%d SECTION_FILE sections", nb);
62 for (int it = 0; it < nb; it++) {
63 n_log(LOG_INFO, "[SECTION_FILE]");
64 char* value = NULL;
65 value = get_config_section_value(config, "SECTION_FILE", it, "file_id", 0);
66 n_log(LOG_INFO, "file_id:%s", (value != NULL) ? value : "NULL");
67 value = get_config_section_value(config, "SECTION_FILE", it, "file", 0);
68 n_log(LOG_INFO, "file:%s", (value != NULL) ? value : "NULL");
69 value = get_config_section_value(config, "SECTION_FILE", it, "check_interval", 0);
70 n_log(LOG_INFO, "check_interval:%s", (value != NULL) ? value : "NULL");
71 value = get_config_section_value(config, "SECTION_FILE", it, "command", 0);
72 n_log(LOG_INFO, "command:%s", (value != NULL) ? value : "NULL");
73 value = get_config_section_value(config, "SECTION_FILE", it, "command_timeout", 0);
74 n_log(LOG_INFO, "command_timeout:%s", (value != NULL) ? value : "NULL");
75 }
76 /* get a loaded config value */
77 nb = get_nb_config_file_sections(config, "SECTION_FILE_MULTICMD");
78 n_log(LOG_INFO, "%d SECTION_FILE_MULTICMD sections", nb);
79 for (int it = 0; it < nb; it++) {
80 n_log(LOG_INFO, "[SECTION_FILE_MULTICMD]");
81 char* value = NULL;
82 value = get_config_section_value(config, "SECTION_FILE_MULTICMD", it, "file_id", 0);
83 n_log(LOG_INFO, "file_id:%s", (value != NULL) ? value : "NULL");
84 value = get_config_section_value(config, "SECTION_FILE_MULTICMD", it, "file", 0);
85 n_log(LOG_INFO, "file:%s", (value != NULL) ? value : "NULL");
86 value = get_config_section_value(config, "SECTION_FILE_MULTICMD", it, "check_interval", 0);
87 n_log(LOG_INFO, "check_interval:%s", (value != NULL) ? value : "NULL");
88 value = get_config_section_value(config, "SECTION_FILE_MULTICMD", it, "command_timeout", 0);
89 n_log(LOG_INFO, "command_timeout:%s", (value != NULL) ? value : "NULL");
90 int nb_cmd = get_nb_config_file_sections_entries(config, "SECTION_FILE_MULTICMD", it, "command");
91 for (int it1 = 0; it1 < nb_cmd; it1++) {
92 value = get_config_section_value(config, "SECTION_FILE_MULTICMD", it, "command", it1);
93 n_log(LOG_INFO, "command:%s", (value != NULL) ? value : "NULL");
94 }
95 }
96 n_log(LOG_INFO, "\n\nCONFIG_FILE foreach !!");
97
98 char *section_name = NULL, *key = NULL, *val = NULL;
99 config_foreach(config, section_name, key, val) {
100 n_log(LOG_INFO, "[%s]%s:%s", section_name, key, val);
101 }
103
104 destroy_config_file(&config);
105 exit(0);
106}
int main(void)
char * key
CONFIG_FILE * load_config_file(char *filename, int *errors)
load a config file
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.
#define config_endfor
Foreach elements of CONFIG_FILE macro END.
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.
#define config_foreach(__config, __section_name, __key, __val)
Foreach elements of CONFIG_FILE macro, i.e config_foreach( config , section , key ,...
size_t get_nb_config_file_sections(CONFIG_FILE *cfg_file, char *section_name)
Get the number of config file with section_name.
Structure of a config file.
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:70
#define LOG_DEBUG
debug-level messages
Definition n_log.h:65
#define LOG_ERR
error conditions
Definition n_log.h:57
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL )
Definition n_log.c:104
#define LOG_INFO
informational
Definition n_log.h:63
Config file reading and writing.