reading a configuration file 
Author Message
 reading a configuration file

I am somewhat new to C.  At least in extensive experience.  I need help
to to create a short snippet that will read values from a configuration
file where each line is in the form of "variable=value".  Spaces being
allowed before and after the "=" symbol.  Also there are blank lines and
comments lines ("#").  This will run on a unix system (Solaris 2.6) from
a cron job.  I currently use a similar method in my perl programs and
would like to use the same config file with this program.  Also any
suggestions on how to process and overide the file based configurations
via the command line would be a bonus.  :-)

I know, I know.  I should just figure it out myself.  With the luxury of
time I could.   I just need a jump start.  I need to rewrite a short
production program running on unix.  This is just a small part of the
rewrite.  I generally plagiarize as much code as I can.  :-)  After
getting familar with a language I can usually figure it out myself
fairly quickly.

Thanks
Mike Winterer

"Sleep is a wholly inadequate substitute for coffee."
--



Thu, 19 Sep 2002 04:00:00 GMT  
 reading a configuration file
Mike Winterer a crit dans le message ...

Quote:
>I am somewhat new to C.  At least in extensive experience.  I need help
>to to create a short snippet that will read values from a configuration
>file where each line is in the form of "variable=value".  Spaces being
>allowed before and after the "=" symbol.  Also there are blank lines and
>comments lines ("#").  This will run on a unix system (Solaris 2.6) from

Its not difficult.

open the file with fopen() from <stdio.h>
read the lines with fgets() from <stdio.h>
parse the lines with strchr() or strstr() from <string.h>
convert the values in binary with strtol() or strtod() from <stdlib.h>
setup your variables.

when the end of the file is reached, close it with fclose() from <stdio.h>.

Quote:
>a cron job.  I currently use a similar method in my perl programs and
>would like to use the same config file with this program.  Also any
>suggestions on how to process and overide the file based configurations
>via the command line would be a bonus.  :-)

Why don't you want to learn how to programm in C ?

Quote:
>I know, I know.  I should just figure it out myself.  With the luxury of

Yes.

Quote:
>time I could.   I just need a jump start.  I need to rewrite a short
>production program running on unix.  This is just a small part of the
>rewrite.  I generally plagiarize as much code as I can.  :-)  After

So do I, but when I need a tool, I make it.

Quote:
>getting familar with a language I can usually figure it out myself
>fairly quickly.

It is now time to fly with your own wings... We can help you for that. Just
make the first step.

--
-hs- "Stove"
CLC-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
ISO-C Library: http://www.dinkum.com/htm_cl
"It's specified. But anyone who writes code like that should be
transmogrified into earthworms and fed to ducks." -- Chris Dollin CLC
--



Sat, 21 Sep 2002 03:00:00 GMT  
 reading a configuration file


[snip]

Quote:
>to to create a short snippet that will read values from a configuration
>file where each line is in the form of "variable=value".  Spaces being
>allowed before and after the "=" symbol.  Also there are blank lines and
>comments lines ("#").  This will run on a unix system (Solaris 2.6) from
>a cron job.  I currently use a similar method in my perl programs and
>would like to use the same config file with this program.  Also any
>suggestions on how to process and overide the file based configurations
>via the command line would be a bonus.  :-)

I did the following when I had a similar need.  Sorry for the
application-specific code and error messages here.

You can add new keyword handlers by following the example of conf_dev()
and editing the fns[] array in read_config_file.  And of course
passing the filename in as a char * instead of using an environment
variable might be more appropriate for most applications.

#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* first some helper functions */
void
die(const char *fmt, ...)
{
    va_list a;
    va_start(a, fmt);
    vfprintf(stderr, fmt, a);
    va_end(a);
    exit(1);

Quote:
}

static char *device;

static int conf_dev(char *p, const char *fname, int lineno)
{
    device = dupstr(p);
    return 0;

Quote:
}

