[ccan] Configuration file parser?

Ryan Graham r.m.graham at gmail.com
Thu Jan 29 05:22:20 EST 2009


On Wed, Jan 28, 2009 at 1:09 AM, Tim Post <echo at echoreply.us> wrote:
> Hi,
>
> Does anyone have a small library (parser included) to parse the
> following types of configuration files:
>
> ** Example 1 **
>
> lockfile = /var/foo/shemp-is-technically-a-stooge.pid
> lock_mem = true
>
> ** Example 2 **
>
> daemon {
>    lockfile = (...)
>    lock_mem = true
> }
>
> ** Example 3 **
>
> daemon {
>    behavior {
>      threads = true;
>      max_threads = 100;
>    }
>
>    logging {
>      type = syslog;
>      loglevel = 3;
>    }
> }
>
> clients {
>    allow {
>       127.0.0.1 = rw
>       127.0.0.2 = ro
>    }
>    deny = all
> }
>
> I have been (for over a year) trying to think of a way to make something
> to parse the above sane and easy to use. I have always just gone with
> XML when I need something more complex than example #1.

The first example can be handled by a generic INI parser, which there
are many of (like http://ndevilla.free.fr/iniparser/).

The second and third remind me of the format used in dhcpd.conf and
some other daemon config files I can't recall at the moment. I'm sure
the parsing code from those daemons could serve as examples to anyone
writing this thing..

iniparser uses a dictionary API to let you retrieve values by name a
la "section:value", or ":lockfile" in your example (no section).
Perhaps that concept could be expanded for traversing the other
formats? Is that the kind of thing you were thinking of?

~Ryan

-- 
http://rmgraham.blogspot.com



More information about the ccan mailing list