Annotation of libaitcfg/src/queue.c, revision 1.6.4.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.6.4.1 ! misho 6: * $Id: queue.c,v 1.6 2011/05/01 17:24:28 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.6.4.1 ! misho 15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
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.6.4.1 ! misho 49: static inline struct tagCfg *
! 50: _selectAttribute(cfg_root_t * __restrict cfg, const char *csSec, const char *csAttr)
1.1 misho 51: {
1.6.4.1 ! misho 52: struct tagCfg fav;
! 53: struct tagCfg *av;
1.1 misho 54:
55: if (!cfg)
56: return NULL;
1.6.4.1 ! misho 57: else
! 58: memset(&fav, 0, sizeof fav);
1.1 misho 59:
1.6.4.1 ! misho 60: if (csSec && *csSec)
! 61: AIT_KEY(&fav.cfg_sec) = crcFletcher16((u_short*) csSec,
! 62: io_align(strlen(csSec), 1) / 2);
! 63: if (csAttr)
! 64: AIT_KEY(&fav.cfg_attr) = crcFletcher16((u_short*) csAttr,
! 65: io_align(strlen(csAttr), 1) / 2);
! 66:
! 67: /*
! 68: RB_FOREACH(av, tagRC, cfg) {
! 69: printf("sec=%s(%d) attr=%s(%d) val=%s\n", AIT_GET_LIKE(&av->cfg_sec, char*), AIT_KEY(&av->cfg_sec),
! 70: AIT_GET_LIKE(&av->cfg_attr, char*), AIT_KEY(&av->cfg_attr), AIT_GET_STR(&av->cfg_val));
1.1 misho 71: }
72:
1.6.4.1 ! misho 73: printf("ssss=%d aaaa=%d\n", AIT_KEY(&fav.cfg_sec), AIT_KEY(&fav.cfg_attr));
! 74: */
! 75: if (!csAttr)
! 76: return RB_NFIND(tagRC, cfg, &fav);
! 77: else
! 78: return RB_FIND(tagRC, cfg, &fav);
1.1 misho 79: }
80:
1.6.4.1 ! misho 81: static inline void
! 82: _destroyAttribute(struct tagCfg *av)
1.1 misho 83: {
1.6.4.1 ! misho 84: if (!av)
1.1 misho 85: return;
86:
1.6.4.1 ! misho 87: AIT_FREE_VAL(&av->cfg_val);
! 88: AIT_FREE_VAL(&av->cfg_attr);
! 89: AIT_FREE_VAL(&av->cfg_sec);
! 90: free(av);
1.1 misho 91: }
92:
93: // ----------------------------------------------
94:
95: /*
1.6.4.1 ! misho 96: * cfg_findAttribute() - Find attribute position in config file
! 97: *
! 98: * @cfg = Config root
1.1 misho 99: * @csSec = Config section //[{csSec}]
100: * @csAttr = Config attribute //{csAttr} = ...
101: * return: 0 not found item; -1 error: null parameters; >0 position in list
1.6.4.1 ! misho 102: */
! 103: inline int
! 104: cfg_findAttribute(cfg_root_t * __restrict cfg, const char *csSec, const char *csAttr)
1.1 misho 105: {
1.6.4.1 ! misho 106: struct tagCfg *av, fav;
1.1 misho 107: register int cx = 0;
108:
1.6.4.1 ! misho 109: if (!cfg || !csAttr) {
! 110: cfg_SetErr(EINVAL, "Invalid argument(s)");
1.1 misho 111: return -1;
1.6.4.1 ! misho 112: } else
! 113: memset(&fav, 0, sizeof fav);
1.1 misho 114:
1.6.4.1 ! misho 115: if (csSec && *csSec)
! 116: AIT_KEY(&fav.cfg_sec) = crcFletcher16((u_short*) csSec,
! 117: io_align(strlen(csSec), 1) / 2);
! 118: if (csAttr)
! 119: AIT_KEY(&fav.cfg_attr) = crcFletcher16((u_short*) csAttr,
! 120: io_align(strlen(csAttr), 1) / 2);
! 121:
! 122: SLIST_FOREACH(av, cfg, cfg_next) {
1.1 misho 123: ++cx;
1.6.4.1 ! misho 124: if (!cfg_tree_cmp(&fav, av))
! 125: return cx;
1.1 misho 126: }
127:
128: return 0;
129: }
1.6.4.1 ! misho 130: #if 0
1.1 misho 131: /*
132: * cfg_UnsetAttribute() Unset item from config list and free resources
133: * @cfg = Head list element
134: * @csSec = Config section //[{csSec}], if NULL unset in *default* section
135: * @csAttr = Config attribute //{csAttr} = ..., if NULL unset as *any* attribute
136: * return: 0 item not found, -1 error: null parameters; >0 position in list
137: */
138: int cfg_UnsetAttribute(sl_config * __restrict cfg, const u_char *csSec, const u_char *csAttr)
139: {
140: struct tagPair *av, *curr;
141: register int cx = 0;
142:
143: if (!cfg || !csAttr)
144: return -1;
145:
146: av = SelectAttribute(cfg, csSec, csAttr);
147: if (!av)
148: return 0;
149:
150: // remove element
151: // remove element when is first!
152: if (cfg->slh_first == av) {
153: cfg->slh_first = cfg->slh_first->sle_next;
154:
155: DestroyAttribute(av);
156: return 1;
157: }
158: // remove element in other cases...
159: curr = cfg->slh_first;
160: while (curr->sle_next != av) {
161: ++cx;
162: curr = curr->sle_next;
163: }
164: curr->sle_next = curr->sle_next->sle_next;
165:
166: DestroyAttribute(av);
167: return cx;
168: }
169:
170: /*
171: * cfg_SetAttribute() Set item in config list or add new item if not exists
172: * @cfg = Head list element
173: * @csSec = Config section //[{csSec}], if NULL set in *default* section
174: * @csAttr = Config attribute //{csAttr} = ..., if NULL set as *any* attribute
175: * @csVal = Config value //... = {csVal} to setup
176: * return: 0 nothing changed, -1 error: not enough memory; 1 find and update item; 2 added new item
177: */
178: int cfg_SetAttribute(sl_config * __restrict cfg, const u_char *csSec, const u_char *csAttr, const u_char *csVal)
179: {
180: struct tagPair *av, *section;
1.3 misho 181: int len;
1.1 misho 182:
183: if (!cfg || !csAttr)
184: return -1;
185:
186: av = SelectAttribute(cfg, csSec, csAttr);
187: if (!av) {
188: section = SelectAttribute(cfg, csSec, NULL);
189:
190: av = malloc(sizeof(struct tagPair));
191: if (!av) {
192: LOGERR;
193: return -1;
194: } else {
195: memset(av, 0, sizeof(struct tagPair));
196:
197: if (!section) {
198: // add new element
199: av->sle_next = cfg->slh_first;
200: cfg->slh_first = av;
201: } else {
202: // add new element in existing section
203: av->sle_next = section->sle_next;
204: section->sle_next = av;
205: }
206: }
207: // added section name to element
208: if (csSec && *csSec) {
1.3 misho 209: len = strlen((char*) csSec) + 1;
210: av->psSection = malloc(len);
1.1 misho 211: if (!av->psSection) {
212: LOGERR;
213: free(av);
214: return -1;
215: } else {
1.3 misho 216: strlcpy((char*) av->psSection, (char*) csSec, len);
1.1 misho 217: }
218: } else
219: av->psSection = NULL;
220:
221: // added attribute to element
1.3 misho 222: len = strlen((char*) csAttr) + 1;
223: av->psAttribute = malloc(len);
1.1 misho 224: if (!av->psAttribute) {
225: LOGERR;
226: free(av->psSection);
227: free(av);
228: return -1;
229: } else {
1.3 misho 230: strlcpy((char*) av->psAttribute, (char*) csAttr, len);
1.1 misho 231: }
232: // added value to element
233: if (csVal && *csVal) {
1.3 misho 234: len = strlen((char*) csVal) + 1;
235: av->psValue = malloc(len);
1.1 misho 236: if (!av->psValue) {
237: LOGERR;
238: free(av->psAttribute);
239: free(av->psSection);
240: free(av);
241: return -1;
242: } else {
1.3 misho 243: strlcpy((char*) av->psValue, (char*) csVal, len);
1.1 misho 244: }
245: } else {
246: av->psValue = malloc(1);
247: *av->psValue = 0;
248: }
249:
250: // Added new element
251: return 2;
252: }
253:
254: if (strcmp((char*) csVal, (char*) av->psValue)) {
1.3 misho 255: len = strlen((char*) csVal) + 1;
256: av->psValue = realloc(av->psValue, len);
257: strlcpy((char*) av->psValue, (char*) csVal, len);
1.1 misho 258:
259: // Update element
260: return 1;
261: }
262:
263: // Nothing happens ... finded & values is equal!
264: return 0;
265: }
266:
1.6.4.1 ! misho 267: #endif
! 268:
1.1 misho 269: /*
1.6.4.1 ! misho 270: * cfg_getAttribute() - Get item from config and return value from it
! 271: *
! 272: * @cfg = Config root
1.1 misho 273: * @csSec = Config section //[{csSec}], if NULL unset in *default* section
274: * @csAttr = Config attribute //{csAttr} = ..., if NULL unset as *any* attribute
275: * return: NULL item not found or null parameters; !=NULL value const string
1.6.4.1 ! misho 276: */
! 277: inline const char *
! 278: cfg_getAttribute(cfg_root_t * __restrict cfg, const char *csSec, const char *csAttr)
1.1 misho 279: {
1.6.4.1 ! misho 280: struct tagCfg *av;
1.1 misho 281:
282: if (!cfg || !csAttr)
283: return NULL;
284:
1.6.4.1 ! misho 285: av = _selectAttribute(cfg, csSec, csAttr);
1.1 misho 286: if (!av)
287: return NULL;
288:
1.6.4.1 ! misho 289: return AIT_GET_STR(&av->cfg_val);
1.1 misho 290: }
1.6.4.1 ! misho 291: #if 0
1.1 misho 292: // --------------------------------------------------------------
293:
294: /*
295: * cfg_LoadAttribute() Extended get attribute, if not found item return *default value*
296: * @cfg = Head list element
297: * @csSec = Config section //[{csSec}], if NULL unset in *default* section
298: * @csAttr = Config attribute //{csAttr} = ..., if NULL unset as *any* attribute
299: * @psVal = Return buffer for item Value //... = {psVal}
300: * @ValLen = Length of buffer //{psVal} for return
301: * @csDefValue = *Default Value* for return in //{psVal}, if not found item in config list
302: * return: 0 item not found, -1 error: null parameters; >0 number of copied bytes in //{psVal}
303: */
304: int cfg_LoadAttribute(sl_config * __restrict cfg, const u_char *csSec, const u_char *csAttr,
305: u_char * __restrict psVal, int ValLen, const char *csDefValue)
306: {
307: struct tagPair *av;
308: int ret = 0;
309:
310: if (!cfg || !csAttr || !ValLen || !psVal)
311: return -1;
312:
313: av = SelectAttribute(cfg, csSec, csAttr);
314: if (!av) {
315: if (csDefValue) {
1.2 misho 316: strlcpy((char*) psVal, csDefValue, ValLen);
1.1 misho 317: ret = strlen((char*) psVal);
318: }
319:
320: return ret;
321: }
322:
323: if (!av->psValue || !*av->psValue) {
324: if (csDefValue) {
1.2 misho 325: strlcpy((char*) psVal, csDefValue, ValLen);
1.1 misho 326: ret = strlen((char*) psVal);
327: }
328: } else {
1.2 misho 329: strlcpy((char*) psVal, (char*) av->psValue, ValLen);
1.1 misho 330: ret = strlen((char*) psVal);
331: }
332:
333: return ret;
334: }
1.6.4.1 ! misho 335: #endif
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>