econf_readFileWithCallback - Man Page

parse a configuration file with a user-defined callback

Synopsis

#include <libeconf​.h>

econf_err econf_readFileWithCallback(econf_file **result, const char *file_name, const char *delim, const char *comment, bool (*callback)(const char *filename, const void *data), const void *callback_data);

Description

econf_readFileWithCallback reads and parses the configuration file specified by file_name​. It functions similarly to econf_readFile, but allows the user to define a callback function​. This callback is executed before the file is parsed, allowing the application to perform checks on the file (e​.g​., verifying file permissions or ownership)​.

The result argument points to a pointer that will hold the parsed configuration data upon success​. The delim argument specifies the delimiter used for key/value pairs (typically "=")​. The comment argument specifies the string that marks the beginning of a comment (typically "#")​.

The callback function is called with the file_name and the provided callback_data​. If the callback returns false, the parsing process is aborted, and the function returns ECONF_PARSING_CALLBACK_FAILED​.

Return Value

On success, econf_readFileWithCallback returns ECONF_SUCCESS​.

On failure, a non-zero error code of type econf_err is returned​.

Errors

ECONF_NOFILE

         The specified file_name does not exist​.

ECONF_NOMEM

         Insufficient memory to allocate the result structure​.

ECONF_MISSING_BRACKET
ECONF_TEXT_AFTER_SECTION
ECONF_EMPTY_SECTION_NAME
ECONF_MISSING_DELIMITER

         The file contains syntax errors and could not be parsed successfully​.

ECONF_PARSING_CALLBACK_FAILED

         The user-provided callback returned false, aborting the operation​.

ECONF_ARGUMENT_IS_NULL_VALUE

         One of the required arguments (such as result or file_name) is NULL​.

Example

#include <libeconf​.h>
#include <stdbool​.h>
#include <stdio​.h>

/* Callback to check if file is not empty or other checks */
bool check_file(const char *filename, const void *data) {
    if (filename == NULL) return false;
    /* Perform custom checks here */
    return true;
}

int main(void) {
    econf_file *key_file = NULL;
    econf_err err;

    err = econf_readFileWithCallback(&key_file, "/etc/test​.conf", "=", "#", check_file, NULL);

    if (err == ECONF_SUCCESS) {
        printf("File parsed successfully​.\n");
        econf_free(key_file);
    } else {
        printf("Error parsing file: %s\n", econf_errString(err));
    }

    return 0;
}

Application Usage

Default behaviour if entries have the same name in one file: The first hit will be returned​. Further entries will be ignored​. This can be changed by setting the environment variable JOIN_SAME_ENTRIES (see econf_set_opt)​. In that case entries with the same name will be joined to one single entry​. Keys have to be unique per section​. Otherwise the behavior is undefined​.

See Also

libeconf(3), econf_readFile(3), econf_free(3), econf_errString(3) econf_set_opt(3)​.

Info

2024-12-13 libeconf Manual