All Projects → cktan → Tomlc99

cktan / Tomlc99

Licence: other
TOML C library

Programming Languages

c
50402 projects - #5 most used programming language

Labels

Projects that are alternatives of or similar to Tomlc99

Unpuzzled
A colorful CLI library with variable provenance.
Stars: ✭ 57 (-73.61%)
Mutual labels:  toml
Devshell
Per project developer environments
Stars: ✭ 129 (-40.28%)
Mutual labels:  toml
Toml
A PHP parser for TOML
Stars: ✭ 152 (-29.63%)
Mutual labels:  toml
Better Toml
toml extension for vs code editor
Stars: ✭ 60 (-72.22%)
Mutual labels:  toml
Staert
Merge your configuration sources
Stars: ✭ 108 (-50%)
Mutual labels:  toml
Taplo
A TOML toolkit written in Rust
Stars: ✭ 136 (-37.04%)
Mutual labels:  toml
Ansible Config encoder filters
Ansible role used to deliver the Config Encoder Filters.
Stars: ✭ 48 (-77.78%)
Mutual labels:  toml
Python Benedict
dict subclass with keylist/keypath support, I/O shortcuts (base64, csv, json, pickle, plist, query-string, toml, xml, yaml) and many utilities. 📘
Stars: ✭ 204 (-5.56%)
Mutual labels:  toml
Datafiles
A file-based ORM for Python dataclasses.
Stars: ✭ 113 (-47.69%)
Mutual labels:  toml
Tinytoml
A header only C++11 library for parsing TOML
Stars: ✭ 153 (-29.17%)
Mutual labels:  toml
To.ml
OCaml library for TOML
Stars: ✭ 68 (-68.52%)
Mutual labels:  toml
Night Config
Powerful java configuration library for toml, yaml, hocon, json and in-memory configurations
Stars: ✭ 93 (-56.94%)
Mutual labels:  toml
Rq
Record Query - A tool for doing record analysis and transformation
Stars: ✭ 1,808 (+737.04%)
Mutual labels:  toml
Re Txt
converts text-formats from one to another, it is very useful if you want to re-format a json file to yaml, toml to yaml, csv to yaml, ... etc
Stars: ✭ 59 (-72.69%)
Mutual labels:  toml
Simple Settings
A simple way to manage your project settings.
Stars: ✭ 165 (-23.61%)
Mutual labels:  toml
Resticprofile
Configuration profiles for restic backup
Stars: ✭ 48 (-77.78%)
Mutual labels:  toml
Toml To Go
Translates TOML into a Go type in your browser instantly
Stars: ✭ 134 (-37.96%)
Mutual labels:  toml
Awesome Cms
📚 A collection of open and closed source Content Management Systems (CMS) for your perusal.
Stars: ✭ 2,498 (+1056.48%)
Mutual labels:  toml
Protodep
Collect necessary .proto files (Protocol Buffers IDL) and manage dependencies
Stars: ✭ 167 (-22.69%)
Mutual labels:  toml
Fig
A minimalist Go configuration library
Stars: ✭ 142 (-34.26%)
Mutual labels:  toml

tomlc99

TOML in c99; v1.0 compliant.

If you are looking for a C++ library, you might try this wrapper: https://github.com/cktan/tomlcpp.

Usage

Please see the toml.h file for details. What follows is a simple example that parses this config file:

[server]
	host = "www.example.com"
	port = [ 8080, 8181, 8282 ]

The steps for getting values from our file is usually :

  1. Parse the TOML file.
  2. Traverse and locate a table in TOML.
  3. Extract values from the table.
  4. Free up allocated memory.

Below is an example of parsing the values from the example table.

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

static void error(const char* msg, const char* msg1)
{
    fprintf(stderr, "ERROR: %s%s\n", msg, msg1?msg1:"");
    exit(1);
}


int main()
{
    FILE* fp;
    char errbuf[200];

    // 1. Read and parse toml file
    fp = fopen("sample.toml", "r");
    if (!fp) {
        error("cannot open sample.toml - ", strerror(errno));
    }

    toml_table_t* conf = toml_parse_file(fp, errbuf, sizeof(errbuf));
    fclose(fp);

    if (!conf) {
        error("cannot parse - ", errbuf);
    }

    // 2. Traverse to a table.
    toml_table_t* server = toml_table_in(conf, "server");
    if (!server) {
        error("missing [server]", "");
    }

    // 3. Extract values
    toml_datum_t host = toml_string_in(server, "host");
    if (!host.ok) {
        error("cannot read server.host", "");
    }

    toml_array_t* portarray = toml_array_in(server, "port");
    if (!portarray) {
        error("cannot read server.port", "");
    }

    printf("host: %s\n", host.u.s);
    printf("port: ");
    for (int i = 0; ; i++) {
        toml_datum_t port = toml_int_at(portarray, i);
        if (!port.ok) break;
        printf("%d ", (int)port.u.i);
    }
    printf("\n");

    // 4. Free memory
    free(host.u.s);
    toml_free(conf);
    return 0;
}

Accessing Table Content

TOML tables are dictionaries where lookups are done using string keys. In general, all access functions on tables are named toml_*_in(...).

In the normal case, you know the key and its content type, and retrievals can be done using one of these functions:

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);

You can also interrogate the keys in a table using an integer 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);
}

Accessing Array Content

TOML arrays can be deref-ed using integer indices. In general, all access methods on arrays are named toml_*_at().

To obtain the size of an array:

int size = toml_array_nelem(arr);

To obtain the content of an array, use a valid index and call one of these functions:

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);

toml_datum_t

Some toml_*_at and toml_*_in functions return a toml_datum_t structure. The ok flag in the structure indicates if the function call was successful. If so, you may proceed to read the value corresponding to the type of the content.

For example:

toml_datum_t host = toml_string_in(tab, "host");
if (host.ok) {
	printf("host: %s\n", host.u.s);
	free(host.u.s);   /* FREE applies to string and timestamp types only */
}

** IMPORTANT: if the accessed value is a string or a timestamp, you must call free(datum.u.s) or free(datum.u.ts) respectively after usage. **

Building and installing

A normal make suffices. You can also simply include the toml.c and toml.h files in your project.

Invoking make install will install the header and library files into /usr/local/{include,lib}.

Alternatively, specify make install prefix=/a/file/path to install into /a/file/path/{include,lib}.

Testing

To test against the standard test set provided by BurntSushi/toml-test:

% make
% cd test1
% bash build.sh   # do this once
% bash run.sh     # this will run the test suite

To test against the standard test set provided by iarna/toml:

% make
% cd test2
% bash build.sh   # do this once
% bash run.sh     # this will run the test suite
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].