Jump to content
  • Advertisement
Sign in to follow this  
robinei

Small config file library

This topic is 3524 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

After doing various ad hoc file formats for config data, and using XML for structured data(which I don't much like), I decided to make a little library for my own format and I thought I'd share it here. It's very minimalistic in terms of features, and just provides the basics needed to traverse the structured data. Values are just stored as strings, and need to be converted to numbers etc. manually. I guess I could expand the interface with some functions for searching and for tokenizing/conversion of value data. I think that performance-wise it would be difficult to improve, and it provides OK error reporting. It hardly does any allocation and just 0-terminates directly in the source text. Here's an example file:
# can't use ; # { } in value or name
no_spaces_in_name
    Value is everything from name
    to semicolon or brace, with leading
    and trailing spaces stripped!
    ;

example 0.1 {
    x 10; y 20;
    items { axe; sword; }
    attribs {
        str 12;
        dex 16;
        int 15;
    }
}

# comments must be between entries

# name is never NULL
foo;             # value and child are NULL
foo{}            # value and child are NULL
foo bar;         # child is NULL
foo { bar; }     # value is NULL
foo bar { baz; } # next is NULL
Here's a bit of code which uses the library for loading a file, and then printing it readably(by the parser). Note that the + character seems to be stripped from the code listings even when I use lang="c".
#include <stdio.h>
#include "conf.h"

static int ind = 0;

static void indent() {
    int i;
    for(i = 0; i < ind; ++i)
        putchar(' ');
}

static void printconf(Conf *conf) {
    Conf *n;
    char *v;
    
    if(!conf)
        return;
    
    indent();
    printf("%s", conf_name(conf));
    
    v = conf_value(conf);
    if(v)
        printf(" %s", v);
    
    n = conf_child(conf);
    if(n) {
        printf(" {\n");
        ind += 4;
        printconf(n);
    }
    else
        printf(";\n");
    
    n = conf_next(conf);
    if(n)
        printconf(n);
    else if(ind) {
        ind -= 4;
        indent();
        printf("}\n");
    }
}

int main(int argc, char *argv[]) {
    Conf *conf = conf_load("test.cfg");
    
    if(!conf)
        return 0;
    
    printconf(conf);
    
    conf_free(conf);
    
    return 0;
}



conf.h:
#ifndef CONF_H
#define CONF_H

typedef struct Conf Conf;

Conf *conf_parse(char *text);
Conf *conf_load(const char *file);
void conf_free(Conf *conf);

const char *conf_name(Conf *conf);
char *conf_value(Conf *conf);
Conf *conf_next(Conf *conf);
Conf *conf_child(Conf *conf);

#endif



conf.c: Edit: I removed this since it blows up the post horizontally. Download library I hope someone will find this helpful.

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!