File:  [ELWIX - Embedded LightWeight unIX -] / libaitcfg / src / queue.c
Revision 1.16.8.1: download - view: text, annotated - select for diffs - revision graph
Thu Nov 21 14:27:25 2019 UTC (4 years, 6 months ago) by misho
Branches: cfg8_0
Diff to: branchpoint 1.16: preferred, unified
change search and fetch algorithm in cfg_getSection()

    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.16.8.1 2019/11/21 14:27:25 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 - 2019
   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: 	if (csSec && !*csSec)
  101: 		csSec = NULL;
  102: 
  103: 	if (csSec && *csSec)
  104: 		AIT_KEY(&fav.cfg_sec) = crcFletcher16((u_short*) csSec, 
  105: 				E_ALIGN(strlen(csSec), 2) / 2);
  106: 
  107: 	arr = array_Init(0);
  108: 	if (!arr) {
  109: 		cfg_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
  110: 		return NULL;
  111: 	}
  112: 
  113: 	TAILQ_FOREACH(av, cfg, cfg_next)
  114: 		if (AIT_KEY(&av->cfg_sec) == AIT_KEY(&fav.cfg_sec)) {
  115: 			if (!csSec) {
  116: 				if (AIT_ISEMPTY(&av->cfg_sec))
  117: 					array_Push(arr, av, 0);
  118: 			} else {
  119: 				if (!AIT_ISEMPTY(&av->cfg_sec) && 
  120: 						!strcmp(AIT_GET_STR(&av->cfg_sec), csSec))
  121: 					array_Push(arr, av, 0);
  122: 			}
  123: 		}
  124: 
  125: 	if (!array_Size(arr))
  126: 		array_Destroy(&arr);
  127: 
  128: 	return arr;
  129: }
  130: 
  131: /*
  132:  * cfg_findAttribute() - Find attribute position in config file
  133:  *
  134:  * @cfg = Config root
  135:  * @csSec = Config section //[{csSec}]
  136:  * @csAttr = Config attribute //{csAttr} = ..., if NULL as *any* attribute
  137:  * return: 0 not found item, -1 error or >0 position in list
  138:  */
  139: int
  140: cfg_findAttribute(cfg_root_t * __restrict cfg, const char *csSec, const char *csAttr)
  141: {
  142: 	struct tagCfg *av, fav;
  143: 	register int cx = 0;
  144: 
  145: 	if (!cfg) {
  146: 		cfg_SetErr(EINVAL, "Invalid argument(s)");
  147: 		return -1;
  148: 	} else
  149: 		memset(&fav, 0, sizeof fav);
  150: 
  151: 	if (csSec && *csSec)
  152: 		AIT_KEY(&fav.cfg_sec) = crcFletcher16((u_short*) csSec, 
  153: 				E_ALIGN(strlen(csSec), 2) / 2);
  154: 	if (csAttr)
  155: 		AIT_KEY(&fav.cfg_attr) = crcFletcher16((u_short*) csAttr, 
  156: 				E_ALIGN(strlen(csAttr), 2) / 2);
  157: 
  158: 	TAILQ_FOREACH(av, cfg, cfg_next) {
  159: 		++cx;
  160: 		if (!cfg_tree_cmp(&fav, av))
  161: 			return cx;
  162: 	}
  163: 
  164: 	return 0;
  165: }
  166: 
  167: /*
  168:  * cfg_unsetAttribute() - Unset item from config and free resources
  169:  *
  170:  * @cfg = Config root
  171:  * @csSec = Config section //[{csSec}], if NULL unset in *default* section
  172:  * @csAttr = Config attribute //{csAttr} = ..., if NULL as *any* attribute
  173:  * return: 0 item not found, -1 error or 1 removed item
  174:  */
  175: int
  176: cfg_unsetAttribute(cfg_root_t * __restrict cfg, const char *csSec, const char *csAttr)
  177: {
  178: 	struct tagCfg *av;
  179: 
  180: 	if (!cfg)
  181: 		return -1;
  182: 
  183: 	av = _selectAttribute(cfg, csSec, csAttr);
  184: 	if (!av)
  185: 		return 0;
  186: 
  187: 	CFG_RC_LOCK(cfg);
  188: 	RB_REMOVE(tagRC, cfg, av);
  189: 	TAILQ_REMOVE(cfg, av, cfg_next);
  190: 	CFG_RC_UNLOCK(cfg);
  191: 
  192: 	AIT_FREE_VAL(&av->cfg_val);
  193: 	AIT_FREE_VAL(&av->cfg_attr);
  194: 	AIT_FREE_VAL(&av->cfg_sec);
  195: 	e_free(av);
  196: 	return 1;
  197: }
  198: 
  199: /*
  200:  * cfg_setAttribute() - Set item in config or adding new item if not exists
  201:  *
  202:  * @cfg = Config root
  203:  * @csSec = Config section //[{csSec}], if NULL set in *default* section
  204:  * @csAttr = Config attribute //{csAttr} = ...
  205:  * @csVal = Config value //... = {csVal} to setup
  206:  * return: 0 nothing changed, -1 error, 1 found and updated item or 2 added new item
  207:  */
  208: int
  209: cfg_setAttribute(cfg_root_t * __restrict cfg, const char *csSec, const char *csAttr, const char *csVal)
  210: {
  211: 	struct tagCfg *av, *section;
  212: 
  213: 	if (!cfg || !csAttr)
  214: 		return -1;
  215: 
  216: 	av = _selectAttribute(cfg, csSec, csAttr);
  217: 	if (!av) {
  218: 		/* adding new element */
  219: 		section = _selectAttribute(cfg, csSec, NULL);
  220: 
  221: 		av = e_malloc(sizeof(struct tagCfg));
  222: 		if (!av) {
  223: 			LOGERR;
  224: 			return -1;
  225: 		} else {
  226: 			memset(av, 0, sizeof(struct tagCfg));
  227: 
  228: 			CFG_RC_LOCK(cfg);
  229: 			if (!section)
  230: 				TAILQ_INSERT_TAIL(cfg, av, cfg_next);
  231: 			else
  232: 				TAILQ_INSERT_BEFORE(section, av, cfg_next);
  233: 			CFG_RC_UNLOCK(cfg);
  234: 		}
  235: 
  236: 		if (csSec && *csSec) {
  237: 			AIT_SET_STR(&av->cfg_sec, csSec);
  238: 			AIT_KEY(&av->cfg_sec) = crcFletcher16(AIT_GET_LIKE(&av->cfg_sec, u_short*), 
  239: 					E_ALIGN(AIT_LEN(&av->cfg_sec) - 1, 2) / 2);
  240: 		}
  241: 		AIT_SET_STR(&av->cfg_val, csVal ? csVal : "");
  242: 		AIT_SET_STR(&av->cfg_attr, csAttr);
  243: 		AIT_KEY(&av->cfg_attr) = crcFletcher16(AIT_GET_LIKE(&av->cfg_attr, u_short*), 
  244: 				E_ALIGN(AIT_LEN(&av->cfg_attr) - 1, 2) / 2);
  245: 
  246: 		CFG_RC_LOCK(cfg);
  247: 		RB_INSERT(tagRC, cfg, av);
  248: 		CFG_RC_UNLOCK(cfg);
  249: 		return 2;
  250: 	}
  251: 
  252: 	if (csVal && AIT_ADDR(&av->cfg_val) && 
  253: 			strcmp((char*) csVal, (char*) AIT_GET_STR(&av->cfg_val))) {
  254: 		/* Update element */
  255: 		AIT_FREE_VAL(&av->cfg_val);
  256: 		AIT_SET_STR(&av->cfg_val, csVal);
  257: 		return 1;
  258: 	}
  259: 
  260: 	/* Nothing happens ... found & values is equal! */
  261: 	return 0;
  262: }
  263: 
  264: /*
  265:  * cfg_getAttribute() - Get item from config and return value from it
  266:  *
  267:  * @cfg = Config root
  268:  * @csSec = Config section //[{csSec}], if NULL unset in *default* section
  269:  * @csAttr = Config attribute //{csAttr} = ..., if NULL as *any* attribute
  270:  * return: NULL item not found or null parameters, !=NULL value const string
  271:  */
  272: const char *
  273: cfg_getAttribute(cfg_root_t * __restrict cfg, const char *csSec, const char *csAttr)
  274: {
  275: 	struct tagCfg *av;
  276: 
  277: 	if (!cfg)
  278: 		return NULL;
  279: 
  280: 	av = _selectAttribute(cfg, csSec, csAttr);
  281: 	if (!av)
  282: 		return NULL;
  283: 
  284: 	return AIT_GET_STR(&av->cfg_val);
  285: }
  286: 
  287: /*
  288:  * cfg_getAttributeLong() - Get item as long from config and return value from it
  289:  *
  290:  * @cfg = Config root
  291:  * @csSec = Config section //[{csSec}], if NULL unset in *default* section
  292:  * @csAttr = Config attribute //{csAttr} = ..., if NULL as *any* attribute
  293:  * return: value
  294:  */
  295: long
  296: cfg_getAttributeLong(cfg_root_t * __restrict cfg, const char *csSec, const char *csAttr)
  297: {
  298: 	const char *str = NULL;
  299: 
  300: 	str = cfg_getAttribute(cfg, csSec, csAttr);
  301: 	return strtol(str ? str : "", NULL, 0);
  302: }
  303: 
  304: /*
  305:  * cfg_getAttributeLLong() - Get item as long long from config and return value from it
  306:  *
  307:  * @cfg = Config root
  308:  * @csSec = Config section //[{csSec}], if NULL unset in *default* section
  309:  * @csAttr = Config attribute //{csAttr} = ..., if NULL as *any* attribute
  310:  * return: value
  311:  */
  312: long long
  313: cfg_getAttributeLLong(cfg_root_t * __restrict cfg, const char *csSec, const char *csAttr)
  314: {
  315: 	const char *str = NULL;
  316: 
  317: 	str = cfg_getAttribute(cfg, csSec, csAttr);
  318: 	return strtoll(str ? str : "", NULL, 0);
  319: }
  320: 
  321: /*
  322:  * cfg_getAttributeDouble() - Get item as double from config and return value from it
  323:  *
  324:  * @cfg = Config root
  325:  * @csSec = Config section //[{csSec}], if NULL unset in *default* section
  326:  * @csAttr = Config attribute //{csAttr} = ..., if NULL as *any* attribute
  327:  * return: value
  328:  */
  329: double
  330: cfg_getAttributeDouble(cfg_root_t * __restrict cfg, const char *csSec, const char *csAttr)
  331: {
  332: 	const char *str = NULL;
  333: 
  334: 	str = cfg_getAttribute(cfg, csSec, csAttr);
  335: 	return strtod(str ? str : "", NULL);
  336: }
  337: 
  338: /*
  339:  * cfg_getAttributeLDouble() - Get item as long double from config and return value from it
  340:  *
  341:  * @cfg = Config root
  342:  * @csSec = Config section //[{csSec}], if NULL unset in *default* section
  343:  * @csAttr = Config attribute //{csAttr} = ..., if NULL as *any* attribute
  344:  * return: value
  345:  */
  346: long double
  347: cfg_getAttributeLDouble(cfg_root_t * __restrict cfg, const char *csSec, const char *csAttr)
  348: {
  349: 	const char *str = NULL;
  350: 
  351: 	str = cfg_getAttribute(cfg, csSec, csAttr);
  352: 	return strtold(str ? str : "", NULL);
  353: }
  354: 
  355: /*
  356:  * cfg_loadAttribute() - Get guarded attribute, if not found item return *default value*
  357:  *
  358:  * @cfg = Config root
  359:  * @csSec = Config section //[{csSec}], if NULL unset in *default* section
  360:  * @csAttr = Config attribute //{csAttr} = ..., if NULL as *any* attribute
  361:  * @val = Return buffer for item Value //... = {val}
  362:  * @csDefValue = *Default Value* for return in //{val}, if not found item in config
  363:  * return: 0 item not found, -1 error or >0 number of copied bytes in //{val}
  364:  */
  365: int
  366: cfg_loadAttribute(cfg_root_t * __restrict cfg, const char *csSec, const char *csAttr, 
  367: 		ait_val_t * __restrict val, const char *csDefValue)
  368: {
  369: 	struct tagCfg *av;
  370: 	int ret = 0;
  371: 
  372: 	if (!cfg || !val) {
  373: 		cfg_SetErr(EINVAL, "Invalid argument(s)");
  374: 		return -1;
  375: 	}
  376: 
  377: 	AIT_INIT_VAL(val);
  378: 	av = _selectAttribute(cfg, csSec, csAttr);
  379: 	if (!av) {
  380: 		/* not found item */
  381: 		if (csDefValue) {
  382: 			AIT_SET_STR(val, csDefValue);
  383: 			ret = AIT_LEN(val);
  384: 		} else
  385: 			AIT_INIT_VAL(val);
  386: 		return ret;
  387: 	}
  388: 
  389: 	if (AIT_ISEMPTY(&av->cfg_val) || !AIT_ADDR(&av->cfg_val) || 
  390: 			!*AIT_GET_LIKE(&av->cfg_val, char*)) {
  391: 		/* empty value */
  392: 		if (csDefValue) {
  393: 			AIT_SET_STR(val, csDefValue);
  394: 			ret = AIT_LEN(val);
  395: 		} else
  396: 			AIT_INIT_VAL(val);
  397: 	} else {
  398: 		/* copy value */
  399: 		AIT_SET_STR(val, AIT_GET_STR(&av->cfg_val));
  400: 		ret = AIT_LEN(val);
  401: 	}
  402: 
  403: 	return ret;
  404: }

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