view ini.h @ 164:654f32079cdc

Use enum
author David Demelier <markand@malikania.fr>
date Wed, 05 Sep 2012 07:44:07 +0200
parents 2563b3e71859
children
line wrap: on
line source

/*
 * ini.h -- parse .ini like files
 *
 * Copyright (c) 2011, 2012, 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_

#ifdef __cplusplus
extern "C" {
#endif

#if !defined(INI_DEFAULT_LINESIZE)
#  define INI_DEFAULT_LINESIZE		1024
#endif

/*
 * sys/queue.h bits.
 */

#if !defined(TAILQ_ENTRY)
#define	TAILQ_ENTRY(type)						\
struct {								\
	struct type *tqe_next;	/* next element */			\
	struct type **tqe_prev;	/* address of previous next element */	\
}
#endif

#if !defined(TAILQ_HEAD)
#define	TAILQ_HEAD(name, type)						\
struct name {								\
	struct type *tqh_first;	/* first element */			\
	struct type **tqh_last;	/* addr of last next element */		\
}
#endif

enum ini_flags {
	INI_VERBOSE		= (1 << 0),	/* be verbose */
	INI_NOREDEFINE		= (1 << 1),	/* do not allow redefinitions */
	INI_FAILERROR		= (1 << 2)	/* abort parsing on first error */
};

typedef struct ini_config	ini_config_t;
typedef struct ini_section	ini_section_t;
typedef struct ini_option	ini_option_t;
typedef struct ini_handler	ini_handler_t;

typedef void (*ini_open_t)(void *, const char *);
typedef void (*ini_get_t)(void *, const char *, const ini_option_t *);

/* --------------------------------------------------------
 * Structure definitions
 * -------------------------------------------------------- */

struct ini_private;

struct ini_config {
	const char		*path;		/* (ro) file path */
	enum ini_flags		flags;		/* (ro) optional flags */
	void			*data;		/* (rw) user data */

	TAILQ_HEAD(, ini_section) sections;	/* (ro) linked-list of sections */

	/* Event driven method */
	void (*open)(void *, const char *);
	void (*get)(void *, const char *, const struct ini_option *);

	/* Private data */
	struct ini_private	*pv;
};

struct ini_option {
	char			*key;		/* (rw) option name */
	char			*value;		/* (rw) option value */

	TAILQ_ENTRY(ini_option)	link;
};

struct ini_section {
	char			*key;		/* (rw) section key */	
	TAILQ_HEAD(, ini_option) options;	/* (rw) list of options */

	TAILQ_ENTRY(ini_section) link;
};

/* --------------------------------------------------------
 * Main functions
 * -------------------------------------------------------- */

struct ini_config *
ini_create(const char *, enum ini_flags);

void
ini_set_handlers(struct ini_config *, void *, ini_open_t, ini_get_t);

int
ini_open(struct ini_config *);

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

struct ini_option *
ini_select_option(const struct ini_section *, const char *);

struct ini_option *
ini_find(const struct ini_config *, const char *, const char *);

const char *
ini_get_error(const struct ini_config *);

void
ini_free(struct ini_config *, int);

#ifdef __cplusplus
}
#endif

#endif /* _INI_H_ */