Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_configfile.c
Go to the documentation of this file.
1/*
2 * Nilorea Library
3 * Copyright (C) 2005-2026 Castagnier Mickael
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
28
29int main(int argc, char* argv[]) {
31 if (argc < 2) {
32 n_log(LOG_ERR, "Not enough arguments. Use ex_configfile file");
33 exit(1);
34 }
35 int errors = 0;
36 CONFIG_FILE* config = load_config_file(argv[1], &errors);
37 if (!config) {
38 n_log(LOG_ERR, "Unable to load config file from %s", argv[1]);
39 exit(1);
40 }
41 if (errors != 0) {
42 n_log(LOG_ERR, "There were %d errors in %s. Check the logs !", errors, argv[1]);
43 }
44 /* default section, only one should be allocated. Let's test it ! */
45 size_t nb = get_nb_config_file_sections(config, "__DEFAULT__");
46 for (size_t it = 0; it < nb; it++) {
47 n_log(LOG_INFO, "[DEFAULT]");
48 char* value = NULL;
49 value = get_config_section_value(config, "__DEFAULT__", it, "check_interval", 0);
50 n_log(LOG_INFO, "check_interval:%s", (value != NULL) ? value : "NULL");
51 value = get_config_section_value(config, "__DEFAULT__", it, "refresh_interval", 0);
52 n_log(LOG_INFO, "refresh_interval:%s", (value != NULL) ? value : "NULL");
53 value = get_config_section_value(config, "__DEFAULT__", it, "cache_file", 0);
54 n_log(LOG_INFO, "cache_file:%s", (value != NULL) ? value : "NULL");
55 value = get_config_section_value(config, "__DEFAULT__", it, "cache_file_swp", 0);
56 n_log(LOG_INFO, "cache_file_swp:%s", (value != NULL) ? value : "NULL");
57 value = get_config_section_value(config, "__DEFAULT__", it, "cache_refresh_interval", 0);
58 n_log(LOG_INFO, "cache_refresh_interval:%s", (value != NULL) ? value : "NULL");
59 }
60 /* get a loaded config value */
61 nb = get_nb_config_file_sections(config, "SECTION");
62 n_log(LOG_INFO, "%zu SECTION sections", nb);
63 for (size_t it = 0; it < nb; it++) {
64 n_log(LOG_INFO, "[SECTION]");
65 char* value = NULL;
66 value = get_config_section_value(config, "SECTION", it, "check_interval", 0);
67 n_log(LOG_INFO, "check_interval:%s", (value != NULL) ? value : "NULL");
68 value = get_config_section_value(config, "SECTION", it, "refresh_interval", 0);
69 n_log(LOG_INFO, "refresh_interval:%s", (value != NULL) ? value : "NULL");
70 value = get_config_section_value(config, "SECTION", it, "cache_file", 0);
71 n_log(LOG_INFO, "cache_file:%s", (value != NULL) ? value : "NULL");
72 value = get_config_section_value(config, "SECTION", it, "cache_file_swp", 0);
73 n_log(LOG_INFO, "cache_file_swp:%s", (value != NULL) ? value : "NULL");
74 value = get_config_section_value(config, "SECTION", it, "cache_refresh_interval", 0);
75 n_log(LOG_INFO, "cache_refresh_interval:%s", (value != NULL) ? value : "NULL");
76 }
77 /* get a loaded config value */
78 nb = get_nb_config_file_sections(config, "SECTION_FILE");
79 n_log(LOG_INFO, "%zu SECTION_FILE sections", nb);
80 for (size_t it = 0; it < nb; it++) {
81 n_log(LOG_INFO, "[SECTION_FILE]");
82 char* value = NULL;
83 value = get_config_section_value(config, "SECTION_FILE", it, "file_id", 0);
84 n_log(LOG_INFO, "file_id:%s", (value != NULL) ? value : "NULL");
85 value = get_config_section_value(config, "SECTION_FILE", it, "file", 0);
86 n_log(LOG_INFO, "file:%s", (value != NULL) ? value : "NULL");
87 value = get_config_section_value(config, "SECTION_FILE", it, "check_interval", 0);
88 n_log(LOG_INFO, "check_interval:%s", (value != NULL) ? value : "NULL");
89 value = get_config_section_value(config, "SECTION_FILE", it, "command", 0);
90 n_log(LOG_INFO, "command:%s", (value != NULL) ? value : "NULL");
91 value = get_config_section_value(config, "SECTION_FILE", it, "command_timeout", 0);
92 n_log(LOG_INFO, "command_timeout:%s", (value != NULL) ? value : "NULL");
93 }
94 /* get a loaded config value */
95 nb = get_nb_config_file_sections(config, "SECTION_FILE_MULTICMD");
96 n_log(LOG_INFO, "%zu SECTION_FILE_MULTICMD sections", nb);
97 for (size_t it = 0; it < nb; it++) {
98 n_log(LOG_INFO, "[SECTION_FILE_MULTICMD]");
99 char* value = NULL;
100 value = get_config_section_value(config, "SECTION_FILE_MULTICMD", it, "file_id", 0);
101 n_log(LOG_INFO, "file_id:%s", (value != NULL) ? value : "NULL");
102 value = get_config_section_value(config, "SECTION_FILE_MULTICMD", it, "file", 0);
103 n_log(LOG_INFO, "file:%s", (value != NULL) ? value : "NULL");
104 value = get_config_section_value(config, "SECTION_FILE_MULTICMD", it, "check_interval", 0);
105 n_log(LOG_INFO, "check_interval:%s", (value != NULL) ? value : "NULL");
106 value = get_config_section_value(config, "SECTION_FILE_MULTICMD", it, "command_timeout", 0);
107 n_log(LOG_INFO, "command_timeout:%s", (value != NULL) ? value : "NULL");
108 size_t nb_cmd = get_nb_config_file_sections_entries(config, "SECTION_FILE_MULTICMD", it, "command");
109 for (size_t it1 = 0; it1 < nb_cmd; it1++) {
110 value = get_config_section_value(config, "SECTION_FILE_MULTICMD", it, "command", it1);
111 n_log(LOG_INFO, "command:%s", (value != NULL) ? value : "NULL");
112 }
113 }
114 n_log(LOG_INFO, "\n\nCONFIG_FILE foreach !!");
115
116 char *section_name = NULL, *key = NULL, *val = NULL;
117 config_foreach(config, section_name, key, val) {
118 n_log(LOG_INFO, "[%s]%s:%s", section_name, key, val);
119 }
121
122 /* test write_config_file */
123 if (write_config_file(config, "nilorea_ex_configfile_out.cfg") == TRUE) {
124 n_log(LOG_INFO, "write_config_file: written to nilorea_ex_configfile_out.cfg");
125 } else {
126 n_log(LOG_ERR, "write_config_file failed");
127 }
128
129 destroy_config_file(&config);
130 exit(0);
131}
int main(void)
char * key
CONFIG_FILE * load_config_file(char *filename, int *errors)
load a config file
int write_config_file(CONFIG_FILE *cfg_file, char *filename)
write a config file
#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, const char *section_name)
Get the number of config file with section_name.
size_t get_nb_config_file_sections_entries(CONFIG_FILE *cfg_file, const char *section_name, size_t section_position, char *entry)
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:88
#define LOG_DEBUG
debug-level messages
Definition n_log.h:83
#define LOG_ERR
error conditions
Definition n_log.h:75
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL )
Definition n_log.c:120
#define LOG_INFO
informational
Definition n_log.h:81
Config file reading and writing.