File:  [ELWIX - Embedded LightWeight unIX -] / libaitcfg / src / aitcfg.c
Revision 1.1.1.1.2.3: download - view: text, annotated - select for diffs - revision graph
Mon May 25 07:43:51 2009 UTC (14 years, 11 months ago) by misho
Branches: cfg3_0
Diff to: branchpoint 1.1.1.1: preferred, colored
fix big bug in unloadconfig

/*************************************************************************
* (C) 2008 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
*  by Michael Pounov <misho@openbsd-bg.org>
*
* $Author: misho $
* $Id: aitcfg.c,v 1.1.1.1.2.3 2009/05/25 07:43:51 misho Exp $
*
*************************************************************************/
#include "global.h"
#include "aitcfg.h"


#pragma GCC visibility push(hidden)

int cfgErrno;
char cfgError[MAX_STR + 1];

#pragma GCC visibility pop


/*
 * InitConfig() Head initializing function for config
 * @cfg = New head element for init
 * return: 0 ok; -1 error:: new head element is null
*/
inline int InitConfig(sl_config * __restrict cfg)
{
	if (!cfg)
		return -1;

	cfg->slh_first = NULL;
	return 0;
}

/*
 * LoadConfig() Load config from file
 * @csConfigName = Filename of config
 * @cfg = Head list element
 * return: 0 ok; -1 error:: can`t load config
*/
int LoadConfig(const char *csConfigName, sl_config * __restrict cfg)
{
	FILE *f;
	int ret;

	if (!csConfigName || !cfg)
		return -1;

	InitConfig(cfg);
	if (access(csConfigName, R_OK)) {
		LOGERR;
		return -1;
	}

	f = fopen(csConfigName, "rt");
	if (!f) {
		LOGERR;
		return -1;
	}
	
	ret ^= ret;
	ret = ReadConfig(f, cfg);

	fclose(f);
	return ret;
}

/*
 * UnloadConfig() Unload config from memory and free resources
 * @cfg = Head list element
*/
void UnloadConfig(sl_config * __restrict cfg)
{
	struct tagPair *av;

	if (!cfg->slh_first)
		return;

	while ((av = cfg->slh_first)) {
		cfg->slh_first = cfg->slh_first->sle_next;

		if (av->psValue)
			free(av->psValue);
		if (av->psAttribute)
			free(av->psAttribute);
		if (av->psSection)
			free(av->psSection);
		free(av);
	}
}

/*
 * CreateConfig() Create config file from memory
 * @csConfigName = New config filename
 * @cfg = Head list element
 * return: 0 ok; -1 error:: can`t save new config
*/
int CreateConfig(const char *csConfigName, sl_config * __restrict cfg)
{
	FILE *f;
	int ret;

	if (!csConfigName || !cfg)
		return -1;

	f = fopen(csConfigName, "wt");
	if (!f) {
		LOGERR;
		return -1;
	}
	
	ret ^= ret;
	ret = WriteConfig(f, cfg);

	fclose(f);
	return ret;
}

// -----------------------------------------------------------

//
// Error maintenance functions ...
//

// cfg_GetErrno() Get error code of last operation
inline int cfg_GetErrno()
{
	return cfgErrno;
}

// cfg_GetError() Get error text of last operation
inline const char *cfg_GetError()
{
	return cfgError;
}

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>