File:  [ELWIX - Embedded LightWeight unIX -] / libaitcfg / src / queue.c
Revision 1.11: download - view: text, annotated - select for diffs - revision graph
Wed Sep 19 15:22:32 2012 UTC (11 years, 7 months ago) by misho
Branches: MAIN
CVS tags: cfg7_0, cfg6_1, HEAD, CFG6_1, CFG6_0
version 6.0

    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: queue.c,v 1.11 2012/09/19 15:22:32 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: 
   48: 
   49: static inline struct tagCfg *
   50: _selectAttribute(cfg_root_t * __restrict cfg, const char *csSec, const char *csAttr)
   51: {
   52: 	struct tagCfg fav, *c;
   53: 
   54: 	if (!cfg)
   55: 		return NULL;
   56: 	else
   57: 		memset(&fav, 0, sizeof fav);
   58: 
   59: 	if (csSec && *csSec)
   60: 		AIT_KEY(&fav.cfg_sec) = crcFletcher16((u_short*) csSec, 
   61: 				io_align(strlen(csSec), 2) / 2);
   62: 	if (csAttr)
   63: 		AIT_KEY(&fav.cfg_attr) = crcFletcher16((u_short*) csAttr, 
   64: 				io_align(strlen(csAttr), 2) / 2);
   65: 
   66: 	if (!csAttr)
   67: 		return RB_NFIND(tagRC, cfg, &fav);
   68: 	else {
   69: 		c = RB_FIND(tagRC, cfg, &fav);
   70: 		if (!c)
   71: 			return NULL;	/* not found */
   72: 		do {
   73: 			if (!strcmp(AIT_GET_STR(&c->cfg_attr), csAttr))
   74: 				return c;	/* FOUND! */
   75: 		} while (RB_NEXT(tagRC, cfg, c) && !cfg_tree_cmp(c, &fav));
   76: 		return NULL;	/* not found */
   77: 	}
   78: }
   79: 
   80: /* --------------------------------------------------------------- */
   81: 
   82: /*
   83:  * cfg_findAttribute() - Find attribute position in config file
   84:  *
   85:  * @cfg = Config root
   86:  * @csSec = Config section //[{csSec}]
   87:  * @csAttr = Config attribute //{csAttr} = ...
   88:  * return: 0 not found item, -1 error or >0 position in list
   89:  */
   90: inline int
   91: cfg_findAttribute(cfg_root_t * __restrict cfg, const char *csSec, const char *csAttr)
   92: {
   93: 	struct tagCfg *av, fav;
   94: 	register int cx = 0;
   95: 
   96: 	if (!cfg || !csAttr) {
   97: 		cfg_SetErr(EINVAL, "Invalid argument(s)");
   98: 		return -1;
   99: 	} else
  100: 		memset(&fav, 0, sizeof fav);
  101: 
  102: 	if (csSec && *csSec)
  103: 		AIT_KEY(&fav.cfg_sec) = crcFletcher16((u_short*) csSec, 
  104: 				io_align(strlen(csSec), 2) / 2);
  105: 	if (csAttr)
  106: 		AIT_KEY(&fav.cfg_attr) = crcFletcher16((u_short*) csAttr, 
  107: 				io_align(strlen(csAttr), 2) / 2);
  108: 
  109: 	SLIST_FOREACH(av, cfg, cfg_next) {
  110: 		++cx;
  111: 		if (!cfg_tree_cmp(&fav, av))
  112: 			return cx;
  113: 	}
  114: 
  115: 	return 0;
  116: }
  117: 
  118: /*
  119:  * cfg_unsetAttribute() - Unset item from config and free resources
  120:  *
  121:  * @cfg = Config root
  122:  * @csSec = Config section //[{csSec}], if NULL unset in *default* section
  123:  * @csAttr = Config attribute //{csAttr} = ...
  124:  * return: 0 item not found, -1 error or 1 removed item
  125:  */
  126: int
  127: cfg_unsetAttribute(cfg_root_t * __restrict cfg, const char *csSec, const char *csAttr)
  128: {
  129: 	struct tagCfg *av;
  130: 
  131: 	if (!cfg || !csAttr)
  132: 		return -1;
  133: 
  134: 	av = _selectAttribute(cfg, csSec, csAttr);
  135: 	if (!av)
  136: 		return 0;
  137: 
  138: 	CFG_RC_LOCK(cfg);
  139: 	RB_REMOVE(tagRC, cfg, av);
  140: 	SLIST_REMOVE(cfg, av, tagCfg, cfg_next);
  141: 	CFG_RC_UNLOCK(cfg);
  142: 
  143: 	AIT_FREE_VAL(&av->cfg_val);
  144: 	AIT_FREE_VAL(&av->cfg_attr);
  145: 	AIT_FREE_VAL(&av->cfg_sec);
  146: 	io_free(av);
  147: 	return 1;
  148: }
  149: 
  150: /*
  151:  * cfg_setAttribute() - Set item in config or adding new item if not exists
  152:  *
  153:  * @cfg = Config root
  154:  * @csSec = Config section //[{csSec}], if NULL set in *default* section
  155:  * @csAttr = Config attribute //{csAttr} = ...
  156:  * @csVal = Config value //... = {csVal} to setup
  157:  * return: 0 nothing changed, -1 error, 1 found and updated item or 2 added new item
  158:  */
  159: int
  160: cfg_setAttribute(cfg_root_t * __restrict cfg, const char *csSec, const char *csAttr, const char *csVal)
  161: {
  162: 	struct tagCfg *av, *section;
  163: 
  164: 	if (!cfg || !csAttr)
  165: 		return -1;
  166: 
  167: 	av = _selectAttribute(cfg, csSec, csAttr);
  168: 	if (!av) {
  169: 		/* adding new element */
  170: 		section = _selectAttribute(cfg, csSec, NULL);
  171: 
  172: 		av = io_malloc(sizeof(struct tagCfg));
  173: 		if (!av) {
  174: 			LOGERR;
  175: 			return -1;
  176: 		} else {
  177: 			memset(av, 0, sizeof(struct tagCfg));
  178: 
  179: 			CFG_RC_LOCK(cfg);
  180: 			if (!section)
  181: 				SLIST_INSERT_HEAD(cfg, av, cfg_next);
  182: 			else
  183: 				SLIST_INSERT_AFTER(section, av, cfg_next);
  184: 			CFG_RC_UNLOCK(cfg);
  185: 		}
  186: 
  187: 		if (csSec && *csSec) {
  188: 			AIT_SET_STR(&av->cfg_sec, csSec);
  189: 			AIT_KEY(&av->cfg_sec) = crcFletcher16(AIT_GET_LIKE(&av->cfg_sec, u_short*), 
  190: 					io_align(AIT_LEN(&av->cfg_sec) - 1, 2) / 2);
  191: 		}
  192: 		AIT_SET_STR(&av->cfg_val, csVal ? csVal : "");
  193: 		AIT_SET_STR(&av->cfg_attr, csAttr);
  194: 		AIT_KEY(&av->cfg_attr) = crcFletcher16(AIT_GET_LIKE(&av->cfg_attr, u_short*), 
  195: 				io_align(AIT_LEN(&av->cfg_attr) - 1, 2) / 2);
  196: 
  197: 		CFG_RC_LOCK(cfg);
  198: 		RB_INSERT(tagRC, cfg, av);
  199: 		CFG_RC_UNLOCK(cfg);
  200: 		return 2;
  201: 	}
  202: 
  203: 	if (csVal && AIT_ADDR(&av->cfg_val) && 
  204: 			strcmp((char*) csVal, (char*) AIT_GET_STR(&av->cfg_val))) {
  205: 		/* Update element */
  206: 		AIT_FREE_VAL(&av->cfg_val);
  207: 		AIT_SET_STR(&av->cfg_val, csVal);
  208: 		return 1;
  209: 	}
  210: 
  211: 	/* Nothing happens ... found & values is equal! */
  212: 	return 0;
  213: }
  214: 
  215: /*
  216:  * cfg_getAttribute() - Get item from config and return value from it
  217:  *
  218:  * @cfg = Config root
  219:  * @csSec = Config section //[{csSec}], if NULL unset in *default* section
  220:  * @csAttr = Config attribute //{csAttr} = ..., if NULL unset as *any* attribute
  221:  * return: NULL item not found or null parameters, !=NULL value const string
  222:  */
  223: inline const char *
  224: cfg_getAttribute(cfg_root_t * __restrict cfg, const char *csSec, const char *csAttr)
  225: {
  226: 	struct tagCfg *av;
  227: 
  228: 	if (!cfg || !csAttr)
  229: 		return NULL;
  230: 
  231: 	av = _selectAttribute(cfg, csSec, csAttr);
  232: 	if (!av)
  233: 		return NULL;
  234: 
  235: 	return AIT_GET_STR(&av->cfg_val);
  236: }
  237: 
  238: /*
  239:  * cfg_loadAttribute() - Get guarded attribute, if not found item return *default value*
  240:  *
  241:  * @cfg = Config root
  242:  * @csSec = Config section //[{csSec}], if NULL unset in *default* section
  243:  * @csAttr = Config attribute //{csAttr} = ...
  244:  * @val = Return buffer for item Value //... = {val}
  245:  * @csDefValue = *Default Value* for return in //{val}, if not found item in config
  246:  * return: 0 item not found, -1 error or >0 number of copied bytes in //{val}
  247:  */
  248: int
  249: cfg_loadAttribute(cfg_root_t * __restrict cfg, const char *csSec, const char *csAttr, 
  250: 		ait_val_t * __restrict val, const char *csDefValue)
  251: {
  252: 	struct tagCfg *av;
  253: 	int ret = 0;
  254: 
  255: 	if (!cfg || !csAttr || !val) {
  256: 		cfg_SetErr(EINVAL, "Invalid argument(s)");
  257: 		return -1;
  258: 	}
  259: 
  260: 	AIT_INIT_VAL(val);
  261: 	av = _selectAttribute(cfg, csSec, csAttr);
  262: 	if (!av) {
  263: 		/* not found item */
  264: 		if (csDefValue) {
  265: 			AIT_SET_STR(val, csDefValue);
  266: 			ret = AIT_LEN(val);
  267: 		} else
  268: 			AIT_INIT_VAL(val);
  269: 		return ret;
  270: 	}
  271: 
  272: 	if (AIT_ISEMPTY(&av->cfg_val) || !AIT_ADDR(&av->cfg_val) || 
  273: 			!*AIT_GET_LIKE(&av->cfg_val, char*)) {
  274: 		/* empty value */
  275: 		if (csDefValue) {
  276: 			AIT_SET_STR(val, csDefValue);
  277: 			ret = AIT_LEN(val);
  278: 		} else
  279: 			AIT_INIT_VAL(val);
  280: 	} else {
  281: 		/* copy value */
  282: 		AIT_SET_STR(val, AIT_GET_STR(&av->cfg_val));
  283: 		ret = AIT_LEN(val);
  284: 	}
  285: 
  286: 	return ret;
  287: }

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