File:  [ELWIX - Embedded LightWeight unIX -] / libaitcfg / src / aitcfg.c
Revision 1.7: download - view: text, annotated - select for diffs - revision graph
Wed Jul 25 15:24:20 2012 UTC (11 years, 9 months ago) by misho
Branches: MAIN
CVS tags: cfg5_5, cfg5_4, cfg5_3, HEAD, CFG5_4, CFG5_3, CFG5_2
version 5.2

    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.7 2012/07/25 15:24:20 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: 	pthread_mutex_init(&cfg->rc_mtx, NULL);
  119: 
  120: 	SLIST_INIT(cfg);
  121: 	RB_INIT(cfg);
  122: 	return 0;
  123: }
  124: 
  125: /*
  126:  * cfgLoadConfig() - Load config from file
  127:  *
  128:  * @cfgName = Config filename
  129:  * @cfg = Config root
  130:  * return: -1 error or 0 ok
  131:  */
  132: int
  133: cfgLoadConfig(const char *cfgName, cfg_root_t * __restrict cfg)
  134: {
  135: 	FILE *f;
  136: 	int ret;
  137: 
  138: 	if (!cfgName || !cfg) {
  139: 		cfg_SetErr(EINVAL, "Invalid parameter(s)");
  140: 		return -1;
  141: 	} else
  142: 		cfgInitConfig(cfg);
  143: 
  144: 	f = fopen(cfgName, "r");
  145: 	if (!f) {
  146: 		LOGERR;
  147: 		return -1;
  148: 	}
  149: 
  150: 	ret = cfgReadConfig(f, cfg);
  151: 
  152: 	fclose(f);
  153: 	return ret;
  154: }
  155: 
  156: /*
  157:  * cfgClearConfig() - Clear config and free resources
  158:  *
  159:  * @cfg = Config root
  160:  * return: none
  161:  */
  162: void
  163: cfgClearConfig(cfg_root_t * __restrict cfg)
  164: {
  165: 	struct tagCfg *av;
  166: 
  167: 	if (!cfg)
  168: 		return;
  169: 
  170: 	CFG_RC_LOCK(cfg);
  171: 	while ((av = SLIST_FIRST(cfg))) {
  172: 		SLIST_REMOVE_HEAD(cfg, cfg_next);
  173: 
  174: 		AIT_FREE_VAL(&av->cfg_val);
  175: 		AIT_FREE_VAL(&av->cfg_attr);
  176: 		AIT_FREE_VAL(&av->cfg_sec);
  177: 		io_free(av);
  178: 	}
  179: 	cfg->rbh_root = NULL;
  180: 	CFG_RC_UNLOCK(cfg);
  181: }
  182: 
  183: /*
  184:  * cfgUnloadConfig() - Unload config from memory and destroy resources
  185:  *
  186:  * @cfg = Config root
  187:  * return: none
  188:  */
  189: void
  190: cfgUnloadConfig(cfg_root_t * __restrict cfg)
  191: {
  192: 	if (!cfg)
  193: 		return;
  194: 
  195: 	cfgClearConfig(cfg);
  196: 	pthread_mutex_destroy(&cfg->rc_mtx);
  197: }
  198: 
  199: /*
  200:  * cfgCreateConfig() - Create config file from memory
  201:  *
  202:  * @csConfigName = New config filename
  203:  * @cfg = Config root
  204:  * @whitespace = Additional whitespace characters to file
  205:  * return: -1 error or 0 ok
  206:  */
  207: int
  208: cfgCreateConfig(const char *csConfigName, cfg_root_t * __restrict cfg, int whitespace)
  209: {
  210: 	FILE *f;
  211: 	int ret;
  212: 
  213: 	if (!csConfigName || !cfg)
  214: 		return -1;
  215: 
  216: 	f = fopen(csConfigName, "w");
  217: 	if (!f) {
  218: 		LOGERR;
  219: 		return -1;
  220: 	}
  221: 	
  222: 	ret = cfgWriteConfig(f, cfg, whitespace);
  223: 
  224: 	fclose(f);
  225: 	return ret;
  226: }

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