view tests/ini_test.c @ 144:594ca7f7139b

Remove useless #endif
author David Demelier <markand@malikania.fr>
date Fri, 11 May 2012 00:12:08 +0200
parents a70010512a1f
children
line wrap: on
line source

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <err.h>

#include "ini.h"

#define LENGTH(x)	(sizeof (x)/ sizeof (x[0]))

/*
 * Use this test file with the ini_test.conf like this :
 *
 * #
 * # config file ini_test.conf
 * #
 * [general]
 * nickname	= "markand"
 * email	= "markand@malikania.fr"
 *
 * [ui]
 * verbose	= true
 * editor	= vim
 *
 * # multiple definition
 * [host]
 * alias	= "freenode"
 * host		= "irc.freenode.net"
 * port		= 6667
 *
 * [host]
 * alias	= "malikania"
 * host		= "irc.malikania.fr"
 * port		= 6667
 */

static void
list_servers(const INI_Config *config)
{
	/* Server parameters */
	char *alias = NULL, *host = NULL;
	int port = 0;

	/* Extract server information */
	INI_Handler settings[] = {
		/* option,	destination,	conversion function	*/
		{ "alias",	&alias,		&ini_convert_string	},
		{ "host",	&host,		&ini_convert_string	},
		{ "port",	&port,		&ini_convert_int	}
	};

	INI_Section *sc;

	/*
	 * All sections are added as linked list, so you can just
	 * seek all and keep only the ones you want.
	 */
	for (sc = config->sections; sc != NULL; sc = sc->next) {
		/* Keep only [host] */
		if (strcmp(sc->key, "host") != 0)
			continue;

		ini_dispatch(sc, settings, LENGTH(settings));
		printf("-- new server\n");
		printf("-> alias = %s\n", (alias != NULL) ? alias : "<null>");
		printf("-> host = %s\n", (host != NULL) ? host : "<null>");
		printf("-> port = %d\n", port);

		free(alias); alias = NULL;
		free(host); host = NULL;
	}
}

static void
list_options(const INI_Config *config)
{
	char *nickname = NULL, *email = NULL, *editor = NULL;
	bool verbose = 0;

	/* Extract [general] to their values */
	INI_Handler general[] = {
		/* option,	destination,	conversion function	*/
		{ "nickname",	&nickname,	&ini_convert_string	},
		{ "email",	&email,		&ini_convert_string	}
	};

	/* Extract [ui] to their values */
	INI_Handler ui[] = {
		/* option,	destination,	conversion function	*/
		{ "verbose",	&verbose,	&ini_convert_bool	},
		{ "editor",	&editor,	&ini_convert_string	}
	};

	INI_Section *sc;

	/* Always check if the section is present before dispatching */
	if ((sc = ini_select_section(config, "general")) != NULL)
		ini_dispatch(sc, general, LENGTH(general));
	if ((sc = ini_select_section(config, "ui")) != NULL)
		ini_dispatch(sc, ui, LENGTH(ui));

	printf("-- general\n");
	printf("-> nickname = %s\n", (nickname != NULL) ? nickname : "<null>");
	printf("-> email = %s\n", (email != NULL) ? email : "<null>");
	printf("-- ui\n");
	printf("-> verbose = %s\n", (verbose) ? "true" : "false");
	printf("-> editor = %s\n", (editor != NULL) ? editor : "<null>");

	free(nickname); free(email); free(editor);
}

static void
list_raw(const INI_Config *config)
{
	INI_Section *sc;
	INI_Option *opt;

	printf("-- dump of config\n");
	for (sc = config->sections; sc != NULL; sc = sc->next)
		for (opt = sc->options; opt != NULL; opt = opt->next)
			printf("%s.%s = %s\n", sc->key, opt->key,
			    (opt->value == NULL) ? "<null>" : opt->value);
}

int
main(void)
{
	INI_Config *config;

	/*
	 * 1. Try to load the file, it may fails (not found, permissions,
	 * memory, ...) so check everytime.
	 *
	 * INI_VERBOSE flag is great to tell the user about typo in file
	 */
	if ((config = ini_load("ini_test.conf", INI_VERBOSE)) == NULL)
		errx(1, "%s", ini_get_error());

	/* 2. List all servers found as [host] */
	list_servers(config);

	/* 3. List options from [general] and [ui] */
	list_options(config);

	/* 4. List all options from all sections */
	list_raw(config);

	/* Finally free everything */
	ini_free(config, 1, 1);

	return 0;
}