8000 GitHub - qxuken/tomlc17: TOML parser in C17
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

qxuken/tomlc17

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

88 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

tomlc17

TOML v1.0 in c17.

Usage

See tomlc17.h for details.

Parsing a toml document creates a tree data structure in memory that reflects the document. Information can be extracted by navigating this data structure.

Note: you can simply include tomlc17.h and tomlc17.c in your project without running make and building the library.

The following is a simple example:

/*
 * Parse the config file simple.toml:
 *
 * [server]
 * host = "www.example.com"
 * port = [8080, 8181, 8282]
 *
 */
#include "../src/tomlc17.h"
#include <errno.h>
#include <stdlib.h>
#include <string.h>

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

int main() {
  // Parse the toml file
  toml_result_t result = toml_parse_file_ex("simple.toml");

  // Check for parse error
  if (!result.ok) {
    error(result.errmsg, 0);
  }

  // Extract values
  toml_datum_t server = toml_get(result.toptab, "server");
  toml_datum_t host = toml_get(server, "host");
  toml_datum_t port = toml_get(server, "port");

  // Print server.host
  if (host.type != TOML_STRING) {
    error("missing or invalid 'server.host' property in config", 0);
  }
  printf("server.host = %s\n", host.u.s);

  // Print server.port
  if (port.type != TOML_ARRAY) {
    error("missing or invalid 'server.port' property in config", 0);
  }
  printf("server.port = [");
  for (int i = 0; i < port.u.arr.size; i++) {
    toml_datum_t elem = port.u.arr.elem[i];
    if (elem.type != TOML_INT64) {
      error("server.port element not an integer", 0);
    }
    printf("%s%d", i ? ", " : "", (int)elem.u.int64);
  }
  printf("]\n");

  // Done!
  toml_free(result);
  return 0;
}

Building

For debug build:

export DEBUG=1
make

For release build:

unset DEBUG
make

Running tests

We run the official toml-test as described here. Refer to this section for prerequisites to run the tests.

The following command invokes the tests:

make test

As of today (05/07/2025), all tests passed:

toml-test v0001-01-01 [/home/cktan/p/tomlc17/test/stdtest/driver]: using embedded tests
  valid tests: 185 passed,  0 failed
invalid tests: 371 passed,  0 failed

Installing

The install command will copy tomlc17.h, tomlcpp.hpp and libtomlc17.a to the $prefix/include and $prefix/lib directories.

unset DEBUG
make clean install prefix=/usr/local

About

TOML parser in C17

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C 85.5%
  • C++ 8.5%
  • Makefile 4.8%
  • Shell 1.2%
0