27

Let's say I have a simple config file that my c program needs to read/parse.

Let's say it looks a little bit like this:

#Some comment
key1=data1
key2=data2

Is there a standard c lib that I can use instead of writing my own parser?

Thanks Johan


Note: Today I have my own little parser, but there must be some standard libs that solves this simple problem.

Johan
  • 20,067
  • 28
  • 92
  • 110
  • Similar question: http://stackoverflow.com/questions/1417765/parse-config-file-in-c-c . There is no standard C library that does this. Writing your own parser might not be a bad idea if your requirements are simple and unlikely to change. – Firas Assaad Feb 12 '10 at 09:05
  • Although this is not exactly what the OP is asking for but FWIW, sometimes a simple shell `sed`/`awk` one-liner invoked using `system()` may be very handy. – Zaxter Aug 30 '17 at 13:18

7 Answers7

14

libconfig but it does quite more than what you're asking

f4.
  • 3,814
  • 1
  • 23
  • 30
10

I find it worth mentioning libConfuse here, and quote its description:

libConfuse is a configuration file parser library, licensed under the terms of the ISC license, and written in C. It supports sections and (lists of) values (strings, integers, floats, booleans or other sections), as well as some other features (such as single/double-quoted strings, environment variable expansion, functions and nested include statements). It makes it very easy to add configuration file capability to a program using a simple API.

The goal of libConfuse is not to be the configuration file parser library with a gazillion of features. Instead, it aims to be easy to use and quick to integrate with your code. libConfuse was called libcfg before, but its name was changed to not confuse itself with other similar libraries.

It seems fairly similar to the already mentioned libconfig. There is a short comparison of C and C++ parsers in A study of the existing libraries to read from configuration files that might be a useful start for anyone choosing among the alternatives.

Community
  • 1
  • 1
sampi
  • 576
  • 5
  • 15
8

Why not just use GLIB?

Among countless other things it has library functions for parsing INI like configuration files:

https://developer.gnome.org/glib/stable/glib-Key-value-file-parser.html

Apart from that it also supports Datatypes (Lists, Hashtables, Strings, Caches), Threading, platform neutral abstractions, unit testing, error handling and lots of other great stuff.

For me it's the most useful C library and I would need to have a very good reason to write a C program without using this library.

ahe
  • 2,319
  • 1
  • 19
  • 22
6

If you need really small and simple INI parser (very good for embedded systems like routers), try INIH. It is only 2-3 pages of code.

Marki555
  • 6,434
  • 3
  • 37
  • 59
3

I have implemented a simple C library which parses configuration file:

https://github.com/taneryilmaz/libconfigini

See the tests directory for usage.

taneryilmaz
  • 442
  • 5
  • 10
1

http://code.jellycan.com/simpleini/

You can use simple INI files.

Vladislav Rastrusny
  • 29,378
  • 23
  • 95
  • 156
1

Here's one I've used with success:

http://ndevilla.free.fr/iniparser/

It's small and independent.

Tim Schaeffer
  • 2,616
  • 1
  • 16
  • 20