Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
n_config_file.h
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
27#ifndef __N_CONFIG_FILE__
28#define __N_CONFIG_FILE__
29
30#ifdef __cplusplus
31extern "C" {
32#endif
33
34#include "n_hash.h"
35#include "n_str.h"
36#include "n_log.h"
37
44#define MAX_CONFIG_LINE_LEN 1024
46#define CONFIG_SECTION_HASH_TABLE_LEN 16
47
55
57typedef struct CONFIG_FILE {
59 char* filename;
63
65CONFIG_FILE* load_config_file(char* filename, int* errors);
67int write_config_file(CONFIG_FILE* cfg_file, char* filename);
69char* get_config_section_value(CONFIG_FILE* cfg_file, char* section_name, size_t section_position, char* entry, size_t entry_position);
71size_t get_nb_config_file_sections(CONFIG_FILE* cfg_file, const char* section_name);
73size_t get_nb_config_file_sections_entries(CONFIG_FILE* cfg_file, const char* section_name, size_t section_position, char* entry);
75int destroy_config_file(CONFIG_FILE** cfg_file);
76
78#define config_foreach(__config, __section_name, __key, __val) \
79 if (!__config || !__config->sections) { \
80 n_log(LOG_ERR, "No config file for %s", #__config); \
81 } else { \
82 list_foreach(listnode, __config->sections) { \
83 CONFIG_FILE_SECTION* __section = (CONFIG_FILE_SECTION*)listnode->ptr; \
84 __section_name = __section->section_name; \
85 ht_foreach(entry, __section->entries) { \
86 HASH_NODE* htnode = (HASH_NODE*)entry->ptr; \
87 if (htnode && htnode->data.ptr) { \
88 __key = htnode->key; \
89 __val = htnode->data.ptr;
90
92#define config_endfor \
93 } \
94 } \
95 } \
96 }
97
102#ifdef __cplusplus
103}
104#endif
105
106#endif /* #ifndef __N_CONFIG_FILE__ */
HASH_TABLE * entries
hash table of key-value entries
char * section_name
name of the config section
char * filename
path and name of the config file
LIST * sections
list of CONFIG_FILE_SECTION
CONFIG_FILE * load_config_file(char *filename, int *errors)
load a config from a file
int write_config_file(CONFIG_FILE *cfg_file, char *filename)
write file from config
int destroy_config_file(CONFIG_FILE **cfg_file)
destroy a loaded config
char * get_config_section_value(CONFIG_FILE *cfg_file, char *section_name, size_t section_position, char *entry, size_t entry_position)
get a loaded config value
size_t get_nb_config_file_sections(CONFIG_FILE *cfg_file, const char *section_name)
get the number of section for 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 entries for section_name/entry
Structure of a config file.
Structure of a config section.
structure of a hash table
Definition n_hash.h:137
Structure of a generic LIST container.
Definition n_list.h:58
Hash functions and table.
Generic log system.
N_STR and string function declaration.