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 * 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
28#ifndef __N_CONFIG_FILE__
29#define __N_CONFIG_FILE__
30
31#ifdef __cplusplus
32extern "C" {
33#endif
34
35#include "n_hash.h"
36#include "n_str.h"
37#include "n_log.h"
38
45#define MAX_CONFIG_LINE_LEN 1024
47#define CONFIG_SECTION_HASH_TABLE_LEN 16
48
56
58typedef struct CONFIG_FILE {
60 char* filename;
64
66CONFIG_FILE* load_config_file(char* filename, int* errors);
68int write_config_file(CONFIG_FILE* cfg_file, char* filename);
70char* get_config_section_value(CONFIG_FILE* cfg_file, char* section_name, size_t section_position, char* entry, size_t entry_position);
72size_t get_nb_config_file_sections(CONFIG_FILE* cfg_file, const char* section_name);
74size_t get_nb_config_file_sections_entries(CONFIG_FILE* cfg_file, const char* section_name, size_t section_position, char* entry);
76int destroy_config_file(CONFIG_FILE** cfg_file);
77
79#define config_foreach(__config, __section_name, __key, __val) \
80 if (!__config || !__config->sections) { \
81 n_log(LOG_ERR, "No config file for %s", #__config); \
82 } else { \
83 list_foreach(listnode, __config->sections) { \
84 CONFIG_FILE_SECTION* __section = (CONFIG_FILE_SECTION*)listnode->ptr; \
85 __section_name = __section->section_name; \
86 ht_foreach(entry, __section->entries) { \
87 HASH_NODE* htnode = (HASH_NODE*)entry->ptr; \
88 if (htnode && htnode->data.ptr) { \
89 __key = htnode->key; \
90 __val = htnode->data.ptr;
91
93#define config_endfor \
94 } \
95 } \
96 } \
97 }
98
103#ifdef __cplusplus
104}
105#endif
106
107#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:138
Structure of a generic LIST container.
Definition n_list.h:59
Hash functions and table.
Generic log system.
N_STR and string function declaration.