Lightweight, strictly compliant TOML v1.1 parser for C and C++. (MIRROR)
  • C 87.2%
  • C++ 7%
  • Makefile 4.8%
  • Shell 1%
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
CK Tan fba134d7d1 Use offsetof() in page_create() instead of null-pointer member access (#56)
The hand-rolled offsetof applied -> to a null pointer, which is
undefined behavior and is reported by clang's UBSan at -Og. offsetof()
computes the same size.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-06 01:32:40 +08:00
.github/workflows disable windows workflow 2026-05-17 14:33:36 -07:00
simple fix windows workflow 2026-05-17 14:30:40 -07:00
src Use offsetof() in page_create() instead of null-pointer member access (#56) 2026-10-06 01:32:40 +08:00
test format-only 2026-09-21 13:09:30 +00:00
.gitignore fix NULL crash in toml_parse and clean up workspace 2026-05-19 23:51:45 +00:00
API.md Limit toml_parse_file() input to 1GB 2026-10-03 19:18:52 -07:00
BAREKEYCHAR.md fix NULL crash in toml_parse and clean up workspace 2026-05-19 23:51:45 +00:00
CLAUDE.md CLAUDE.md 2026-07-07 17:12:00 +00:00
DESIGN.md Add magic number to page_t and a whitebox unit test for pool_t 2026-07-07 18:05:42 +00:00
LICENSE Fix LICENSE to match standard MIT template 2026-07-12 19:16:36 +00:00
Makefile Fix typos: Dictinary→Dictionary, SECTOIN→SECTION, tet→test 2026-04-10 06:57:43 +00:00
OPTIONS.md Clarify custom allocator documentation and requirements (#51) 2026-09-21 13:07:48 +00:00
README.md badges 2026-07-12 19:11:25 +00:00
README_CXX.md comment-only 2025-07-12 23:39:43 -07:00

tomlc17

C/C++ CI Latest release License: MIT TOML v1.1

A lightweight, strictly compliant TOML v1.1 parser for C and C++.

Overview

tomlc17 parses TOML documents into an in-memory tree structure for straightforward navigation. It is optimized for clean integration and efficient execution, utilizing a single-pass scanner, a dedicated string memory pool, and safe recursive teardowns.

  • Compliance: Fully implements TOML v1.1 and passes the standard toml-test validation suite.
  • Compatibility: Written in C17. Fully compatible with C99 and C++.
  • Modern C++ Support: Includes dedicated C++20 accessors (see README_CXX.md).
  • Zero-Friction Integration: Amalgamated design. Simply drop tomlc17.h and tomlc17.c into your source tree, or build it as a library.

Quick Start

For complete API details, refer to API.md.

Example: Parsing & Extraction

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

/*
 * Parse the config file simple.toml:
 *
 * [server]
 * host = "www.example.com"
 * port = [8080, 8181, 8282]
 *
 */
#include "../src/tomlc17.h"
#include <errno.h>
#include <inttypes.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 host = toml_seek(result.toptab, "server.host");
  toml_datum_t port = toml_seek(result.toptab, "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%" PRId64, i ? ", " : "", 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 May 7, 2025, all tests passed for TOML v1.0:

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

As of Dec 25, 2025, all tests passed for TOML v1.1:

toml-test v0001-01-01 [/home/cktan/p/tomlc17/test/stdtest/driver] [no encoder]
  valid tests: 214 passed,  0 failed
encoder tests: no encoder command given
invalid tests: 466 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

Options

For information on configuring library options, such as setting custom memory allocators, see OPTIONS.md.