ECONF\_READFILEWITHCALLBACK(3) libeconf Manual ECONF\_READFILEWITHCALLBACK(3)

# [NAME](#NAME)

econf\_readFileWithCallback - parse a configuration file with a user-defined callback

# [SYNOPSIS](#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](#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](#RETURN_VALUE)

On success, **econf\_readFileWithCallback** returns **ECONF\_SUCCESS**.

On failure, a non-zero error code of type **econf\_err** is returned.

# [ERRORS](#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](#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](#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](#SEE_ALSO)

[**libeconf**(3)](/man/os/libeconf/libeconf.3), [**econf\_readFile**(3)](/man/os/libeconf/econf_readFile.3), [**econf\_free**(3)](/man/os/libeconf/econf_free.3), [**econf\_errString**(3)](/man/os/libeconf/econf_errString.3) [**econf\_set\_opt**(3)](/man/os/libeconf/econf_set_opt.3).

2024-12-13 libeconf

---

Document: econf_readFileWithCallback(3) from libeconf 0.8.6-r2 (Chainguard OS)
Source: https://github.com/openSUSE/libeconf