static int read_config_file(void)
{
    char *conffile;
    FILE *f;
    int lineno, retval = 0;
    struct {
        char *token;
        int (*f)(char *, const char *, int);
    } fns[] = {
        { "device", conf_dev },
        { 0, 0 },
    };

    conffile = getenv("VIASH_CONF");
    if(!conffile)
        conffile = "viash.conf";

    f = fopen(conffile, "r");
    if(!f)
        die("viashmem: unable to open %s\n", conffile);

    for(lineno=1; !feof(f); lineno++) {
        /* XXX need smarter config file parser here... */
        int i;
        char *token, *value;
#define BUFLEN 1024
        char buf[BUFLEN];

        fgets(buf, BUFLEN, f);
        if(feof(f))
            break;
        token = strchr(buf, '\n');
        if(token)
            *token = '\0';
        token = buf + strspn(buf, " \t");
        if(*token == '\0' || *token == '#' || *token == '\n')
            continue;
        value = strchr(token, '=');
        if(!value)
            die("%s:%d: syntax error\n", conffile, lineno);
        *value = '\0';
        value++;
#if 0
        printf("%d: '%s' '%s'\n", lineno, token, value);
#endif

        for(i = 0; fns[i].token; i++) {
            if(!strcmp(fns[i].token, token)) {
                retval = fns[i].f(value, conffile, lineno);
                break;
            }
        }
        if(!fns[i].token)
            die("%s:%d: unrecognized keyword '%s'\n", conffile, lineno, token);
        if(retval)
            break;
    }
    fclose(f);
    return retval;

Quote:
}

-andy
--



Sat, 21 Sep 2002 03:00:00 GMT  
 reading a configuration file
The best suggestion that I have is to read the file or a line at a time
into a string and use the strtok function and strcpy to parse the string
and store the values in variables. You can get information on the strtok
and strcpy function from the man pages. The strtok function takes a string
and delimiters and returns a pointer to the first character that is not in
the delimiter list. The strtok function is not the best method for some
applications, without knowing the whole story it is the best recommendation
that I can lend. If you need more information then email me and I will send
you a function that I wrote to parse strings using the strtok function.

John

Quote:

> I am somewhat new to C.  At least in extensive experience.  I need help
> to to create a short snippet that will read values from a configuration
> file where each line is in the form of "variable=value".  Spaces being
> allowed before and after the "=" symbol.  Also there are blank lines and
> comments lines ("#").  This will run on a unix system (Solaris 2.6) from
> a cron job.  I currently use a similar method in my perl programs and
> would like to use the same config file with this program.  Also any
> suggestions on how to process and overide the file based configurations
> via the command line would be a bonus.  :-)

> I know, I know.  I should just figure it out myself.  With the luxury of
> time I could.   I just need a jump start.  I need to rewrite a short
> production program running on unix.  This is just a small part of the
> rewrite.  I generally plagiarize as much code as I can.  :-)  After
> getting familar with a language I can usually figure it out myself
> fairly quickly.

> Thanks
> Mike Winterer

> "Sleep is a wholly inadequate substitute for coffee."
> --


--



Sat, 21 Sep 2002 03:00:00 GMT  
 reading a configuration file

Quote:

> I am somewhat new to C.  At least in extensive experience.  I need help
> to to create a short snippet that will read values from a configuration
> file where each line is in the form of "variable=value".  Spaces being
> allowed before and after the "=" symbol.  Also there are blank lines and
> comments lines ("#").  This will run on a unix system (Solaris 2.6) from
> a cron job.  I currently use a similar method in my perl programs and
> would like to use the same config file with this program.

Especially if the set of variable names is fixed then for speed and
ease of programming I'd use an automated method of generating C source
for a lexical analyser such as lex or flex. It should be available on
your system. Try 'man lex' or 'man flex'.

Also usable if the variable names are not fixed, but might then be
considered overkill.

Malcolm Kay
--



Sat, 21 Sep 2002 03:00:00 GMT  
 reading a configuration file

Quote:
>I am somewhat new to C.  At least in extensive experience.  I need
help
>to to create a short snippet that will read values from a
configuration
>file where each line is in the form of "variable=value".  Spaces
being
>allowed before and after the "=" symbol.  Also there are blank lines
and
>comments lines ("#").  This will run on a unix system (Solaris 2.6)
from
>a cron job.  I currently use a similar method in my perl programs
and
>would like to use the same config file with this program.  Also any
>suggestions on how to process and overide the file based
configurations
>via the command line would be a bonus.  :-)

Have a look at http://www.*-*-*.com/ ~Eulenspiegel - You'll find
a C section with sources for a general string library. There are
defined some functions for splitting and parsing strings, t{*filter*}
...

--
hope this helps
G.Pohl


