/************************************************************************* * (C) 2008 AITNET ltd - Sofia/Bulgaria - * by Michael Pounov * * $Author: misho $ * $Id: aitcfg.c,v 1.2 2009/09/09 09:07:31 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; }