All Projects → cktan → tomlcpp

cktan / tomlcpp

Licence: MIT license
No fanfare TOML C++ Library

Programming Languages

c
50402 projects - #5 most used programming language
C++
36643 projects - #6 most used programming language
Makefile
30231 projects

Projects that are alternatives of or similar to tomlcpp

tomli
A lil' TOML parser
Stars: ✭ 313 (+1390.48%)
Mutual labels:  toml-parser, toml
Tomlet
Zero-Dependency, model-based TOML De/Serializer for .NET
Stars: ✭ 56 (+166.67%)
Mutual labels:  toml-parser, toml
rcpptoml
Rcpp Bindings to C++ parser for TOML files
Stars: ✭ 26 (+23.81%)
Mutual labels:  toml-parser, toml
tomland
🏝 Bidirectional TOML serialization
Stars: ✭ 103 (+390.48%)
Mutual labels:  toml-parser, toml
pytomlpp
A python wrapper for tomlplusplus
Stars: ✭ 56 (+166.67%)
Mutual labels:  toml-parser, toml
toml-f
TOML parser implementation for data serialization and deserialization in Fortran
Stars: ✭ 69 (+228.57%)
Mutual labels:  toml-parser, toml
qtoml
Another Python TOML encoder/decoder
Stars: ✭ 26 (+23.81%)
Mutual labels:  toml-parser, toml
pp-toml
Paul's Parser for Tom's Own Minimal Language
Stars: ✭ 17 (-19.05%)
Mutual labels:  toml-parser, toml
Boost.toml
header-only C++(98|11|14|17) TOML v0.5.0 parser/encoder depending on Boost
Stars: ✭ 26 (+23.81%)
Mutual labels:  toml
crates
crates is an extension aims to help people to manage their dependencies for rust (crates.io & TOML).
Stars: ✭ 156 (+642.86%)
Mutual labels:  toml
llmk
Light LaTeX Make
Stars: ✭ 93 (+342.86%)
Mutual labels:  toml
cmkr
Modern build system based on CMake and TOML.
Stars: ✭ 211 (+904.76%)
Mutual labels:  toml
generate-awesome
🖨 A command-line tool for generating Awesome Lists from a set of data files.
Stars: ✭ 22 (+4.76%)
Mutual labels:  toml
contentful-export
Extract Contentful to Hugo
Stars: ✭ 22 (+4.76%)
Mutual labels:  toml
parsetoml
A Nim library to parse TOML files
Stars: ✭ 96 (+357.14%)
Mutual labels:  toml
climatecontrol
Python library for loading settings and config data from files and environment variables
Stars: ✭ 20 (-4.76%)
Mutual labels:  toml
config-cpp
C++ Configuration management library inspired by the Viper package for golang.
Stars: ✭ 21 (+0%)
Mutual labels:  toml
toml
TOML parser and encoder for Go with reflection.
Stars: ✭ 19 (-9.52%)
Mutual labels:  toml
htoml
TOML file format parser in Haskell
Stars: ✭ 39 (+85.71%)
Mutual labels:  toml
paerser
No description or website provided.
Stars: ✭ 38 (+80.95%)
Mutual labels:  toml

tomlcpp

TOML in C++; v1.0 compliant.

This is a C++ wrapper around the C library available here: https://github.com/cktan/tomlc99.

Usage

Here is a simple example that parses this config file:

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

Steps for getting values:

  1. Call toml::parseFile on a toml file
  2. Get the top-level table
  3. Get values from the top-level table
  4. Examine the values
#include <utility>
#include <string>
#include <vector>
#include <memory>
#include <iostream>
#include "tomlcpp.hpp"

using std::cerr;
using std::cout;

void error(std::string msg)
{
    cerr << "ERROR: " << msg << "\n";
    exit(1);
}

int main()
{
    // 1. parse file
    auto res = toml::parseFile("sample.toml");
    if (!res.table) {
        error("cannot parse file: " + res.errmsg);
    }

    // 2. get top level table
    auto server = res.table->getTable("server");
    if (!server) {
        error("missing [server]");
    }

    // 3. extract values from the top level table
    auto [ ok, host ] = server->getString("host");
    if (!ok) {
        fatal("missing or bad host entry");
    }

    auto portArray = server->getArray("port");
    if (!portArray) {
        fatal("missing 'port' array");
    }

    // 4. examine the values
    cout << "host: " << host << "\n";
    cout << "port: ";
    for (int i = 0; ; i++) {
        auto p = portArray->getInt(i);
        if (!p.first) break;

        cout << p.second << " ";
    }
    cout << "\n";

    return 0;
}

Parsing

To parse a toml text or file, invoke toml::parse(text) or toml::parseFile(path). The return value is a Result struct. On success, the Result.table will have a non-NULL pointer to the toml table content. Otherwise, the Result.table will be NULL, and Result.errmsg stores a string describing the error.

Traversing table

Toml tables are key-value maps.

Keys

The method Table::keys() returns a vector of keys.

Content

To extract values in a Table, call the Table::getXXXX(key) methods and supply the key:

Table::getString(key)
Table::getBool(key)
Table::getInt(key)
Table::getDouble(key)
Table::getTimestamp(key)

These methods return a C++ pair, in which pair.first is a success indicator, and pair.second is the result value.

To access table or array in a Table, use these methods which return a unique_ptr to a Table or Array:

Table::getTable(key)
Table::getArray(key)

Traversing array

Similarly, to extract the primitive content of a toml::Array, call one of these methods:

Array::getString(idx)
Array::getBool(idx)
Array::getInt(idx)
Array::getDouble(idx)
Array::getTimestamp(idx)
Array::getArray(idx)
Array::getTable(idx)

Building and installing

A normal make suffices. You can also simply include the toml.c, toml.h, tomlcpp.cpp, tomlcpp.hpp 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}.

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].