Use an INI file in C on Linux
Asked Answered
A

7

26

Is there a standard way of reading a kind of configuration like INI files for Linux using C?

I am working on a Linux based handheld and writing code in C.

Otherwise, I shall like to know about any alternatives.

Final update:

I have explored and even used LibConfig. But the footprint is high and my usage is too simple. So, to reduce the footprint, I have rolled out my own implementation. The implementation is not too generic, in fact quite coupled as of now. The configuration file is parsed once at the time of starting the application and set to some global variables.

Ankylostomiasis answered 17/2, 2010 at 11:58 Comment(7)
Definitely +1 for asking for the way to go instead of doing roll-your-own #74632!Amon
@DevSolar, aw cmon, any chance to roll your own parser instead of doing more productive things is a good one! (tongue in cheek)Barquentine
If you want a list of options, you should edit your question to say so. Right now, the correct answer is 'no, there is no standard way' :)Strophic
Thanks a lot for the motivating pat.Ankylostomiasis
I have finally rolled one for myself.Ankylostomiasis
I have explored and even used LibConfig. But the footprint is high and my usage is too simple. So, to reduce the footprint, I have rolled out my own implementation. The implementation is not too generic, in fact quite coupled as of now. The config file is parsed once at the time of starting the application and set to some global variables.Ankylostomiasis
My two cents (a bit of documentation is available here).Stapleton
D
16

Try libconfig:

a simple library for processing structured configuration files, like this one: test.cfg. This file format is more compact and more readable than XML. And unlike XML, it is type-aware, so it is not necessary to do string parsing in application code.

Libconfig is very compact — a fraction of the size of the expat XML parser library. This makes it well-suited for memory-constrained systems like handheld devices.

The library includes bindings for both the C and C++ languages. It works on POSIX-compliant UNIX and UNIX-like systems (GNU/Linux, Mac OS X, Solaris, FreeBSD), Android, and Windows (2000, XP and later)...

Ditzel answered 17/2, 2010 at 12:5 Comment(2)
It seems libconfig is the right chice looking at its simplicity and small size. Thanks a lot.Ankylostomiasis
libconfig looks good, but it has a toxic GNU license for closed-source projects.Cloakroom
S
12

No, there isn't one standard way. I'm sorry, but that is probably the most precise answer :)

You could look at this list of Linux configuration file libraries, though. That might be helpful.

Strophic answered 17/2, 2010 at 12:0 Comment(4)
funny how the OP asks for a standard method, gets 7 different suggestions (all modded up) and one answer that says there is no standard gets modded down.Struble
Before the edit, the answer just said "no", without giving any hint as to available options. That's why it got -1 from me, because that's about as helpful as hitting F1 on Windows. Now, with the list of options, it's a good answer, and that's why I revert the vote.Amon
It dawned on me that maybe, even though the question asked for a standard way, the author really wanted some different options. But that's not really what he's asking, thus the initial 'no'.Strophic
@gnud: Though I asked for any standard way, the intent is to know the solution to a problem. I understood that the question should have been put in such a way to also get other answers. I am updating the question. Thanks a lot.Ankylostomiasis
B
11

Here are four options:

  1. Iniparser
  2. libini
  3. sdl-cfg
  4. RWini
Benzoyl answered 17/2, 2010 at 12:3 Comment(1)
Don't bother with #1 (Iniparser). That has to be the worst implementation I have seen yet.Pratique
C
7

If you can use the (excellent, in any C-based application) glib, it has a key-value file parser that is suitable for .ini-style files. Of course, you'd also get access to the various (very nice) data structures in glib, "for free".

Crassulaceous answered 17/2, 2010 at 12:5 Comment(0)
B
4

There is an updated fork of iniparser at ccan, the original author has not been able to give it much attention over the years. Disclaimer - I maintain it.

Additionally, iniparser contains a dictionary that is very useful on its own.

Barquentine answered 17/2, 2010 at 12:6 Comment(0)
S
4

If you need a fast and small code just for reading config files I suggest the inih

It loads the config file content just once, parse the content and calls a callback function for each key/value pair.

Really small. It can be used on embedded systems too.

Stifle answered 7/12, 2015 at 3:14 Comment(0)
S
2

I hate to suggest something entirely different in suggesting XML, but libexpat is pretty minimal, but does XML.

I came to this conclusion as I had the same question as you did, but then I realized the project already had libexpat linked-in--and I should probably just use that.

Spasmodic answered 2/12, 2011 at 18:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.