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 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14 * implied. See the License for the specific language governing
15 * permissions and limitations under the License.
16 *
17 * SPDX-License-Identifier: Apache-2.0
18 */
19
29
30int main(int argc, char* argv[]) {
32 if (argc < 2) {
33 n_log(LOG_ERR, "Not enough arguments. Use ex_configfile file");
34 exit(1);
35 }
36 int errors = 0;
37 CONFIG_FILE* config = load_config_file(argv[1], &errors);
38 if (!config) {
39 n_log(LOG_ERR, "Unable to load config file from %s", argv[1]);
40 exit(1);
41 }
42 if (errors != 0) {
43 n_log(LOG_ERR, "There were %d errors in %s. Check the logs !", errors, argv[1]);
44 }
45 /* default section, only one should be allocated. Let's test it ! */
46 size_t nb = get_nb_config_file_sections(config, "__DEFAULT__");
47 for (size_t it = 0; it < nb; it++) {
48 n_log(LOG_INFO, "[DEFAULT]");
49 char* value = NULL;
50 value = get_config_section_value(config, "__DEFAULT__", it, "check_interval", 0);
51 n_log(LOG_INFO, "check_interval:%s", (value != NULL) ? value : "NULL");
52 value = get_config_section_value(config, "__DEFAULT__", it, "refresh_interval", 0);
53 n_log(LOG_INFO, "refresh_interval:%s", (value != NULL) ? value : "NULL");
54 value = get_config_section_value(config, "__DEFAULT__", it, "cache_file", 0);
55 n_log(LOG_INFO, "cache_file:%s", (value != NULL) ? value : "NULL");
56 value = get_config_section_value(config, "__DEFAULT__", it, "cache_file_swp", 0);
57 n_log(LOG_INFO, "cache_file_swp:%s", (value != NULL) ? value : "NULL");
58 value = get_config_section_value(config, "__DEFAULT__", it, "cache_refresh_interval", 0);
59 n_log(LOG_INFO, "cache_refresh_interval:%s", (value != NULL) ? value : "NULL");
60 }
61 /* get a loaded config value */
62 nb = get_nb_config_file_sections(config, "SECTION");
63 n_log(LOG_INFO, "%zu SECTION sections", nb);
64 for (size_t it = 0; it < nb; it++) {
65 n_log(LOG_INFO, "[SECTION]");
66 char* value = NULL;
67 value = get_config_section_value(config, "SECTION", it, "check_interval", 0);
68 n_log(LOG_INFO, "check_interval:%s", (value != NULL) ? value : "NULL");
69 value = get_config_section_value(config, "SECTION", it, "refresh_interval", 0);
70 n_log(LOG_INFO, "refresh_interval:%s", (value != NULL) ? value : "NULL");
71 value = get_config_section_value(config, "SECTION", it, "cache_file", 0);
72 n_log(LOG_INFO, "cache_file:%s", (value != NULL) ? value : "NULL");
73 value = get_config_section_value(config, "SECTION", it, "cache_file_swp", 0);
74 n_log(LOG_INFO, "cache_file_swp:%s", (value != NULL) ? value : "NULL");
75 value = get_config_section_value(config, "SECTION", it, "cache_refresh_interval", 0);
76 n_log(LOG_INFO, "cache_refresh_interval:%s", (value != NULL) ? value : "NULL");
77 }
78 /* get a loaded config value */
79 nb = get_nb_config_file_sections(config, "SECTION_FILE");
80 n_log(LOG_INFO, "%zu SECTION_FILE sections", nb);
81 for (size_t it = 0; it < nb; it++) {
82 n_log(LOG_INFO, "[SECTION_FILE]");
83 char* value = NULL;
84 value = get_config_section_value(config, "SECTION_FILE", it, "file_id", 0);
85 n_log(LOG_INFO, "file_id:%s", (value != NULL) ? value : "NULL");
86 value = get_config_section_value(config, "SECTION_FILE", it, "file", 0);
87 n_log(LOG_INFO, "file:%s", (value != NULL) ? value : "NULL");
88 value = get_config_section_value(config, "SECTION_FILE", it, "check_interval", 0);
89 n_log(LOG_INFO, "check_interval:%s", (value != NULL) ? value : "NULL");
90 value = get_config_section_value(config, "SECTION_FILE", it, "command", 0);
91 n_log(LOG_INFO, "command:%s", (value != NULL) ? value : "NULL");
92 value = get_config_section_value(config, "SECTION_FILE", it, "command_timeout", 0);
93 n_log(LOG_INFO, "command_timeout:%s", (value != NULL) ? value : "NULL");
94 }
95 /* get a loaded config value */
96 nb = get_nb_config_file_sections(config, "SECTION_FILE_MULTICMD");
97 n_log(LOG_INFO, "%zu SECTION_FILE_MULTICMD sections", nb);
98 for (size_t it = 0; it < nb; it++) {
99 n_log(LOG_INFO, "[SECTION_FILE_MULTICMD]");
100 char* value = NULL;
101 value = get_config_section_value(config, "SECTION_FILE_MULTICMD", it, "file_id", 0);
102 n_log(LOG_INFO, "file_id:%s", (value != NULL) ? value : "NULL");
103 value = get_config_section_value(config, "SECTION_FILE_MULTICMD", it, "file", 0);
104 n_log(LOG_INFO, "file:%s", (value != NULL) ? value : "NULL");
105 value = get_config_section_value(config, "SECTION_FILE_MULTICMD", it, "check_interval", 0);
106 n_log(LOG_INFO, "check_interval:%s", (value != NULL) ? value : "NULL");
107 value = get_config_section_value(config, "SECTION_FILE_MULTICMD", it, "command_timeout", 0);
108 n_log(LOG_INFO, "command_timeout:%s", (value != NULL) ? value : "NULL");
109 size_t nb_cmd = get_nb_config_file_sections_entries(config, "SECTION_FILE_MULTICMD", it, "command");
110 for (size_t it1 = 0; it1 < nb_cmd; it1++) {
111 value = get_config_section_value(config, "SECTION_FILE_MULTICMD", it, "command", it1);
112 n_log(LOG_INFO, "command:%s", (value != NULL) ? value : "NULL");
113 }
114 }
115 n_log(LOG_INFO, "\n\nCONFIG_FILE foreach !!");
116
117 char *section_name = NULL, *key = NULL, *val = NULL;
118 config_foreach(config, section_name, key, val) {
119 n_log(LOG_INFO, "[%s]%s:%s", section_name, key, val);
120 }
122
123 /* test write_config_file */
124 if (write_config_file(config, "nilorea_ex_configfile_out.cfg") == TRUE) {
125 n_log(LOG_INFO, "write_config_file: written to nilorea_ex_configfile_out.cfg");
126 } else {
127 n_log(LOG_ERR, "write_config_file failed");
128 }
129
130 destroy_config_file(&config);
131 exit(0);
132}
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:89
#define LOG_DEBUG
debug-level messages
Definition n_log.h:84
#define LOG_ERR
error conditions
Definition n_log.h:76
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL )
Definition n_log.c:121
#define LOG_INFO
informational
Definition n_log.h:82
Config file reading and writing.