Annotation of libaitcfg/src/queue.c, revision 1.16.8.1
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.16.8.1! misho 6: * $Id: queue.c,v 1.16 2014/04/14 19:58:21 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.16.8.1! misho 15: Copyright 2004 - 2019
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.10 misho 52: struct tagCfg fav, *c;
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:
66: if (!csAttr)
67: return RB_NFIND(tagRC, cfg, &fav);
1.10 misho 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! */
1.15 misho 75: } while ((c = RB_NEXT(tagRC, cfg, c)) && c && !cfg_tree_cmp(c, &fav));
1.10 misho 76: return NULL; /* not found */
77: }
1.1 misho 78: }
79:
1.7 misho 80: /* --------------------------------------------------------------- */
1.1 misho 81:
82: /*
1.15 misho 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:
1.16.8.1! misho 107: arr = array_Init(0);
1.15 misho 108: if (!arr) {
109: cfg_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
110: return NULL;
1.16.8.1! misho 111: }
1.15 misho 112:
1.16.8.1! misho 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: }
1.15 misho 123: }
124:
1.16.8.1! misho 125: if (!array_Size(arr))
! 126: array_Destroy(&arr);
1.15 misho 127:
128: return arr;
129: }
130:
131: /*
1.7 misho 132: * cfg_findAttribute() - Find attribute position in config file
133: *
134: * @cfg = Config root
1.1 misho 135: * @csSec = Config section //[{csSec}]
1.15 misho 136: * @csAttr = Config attribute //{csAttr} = ..., if NULL as *any* attribute
1.7 misho 137: * return: 0 not found item, -1 error or >0 position in list
138: */
1.12 misho 139: int
1.7 misho 140: cfg_findAttribute(cfg_root_t * __restrict cfg, const char *csSec, const char *csAttr)
1.1 misho 141: {
1.7 misho 142: struct tagCfg *av, fav;
1.1 misho 143: register int cx = 0;
144:
1.15 misho 145: if (!cfg) {
1.7 misho 146: cfg_SetErr(EINVAL, "Invalid argument(s)");
1.1 misho 147: return -1;
1.7 misho 148: } else
149: memset(&fav, 0, sizeof fav);
1.1 misho 150:
1.7 misho 151: if (csSec && *csSec)
152: AIT_KEY(&fav.cfg_sec) = crcFletcher16((u_short*) csSec,
1.12 misho 153: E_ALIGN(strlen(csSec), 2) / 2);
1.7 misho 154: if (csAttr)
155: AIT_KEY(&fav.cfg_attr) = crcFletcher16((u_short*) csAttr,
1.12 misho 156: E_ALIGN(strlen(csAttr), 2) / 2);
1.7 misho 157:
1.14 misho 158: TAILQ_FOREACH(av, cfg, cfg_next) {
1.1 misho 159: ++cx;
1.7 misho 160: if (!cfg_tree_cmp(&fav, av))
161: return cx;
1.1 misho 162: }
163:
164: return 0;
165: }
166:
167: /*
1.7 misho 168: * cfg_unsetAttribute() - Unset item from config and free resources
169: *
170: * @cfg = Config root
1.1 misho 171: * @csSec = Config section //[{csSec}], if NULL unset in *default* section
1.15 misho 172: * @csAttr = Config attribute //{csAttr} = ..., if NULL as *any* attribute
1.7 misho 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)
1.1 misho 177: {
1.7 misho 178: struct tagCfg *av;
1.1 misho 179:
1.15 misho 180: if (!cfg)
1.1 misho 181: return -1;
182:
1.7 misho 183: av = _selectAttribute(cfg, csSec, csAttr);
1.1 misho 184: if (!av)
185: return 0;
186:
1.7 misho 187: CFG_RC_LOCK(cfg);
188: RB_REMOVE(tagRC, cfg, av);
1.14 misho 189: TAILQ_REMOVE(cfg, av, cfg_next);
1.7 misho 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);
1.12 misho 195: e_free(av);
1.7 misho 196: return 1;
1.1 misho 197: }
198:
199: /*
1.7 misho 200: * cfg_setAttribute() - Set item in config or adding new item if not exists
201: *
202: * @cfg = Config root
1.1 misho 203: * @csSec = Config section //[{csSec}], if NULL set in *default* section
1.7 misho 204: * @csAttr = Config attribute //{csAttr} = ...
1.1 misho 205: * @csVal = Config value //... = {csVal} to setup
1.7 misho 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)
1.1 misho 210: {
1.7 misho 211: struct tagCfg *av, *section;
1.1 misho 212:
213: if (!cfg || !csAttr)
214: return -1;
215:
1.7 misho 216: av = _selectAttribute(cfg, csSec, csAttr);
1.1 misho 217: if (!av) {
1.7 misho 218: /* adding new element */
219: section = _selectAttribute(cfg, csSec, NULL);
1.1 misho 220:
1.12 misho 221: av = e_malloc(sizeof(struct tagCfg));
1.1 misho 222: if (!av) {
223: LOGERR;
224: return -1;
225: } else {
1.7 misho 226: memset(av, 0, sizeof(struct tagCfg));
1.1 misho 227:
1.7 misho 228: CFG_RC_LOCK(cfg);
229: if (!section)
1.14 misho 230: TAILQ_INSERT_TAIL(cfg, av, cfg_next);
1.7 misho 231: else
1.14 misho 232: TAILQ_INSERT_BEFORE(section, av, cfg_next);
1.7 misho 233: CFG_RC_UNLOCK(cfg);
1.1 misho 234: }
1.7 misho 235:
1.1 misho 236: if (csSec && *csSec) {
1.7 misho 237: AIT_SET_STR(&av->cfg_sec, csSec);
238: AIT_KEY(&av->cfg_sec) = crcFletcher16(AIT_GET_LIKE(&av->cfg_sec, u_short*),
1.12 misho 239: E_ALIGN(AIT_LEN(&av->cfg_sec) - 1, 2) / 2);
1.1 misho 240: }
1.7 misho 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*),
1.12 misho 244: E_ALIGN(AIT_LEN(&av->cfg_attr) - 1, 2) / 2);
1.7 misho 245:
246: CFG_RC_LOCK(cfg);
247: RB_INSERT(tagRC, cfg, av);
248: CFG_RC_UNLOCK(cfg);
1.1 misho 249: return 2;
250: }
251:
1.10 misho 252: if (csVal && AIT_ADDR(&av->cfg_val) &&
253: strcmp((char*) csVal, (char*) AIT_GET_STR(&av->cfg_val))) {
1.7 misho 254: /* Update element */
255: AIT_FREE_VAL(&av->cfg_val);
256: AIT_SET_STR(&av->cfg_val, csVal);
1.1 misho 257: return 1;
258: }
259:
1.7 misho 260: /* Nothing happens ... found & values is equal! */
1.1 misho 261: return 0;
262: }
263:
264: /*
1.7 misho 265: * cfg_getAttribute() - Get item from config and return value from it
266: *
267: * @cfg = Config root
1.1 misho 268: * @csSec = Config section //[{csSec}], if NULL unset in *default* section
1.15 misho 269: * @csAttr = Config attribute //{csAttr} = ..., if NULL as *any* attribute
1.7 misho 270: * return: NULL item not found or null parameters, !=NULL value const string
271: */
1.12 misho 272: const char *
1.7 misho 273: cfg_getAttribute(cfg_root_t * __restrict cfg, const char *csSec, const char *csAttr)
1.1 misho 274: {
1.7 misho 275: struct tagCfg *av;
1.1 misho 276:
1.15 misho 277: if (!cfg)
1.1 misho 278: return NULL;
279:
1.7 misho 280: av = _selectAttribute(cfg, csSec, csAttr);
1.1 misho 281: if (!av)
282: return NULL;
283:
1.7 misho 284: return AIT_GET_STR(&av->cfg_val);
1.4 misho 285: }
286:
1.1 misho 287: /*
1.16 misho 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: /*
1.7 misho 356: * cfg_loadAttribute() - Get guarded attribute, if not found item return *default value*
357: *
358: * @cfg = Config root
1.1 misho 359: * @csSec = Config section //[{csSec}], if NULL unset in *default* section
1.15 misho 360: * @csAttr = Config attribute //{csAttr} = ..., if NULL as *any* attribute
1.7 misho 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)
1.1 misho 368: {
1.7 misho 369: struct tagCfg *av;
1.1 misho 370: int ret = 0;
371:
1.15 misho 372: if (!cfg || !val) {
1.7 misho 373: cfg_SetErr(EINVAL, "Invalid argument(s)");
1.1 misho 374: return -1;
1.7 misho 375: }
1.1 misho 376:
1.9 misho 377: AIT_INIT_VAL(val);
1.7 misho 378: av = _selectAttribute(cfg, csSec, csAttr);
1.1 misho 379: if (!av) {
1.7 misho 380: /* not found item */
1.1 misho 381: if (csDefValue) {
1.7 misho 382: AIT_SET_STR(val, csDefValue);
383: ret = AIT_LEN(val);
1.8 misho 384: } else
385: AIT_INIT_VAL(val);
1.1 misho 386: return ret;
387: }
388:
1.10 misho 389: if (AIT_ISEMPTY(&av->cfg_val) || !AIT_ADDR(&av->cfg_val) ||
390: !*AIT_GET_LIKE(&av->cfg_val, char*)) {
1.7 misho 391: /* empty value */
1.1 misho 392: if (csDefValue) {
1.7 misho 393: AIT_SET_STR(val, csDefValue);
394: ret = AIT_LEN(val);
1.8 misho 395: } else
396: AIT_INIT_VAL(val);
1.1 misho 397: } else {
1.7 misho 398: /* copy value */
399: AIT_SET_STR(val, AIT_GET_STR(&av->cfg_val));
400: ret = AIT_LEN(val);
1.1 misho 401: }
402:
403: return ret;
404: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>