Configuration files for C on Linux

I have an executable where the runtime needs to accept configuration parameters from a script file. This way I don't have to recompile the code for every configuration change. Right now I have all the config values ​​in a .h file. Every time I change it, I need to recompile.

C platform, gcc under Linux.

What's the best solution for this problem? I have looked on google and so XML, phthon and Lua bindings for C. Is using a separate scripting language a better approach? If so, which one would you recommend for my need?

Appendix: What if I would like to mirror data structures in script files? If I have an array of structures, for example, if there is an easy way to save and load it?

thanks

+2


a source to share


7 replies


I recommend Lua . It was designed for configuration.



+5


a source


The easiest way is to have a text file containing something like:

key = value
key2 = anothervalue
....
keyn = etc

      



And then you just open that file and parse it, putting everything in something like a hashmap / dictionary.

A quick google search brings up a library ( libConfuse ) that can do this for you.

+2


a source


Check out CCAN ciniparser . Its a fork of the original iniparser (which is no longer supported) and makes it easier to parse INI config files.

Code from example (almost mirrored by unit tests):

#include <stdio.h>
#include <stdbool.h>
#include <ccan/ciniparser/ciniparser.h>

#define CONFIG_FILE "/etc/config.ini"

int main(int argc, char *argv[])
{
        dictionary *d;
        char *val1;
        bool val2;
        double val3;
        int val4;

        d = ciniparser_load(CONFIG_FILE);
        if (d == NULL)
            return 1;

        val1 = ciniparser_getstring(d, "daemon:pidfile", NULL);
        val2 = ciniparser_getboolean(d, "daemon:debug", false);
        val3 = ciniparser_getdouble(d, "daemon:maxload", 3.5);
        val4 = ciniparser_getint(d, "daemon:maxchild", 5);

        ciniparser_freedict(d);

        return 0;
}

      

Of course, you can just drop a few files that are needed in your tree, and #include "iniparser.h"

there are no dependencies on other CCAN modules unless you want to run unit tests.

An example configuration might look like this:

[stooges]
larry=larry_stooge
curly=curly_stooge
moe=moe_stooge
shemp=questionable

[cartoons]
tom_hates=jerry

      

Retrieving the value stooges:shemp

will result in statically allocated questionable

, which you would use as is (no change), or highlight and duplicate (i.e. strdup()

). It's not much easier. Wrap the dictionary access with a simple mutex and thread safe.

CCAN is a comprehensive C Archive network. Think CPAN

simple C

. The Rusty Russell project was recently created and is finally gaining ground.

Disclaimer: I maintain the module.

+2


a source


What happened to the text file? Are these configuration settings read once when the application is launched or continuously throughout the life of the application? When did they write? It looks like they only read once at the start of the application, because you have to recompile every time they change.

If there are only a few values, then I would say it is simple and stick to a text file formatted as key / value pairs:

Config1 = 1
ConfigValue2 = 33.4
ConfigValue3 = ABC

      

If you have more complex settings, perhaps some hierarchy of values, XML might make more sense.

+1


a source


How many settings do you need for it to be a "script file"?

I just keep a small chunk of code that's handy for parsing ini.

+1


a source


You can re-read the config file when you receive a signal such as SIGUSR1

.

0


a source


you can just store all config values ​​externally and read it from old plain C

you can do something like:

pairs = value param2 = value2

and read it usually with fgets and then do strtok looking for '=' and then you have both options and values.

if you want to use XML I recommend libxml2 for you .

0


a source







All Articles