INI FIle API Reference

INI File API

Simple INI File API for reading and modifying INI files.

Example

The following example demonstrates how to use the INI File API.

// Copyright 2025 Robert Bosch GmbH

#include <stdio.h>
#include <dse/clib/ini/ini.h>

#define INI_FILE "dse_clib.ini"

int main(void)
{
    // Open and read an INI file (if it exists).
    IniDesc ini = ini_open(INI_FILE);

    // Get an INI value.
    printf("foo (initial value) = %s\n", ini_get_val(&ini, "foo"));

    // Set an INI value.
    ini_set_val(&ini, "foo", "fubar", true); /* Overwrite existing value. */
    ini_set_val(&ini, "bar", "bar", false);  /* Only if not already set. */
    printf("foo (updated value) = %s\n", ini_get_val(&ini, "foo"));
    printf("bar (default value) = %s\n", ini_get_val(&ini, "bar"));

    // Expand an environment var.
    ini_set_val(&ini, "user", "${USER}", true);
    ini_expand_vars(&ini);
    printf("user (env value) = %s\n", ini_get_val(&ini, "user"));

    // Delete a value.
    ini_delete_key(&ini, "user");
    printf("user (deleted) = %s\n", ini_get_val(&ini, "user"));

    // Save the INI file.
    ini_write(&ini, INI_FILE);

    // Close the desc object (and release used memory).
    ini_close(&ini);
    return 0;
}

Typedefs

IniDesc

typedef struct IniDesc {
    int lines;
}

Functions

ini_close

Release any resources allocated by the INI File object.

Parameters

ini (IniDesc*)
INI File object.

ini_delete_key

Delete the specified key from the INI File object.

Parameters

ini (IniDesc*)
INI File object.
key (const char*)
The key to delete.

ini_expand_vars

Expand environment variables contained within the INI File values.

Example INI File

user=${USER:-default-user}`

Parameters

ini (IniDesc*)
INI File object.

ini_get_val

Set a key-value pair on the INI File object.

Parameters

ini (IniDesc*)
INI File object.
key (const char*)
The key to get.
overwrite (bool)
When true, if the key already exists then overwrite with the provided value.

Returns

char*
The corresponding value of key, or NULL if key is was not found.

ini_open

Configure and load an INI File object.

Parameters

path (const char*)
Path to an INI File to load. If NULL or missing no error will occur.

Returns

IniDesc (struct)
INI File object.

ini_set_val

Set a key-value pair on the INI File object.

Parameters

ini (IniDesc*)
INI File object.
key (const char*)
The key to set.
val (const char*)
The corresponding value to set.
overwrite (bool)
When true, if the key already exists then overwrite with the provided value.

ini_write

Write the key-value pair of the INI File object to the named file.

Parameters

ini (IniDesc*)
INI File object.
path (const char*)
The file to write the key-value pairs to.