6 n__config__file_8h
gullradriel edited this page 2026-10-09 00:55:18 +02:00

include/nilorea/n_config_file.h

Config file reading and writing. More...

Structures

Name
struct CONFIG_FILE_SECTION
Structure of a config section.
struct CONFIG_FILE
Structure of a config file.

Detailed Description

Config file reading and writing.

Author: Castagnier Mickael

Version: 1.0

Date: 24/03/05

Source code

/*
 * Nilorea Library
 * Copyright (C) 2005-2026 Castagnier Mickael
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * permissions and limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 */


#ifndef __N_CONFIG_FILE__
#define __N_CONFIG_FILE__

#ifdef __cplusplus
extern "C" {
#endif

#include "n_hash.h"
#include "n_str.h"
#include "n_log.h"


#define MAX_CONFIG_LINE_LEN 1024
#define CONFIG_SECTION_HASH_TABLE_LEN 16

typedef struct CONFIG_FILE_SECTION {
    char* section_name;
    HASH_TABLE* entries;
} CONFIG_FILE_SECTION;

typedef struct CONFIG_FILE {
    char* filename;
    LIST* sections;
} CONFIG_FILE;

CONFIG_FILE* load_config_file(char* filename, int* errors);
int write_config_file(CONFIG_FILE* cfg_file, char* filename);
char* get_config_section_value(CONFIG_FILE* cfg_file, char* section_name, size_t section_position, char* entry, size_t entry_position);
size_t get_nb_config_file_sections(CONFIG_FILE* cfg_file, const char* section_name);
size_t get_nb_config_file_sections_entries(CONFIG_FILE* cfg_file, const char* section_name, size_t section_position, char* entry);
int destroy_config_file(CONFIG_FILE** cfg_file);

#define config_foreach(__config, __section_name, __key, __val)                    \
    if (!__config || !__config->sections) {                                       \
        n_log(LOG_ERR, "No config file for %s", #__config);                       \
    } else {                                                                      \
        list_foreach(listnode, __config->sections) {                              \
            CONFIG_FILE_SECTION* __section = (CONFIG_FILE_SECTION*)listnode->ptr; \
            __section_name = __section->section_name;                             \
            ht_foreach(entry, __section->entries) {                               \
                HASH_NODE* htnode = (HASH_NODE*)entry->ptr;                       \
                if (htnode && htnode->data.ptr) {                                 \
                    __key = htnode->key;                                          \
                    __val = htnode->data.ptr;

#define config_endfor \
    }                 \
    }                 \
    }                 \
    }


#ifdef __cplusplus
}
#endif

#endif /* #ifndef __N_CONFIG_FILE__ */