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