File:  [ELWIX - Embedded LightWeight unIX -] / libaitcfg / src / aitcfg.c
Revision 1.2: download - view: text, annotated - select for diffs - revision graph
Wed Sep 9 09:07:31 2009 UTC (14 years, 8 months ago) by misho
Branches: MAIN
CVS tags: cfg3_1, HEAD, CFG3_0
prepare ver

    1: /*************************************************************************
    2: * (C) 2008 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
    3: *  by Michael Pounov <misho@openbsd-bg.org>
    4: *
    5: * $Author: misho $
    6: * $Id: aitcfg.c,v 1.2 2009/09/09 09:07:31 misho Exp $
    7: *
    8: *************************************************************************/
    9: #include "global.h"
   10: #include "aitcfg.h"
   11: 
   12: 
   13: #pragma GCC visibility push(hidden)
   14: 
   15: int cfgErrno;
   16: char cfgError[MAX_STR + 1];
   17: 
   18: #pragma GCC visibility pop
   19: 
   20: 
   21: /*
   22:  * InitConfig() Head initializing function for config
   23:  * @cfg = New head element for init
   24:  * return: 0 ok; -1 error:: new head element is null
   25: */
   26: inline int InitConfig(sl_config * __restrict cfg)
   27: {
   28: 	if (!cfg)
   29: 		return -1;
   30: 
   31: 	cfg->slh_first = NULL;
   32: 	return 0;
   33: }
   34: 
   35: /*
   36:  * LoadConfig() Load config from file
   37:  * @csConfigName = Filename of config
   38:  * @cfg = Head list element
   39:  * return: 0 ok; -1 error:: can`t load config
   40: */
   41: int LoadConfig(const char *csConfigName, sl_config * __restrict cfg)
   42: {
   43: 	FILE *f;
   44: 	int ret;
   45: 
   46: 	if (!csConfigName || !cfg)
   47: 		return -1;
   48: 
   49: 	InitConfig(cfg);
   50: 	if (access(csConfigName, R_OK)) {
   51: 		LOGERR;
   52: 		return -1;
   53: 	}
   54: 
   55: 	f = fopen(csConfigName, "rt");
   56: 	if (!f) {
   57: 		LOGERR;
   58: 		return -1;
   59: 	}
   60: 	
   61: 	ret ^= ret;
   62: 	ret = ReadConfig(f, cfg);
   63: 
   64: 	fclose(f);
   65: 	return ret;
   66: }
   67: 
   68: /*
   69:  * UnloadConfig() Unload config from memory and free resources
   70:  * @cfg = Head list element
   71: */
   72: void UnloadConfig(sl_config * __restrict cfg)
   73: {
   74: 	struct tagPair *av;
   75: 
   76: 	if (!cfg->slh_first)
   77: 		return;
   78: 
   79: 	while ((av = cfg->slh_first)) {
   80: 		cfg->slh_first = cfg->slh_first->sle_next;
   81: 
   82: 		if (av->psValue)
   83: 			free(av->psValue);
   84: 		if (av->psAttribute)
   85: 			free(av->psAttribute);
   86: 		if (av->psSection)
   87: 			free(av->psSection);
   88: 		free(av);
   89: 	}
   90: }
   91: 
   92: /*
   93:  * CreateConfig() Create config file from memory
   94:  * @csConfigName = New config filename
   95:  * @cfg = Head list element
   96:  * return: 0 ok; -1 error:: can`t save new config
   97: */
   98: int CreateConfig(const char *csConfigName, sl_config * __restrict cfg)
   99: {
  100: 	FILE *f;
  101: 	int ret;
  102: 
  103: 	if (!csConfigName || !cfg)
  104: 		return -1;
  105: 
  106: 	f = fopen(csConfigName, "wt");
  107: 	if (!f) {
  108: 		LOGERR;
  109: 		return -1;
  110: 	}
  111: 	
  112: 	ret ^= ret;
  113: 	ret = WriteConfig(f, cfg);
  114: 
  115: 	fclose(f);
  116: 	return ret;
  117: }
  118: 
  119: // -----------------------------------------------------------
  120: 
  121: //
  122: // Error maintenance functions ...
  123: //
  124: 
  125: // cfg_GetErrno() Get error code of last operation
  126: inline int cfg_GetErrno()
  127: {
  128: 	return cfgErrno;
  129: }
  130: 
  131: // cfg_GetError() Get error text of last operation
  132: inline const char *cfg_GetError()
  133: {
  134: 	return cfgError;
  135: }

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