Giter VIP home page Giter VIP logo

c-toml's Introduction

c-toml


c-toml is a TOML parsing library written in C with performance, ease of use and customizability in mind.

Content table

  1. Usage
  2. Requirements
  3. Examples
    1. Parsing values
      1. Literals
      2. Compounds
    2. Parsing tables
    3. Parsing files

Usage

First clone the repository

git clone https://github.com/fabriciopashaj/c-toml

and then #include it into your project like this

// ...
#include <c-toml/lib.h>
// ...

Make sure to have built the static library when you build your project.

Requirements

The library has some minor requirements, utilities for it to function. They are

To have them installed you only need to run setup.sh and you don't need to worry about building them, since they are header-only libraries.

Examples

Here are some examples on how to use the library. c-toml provides you with functions to parse from whole TOMl files to individual key-value pairs and values.

Parsing values

Literals

Integers:

TOMLCtx ctx;
StringBuffer toml = StringBuffer_from_strlit("123456754");
TOML_init(&ctx, toml);
TOMLValue value;

/////////////////////////////////////////////////////
assert(TOML_parse_number(&ctx, &value) == TOML_S_OK);
/////////////////////////////////////////////////////

assert(value->kind == TOML_INTEGER);
assert(value->integer == 123456754);

Floats:

TOMLCtx ctx;
StringBuffer toml = StringBuffer_from_strlit("745.236");
TOML_init(&ctx, toml);

/////////////////////////////////////////////////////
assert(TOML_parse_number(&ctx, &value) == TOML_S_OK);
/////////////////////////////////////////////////////

assert(value->kind == TOML_FLOAT);
assert(value->float_ == 745.236);

Strings:

TOMLCtx ctx;
StringBuffer toml = StringBuffer_from_strlit("\"a string\"");
String str;
TOML_init(&ctx, toml);

//////////////////////////////////////////////////////
assert(TOML_parse_sl_string(&ctx, &str) == TOML_S_OK);
//////////////////////////////////////////////////////

assert(String_equal(String_from_strlit("a string"), str));
// TOMLValue_destroy(&value); // don't forget this when you are done

Booleans:

TOMLCtx ctx;
StringBuffer toml = StringBuffer_from_strlit("true");
TOML_init(&ctx, toml);

////////////////////////////////////////////////////
assert(TOML_parse_value(&ctx, &value) == TOML_S_OK);
////////////////////////////////////////////////////

assert(value->kind == TOML_BOOLEAN);
assert(value->boolean);

Compounds

Arrays:

TOMLCtx ctx;
StringBuffer toml = StringBuffer_from_strlit("[1, 2, 3]");
TOML_init(&ctx, toml);
TOMLArray arr;

//////////////////////////////////////////////////
assert(TOML_parse_array(&ctx, &arr) == TOML_S_OK);
//////////////////////////////////////////////////

assert(arr != NULL);
assert(TOMLArray_len(arr) == 3);
for (int i = 0; i < 3; ++i)
{
  assert(arr[i].kind == TOML_INTEGER);
  assert(arr[i].integer == i + 1);
}
// TOMLArray_destroy(arr); // don't forget this when you are done

Inline tables:

TOMLCtx ctx;
StringBuffer toml = StringBuffer_from_strlit("{a = 1, b = 2, cd = 3}");
TOML_init(&ctx, toml);
TOMLTable table = TOMLTable_new();

///////////////////////////////////////////////////////////
assert(TOML_parse_inline_table(&ctx, &value) == TOML_S_OK);
///////////////////////////////////////////////////////////

assert(table != NULL);
assert(TOMLTable_count(table) == 3);
String keys[] = {
  String_from_strlit("a"),
  String_from_strlit("b"),
  String_from_strlit("cd")
};
for (int i = 0; i < 3; ++i)
{
  assert(TOMLTable_get(table, keys[i]).kind == TOML_INTEGER);
  assert(TOMLTable_get(table, keys[i]).integer == i + 1);
}
// TOMLTable_destroy(table); // don't forget this when you are done

Parsing tables

Tables

TOMLCtx ctx;
StringBuffer toml = StringBuffer_from_strlit("\
[foo]       \
bar = 1     \
baz = 4.5   \
");
TOML_init(&ctx, toml);
TOMLTable table = TOMLTable_new();

////////////////////////////////////////////////////
assert(TOML_parse_table(&ctx, &table) == TOML_S_OK);
////////////////////////////////////////////////////

String const foo_str = String_from_strlit("foo");
String const bar_str = String_from_strlit("bar");
String const baz_str = String_from_strlit("baz");
assert(TOMLTable_get(table, foo_str) != NULL);
assert(TOMLTable_get(table, foo_str)->kind == TOML_TABLE);
TOMLTable foo = TOMLTable_get(table, foo_str)->table;
assert(TOMLTable_get(foo, bar_str) != NULL);
assert(TOMLTable_get(foo, bar_str)->kind == TOML_INTEGER);
assert(TOMLTable_get(foo, bar_str)->integer == 1);
assert(TOMLTable_get(foo, baz_str) != NULL);
assert(TOMLTable_get(foo, baz_str)->kind == TOML_FLOAT);
assert(TOMLTable_get(foo, baz_str)->float_ == 4.5);
// TOMLTable_destroy(table); // don't forget this when you are done

Table arrays:

TOMLCtx ctx;
StringBuffer toml = StringBuffer_from_strlit("\
[[foo]]     \
bar = 4.5   \
");
TOML_init(&ctx, toml);
TOMLTable table = TOMLTable_new();

////////////////////////////////////////////////////
assert(TOML_parse_table(&ctx, &table) == TOML_S_OK);
////////////////////////////////////////////////////

String const foo_str = String_from_strlit("foo");
String const bar_str = String_from_strlit("bar");
String const baz_str = String_from_strlit("baz");
assert(TOMLTable_get(table, foo_str) != NULL);
assert(TOMLTable_get(table, foo_str)->kind == TOML_TABLE_ARRAY);
assert(TOMLTable_get(table, foo_str)->array[0].kind == TOML_TABLE);
TOMLArray foo = TOMLTable_get(table, foo_str)->array[0].table;
assert(TOMLTable_get(foo, bar_str) != NULL);
assert(TOMLTable_get(foo, bar_str)->kind == TOML_INTEGER);
assert(TOMLTable_get(foo, bar_str)->integer == 1);
assert(TOMLTable_get(foo, baz_str) != NULL);
assert(TOMLTable_get(foo, baz_str)->kind == TOML_FLOAT);
assert(TOMLTable_get(foo, baz_str)->float_ == 4.5);
// TOMLTable_destroy(table); // don't forget this when you are done

Parsing files

FILE *toml_file = fopen("config.toml", "r");
fseek(toml_file, 0, SEEK_END);
int size = ftell(toml_file);
fseek(toml_file, 0, SEEK_SET);
StringBuffer toml = StringBuffer_with_length(size);
fread(toml, 1, size, toml_file);
fclose(toml_file);
TOMLCtx ctx;
TOML_init(&ctx, toml);
TOMLTable config = TOMLTable_new();

///////////////////////////////////////////////
assert(TOML_parse(&ctx, &config) == TOML_S_OK);
///////////////////////////////////////////////

// ...

// TOMLTable_destroy(config); // don't forget this when you are done

For more examples check the tests.

c-toml's People

Contributors

fabricio-p avatar

Stargazers

Evelyn Herzog avatar Chukobyte avatar Farhan Alvi avatar Willians Faria avatar Daulet Nassipkali avatar Juan Mañanes avatar Jairus avatar

Watchers

 avatar

Forkers

foxyfocus

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.