[Debburn-devel] Rewritten config parser

Albert Cahalan acahalan at gmail.com
Sat Sep 9 21:58:21 UTC 2006


On 9/9/06, Eduard Bloch <edi at gmx.de> wrote:

> As said on IRC, I have rewritten the config file parser of cdrkit to
> make it more robust, more flexible and more user-friendly. You will
> find the suggested change attaced, as files and as svn diff. Please
> proofread and tell me if you find anythign suspicious, I hope that I
> documented the code well enough.

OK. You asked for it. :-)

> See attachement. I intend to commit it to the stable trunk RSN.

Grrrr. I'm not liking being excluded. Anyway...

   rewind(srcfile);

Usually it is not nice to rewind files and keep reparsing them.

   while(fgets(linebuf, 256, srcfile)) {

Most occurances of "256" should be "sizeof linebuf".

Some ideas that may help you:

The states do not change arbitrarily. They go up, but
sometimes get reset to zero. Consider a function to
parse the line, falling naturally from one state to the
next, with "return" being used for the next_line behavior.

Unless you want to get fancy with quoted strings and
escapes, changing the first '#' to a '\0' would be good.
Users tend to place comments on lines with data.

Your sscanf has a buffer overflow. You forgot '\0'.
(never trust your memory; always read the man page)

Use strtol instead of atoi if you want error checking.

In general I think it is nicer to fill in a struct with the
content of a config file, fill in a struct with info about
a device, etc. It is nicer to do this in stages, so that
looking at environment variables is not mixed in with
looking at config files or looking at command options.



More information about the Debburn-devel mailing list