LIBBB: config_*()

Denys Vlasenko vda.linux at googlemail.com
Sun Jul 13 13:02:11 UTC 2008


Vladimir, please CC: busybox at busybox.net

On Sunday 13 July 2008 13:55, dronnikov at gmail.com wrote:
> Attached is a heavily rewritten set of helper functions for parsing config files.
> 
> The main function is prototyped as
> char* config_read(PARSER *parser, char comment, const char *delims, int ntokens, char **tokens);
> 
> As one can see, tunable are not only delimiters but also comment char. This should fit a lot of config files schemes.
> No comment stripping is performed when !comment.
> 
> Also tried to attack the problem of subsequent delimiters:
> whether they should be treated as one (case A) (likely when delimiters are whitespaces),
> or as separates (as, e.g., : in /etc/passwd) (case B).
> The sign of ntokens controls this: ntokens > 0 orders case A, ntokens < 0 -- case B.
> The special case of ntokens == 0 orders to just return the valuable lines from config file.
> 
> An example of usage is included in parse_config.c.
> 
> N.B. A simple format string can someday be added to perform tokens typecasting.
> That way we would be able to reduce calls to strto*()s outside config parsing procedure.

I have a performance objection. You read entire file in one go.
Config files often have big comments. Not to mention that
in many cases, keeping entire file (even sans comments)
is not needed, the file can be processed line-by-line.

Can you read file line-by-line, and throw away empty lines,
comments, excessive delimiters etc?

Naming your structure PARSER is strange. Is it mimicking FILE naming scheme?
How about

typedef struct parser_t {
	char *data;
	char *line;
        ...
} parser_t;

Regarding config_read: can you keep state and parser config in parser struct
so that config_read() does not need so many parameters? Then config_read()
can work similarly to xmalloc_fgets: take only one parameter (parser struct ptr)
and return result (malloced, NULL terminated char* vector?
pointer to pre-allocated, NULL padded char* vector[MAX] inside parser struct?
you decide).

char* FAST_FUNC config_read(PARSER *parser, char comment, const char *delims, int ntokens, char **tokens)
--
vda



More information about the busybox mailing list