Design opinions/urls/snippets requested; parsing simplistic config files

Discussion in 'C Programming' started by John Doe, Jul 8, 2003.

  1. John Doe

    John Doe Guest

    I've been doing some reading/research on parsing simple configuration
    files through C, and have heard various opinions on the matter. I'd like
    to solicit some opinions and design criteria (as well as "gotchas") for
    doing this.

    I'm implementing a program that needs to read a standard configuration
    file, in key=value pairs for starters (though I'm open to other ideas).
    Basically it would be no more than about 20 lines total, one key per line.
    Something like the following:

    foo = bar
    port = /dev/ttyS0
    user = John Q. Public
    backup = $HOME/backup

    I've also seen suggestions about breaking this up into the "Windows'y"
    ..ini file syntax, so that could be reconstructed to be something like:

    [global]
    foo = bar

    [ports]
    port = /dev/ttyS0

    [user]
    user = John Q. Public

    [path]
    backup = $HOME/backup

    Either method of storage is fine, though it gets a bit harder parsing out
    global blocks. The other thing that could get complicated, is the presence
    of /etc/foo/foorc and $HOME/.foorc. Which overrides which? What about
    merging settings?

    I've looked at the following links, but they're all MUCH larger than I
    need for this project:

    Coman - LGPL - http://www.digitaltunnel.com/~lepware/coman/ChangeLog
    GConf - LGPL - http://cvs.gnome.org/lxr/source/gconf/ChangeLog
    Conf - GPL - http://www.cs.vu.nl/~bernsti/conf/index.html
    dot.conf - GPL - http://www.azzit.de/dotconf/
    Keeper - LGPL - http://www.inf.bme.hu/~mszeredi/keeper/
    libconf - GPL - http://metalab.unc.edu/pub/Linux/libs/
    libproplist - LGPL - http://www.windowmaker.org/src/lPL/ChangeLog
    parsecfg - LGPL - http://www.enjoy.ne.jp/~gm/program/parsecfg/

    Another option that was thrown my way, was to use lexx/yacc to create the
    parser and logic for this. I'm completely unfamiliar with lexx/yacc for
    this, so I don't know where to start. An hour of googling didn't produce
    much of anything I could use for an jumping point to get me started.

    Lastly, the old standby.. XML. In this case, XML would be a good storage
    method for my config variables, but my project would then require an
    additional dependancy on libxml or expat (which isn't a validating
    parser).

    Ideas? Code snippets? URLs I can check for insight?

    Thanks in advance.
     
    John Doe, Jul 8, 2003
    #1
    1. Advertising

  2. Re: Design opinions/urls/snippets requested; parsing simplistic configfiles

    John Doe wrote:
    > I've been doing some reading/research on parsing simple configuration
    > files through C, and have heard various opinions on the matter. I'd like
    > to solicit some opinions and design criteria (as well as "gotchas") for
    > doing this.
    >
    > I'm implementing a program that needs to read a standard configuration
    > file, in key=value pairs for starters (though I'm open to other ideas).
    > Basically it would be no more than about 20 lines total, one key per line.
    > Something like the following:
    >
    > foo = bar
    > port = /dev/ttyS0
    > user = John Q. Public
    > backup = $HOME/backup
    >
    > I've also seen suggestions about breaking this up into the "Windows'y"
    > ..ini file syntax, so that could be reconstructed to be something like:
    >
    > [global]
    > foo = bar
    >
    > [ports]
    > port = /dev/ttyS0
    >
    > [user]
    > user = John Q. Public
    >
    > [path]
    > backup = $HOME/backup
    >
    > Either method of storage is fine, though it gets a bit harder parsing out
    > global blocks. The other thing that could get complicated, is the presence
    > of /etc/foo/foorc and $HOME/.foorc. Which overrides which? What about
    > merging settings?
    >

    Looks like your files are small/simple enough that you want to do the
    reading/writing module yourself.

    As far as merging/overriding, you might want to take a look at Java's
    old java.util.Properties, which presents a (IMHO) good concept.

    My $0.02...

    --
    Bertrand Mollinier Toublet
    "Reality exists" - Richard Heathfield, 1 July 2003
     
    Bertrand Mollinier Toublet, Jul 8, 2003
    #2
    1. Advertising

  3. John Doe

    John Doe Guest

    On Tue, 08 Jul 2003 07:53:41 -0700, Bertrand Mollinier Toublet wrote:

    > Looks like your files are small/simple enough that you want to do the
    > reading/writing module yourself.


    This is what I've come up with so far, but I've run into a snag.. it
    doesn't like a value with spaces in it, like:

    name = John Q. Public

    Which only grabs "John" from the RHS. Ideas? Code below:

    #include <stdio.h>
    #include <stdlib.h>
    #include <limits.h>

    #define CONFIG_FILE ".foorc"

    char cfg[PATH_MAX];

    int main(void)
    {
    char buff[256],
    key[100],
    val[100];

    int line,
    status = EXIT_FAILURE;

    FILE *fp;

    sprintf(cfg, "%s/%s", getenv("HOME"), CONFIG_FILE);
    if ((fp = fopen(cfg, "r")) == NULL) {
    printf("Cannot open file %s\n", CONFIG_FILE);
    exit(status);
    }

    for (line = 1; fgets(buff, sizeof(buff), fp); line++) {
    if (sscanf(buff, "%s = %s", &key, &val) != 2) {
    printf("Scanf() failure on line %d\n", line);
    (void) fclose(fp);
    exit(status);
    }
    printf("Line %d:\t %s = %s\n", line, key, val);
    }
    if (feof(fp))
    status = EXIT_SUCCESS;
    else
    printf("Error reading file %s, line %d\n", CONFIG_FILE,
    line);
    (void) fclose(fp);

    return status;
    }
     
    John Doe, Jul 8, 2003
    #3
  4. John Doe

    Sandeep Guest

    For simple cases, using environment variables might be a better solution.
    You can just use the standard c function getenv(). This way you
    don't have to write any code to parse the configuration parameters.

    Sandeep
    --
    http://www.EventHelix.com
    EventStudio - Generate Sequence Diagrams and Use Cases in PDF
     
    Sandeep, Jul 9, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. (Pete Cresswell)

    Frames: Simplistic Summary For The Noob?

    (Pete Cresswell), Oct 28, 2004, in forum: HTML
    Replies:
    20
    Views:
    811
    Dave Patton
    Oct 29, 2004
  2. gonebye

    CSS Opinions Requested

    gonebye, Jan 25, 2005, in forum: HTML
    Replies:
    4
    Views:
    415
    Duende
    Jan 26, 2005
  3. Tim H

    design opinions requested

    Tim H, Jun 3, 2007, in forum: C++
    Replies:
    15
    Views:
    559
    Daniel T.
    Jun 4, 2007
  4. Smithers

    Simplistic URL Rewriting

    Smithers, Nov 30, 2007, in forum: ASP .Net
    Replies:
    3
    Views:
    330
    Alexey Smirnov
    Nov 30, 2007
  5. BH
    Replies:
    8
    Views:
    121
    John W. Kennedy
    Feb 29, 2008
Loading...

Share This Page