File:  [ELWIX - Embedded LightWeight unIX -] / libaitcfg / src / queue.c
Revision 1.14.4.3: download - view: text, annotated - select for diffs - revision graph
Wed Mar 19 17:44:48 2014 UTC (10 years, 3 months ago) by misho
Branches: cfg7_5
Diff to: branchpoint 1.14: preferred, unified
kill bugs :D

    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.14.4.3 2014/03/19 17:44:48 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 - 2014
   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: 				E_ALIGN(strlen(csSec), 2) / 2);
   62: 	if (csAttr)
   63: 		AIT_KEY(&fav.cfg_attr) = crcFletcher16((u_short*) csAttr, 
   64: 				E_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 ((c = RB_NEXT(tagRC, cfg, c)) && c && !cfg_tree_cmp(c, &fav));
   76: 		return NULL;	/* not found */
   77: 	}
   78: }
   79: 
   80: /* --------------------------------------------------------------- */
   81: 
   82: /*
   83:  * cfg_getSection() - Get entire section attributes into array
   84:  *
   85:  * @cfg = Config root
   86:  * @csSec = Config section //[{csSec}]
   87:  * return: NULL not found or !=NULL allocated array, must free with array_Destroy() after use!
   88:  */
   89: array_t *
   90: cfg_getSection(cfg_root_t * __restrict cfg, const char *csSec)
   91: {
   92: 	array_t *arr = NULL;
   93: 	struct tagCfg *av, fav;
   94: 
   95: 	if (!cfg) {
   96: 		cfg_SetErr(EINVAL, "Invalid argument(s)");
   97: 		return NULL;
   98: 	} else
   99: 		memset(&fav, 0, sizeof fav);
  100: 
  101: 	if (csSec && *csSec)
  102: 		AIT_KEY(&fav.cfg_sec) = crcFletcher16((u_short*) csSec, 
  103: 				E_ALIGN(strlen(csSec), 2) / 2);
  104: 
  105: 	av = RB_NFIND(tagRC, cfg, &fav);
  106: 	if (!av)
  107: 		return NULL;
  108: 	if (strcmp(AIT_GET_STR(&av->cfg_sec), csSec))
  109: 		return NULL;
  110: 
  111: 	arr = array_Init(1);
  112: 	if (!arr) {
  113: 		cfg_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
  114: 		return NULL;
  115: 	} else
  116: 		array_Push(arr, av, 0);
  117: 
  118: 	while ((av = RB_NEXT(tagRC, cfg, av)) && av && !AIT_ISEMPTY(&av->cfg_sec) && 
  119: 			!strcmp(AIT_GET_STR(&av->cfg_sec), csSec))
  120: 		array_Push(arr, av, 0);
  121: 
  122: 	return arr;
  123: }
  124: 
  125: /*
  126:  * cfg_findAttribute() - Find attribute position in config file
  127:  *
  128:  * @cfg = Config root
  129:  * @csSec = Config section //[{csSec}]
  130:  * @csAttr = Config attribute //{csAttr} = ..., if NULL as *any* attribute
  131:  * return: 0 not found item, -1 error or >0 position in list
  132:  */
  133: int
  134: cfg_findAttribute(cfg_root_t * __restrict cfg, const char *csSec, const char *csAttr)
  135: {
  136: 	struct tagCfg *av, fav;
  137: 	register int cx = 0;
  138: 
  139: 	if (!cfg) {
  140: 		cfg_SetErr(EINVAL, "Invalid argument(s)");
  141: 		return -1;
  142: 	} else
  143: 		memset(&fav, 0, sizeof fav);
  144: 
  145: 	if (csSec && *csSec)
  146: 		AIT_KEY(&fav.cfg_sec) = crcFletcher16((u_short*) csSec, 
  147: 				E_ALIGN(strlen(csSec), 2) / 2);
  148: 	if (csAttr)
  149: 		AIT_KEY(&fav.cfg_attr) = crcFletcher16((u_short*) csAttr, 
  150: 				E_ALIGN(strlen(csAttr), 2) / 2);
  151: 
  152: 	TAILQ_FOREACH(av, cfg, cfg_next) {
  153: 		++cx;
  154: 		if (!cfg_tree_cmp(&fav, av))
  155: 			return cx;
  156: 	}
  157: 
  158: 	return 0;
  159: }
  160: 
  161: /*
  162:  * cfg_unsetAttribute() - Unset item from config and free resources
  163:  *
  164:  * @cfg = Config root
  165:  * @csSec = Config section //[{csSec}], if NULL unset in *default* section
  166:  * @csAttr = Config attribute //{csAttr} = ..., if NULL as *any* attribute
  167:  * return: 0 item not found, -1 error or 1 removed item
  168:  */
  169: int
  170: cfg_unsetAttribute(cfg_root_t * __restrict cfg, const char *csSec, const char *csAttr)
  171: {
  172: 	struct tagCfg *av;
  173: 
  174: 	if (!cfg)
  175: 		return -1;
  176: 
  177: 	av = _selectAttribute(cfg, csSec, csAttr);
  178: 	if (!av)
  179: 		return 0;
  180: 
  181: 	CFG_RC_LOCK(cfg);
  182: 	RB_REMOVE(tagRC, cfg, av);
  183: 	TAILQ_REMOVE(cfg, av, cfg_next);
  184: 	CFG_RC_UNLOCK(cfg);
  185: 
  186: 	AIT_FREE_VAL(&av->cfg_val);
  187: 	AIT_FREE_VAL(&av->cfg_attr);
  188: 	AIT_FREE_VAL(&av->cfg_sec);
  189: 	e_free(av);
  190: 	return 1;
  191: }
  192: 
  193: /*
  194:  * cfg_setAttribute() - Set item in config or adding new item if not exists
  195:  *
  196:  * @cfg = Config root
  197:  * @csSec = Config section //[{csSec}], if NULL set in *default* section
  198:  * @csAttr = Config attribute //{csAttr} = ...
  199:  * @csVal = Config value //... = {csVal} to setup
  200:  * return: 0 nothing changed, -1 error, 1 found and updated item or 2 added new item
  201:  */
  202: int
  203: cfg_setAttribute(cfg_root_t * __restrict cfg, const char *csSec, const char *csAttr, const char *csVal)
  204: {
  205: 	struct tagCfg *av, *section;
  206: 
  207: 	if (!cfg || !csAttr)
  208: 		return -1;
  209: 
  210: 	av = _selectAttribute(cfg, csSec, csAttr);
  211: 	if (!av) {
  212: 		/* adding new element */
  213: 		section = _selectAttribute(cfg, csSec, NULL);
  214: 
  215: 		av = e_malloc(sizeof(struct tagCfg));
  216: 		if (!av) {
  217: 			LOGERR;
  218: 			return -1;
  219: 		} else {
  220: 			memset(av, 0, sizeof(struct tagCfg));
  221: 
  222: 			CFG_RC_LOCK(cfg);
  223: 			if (!section)
  224: 				TAILQ_INSERT_TAIL(cfg, av, cfg_next);
  225: 			else
  226: 				TAILQ_INSERT_BEFORE(section, av, cfg_next);
  227: 			CFG_RC_UNLOCK(cfg);
  228: 		}
  229: 
  230: 		if (csSec && *csSec) {
  231: 			AIT_SET_STR(&av->cfg_sec, csSec);
  232: 			AIT_KEY(&av->cfg_sec) = crcFletcher16(AIT_GET_LIKE(&av->cfg_sec, u_short*), 
  233: 					E_ALIGN(AIT_LEN(&av->cfg_sec) - 1, 2) / 2);
  234: 		}
  235: 		AIT_SET_STR(&av->cfg_val, csVal ? csVal : "");
  236: 		AIT_SET_STR(&av->cfg_attr, csAttr);
  237: 		AIT_KEY(&av->cfg_attr) = crcFletcher16(AIT_GET_LIKE(&av->cfg_attr, u_short*), 
  238: 				E_ALIGN(AIT_LEN(&av->cfg_attr) - 1, 2) / 2);
  239: 
  240: 		CFG_RC_LOCK(cfg);
  241: 		RB_INSERT(tagRC, cfg, av);
  242: 		CFG_RC_UNLOCK(cfg);
  243: 		return 2;
  244: 	}
  245: 
  246: 	if (csVal && AIT_ADDR(&av->cfg_val) && 
  247: 			strcmp((char*) csVal, (char*) AIT_GET_STR(&av->cfg_val))) {
  248: 		/* Update element */
  249: 		AIT_FREE_VAL(&av->cfg_val);
  250: 		AIT_SET_STR(&av->cfg_val, csVal);
  251: 		return 1;
  252: 	}
  253: 
  254: 	/* Nothing happens ... found & values is equal! */
  255: 	return 0;
  256: }
  257: 
  258: /*
  259:  * cfg_getAttribute() - Get item from config and return value from it
  260:  *
  261:  * @cfg = Config root
  262:  * @csSec = Config section //[{csSec}], if NULL unset in *default* section
  263:  * @csAttr = Config attribute //{csAttr} = ..., if NULL as *any* attribute
  264:  * return: NULL item not found or null parameters, !=NULL value const string
  265:  */
  266: const char *
  267: cfg_getAttribute(cfg_root_t * __restrict cfg, const char *csSec, const char *csAttr)
  268: {
  269: 	struct tagCfg *av;
  270: 
  271: 	if (!cfg)
  272: 		return NULL;
  273: 
  274: 	av = _selectAttribute(cfg, csSec, csAttr);
  275: 	if (!av)
  276: 		return NULL;
  277: 
  278: 	return AIT_GET_STR(&av->cfg_val);
  279: }
  280: 
  281: /*
  282:  * cfg_loadAttribute() - Get guarded attribute, if not found item return *default value*
  283:  *
  284:  * @cfg = Config root
  285:  * @csSec = Config section //[{csSec}], if NULL unset in *default* section
  286:  * @csAttr = Config attribute //{csAttr} = ..., if NULL as *any* attribute
  287:  * @val = Return buffer for item Value //... = {val}
  288:  * @csDefValue = *Default Value* for return in //{val}, if not found item in config
  289:  * return: 0 item not found, -1 error or >0 number of copied bytes in //{val}
  290:  */
  291: int
  292: cfg_loadAttribute(cfg_root_t * __restrict cfg, const char *csSec, const char *csAttr, 
  293: 		ait_val_t * __restrict val, const char *csDefValue)
  294: {
  295: 	struct tagCfg *av;
  296: 	int ret = 0;
  297: 
  298: 	if (!cfg || !val) {
  299: 		cfg_SetErr(EINVAL, "Invalid argument(s)");
  300: 		return -1;
  301: 	}
  302: 
  303: 	AIT_INIT_VAL(val);
  304: 	av = _selectAttribute(cfg, csSec, csAttr);
  305: 	if (!av) {
  306: 		/* not found item */
  307: 		if (csDefValue) {
  308: 			AIT_SET_STR(val, csDefValue);
  309: 			ret = AIT_LEN(val);
  310: 		} else
  311: 			AIT_INIT_VAL(val);
  312: 		return ret;
  313: 	}
  314: 
  315: 	if (AIT_ISEMPTY(&av->cfg_val) || !AIT_ADDR(&av->cfg_val) || 
  316: 			!*AIT_GET_LIKE(&av->cfg_val, char*)) {
  317: 		/* empty value */
  318: 		if (csDefValue) {
  319: 			AIT_SET_STR(val, csDefValue);
  320: 			ret = AIT_LEN(val);
  321: 		} else
  322: 			AIT_INIT_VAL(val);
  323: 	} else {
  324: 		/* copy value */
  325: 		AIT_SET_STR(val, AIT_GET_STR(&av->cfg_val));
  326: 		ret = AIT_LEN(val);
  327: 	}
  328: 
  329: 	return ret;
  330: }

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