view ini.h @ 93:9ebea85c7765

Use pointer instead of copying variable in ARRAY_FOREACH, then you can modify it
author David Demelier <markand@malikania.fr>
date Wed, 04 Jan 2012 13:21:15 +0100
parents 145493469aa0
children dcaf2c61c902
line wrap: on
line source

/*
 * ini.h -- parse .ini like files
 *
 * Copyright (c) 2011, David Demelier <markand@malikania.fr>
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#ifndef _INI_H_
#define _INI_H_

#define	INI_VERBOSE	(1 << 0)	/* be verbose */
#define	INI_NOREDEFINE	(1 << 1)	/* do not allow redefinitions */
#define INI_FAILERROR	(1 << 2)	/* abort parsing on first error */

struct ini_config;
struct ini_section;

struct ini_handler {
	const char	*section;	/* section to query */
	const char	*option;	/* option to check */
	void		*dst;		/* where to store */

	/* Conversion function */
	void (*handler)(void *, const char *, void *);
	void		*userdata;	/* optional user data */
};

/* --------------------------------------------------------
 * struct ini_config functions.
 * -------------------------------------------------------- */

struct ini_config *
ini_load(const char *, int);

char **
ini_get_sections_names(const struct ini_config *, int *);

struct ini_section **
ini_get_sections(const struct ini_config *, const char *, int *);

struct ini_section *
ini_select_section(const struct ini_config *, const char *);

char *
ini_get_option_once(const struct ini_config *, const char *, const char *);

void
ini_free(struct ini_config *, int, int);

/* --------------------------------------------------------
 * struct ini_section functions.
 * -------------------------------------------------------- */

char **
ini_get_option_names(const struct ini_section *, int *);

char *
ini_get_option(const struct ini_section *, const char *key);

/* --------------------------------------------------------
 * convenient api to query and convert data
 * -------------------------------------------------------- */

char *
ini_get_error(void);

/*
 * For the config, read all available value and store them in
 * the array ini_handler.
 */

void
ini_value_dispatch(struct ini_config *, struct ini_handler *, int);

/*
 * Convert to bool. dst must be (char *).
 * It converts "yes" "true" "1" or opposites. Only lower
 * case is supported right now.
 */

void
ini_convert_bool(void *, const char *, void *);

/*
 * Convert to a int. dst must be (int *).
 */

void
ini_convert_int(void *, const char *, void *);

/*
 * Convert to a short. dst must be (short *).
 */

void
ini_convert_short(void *, const char *, void *);

/*
 * Convert to a char *. dst must be (char **). This
 * function use strdup() you need to free the dst
 * pointer.
 */

void
ini_convert_string(void *, const char *, void *);

#endif /* _INI_H_ */