Giter VIP home page Giter VIP logo

ini's Introduction

ini.h

Simple single-header ini parser in c99

Usage

In just one c/c++ file do this:

#define INI_IMPLEMENTATION
#include "ini.h"

In every other file you can simply include ini.h

Options

  • merge_duplicate_tables:

    By default, when there are multiple tables with the same name, the parser simply keeps adding to the tables list, it wastes memory but it is also much faster, especially for bigger files. if you want the parser to merge all the tables together use this option

  • override_duplicate_keys:

    when adding keys, if a table has two same keys, the parser adds it to the table anyway, meaning that when it searches for the key with ini_get it will get the first one but it will still have both in table.values, to override this behaviour and only keep the last value use this option

  • key_value_divider:

    the default key/value divider is '=', if you want to change it use this option

Simple example

From file:

ini_t ini = ini_parse("file.ini", NULL);

char *name = ini_as_str(ini_get(ini_get_table(&ini, INI_ROOT), "name"), false);

initable_t *server = ini_get_table(&ini, "server");
int port = (int)ini_as_int(ini_get(server, "port"));
bool use_threads = ini_as_bool(ini_get(server, "use threads"));

ini_free(&ini);
free(name);

From string:

const char *ini_str = 
    "name : web-server\n"
    "[server]\n"
    "port : 8080\n"
    "ip : localhost\n"
    "use threads : false";
ini_t ini = ini_parse_str(ini_str, &(iniopts_t){ .key_value_divider = ':' });

char *name = ini_as_str(ini_get(ini_get_table(&ini, INI_ROOT), "name"), false);

initable_t *server = ini_get_table(&ini, "server");
int port = (int)ini_as_int(ini_get(server, "port"));
bool use_threads = ini_as_bool(ini_get(server, "use threads"));
char ip[64];
int iplen = ini_to_str(ini_get(server, "ip"), ip, sizeof(ip), false);
if (iplen < 0) {
    printf("(err) couldn't get ip: %s\n", ini_explain(iplen));
}

free(name);
ini_free(&ini);

ini's People

Contributors

alessandrobason avatar

Stargazers

z04a avatar Herwin Bozet avatar  avatar Jarrod Davis avatar Ferenc Fejes avatar Brandon Jordan avatar pixie avatar Cattail avatar Marc-Alexandre Espiaut avatar  avatar  avatar Artem Mironov avatar Nicole Whitman Sager  avatar Daniel Phan avatar Eva Pace avatar Raimon Grau avatar eocore kuneho avatar Hanks avatar Ian Smith avatar Brad Svercl avatar Jeremy Legendre avatar  avatar Brett Nash avatar

Watchers

 avatar  avatar Jarrod Davis avatar

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.