Giter VIP home page Giter VIP logo

tomlcpp's Introduction

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

tomlcpp's People

Contributors

andersontorres avatar cktan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

tomlcpp's Issues

get*Vector() methods shall be implemented with zero vector element

The get*Vector() methods of class Array shall be fixed as below:
The statement "auto ret = std::make_unique<vector<int64_t>>(top);" shall be modified to construct with zero element, other wise the real size of vector return by these routines is incorrect, that is twice the size of real-world elements.
auto ret = std::make_unique<vector<int64_t>>();

getTimestamp Bug

In tomlcpp.cpp, line90

pair<bool, Timestamp> Table::getTimestamp(const string &key) const {
  Timestamp ret;
  toml_datum_t p = toml_timestamp_in(m_table, key.c_str());
  if (p.ok) {
    toml_timestamp_t &ts = *p.u.ts;
    ret.year = (ts.year ? *ts.year : -1);
    ret.month = (ts.month ? *ts.month : -1);
    ret.day = (ts.day ? *ts.day : -1);
    ret.hour = (ts.hour ? *ts.hour : -1);
    ret.second = (ts.second ? *ts.second : -1);
    ret.millisec = (ts.millisec ? *ts.millisec : -1);
    ret.z = ts.z ? string(ts.z) : "";
    toml_myfree(p.u.ts);
  }
  return {p.ok, ret};
}

the code does not assign minute

Failure installing header file in install phase

Hello! I have found an error while packaging it for Nixpkgs.

  installing
  install flags: SHELL=/nix/store/f7jzmxq9bpbxsg69cszx56mw14n115n5-bash-4.4-p23/bin/bash prefix=/nix/store/h2yvyy0k3m617dc847dk1v5dlxgip0pr-tomlcpp-unstable-2021-02-16 install
  install -d /nix/store/h2yvyy0k3m617dc847dk1v5dlxgip0pr-tomlcpp-unstable-2021-02-16/include /nix/store/h2yvyy0k3m617dc847dk1v5dlxgip0pr-tomlcpp-unstable-2021-02-16/lib
  install toml.h /nix/store/h2yvyy0k3m617dc847dk1v5dlxgip0pr-tomlcpp-unstable-2021-02-16/include
  install toml.hpp /nix/store/h2yvyy0k3m617dc847dk1v5dlxgip0pr-tomlcpp-unstable-2021-02-16/include
  install: cannot stat 'toml.hpp': No such file or directory
  make: *** [Makefile:41: install] Error 1

Offending line:

install toml.hpp ${prefix}/include

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.