File:  [ELWIX - Embedded LightWeight unIX -] / libaitcfg / src / aitcfg.c
Revision 1.1: download - view: text, annotated - select for diffs - revision graph
Thu Aug 28 13:17:41 2008 UTC (15 years, 8 months ago) by misho
Branches: MAIN
CVS tags: HEAD
Initial revision

#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;

	return (int) (cfg->slh_first = NULL);
}

/*
 * 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;

	for (av = cfg->slh_first; av; av = av->sle_next) {
		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>