File:
[ELWIX - Embedded LightWeight unIX -] /
libaitcfg /
src /
queue.c
Revision
1.19:
download - view:
text,
annotated -
select for diffs -
revision graph
Fri Nov 26 01:15:03 2021 UTC (2 years, 8 months ago) by
misho
Branches:
MAIN
CVS tags:
cfg9_5,
cfg9_4,
cfg9_3,
cfg9_2,
cfg9_1,
HEAD,
CFG9_4,
CFG9_3,
CFG9_2,
CFG9_1,
CFG9_0
version 9.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.19 2021/11/26 01:15:03 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 - 2021
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, *n;
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: 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 {
79: c = RB_FIND(tagRC, cfg, &fav);
80: if (!c)
81: return NULL; /* not found */
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: }
93: do {
94: if (!strcmp(AIT_GET_STR(&c->cfg_attr), csAttr))
95: return c; /* FOUND! */
96: } while ((c = RB_NEXT(tagRC, cfg, c)) && c && !cfg_tree_cmp(c, &fav));
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: }
106: return NULL; /* not found */
107: }
108: }
109:
110: /* --------------------------------------------------------------- */
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: }
142:
143: /*
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:
168: arr = array_Init(0);
169: if (!arr) {
170: cfg_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
171: return NULL;
172: }
173:
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: }
184: }
185:
186: if (!array_Size(arr))
187: array_Destroy(&arr);
188:
189: return arr;
190: }
191:
192: /*
193: * cfg_findAttribute() - Find attribute position in config file
194: *
195: * @cfg = Config root
196: * @csSec = Config section //[{csSec}]
197: * @csAttr = Config attribute //{csAttr} = ...
198: * return: 0 not found item, -1 error or >0 position in list
199: */
200: int
201: cfg_findAttribute(cfg_root_t * __restrict cfg, const char *csSec, const char *csAttr)
202: {
203: struct tagCfg *av, *n;
204: register int cx = 0;
205:
206: if (!cfg) {
207: cfg_SetErr(EINVAL, "Invalid argument(s)");
208: return -1;
209: }
210:
211: TAILQ_FOREACH_SAFE(av, cfg, cfg_next, n) {
212: ++cx;
213:
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;
220: }
221:
222: return 0;
223: }
224:
225: /*
226: * cfg_unsetAttribute() - Unset item from config and free resources
227: *
228: * @cfg = Config root
229: * @csSec = Config section //[{csSec}], if NULL unset in *default* section
230: * @csAttr = Config attribute //{csAttr} = ...
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)
235: {
236: struct tagCfg *av;
237:
238: if (!cfg)
239: return -1;
240:
241: av = _selectAttribute(cfg, csSec, csAttr);
242: if (!av)
243: return 0;
244:
245: CFG_RC_LOCK(cfg);
246: RB_REMOVE(tagRC, cfg, av);
247: TAILQ_REMOVE(cfg, av, cfg_next);
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);
253: e_free(av);
254: return 1;
255: }
256:
257: /*
258: * cfg_setAttribute() - Set item in config or adding new item if not exists
259: *
260: * @cfg = Config root
261: * @csSec = Config section //[{csSec}], if NULL set in *default* section
262: * @csAttr = Config attribute //{csAttr} = ...
263: * @csVal = Config value //... = {csVal} to setup
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)
268: {
269: struct tagCfg *av, *section;
270:
271: if (!cfg || !csAttr)
272: return -1;
273:
274: av = _selectAttribute(cfg, csSec, csAttr);
275: if (!av) {
276: /* adding new element */
277: section = _selectAttribute(cfg, csSec, NULL);
278:
279: av = e_malloc(sizeof(struct tagCfg));
280: if (!av) {
281: LOGERR;
282: return -1;
283: } else {
284: memset(av, 0, sizeof(struct tagCfg));
285:
286: CFG_RC_LOCK(cfg);
287: if (!section)
288: TAILQ_INSERT_TAIL(cfg, av, cfg_next);
289: else
290: TAILQ_INSERT_BEFORE(section, av, cfg_next);
291: CFG_RC_UNLOCK(cfg);
292: }
293:
294: if (csSec && *csSec) {
295: AIT_SET_STR(&av->cfg_sec, csSec);
296: AIT_KEY(&av->cfg_sec) = crcFletcher16(AIT_GET_LIKE(&av->cfg_sec, u_short*),
297: E_ALIGN(AIT_LEN(&av->cfg_sec) - 1, 2) / 2);
298: }
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*),
302: E_ALIGN(AIT_LEN(&av->cfg_attr) - 1, 2) / 2);
303:
304: CFG_RC_LOCK(cfg);
305: RB_INSERT(tagRC, cfg, av);
306: CFG_RC_UNLOCK(cfg);
307: return 2;
308: }
309:
310: if (csVal && AIT_ADDR(&av->cfg_val) &&
311: strcmp((char*) csVal, (char*) AIT_GET_STR(&av->cfg_val))) {
312: /* Update element */
313: AIT_FREE_VAL(&av->cfg_val);
314: AIT_SET_STR(&av->cfg_val, csVal);
315: return 1;
316: }
317:
318: /* Nothing happens ... found & values is equal! */
319: return 0;
320: }
321:
322: /*
323: * cfg_getAttribute() - Get item from config and return value from it
324: *
325: * @cfg = Config root
326: * @csSec = Config section //[{csSec}], if NULL unset in *default* section
327: * @csAttr = Config attribute //{csAttr} = ...
328: * return: NULL item not found or null parameters, !=NULL value const string
329: */
330: const char *
331: cfg_getAttribute(cfg_root_t * __restrict cfg, const char *csSec, const char *csAttr)
332: {
333: struct tagCfg *av;
334:
335: if (!cfg)
336: return NULL;
337:
338: av = _selectAttribute(cfg, csSec, csAttr);
339: if (!av)
340: return NULL;
341:
342: return AIT_GET_STR(&av->cfg_val);
343: }
344:
345: /*
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
350: * @csAttr = Config attribute //{csAttr} = ...
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
367: * @csAttr = Config attribute //{csAttr} = ...
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
384: * @csAttr = Config attribute //{csAttr} = ...
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
401: * @csAttr = Config attribute //{csAttr} = ...
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: /*
414: * cfg_loadAttribute() - Get guarded attribute, if not found item return *default value*
415: *
416: * @cfg = Config root
417: * @csSec = Config section //[{csSec}], if NULL unset in *default* section
418: * @csAttr = Config attribute //{csAttr} = ...
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)
426: {
427: struct tagCfg *av;
428: int ret = 0;
429:
430: if (!cfg || !val) {
431: cfg_SetErr(EINVAL, "Invalid argument(s)");
432: return -1;
433: }
434:
435: AIT_INIT_VAL(val);
436: av = _selectAttribute(cfg, csSec, csAttr);
437: if (!av) {
438: /* not found item */
439: if (csDefValue) {
440: AIT_SET_STR(val, csDefValue);
441: ret = AIT_LEN(val);
442: } else
443: AIT_INIT_VAL(val);
444: return ret;
445: }
446:
447: if (AIT_ISEMPTY(&av->cfg_val) || !AIT_ADDR(&av->cfg_val) ||
448: !*AIT_GET_LIKE(&av->cfg_val, char*)) {
449: /* empty value */
450: if (csDefValue) {
451: AIT_SET_STR(val, csDefValue);
452: ret = AIT_LEN(val);
453: } else
454: AIT_INIT_VAL(val);
455: } else {
456: /* copy value */
457: AIT_SET_STR(val, AIT_GET_STR(&av->cfg_val));
458: ret = AIT_LEN(val);
459: }
460:
461: return ret;
462: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>