Coder Social home page Coder Social logo

skvconf's Introduction

skvconf

Simple key-value configuration language written in C

Compiling from source

Dependencies

  • meson
  • A C compiler (e.g. gcc)
  • flex
  • bison

Build Instructions

  1. meson setup build .
  2. meson compile -C build
  3. meson install -C build (optional)

If you use meson in your project you can also use skvconf through a subproject.

Tests

If you wish to contribute (for whatever reason) or just check wether the API behaves correctly use meson test -C build. When contributing add test cases that check your expected behavior.

Basic Usage

skvconf is simple. Both in it's language and API. A basic example looks like this:

#include <skvconf.h>
#include <stdio.h>

int main(void) {
    skvconf_elm_t *conf;
    skvconf_parse_file("test.conf", &conf);
    skvconf_elm_t *elm = skvconf_find_element(conf, "test.value", NULL);
    if (!elm) {
        printf("undefined\n");
        return 1;
    }
    if (elm->type != SKVCONF_TYPE_STR) {
        printf("wrong type!\n");
        return 1;
    }
    printf("test.value: %s\n", elm->val.str);
    skvconf_elm_destroy(conf);
    return 0;
}

Your main interactions are going to be with three functions:

  • skvconf_parse_file
  • skvconf_find_element
  • skvconf_elm_destroy All of them are made available through the skvconf.h header, which also includes a few other headers automatically.

The definition for an skvconf_elm_t can be found in skvconf_types.h:

typedef struct skvconf_elm {
    skvconf_type_t type;
    union {
        int num;
        char *str;
        bool boolean;
    } val;
    char *id;
    struct skvconf_elm *next;
    struct skvconf_elm *child;
} skvconf_elm_t;

the type is simply an enum specifying the type of the object:

typedef enum {
    SKVCONF_TYPE_NUM,
    SKVCONF_TYPE_STR,
    SKVCONF_TYPE_BOOLEAN,
    SKVCONF_TYPE_GROUP,
} skvconf_type_t;

Depending on the type of the object you can access the corresponding member of the union val in an element.

Notes

  • You can't use skvconf_find_element to find a group element
  • If a single key has been defined with multiple values, the last one is used, regardless of type (this is due to the find function exiting after finding the first element that matches and the parser returning the elements in reverse order).

Configuration file syntax

A configuration file has a very basic syntax:

a = 3
b = "abc"
c = true
d {
    e = false
}

It is made up of key-value pairs as well as groups, which are basically namespaces. In this example, a, b and c are simply key-value pairs accessible by their respective names. d is a group which means it cannot be found by the skvconf_find_element function and e is another key-value pair which is accessible as d.e. You can also have nested groups.

skvconf's People

Contributors

uludev avatar

Stargazers

 avatar

Watchers

 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.