C:
Working with TOML

How to:

To work with TOML in C, you first need a library capable of parsing TOML files, as the C standard library does not include this functionality. A popular choice is tomlc99, a lightweight TOML parser for C99. Here’s a quick guide to read a simple TOML config file:

First, ensure you’ve tomlc99 installed and properly linked in your project.

Sample TOML file (config.toml):

[database]
server = "192.168.1.1"
ports = [ 8001, 8001, 8002 ]
connection_max = 5000
enabled = true

C code to parse this file:

#include <stdio.h>
#include <stdlib.h>
#include "toml.h"

int main() {
    FILE *configFile;
    configFile = fopen("config.toml", "r");
    if (!configFile) {
        perror("Cannot open file");
        return EXIT_FAILURE;
    }

    toml_table_t *config = toml_parse_file(configFile, NULL, 0);
    if (!config) {
        fprintf(stderr, "Error parsing file\n");
        fclose(configFile);
        return EXIT_FAILURE;
    }

    toml_table_t *database = toml_table_in(config, "database");
    if (database) {
        const char *server = toml_raw_in(database, "server");
        printf("Database Server: %s\n", server);

        toml_array_t *ports = toml_array_in(database, "ports");
        for (int i = 0; i < toml_array_nelem(ports); i++) {
            int64_t port;
            toml_int_at(ports, i, &port);
            printf("Port %d: %ld\n", i, port);
        }
    }

    toml_free(config);
    fclose(configFile);
    return EXIT_SUCCESS;
}

Output:

Database Server: "192.168.1.1"
Port 0: 8001
Port 1: 8001
Port 2: 8002

Deep Dive

TOML was created by Tom Preston-Werner, co-founder of GitHub, as a response to the limitations he perceived in other configuration file formats. Its goal is to be straightforward and unambiguous, both for humans and computers, to read and write without needing complex parsing rules. In the C ecosystem, TOML isn’t a first-class citizen like it might be in higher-level languages such as Rust with its serde_toml or Python with toml, which have libraries with native support. Rather, C developers need to rely on external libraries like tomlc99, but this is typical given C’s emphasis on minimalism and performance.

While TOML is praised for its clarity, when choosing a configuration file format, it’s vital to consider the project’s needs. In scenarios requiring more complex structures or interactivity with web APIs, JSON or even YAML might offer a better fit despite their increased complexity. TOML shines in configurations where readability and simplicity are paramount, not necessarily where the most advanced data structures are needed.