LIBBB: config_*()

Tito farmatito at tiscali.it
Sun Jul 13 13:05:48 UTC 2008


On Sunday 13 July 2008 13:55:00 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.
> 
> Please, try, comment and consider applying.
> 
> TIA,
> --
> Vladimir
> 
> 

Hi,
just for the record, for the fun and in the hope that my work could be somehow useful
to somebody in the future, attached you will find my  alternative implementation
of parse config routines.  :-)
There is no patch at this time as it seems to me that Vladimir's work is more likely to be merged.
The functions are tested and seem to do their work as expected.

Ciao,
TIto

/*
 *  Returns a linked list of all retrieved elements.
 *  Comment lines starting with delim are ignored.
 *  End-of-line comments are supported.
 *  Blank lines are ignored.
 *  Lines may be indented freely.
 *  A "\" character at the very end of the line indicates the next line
 *  should be treated as a continuation of the current one.
 *  Config file is closed after use.
 */

llist_t *parse_config(const char *filename, int delim)

/*
 * Retrieves the VALUE of the configuration option named OPTION from
 * the config file data linked list as generated by parse_config()
 * The option must be in the format:
 * OPTION=VALUE or OPTION = VALUE
 * The separator character must be specified by the caller.
 * The returned value is malloced and must be freed by the caller.
 * If the option is found the linked list element is removed from
 * the list and the data are freed.
 */

char * parse_named_option(const char *option_name, char separator, llist_t **option_list)

/* Divides a malloced line of a config file as generated by parse_config() 
 * in max_fields number of tokens. Missing tokens are set to '\0'.
 * The returned tokens are malloced and must be freed by the caller.
 * The supplied data line is freed at the end.
 */

void parse_option_lines(char *data, int max_fields, ...)

A simple usage example is:

int main(int argc, char **argv)
{
	llist_t *config_options = NULL;
	char *shell = NULL;
	char *path  = NULL;
	char *option_1, *option_2, option_3;

	config_options = parse_config("path_to_config_file", '#');

	if (config_options) {
		shell = parse_named_option("SHELL", '=', &config_options);
		path = parse_named_option("PATH", '=', &config_options);
		printf("SHELL='%s'\nPATH: %S", shell, path);
		free(shell);
		free(path);
		// This example implies a config file with just one line to parse here
		// else you need to put list_pop in a loop.
		parse_option_lines(llist_pop(&config_options), 3,	&option_1, &option_2, &option_3);
		printf("option_1: %s\noption_2: %s\noption_3: %s\n", option_1, option_2, option_3);
		// If the app runs in one-shot mode and config file is read just one time
		// the memory will by freed at program termination so this is not needed.
		// If the app does periodically parsing of the config file it is best to free
		// the used memory to avoid memory leaks.
		free(option_1);
		free(option_2);
		free(option_3);
		// Free any left elements of the list
		llist_free(config_options, free);
		free(config_options);
	}
	return 0;
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: parse_config.c
Type: text/x-csrc
Size: 4868 bytes
Desc: not available
Url : http://lists.busybox.net/pipermail/busybox/attachments/20080713/437eefb9/attachment-0002.c 


More information about the busybox mailing list