File:  [ELWIX - Embedded LightWeight unIX -] / libaitcfg / src / aitcfg.c
Revision 1.4.4.2: download - view: text, annotated - select for diffs - revision graph
Tue Apr 3 09:21:06 2012 UTC (12 years, 2 months ago) by misho
Branches: cfg5_0
Diff to: branchpoint 1.4: preferred, unified
add new funcs

    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.4.4.2 2012/04/03 09:21:06 misho Exp $
    7: *
    8: **************************************************************************
    9: The ELWIX and AITNET software is distributed under the following
   10: terms:
   11: 
   12: All of the documentation and software included in the ELWIX and AITNET
   13: Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
   14: 
   15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
   16: 	by Michael Pounov <misho@elwix.org>.  All rights reserved.
   17: 
   18: Redistribution and use in source and binary forms, with or without
   19: modification, are permitted provided that the following conditions
   20: are met:
   21: 1. Redistributions of source code must retain the above copyright
   22:    notice, this list of conditions and the following disclaimer.
   23: 2. Redistributions in binary form must reproduce the above copyright
   24:    notice, this list of conditions and the following disclaimer in the
   25:    documentation and/or other materials provided with the distribution.
   26: 3. All advertising materials mentioning features or use of this software
   27:    must display the following acknowledgement:
   28: This product includes software developed by Michael Pounov <misho@elwix.org>
   29: ELWIX - Embedded LightWeight unIX and its contributors.
   30: 4. Neither the name of AITNET nor the names of its contributors
   31:    may be used to endorse or promote products derived from this software
   32:    without specific prior written permission.
   33: 
   34: THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
   35: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   36: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   37: ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   38: FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   39: DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   40: OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   41: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   42: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   43: OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   44: SUCH DAMAGE.
   45: */
   46: #include "global.h"
   47: #include "aitcfg.h"
   48: 
   49: 
   50: #pragma GCC visibility push(hidden)
   51: 
   52: int cfg_Errno;
   53: char cfg_Error[STRSIZ];
   54: 
   55: inline int
   56: cfg_tree_cmp(struct tagCfg *a, struct tagCfg *b)
   57: {
   58: 	int ret;
   59: 
   60: 	assert(a && b);
   61: 
   62: 	ret = ((AIT_KEY(&a->cfg_sec) << 16) | AIT_KEY(&a->cfg_attr)) - 
   63: 		((AIT_KEY(&b->cfg_sec) << 16) | AIT_KEY(&b->cfg_attr));
   64: 
   65: 	if (ret < 0)
   66: 		return -1;
   67: 	else if (ret > 0)
   68: 		return 1;
   69: 
   70: 	return ret;
   71: }
   72: 
   73: RB_GENERATE(tagRC, tagCfg, cfg_node, cfg_tree_cmp);
   74: 
   75: #pragma GCC visibility pop
   76: 
   77: 
   78: // cfg_GetErrno() Get error code of last operation
   79: inline int
   80: cfg_GetErrno()
   81: {
   82: 	return cfg_Errno;
   83: }
   84: 
   85: // cfg_GetError() Get error text of last operation
   86: inline const char *
   87: cfg_GetError()
   88: {
   89: 	return cfg_Error;
   90: }
   91: 
   92: // cfg_SetErr() Set error to variables for internal use!!!
   93: inline void
   94: cfg_SetErr(int eno, char *estr, ...)
   95: {
   96: 	va_list lst;
   97: 
   98: 	cfg_Errno = eno;
   99: 	memset(cfg_Error, 0, sizeof cfg_Error);
  100: 	va_start(lst, estr);
  101: 	vsnprintf(cfg_Error, sizeof cfg_Error, estr, lst);
  102: 	va_end(lst);
  103: }
  104: 
  105: 
  106: /*
  107:  * cfgInitConfig() - Init config root
  108:  *
  109:  * @cfg = Config root
  110:  * return: -1 error or 0 ok
  111:  */
  112: int
  113: cfgInitConfig(cfg_root_t * __restrict cfg)
  114: {
  115: 	if (!cfg)
  116: 		return -1;
  117: 
  118: #ifdef HAVE_LIBPTHREAD
  119: 	pthread_mutex_init(&cfg->rc_mtx, NULL);
  120: #endif
  121: 	SLIST_INIT(cfg);
  122: 	RB_INIT(cfg);
  123: 	return 0;
  124: }
  125: 
  126: /*
  127:  * cfgLoadConfig() - Load config from file
  128:  *
  129:  * @cfgName = Config filename
  130:  * @cfg = Config root
  131:  * return: -1 error or 0 ok
  132:  */
  133: int
  134: cfgLoadConfig(const char *cfgName, cfg_root_t * __restrict cfg)
  135: {
  136: 	FILE *f;
  137: 	int ret;
  138: 
  139: 	if (!cfgName || !cfg) {
  140: 		cfg_SetErr(EINVAL, "Invalid parameter(s)");
  141: 		return -1;
  142: 	} else
  143: 		cfgInitConfig(cfg);
  144: 
  145: 	f = fopen(cfgName, "r");
  146: 	if (!f) {
  147: 		LOGERR;
  148: 		return -1;
  149: 	}
  150: 
  151: 	ret = cfgReadConfig(f, cfg);
  152: 
  153: 	fclose(f);
  154: 	return ret;
  155: }
  156: 
  157: /*
  158:  * cfgUnloadConfig() - Unload config from memory and free resources
  159:  *
  160:  * @cfg = Config root
  161:  * return: none
  162:  */
  163: void
  164: cfgUnloadConfig(cfg_root_t * __restrict cfg)
  165: {
  166: 	struct tagCfg *av;
  167: 
  168: 	if (!cfg)
  169: 		return;
  170: 
  171: 	CFG_RC_LOCK(cfg);
  172: 	while ((av = SLIST_FIRST(cfg))) {
  173: 		SLIST_REMOVE_HEAD(cfg, cfg_next);
  174: 
  175: 		AIT_FREE_VAL(&av->cfg_val);
  176: 		AIT_FREE_VAL(&av->cfg_attr);
  177: 		AIT_FREE_VAL(&av->cfg_sec);
  178: 		free(av);
  179: 	}
  180: 	cfg->rbh_root = NULL;
  181: 	CFG_RC_UNLOCK(cfg);
  182: 
  183: #ifdef HAVE_LIBPTHREAD
  184: 	pthread_mutex_destroy(&cfg->rc_mtx);
  185: #endif
  186: }
  187: 
  188: #if 0
  189: /*
  190:  * CreateConfig() Create config file from memory
  191:  * @csConfigName = New config filename
  192:  * @cfg = Head list element
  193:  * return: 0 ok; -1 error:: can`t save new config
  194: */
  195: int CreateConfig(const char *csConfigName, sl_config * __restrict cfg)
  196: {
  197: 	FILE *f;
  198: 	int ret;
  199: 
  200: 	if (!csConfigName || !cfg)
  201: 		return -1;
  202: 
  203: 	f = fopen(csConfigName, "wt");
  204: 	if (!f) {
  205: 		LOGERR;
  206: 		return -1;
  207: 	}
  208: 	
  209: 	ret ^= ret;
  210: 	ret = WriteConfig(f, cfg);
  211: 
  212: 	fclose(f);
  213: 	return ret;
  214: }
  215: 
  216: /*
  217:  * cfg_CreateConfig() Create config file from memory without whitespaces!
  218:  * @csConfigName = New config filename
  219:  * @cfg = Head list element
  220:  * return: 0 ok; -1 error:: can`t save new config
  221: */
  222: int cfg_CreateConfig(const char *csConfigName, sl_config * __restrict cfg)
  223: {
  224: 	FILE *f;
  225: 	int ret;
  226: 
  227: 	if (!csConfigName || !cfg)
  228: 		return -1;
  229: 
  230: 	f = fopen(csConfigName, "wt");
  231: 	if (!f) {
  232: 		LOGERR;
  233: 		return -1;
  234: 	}
  235: 	
  236: 	ret ^= ret;
  237: 	ret = cfg_WriteConfig(f, cfg);
  238: 
  239: 	fclose(f);
  240: 	return ret;
  241: }
  242: #endif

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