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