File:  [ELWIX - Embedded LightWeight unIX -] / libaitcfg / src / aitcfg.c
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Thu Aug 28 13:17:41 2008 UTC (15 years, 9 months ago) by misho
Branches: misho
CVS tags: start, cfg3_0
AITNET library for configs

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

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