Annotation of libaitcfg/src/queue.c, revision 1.19
1.2 misho 1: /*************************************************************************
2: * (C) 2008 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
3: * by Michael Pounov <misho@openbsd-bg.org>
4: *
5: * $Author: misho $
1.19 ! misho 6: * $Id: queue.c,v 1.18.2.1 2021/11/26 01:10:34 misho Exp $
1.2 misho 7: *
1.6 misho 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:
1.19 ! misho 15: Copyright 2004 - 2021
1.6 misho 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: */
1.1 misho 46: #include "global.h"
47:
48:
1.7 misho 49: static inline struct tagCfg *
50: _selectAttribute(cfg_root_t * __restrict cfg, const char *csSec, const char *csAttr)
1.1 misho 51: {
1.19 ! misho 52: struct tagCfg fav, *c, *n;
1.1 misho 53:
54: if (!cfg)
55: return NULL;
1.7 misho 56: else
57: memset(&fav, 0, sizeof fav);
1.1 misho 58:
1.7 misho 59: if (csSec && *csSec)
60: AIT_KEY(&fav.cfg_sec) = crcFletcher16((u_short*) csSec,
1.12 misho 61: E_ALIGN(strlen(csSec), 2) / 2);
1.7 misho 62: if (csAttr)
63: AIT_KEY(&fav.cfg_attr) = crcFletcher16((u_short*) csAttr,
1.12 misho 64: E_ALIGN(strlen(csAttr), 2) / 2);
1.7 misho 65:
1.19 ! misho 66: if (!csAttr) {
! 67: c = RB_NFIND(tagRC, cfg, &fav);
! 68: if (!c)
! 69: return NULL; /* not found */
! 70: if (csSec && !AIT_ISEMPTY(&c->cfg_sec) &&
! 71: strcmp(csSec, AIT_GET_STR(&c->cfg_sec)))
! 72: TAILQ_FOREACH_SAFE(c, cfg, cfg_next, n) {
! 73: if (!AIT_ISEMPTY(&c->cfg_sec) &&
! 74: !strcmp(csSec, AIT_GET_STR(&c->cfg_sec)))
! 75: break;
! 76: }
! 77: return c;
! 78: } else {
1.10 misho 79: c = RB_FIND(tagRC, cfg, &fav);
80: if (!c)
81: return NULL; /* not found */
1.19 ! misho 82: /* if cannot find right section */
! 83: if (csSec && !AIT_ISEMPTY(&c->cfg_sec) &&
! 84: strcmp(csSec, AIT_GET_STR(&c->cfg_sec))) {
! 85: TAILQ_FOREACH_SAFE(c, cfg, cfg_next, n) {
! 86: if (!AIT_ISEMPTY(&c->cfg_sec) && csAttr &&
! 87: !strcmp(csSec, AIT_GET_STR(&c->cfg_sec)) &&
! 88: !strcmp(csAttr, AIT_GET_STR(&c->cfg_attr)))
! 89: return c; /* FOUND! */
! 90: }
! 91: return NULL; /* not found */
! 92: }
1.10 misho 93: do {
94: if (!strcmp(AIT_GET_STR(&c->cfg_attr), csAttr))
95: return c; /* FOUND! */
1.15 misho 96: } while ((c = RB_NEXT(tagRC, cfg, c)) && c && !cfg_tree_cmp(c, &fav));
1.19 ! misho 97: /* if cannot find right attribute */
! 98: TAILQ_FOREACH_SAFE(c, cfg, cfg_next, n) {
! 99: if ((!csSec && AIT_ISEMPTY(&c->cfg_sec)) ||
! 100: (csSec && !AIT_ISEMPTY(&c->cfg_sec) &&
! 101: !strcmp(csSec, AIT_GET_STR(&c->cfg_sec))))
! 102: if (!AIT_ISEMPTY(&c->cfg_attr) && csAttr &&
! 103: !strcmp(csAttr, AIT_GET_STR(&c->cfg_attr)))
! 104: return c; /* FOUND! */
! 105: }
1.10 misho 106: return NULL; /* not found */
107: }
1.1 misho 108: }
109:
1.7 misho 110: /* --------------------------------------------------------------- */
1.18 misho 111:
112: /*
113: * cfg_dumpCfg() - dump config data
114: *
115: * @cfg = Config root
116: * return: none
117: */
118: void
119: cfg_dumpCfg(cfg_root_t * __restrict cfg)
120: {
121: struct tagCfg *r, *c, *ctmp, *q, *qtmp;
122: int syn;
123:
124: r = RB_ROOT(cfg);
125: printf("ROOT:: KEY=%.8x [%s] %s=%s\n", ((AIT_KEY(&r->cfg_sec) << 15) | AIT_KEY(&r->cfg_attr)),
126: AIT_ADDR(&r->cfg_sec), AIT_ADDR(&r->cfg_attr), AIT_ADDR(&r->cfg_val));
127:
128: RB_FOREACH_SAFE(c, tagRC, cfg, ctmp) {
129: syn ^= syn;
130: TAILQ_FOREACH_SAFE(q, cfg, cfg_next, qtmp) {
131: if (c == q) {
132: syn = 42;
133: break;
134: }
135: }
136:
137: printf("%s KEY=%.8x [%s] %s=%s sync=%d\n", c == r ? "*" : "",
138: ((AIT_KEY(&c->cfg_sec) << 15) | AIT_KEY(&c->cfg_attr)),
139: AIT_ADDR(&c->cfg_sec), AIT_ADDR(&c->cfg_attr), AIT_ADDR(&c->cfg_val), syn);
140: }
141: }
1.1 misho 142:
143: /*
1.15 misho 144: * cfg_getSection() - Get entire section attributes into array
145: *
146: * @cfg = Config root
147: * @csSec = Config section //[{csSec}]
148: * return: NULL not found or !=NULL allocated array, must free with array_Destroy() after use!
149: */
150: array_t *
151: cfg_getSection(cfg_root_t * __restrict cfg, const char *csSec)
152: {
153: array_t *arr = NULL;
154: struct tagCfg *av, fav;
155:
156: if (!cfg) {
157: cfg_SetErr(EINVAL, "Invalid argument(s)");
158: return NULL;
159: } else
160: memset(&fav, 0, sizeof fav);
161: if (csSec && !*csSec)
162: csSec = NULL;
163:
164: if (csSec && *csSec)
165: AIT_KEY(&fav.cfg_sec) = crcFletcher16((u_short*) csSec,
166: E_ALIGN(strlen(csSec), 2) / 2);
167:
1.17 misho 168: arr = array_Init(0);
1.15 misho 169: if (!arr) {
170: cfg_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
171: return NULL;
1.17 misho 172: }
1.15 misho 173:
1.17 misho 174: TAILQ_FOREACH(av, cfg, cfg_next)
175: if (AIT_KEY(&av->cfg_sec) == AIT_KEY(&fav.cfg_sec)) {
176: if (!csSec) {
177: if (AIT_ISEMPTY(&av->cfg_sec))
178: array_Push(arr, av, 0);
179: } else {
180: if (!AIT_ISEMPTY(&av->cfg_sec) &&
181: !strcmp(AIT_GET_STR(&av->cfg_sec), csSec))
182: array_Push(arr, av, 0);
183: }
1.15 misho 184: }
185:
1.17 misho 186: if (!array_Size(arr))
187: array_Destroy(&arr);
1.15 misho 188:
189: return arr;
190: }
191:
192: /*
1.7 misho 193: * cfg_findAttribute() - Find attribute position in config file
194: *
195: * @cfg = Config root
1.1 misho 196: * @csSec = Config section //[{csSec}]
1.19 ! misho 197: * @csAttr = Config attribute //{csAttr} = ...
1.7 misho 198: * return: 0 not found item, -1 error or >0 position in list
199: */
1.12 misho 200: int
1.7 misho 201: cfg_findAttribute(cfg_root_t * __restrict cfg, const char *csSec, const char *csAttr)
1.1 misho 202: {
1.19 ! misho 203: struct tagCfg *av, *n;
1.1 misho 204: register int cx = 0;
205:
1.15 misho 206: if (!cfg) {
1.7 misho 207: cfg_SetErr(EINVAL, "Invalid argument(s)");
1.1 misho 208: return -1;
1.19 ! misho 209: }
1.1 misho 210:
1.19 ! misho 211: TAILQ_FOREACH_SAFE(av, cfg, cfg_next, n) {
! 212: ++cx;
1.7 misho 213:
1.19 ! misho 214: if ((!csSec && AIT_ISEMPTY(&av->cfg_sec)) ||
! 215: (csSec && !AIT_ISEMPTY(&av->cfg_sec) &&
! 216: !strcmp(csSec, AIT_GET_STR(&av->cfg_sec))))
! 217: if (!AIT_ISEMPTY(&av->cfg_attr) && csAttr &&
! 218: !strcmp(csAttr, AIT_GET_STR(&av->cfg_attr)))
! 219: return cx;
1.1 misho 220: }
221:
222: return 0;
223: }
224:
225: /*
1.7 misho 226: * cfg_unsetAttribute() - Unset item from config and free resources
227: *
228: * @cfg = Config root
1.1 misho 229: * @csSec = Config section //[{csSec}], if NULL unset in *default* section
1.19 ! misho 230: * @csAttr = Config attribute //{csAttr} = ...
1.7 misho 231: * return: 0 item not found, -1 error or 1 removed item
232: */
233: int
234: cfg_unsetAttribute(cfg_root_t * __restrict cfg, const char *csSec, const char *csAttr)
1.1 misho 235: {
1.7 misho 236: struct tagCfg *av;
1.1 misho 237:
1.15 misho 238: if (!cfg)
1.1 misho 239: return -1;
240:
1.7 misho 241: av = _selectAttribute(cfg, csSec, csAttr);
1.1 misho 242: if (!av)
243: return 0;
244:
1.7 misho 245: CFG_RC_LOCK(cfg);
246: RB_REMOVE(tagRC, cfg, av);
1.14 misho 247: TAILQ_REMOVE(cfg, av, cfg_next);
1.7 misho 248: CFG_RC_UNLOCK(cfg);
249:
250: AIT_FREE_VAL(&av->cfg_val);
251: AIT_FREE_VAL(&av->cfg_attr);
252: AIT_FREE_VAL(&av->cfg_sec);
1.12 misho 253: e_free(av);
1.7 misho 254: return 1;
1.1 misho 255: }
256:
257: /*
1.7 misho 258: * cfg_setAttribute() - Set item in config or adding new item if not exists
259: *
260: * @cfg = Config root
1.1 misho 261: * @csSec = Config section //[{csSec}], if NULL set in *default* section
1.7 misho 262: * @csAttr = Config attribute //{csAttr} = ...
1.1 misho 263: * @csVal = Config value //... = {csVal} to setup
1.7 misho 264: * return: 0 nothing changed, -1 error, 1 found and updated item or 2 added new item
265: */
266: int
267: cfg_setAttribute(cfg_root_t * __restrict cfg, const char *csSec, const char *csAttr, const char *csVal)
1.1 misho 268: {
1.7 misho 269: struct tagCfg *av, *section;
1.1 misho 270:
271: if (!cfg || !csAttr)
272: return -1;
273:
1.7 misho 274: av = _selectAttribute(cfg, csSec, csAttr);
1.1 misho 275: if (!av) {
1.7 misho 276: /* adding new element */
277: section = _selectAttribute(cfg, csSec, NULL);
1.1 misho 278:
1.12 misho 279: av = e_malloc(sizeof(struct tagCfg));
1.1 misho 280: if (!av) {
281: LOGERR;
282: return -1;
283: } else {
1.7 misho 284: memset(av, 0, sizeof(struct tagCfg));
1.1 misho 285:
1.7 misho 286: CFG_RC_LOCK(cfg);
287: if (!section)
1.14 misho 288: TAILQ_INSERT_TAIL(cfg, av, cfg_next);
1.7 misho 289: else
1.14 misho 290: TAILQ_INSERT_BEFORE(section, av, cfg_next);
1.7 misho 291: CFG_RC_UNLOCK(cfg);
1.1 misho 292: }
1.7 misho 293:
1.1 misho 294: if (csSec && *csSec) {
1.7 misho 295: AIT_SET_STR(&av->cfg_sec, csSec);
296: AIT_KEY(&av->cfg_sec) = crcFletcher16(AIT_GET_LIKE(&av->cfg_sec, u_short*),
1.12 misho 297: E_ALIGN(AIT_LEN(&av->cfg_sec) - 1, 2) / 2);
1.1 misho 298: }
1.7 misho 299: AIT_SET_STR(&av->cfg_val, csVal ? csVal : "");
300: AIT_SET_STR(&av->cfg_attr, csAttr);
301: AIT_KEY(&av->cfg_attr) = crcFletcher16(AIT_GET_LIKE(&av->cfg_attr, u_short*),
1.12 misho 302: E_ALIGN(AIT_LEN(&av->cfg_attr) - 1, 2) / 2);
1.7 misho 303:
304: CFG_RC_LOCK(cfg);
305: RB_INSERT(tagRC, cfg, av);
306: CFG_RC_UNLOCK(cfg);
1.1 misho 307: return 2;
308: }
309:
1.10 misho 310: if (csVal && AIT_ADDR(&av->cfg_val) &&
311: strcmp((char*) csVal, (char*) AIT_GET_STR(&av->cfg_val))) {
1.7 misho 312: /* Update element */
313: AIT_FREE_VAL(&av->cfg_val);
314: AIT_SET_STR(&av->cfg_val, csVal);
1.1 misho 315: return 1;
316: }
317:
1.7 misho 318: /* Nothing happens ... found & values is equal! */
1.1 misho 319: return 0;
320: }
321:
322: /*
1.7 misho 323: * cfg_getAttribute() - Get item from config and return value from it
324: *
325: * @cfg = Config root
1.1 misho 326: * @csSec = Config section //[{csSec}], if NULL unset in *default* section
1.19 ! misho 327: * @csAttr = Config attribute //{csAttr} = ...
1.7 misho 328: * return: NULL item not found or null parameters, !=NULL value const string
329: */
1.12 misho 330: const char *
1.7 misho 331: cfg_getAttribute(cfg_root_t * __restrict cfg, const char *csSec, const char *csAttr)
1.1 misho 332: {
1.7 misho 333: struct tagCfg *av;
1.1 misho 334:
1.15 misho 335: if (!cfg)
1.1 misho 336: return NULL;
337:
1.7 misho 338: av = _selectAttribute(cfg, csSec, csAttr);
1.1 misho 339: if (!av)
340: return NULL;
341:
1.7 misho 342: return AIT_GET_STR(&av->cfg_val);
1.4 misho 343: }
344:
1.1 misho 345: /*
1.16 misho 346: * cfg_getAttributeLong() - Get item as long from config and return value from it
347: *
348: * @cfg = Config root
349: * @csSec = Config section //[{csSec}], if NULL unset in *default* section
1.19 ! misho 350: * @csAttr = Config attribute //{csAttr} = ...
1.16 misho 351: * return: value
352: */
353: long
354: cfg_getAttributeLong(cfg_root_t * __restrict cfg, const char *csSec, const char *csAttr)
355: {
356: const char *str = NULL;
357:
358: str = cfg_getAttribute(cfg, csSec, csAttr);
359: return strtol(str ? str : "", NULL, 0);
360: }
361:
362: /*
363: * cfg_getAttributeLLong() - Get item as long long from config and return value from it
364: *
365: * @cfg = Config root
366: * @csSec = Config section //[{csSec}], if NULL unset in *default* section
1.19 ! misho 367: * @csAttr = Config attribute //{csAttr} = ...
1.16 misho 368: * return: value
369: */
370: long long
371: cfg_getAttributeLLong(cfg_root_t * __restrict cfg, const char *csSec, const char *csAttr)
372: {
373: const char *str = NULL;
374:
375: str = cfg_getAttribute(cfg, csSec, csAttr);
376: return strtoll(str ? str : "", NULL, 0);
377: }
378:
379: /*
380: * cfg_getAttributeDouble() - Get item as double from config and return value from it
381: *
382: * @cfg = Config root
383: * @csSec = Config section //[{csSec}], if NULL unset in *default* section
1.19 ! misho 384: * @csAttr = Config attribute //{csAttr} = ...
1.16 misho 385: * return: value
386: */
387: double
388: cfg_getAttributeDouble(cfg_root_t * __restrict cfg, const char *csSec, const char *csAttr)
389: {
390: const char *str = NULL;
391:
392: str = cfg_getAttribute(cfg, csSec, csAttr);
393: return strtod(str ? str : "", NULL);
394: }
395:
396: /*
397: * cfg_getAttributeLDouble() - Get item as long double from config and return value from it
398: *
399: * @cfg = Config root
400: * @csSec = Config section //[{csSec}], if NULL unset in *default* section
1.19 ! misho 401: * @csAttr = Config attribute //{csAttr} = ...
1.16 misho 402: * return: value
403: */
404: long double
405: cfg_getAttributeLDouble(cfg_root_t * __restrict cfg, const char *csSec, const char *csAttr)
406: {
407: const char *str = NULL;
408:
409: str = cfg_getAttribute(cfg, csSec, csAttr);
410: return strtold(str ? str : "", NULL);
411: }
412:
413: /*
1.7 misho 414: * cfg_loadAttribute() - Get guarded attribute, if not found item return *default value*
415: *
416: * @cfg = Config root
1.1 misho 417: * @csSec = Config section //[{csSec}], if NULL unset in *default* section
1.19 ! misho 418: * @csAttr = Config attribute //{csAttr} = ...
1.7 misho 419: * @val = Return buffer for item Value //... = {val}
420: * @csDefValue = *Default Value* for return in //{val}, if not found item in config
421: * return: 0 item not found, -1 error or >0 number of copied bytes in //{val}
422: */
423: int
424: cfg_loadAttribute(cfg_root_t * __restrict cfg, const char *csSec, const char *csAttr,
425: ait_val_t * __restrict val, const char *csDefValue)
1.1 misho 426: {
1.7 misho 427: struct tagCfg *av;
1.1 misho 428: int ret = 0;
429:
1.15 misho 430: if (!cfg || !val) {
1.7 misho 431: cfg_SetErr(EINVAL, "Invalid argument(s)");
1.1 misho 432: return -1;
1.7 misho 433: }
1.1 misho 434:
1.9 misho 435: AIT_INIT_VAL(val);
1.7 misho 436: av = _selectAttribute(cfg, csSec, csAttr);
1.1 misho 437: if (!av) {
1.7 misho 438: /* not found item */
1.1 misho 439: if (csDefValue) {
1.7 misho 440: AIT_SET_STR(val, csDefValue);
441: ret = AIT_LEN(val);
1.8 misho 442: } else
443: AIT_INIT_VAL(val);
1.1 misho 444: return ret;
445: }
446:
1.10 misho 447: if (AIT_ISEMPTY(&av->cfg_val) || !AIT_ADDR(&av->cfg_val) ||
448: !*AIT_GET_LIKE(&av->cfg_val, char*)) {
1.7 misho 449: /* empty value */
1.1 misho 450: if (csDefValue) {
1.7 misho 451: AIT_SET_STR(val, csDefValue);
452: ret = AIT_LEN(val);
1.8 misho 453: } else
454: AIT_INIT_VAL(val);
1.1 misho 455: } else {
1.7 misho 456: /* copy value */
457: AIT_SET_STR(val, AIT_GET_STR(&av->cfg_val));
458: ret = AIT_LEN(val);
1.1 misho 459: }
460:
461: return ret;
462: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>