table of contents
- Tumbleweed 0.8.2-1.1
- Leap-16.0
| ECONF_READFILEWITHCALLBACK(3) | libeconf Manual | ECONF_READFILEWITHCALLBACK(3) |
NAME¶
econf_readFileWithCallback - 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.
SEE ALSO¶
libeconf(3), econf_readFile(3), econf_free(3), econf_errString(3) econf_set_opt(3).
| 2024-12-13 | libeconf |