tomlc99

repository·master·Indexed 20 days ago

https://github.com/cktan/tomlc99

An obsolete C99 library for parsing TOML files. It provides functionality to parse files into toml_table_t structures, traverse tables, and extract values using type-specific functions like toml_string_in and toml_int_in. Users are advised to migrate to the newer tomlc17 version.

Tokens
1.1K
Snippets
6
Records
6
Agent score
21%

What's inside tomlc99

  1. Understanding the toml_datum_t structure

    master

    Functions like toml_*_at and toml_*_in return a toml_datum_t structure. This structure contains an ok flag to indicate if the lookup was successful.

    If ok is true, you can access the value via the u union (e.g., u.s for strings, u.i for integers).

    CRITICAL MEMORY MANAGEMENT: If the accessed value is a string (u.s) or a timestamp (u.ts), you must call free() on that specific field after use to avoid memory leaks.

    toml_datum_t host = toml_string_in(tab, "host");
    if (host.ok) {
        printf("host: %s\n", host.u.s);
        free(host.u.s); // Required for string types
    }
  2. Use tomlc99 to parse TOML files in C

    master

    To use tomlc99, follow these four steps:

    1. Parse: Use toml_parse_file to load a file into a toml_table_t.
    2. Traverse: Use toml_table_in to locate specific tables within the parsed structure.
    3. Extract: Use type-specific functions (like toml_string_in or toml_int_in) to retrieve values.
    4. Free: Release allocated memory using toml_free for the table and free() for strings or timestamps.

    Note: This library is OBSOLETE. A newer version compatible with c99 is available at tomlc17.

    #include "toml.h"
    
    // 1. Parse
    char errbuf[200];
    FILE* fp = fopen("sample.toml", "r");
    toml_table_t* conf = toml_parse_file(fp, errbuf, sizeof(errbuf));
    
    // 2. Traverse
    toml_table_t* server = toml_table_in(conf, "server");
    
    // 3. Extract
    toml_datum_t host = toml_string_in(server, "host");
    if (host.ok) {
        printf("host: %s\n", host.u.s);
        free(host.u.s); // Must free strings
    }
    
    // 4. Free
    toml_free(conf);
  3. Install tomlc99

    master

    You can integrate tomlc99 into your project in two ways:

    1. Direct Inclusion: Simply copy toml.c and toml.h into your project source tree.
    2. System Installation: Use make to build and install.

    To install to the default system path (/usr/local/include and /usr/local/lib):

    make install

    To install to a custom directory:

    make install prefix=/your/custom/path
    make install prefix=/a/file/path
  4. Accessing Array Content in tomlc99

    master

    TOML arrays are accessed using integer indices via functions named toml_*_at(array, index).

    Array Operations

    • Get Size: Use toml_array_nelem(arr) to get the number of elements.
    • Get Element: Use the following functions with a valid index:
      • toml_string_at(arr, idx)
      • toml_bool_at(arr, idx)
      • toml_int_at(arr, idx)
      • toml_double_at(arr, idx)
      • toml_timestamp_at(arr, idx)
      • toml_table_at(arr, idx)
      • toml_array_at(arr, idx)
    int size = toml_array_nelem(arr);
    toml_datum_t val = toml_int_at(arr, 0);
  5. Accessing Table Content in tomlc99

    master

    TOML tables act as dictionaries where lookups are performed using string keys. Most table access functions follow the toml_*_in(table, key) pattern.

    Key Retrieval Functions

    Use these when you know the expected type of the value:

    • toml_string_in(tab, key)
    • toml_bool_in(tab, key)
    • toml_int_in(tab, key)
    • toml_double_in(tab, key)
    • toml_timestamp_in(tab, key)
    • toml_table_in(tab, key)
    • toml_array_in(tab, key)

    Iterating over Keys

    You can iterate through a table using an integer index with toml_key_in(tab, index):

    toml_table_t* tab = toml_parse_file(...);
    for (int i = 0; ; i++) {
        const char* key = toml_key_in(tab, i);
        if (!key) break;
        printf("key %d: %s\n", i, key);
    }