Web:   www.online-club.de/~Eulenspiegel/english
       www.online-club.de/~Eulenspiegel/german
       Download freeware for DOS, C libraries
--



Sat, 21 Sep 2002 03:00:00 GMT  
 reading a configuration file
Look into using strtok().  After opening and reading a line from the config
file, examine the first char read to be
sure it isn't a '#' (or a blank line) after that, it should be trivial to
create the main parsing logic by using strtok().

Eric Petroelje


Quote:
> I am somewhat new to C.  At least in extensive experience.  I need help
> to to create a short snippet that will read values from a configuration
> file where each line is in the form of "variable=value".  Spaces being
> allowed before and after the "=" symbol.  Also there are blank lines and
> comments lines ("#").  This will run on a unix system (Solaris 2.6) from
> a cron job.  I currently use a similar method in my perl programs and
> would like to use the same config file with this program.  Also any
> suggestions on how to process and overide the file based configurations
> via the command line would be a bonus.  :-)

> I know, I know.  I should just figure it out myself.  With the luxury of
> time I could.   I just need a jump start.  I need to rewrite a short
> production program running on unix.  This is just a small part of the
> rewrite.  I generally plagiarize as much code as I can.  :-)  After
> getting familar with a language I can usually figure it out myself
> fairly quickly.

> Thanks
> Mike Winterer

> "Sleep is a wholly inadequate substitute for coffee."
> --


--



Sat, 21 Sep 2002 03:00:00 GMT  
 reading a configuration file


Quote:
> I am somewhat new to C.  At least in extensive experience.  I need help
> to to create a short snippet that will read values from a configuration
> file where each line is in the form of "variable=value".  Spaces being
> allowed before and after the "=" symbol.  Also there are blank lines and
> comments lines ("#").  This will run on a unix system (Solaris 2.6) from

See http://www.snippets.org   files cfg.* You'll need to replace a ';' by
'#'  somewhere near line 44 in cfg.c I think.

--
Greetings from
 _____
 /_|__| Auke Reitsma, Delft, The Netherlands.
/  | \  -------------------------------------
        Remove SPAMBLOCK from my address ...
--



Sat, 21 Sep 2002 03:00:00 GMT  
 reading a configuration file

Quote:

> I am somewhat new to C.  At least in extensive experience.  I need help
> to to create a short snippet that will read values from a configuration
> file where each line is in the form of "variable=value".  Spaces being
> allowed before and after the "=" symbol.  Also there are blank lines and
> comments lines ("#").  This will run on a unix system (Solaris 2.6) from
> a cron job.  I currently use a similar method in my perl programs and
> would like to use the same config file with this program.  Also any
> suggestions on how to process and overide the file based configurations
> via the command line would be a bonus.  :-)

> I know, I know.  I should just figure it out myself.  With the luxury of
> time I could.   I just need a jump start.  I need to rewrite a short
> production program running on unix.  This is just a small part of the
> rewrite.  I generally plagiarize as much code as I can.  :-)  After
> getting familar with a language I can usually figure it out myself
> fairly quickly.

> Thanks Mike Winterer

> "Sleep is a wholly inadequate substitute for coffee."

Look at freshmeat http://freshmeat.net for libconfig

regards
--



Mon, 23 Sep 2002 03:00:00 GMT  
 reading a configuration file

Quote:
> I am somewhat new to C.  At least in extensive experience.  I need help
> to to create a short snippet that will read values from a configuration
> file where each line is in the form of "variable=value".

You might want to take a look at:

http://www.snippets.org/#section1group3

--
    Later,
    Jerry.

The universe is a figment of its own imagination.
--



Mon, 23 Sep 2002 03:00:00 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. fgets reading a configuration file: compiles OK but output wrong

2. fgets reading a configuration file:

3. reading in configuration files

4. Reading a configuration file

5. reading a configuration file

6. how to: XML file as application configuration file

7. Configuration.AppSetting is not read for WinForms Application

8. Configuration Manager loses configurations

9. Unhandled Exception: System.Configuration.ConfigurationException: Could not create System Configuration.NameValueSectionHandler, System

10. saving configuration files

11. Project Configurations are out of date -- another solution for Libraries/LIB files

12. Configuration files for DLL`s when app is unmanaged .exe

 

 
Powered by phpBB® Forum Software