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.6.4.4 2012/04/02 16:00:00 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, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
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;
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: io_align(strlen(csSec), 1) / 2);
62: if (csAttr)
63: AIT_KEY(&fav.cfg_attr) = crcFletcher16((u_short*) csAttr,
64: io_align(strlen(csAttr), 1) / 2);
65:
66: /*
67: struct tagCfg *av;
68:
69: RB_FOREACH(av, tagRC, cfg) {
70: printf("sec=%s(%d) attr=%s(%d) val=%s\n", AIT_GET_LIKE(&av->cfg_sec, char*), AIT_KEY(&av->cfg_sec),
71: AIT_GET_LIKE(&av->cfg_attr, char*), AIT_KEY(&av->cfg_attr), AIT_GET_STR(&av->cfg_val));
72: }
73:
74: printf("ssss=%d aaaa=%d\n", AIT_KEY(&fav.cfg_sec), AIT_KEY(&fav.cfg_attr));
75: */
76: if (!csAttr)
77: return RB_NFIND(tagRC, cfg, &fav);
78: else
79: return RB_FIND(tagRC, cfg, &fav);
80: }
81:
82: static inline void
83: _destroyAttribute(struct tagCfg *av)
84: {
85: if (!av)
86: return;
87:
88: }
89:
90: // ----------------------------------------------
91:
92: /*
93: * cfg_findAttribute() - Find attribute position in config file
94: *
95: * @cfg = Config root
96: * @csSec = Config section //[{csSec}]
97: * @csAttr = Config attribute //{csAttr} = ...
98: * return: 0 not found item, -1 error or >0 position in list
99: */
100: inline int
101: cfg_findAttribute(cfg_root_t * __restrict cfg, const char *csSec, const char *csAttr)
102: {
103: struct tagCfg *av, fav;
104: register int cx = 0;
105:
106: if (!cfg || !csAttr) {
107: cfg_SetErr(EINVAL, "Invalid argument(s)");
108: return -1;
109: } else
110: memset(&fav, 0, sizeof fav);
111:
112: if (csSec && *csSec)
113: AIT_KEY(&fav.cfg_sec) = crcFletcher16((u_short*) csSec,
114: io_align(strlen(csSec), 1) / 2);
115: if (csAttr)
116: AIT_KEY(&fav.cfg_attr) = crcFletcher16((u_short*) csAttr,
117: io_align(strlen(csAttr), 1) / 2);
118:
119: SLIST_FOREACH(av, cfg, cfg_next) {
120: ++cx;
121: if (!cfg_tree_cmp(&fav, av))
122: return cx;
123: }
124:
125: return 0;
126: }
127:
128: /*
129: * cfg_unsetAttribute() - Unset item from config and free resources
130: *
131: * @cfg = Config root
132: * @csSec = Config section //[{csSec}], if NULL unset in *default* section
133: * @csAttr = Config attribute //{csAttr} = ...
134: * return: 0 item not found, -1 error or 1 removed item
135: */
136: int
137: cfg_unsetAttribute(cfg_root_t * __restrict cfg, const char *csSec, const char *csAttr)
138: {
139: struct tagCfg *av;
140:
141: if (!cfg || !csAttr)
142: return -1;
143:
144: av = _selectAttribute(cfg, csSec, csAttr);
145: if (!av)
146: return 0;
147:
148: CFG_RC_LOCK(cfg);
149: RB_REMOVE(tagRC, cfg, av);
150: SLIST_REMOVE(cfg, av, tagCfg, cfg_next);
151: CFG_RC_UNLOCK(cfg);
152:
153: AIT_FREE_VAL(&av->cfg_val);
154: AIT_FREE_VAL(&av->cfg_attr);
155: AIT_FREE_VAL(&av->cfg_sec);
156: free(av);
157: return 1;
158: }
159:
160: /*
161: * cfg_setAttribute() - Set item in config or adding new item if not exists
162: *
163: * @cfg = Config root
164: * @csSec = Config section //[{csSec}], if NULL set in *default* section
165: * @csAttr = Config attribute //{csAttr} = ...
166: * @csVal = Config value //... = {csVal} to setup
167: * return: 0 nothing changed, -1 error, 1 found and updated item or 2 added new item
168: */
169: int
170: cfg_setAttribute(cfg_root_t * __restrict cfg, const char *csSec, const char *csAttr, const char *csVal)
171: {
172: struct tagCfg *av, *section;
173:
174: if (!cfg || !csAttr || !csVal)
175: return -1;
176:
177: av = _selectAttribute(cfg, csSec, csAttr);
178: if (!av) {
179: /* adding new element */
180: section = _selectAttribute(cfg, csSec, NULL);
181:
182: av = malloc(sizeof(struct tagCfg));
183: if (!av) {
184: LOGERR;
185: return -1;
186: } else {
187: memset(av, 0, sizeof(struct tagCfg));
188:
189: CFG_RC_LOCK(cfg);
190: if (!section)
191: SLIST_INSERT_HEAD(cfg, av, cfg_next);
192: else
193: SLIST_INSERT_AFTER(section, av, cfg_next);
194: CFG_RC_UNLOCK(cfg);
195: }
196:
197: if (csSec && *csSec) {
198: AIT_SET_STR(&av->cfg_sec, csSec);
199: AIT_KEY(&av->cfg_sec) = crcFletcher16(AIT_GET_LIKE(&av->cfg_sec, u_short*),
200: io_align(AIT_LEN(&av->cfg_sec) - 1, 1) / 2);
201: }
202: AIT_SET_STR(&av->cfg_val, csVal);
203: AIT_SET_STR(&av->cfg_attr, csAttr);
204: AIT_KEY(&av->cfg_attr) = crcFletcher16(AIT_GET_LIKE(&av->cfg_attr, u_short*),
205: io_align(AIT_LEN(&av->cfg_attr) - 1, 1) / 2);
206:
207: CFG_RC_LOCK(cfg);
208: RB_INSERT(tagRC, cfg, av);
209: CFG_RC_UNLOCK(cfg);
210: return 2;
211: }
212:
213: if (strcmp((char*) csVal, (char*) AIT_GET_STR(&av->cfg_val))) {
214: /* Update element */
215: AIT_FREE_VAL(&av->cfg_val);
216: AIT_SET_STR(&av->cfg_val, csVal);
217: return 1;
218: }
219:
220: /* Nothing happens ... found & values is equal! */
221: return 0;
222: }
223:
224: /*
225: * cfg_getAttribute() - Get item from config and return value from it
226: *
227: * @cfg = Config root
228: * @csSec = Config section //[{csSec}], if NULL unset in *default* section
229: * @csAttr = Config attribute //{csAttr} = ..., if NULL unset as *any* attribute
230: * return: NULL item not found or null parameters, !=NULL value const string
231: */
232: inline const char *
233: cfg_getAttribute(cfg_root_t * __restrict cfg, const char *csSec, const char *csAttr)
234: {
235: struct tagCfg *av;
236:
237: if (!cfg || !csAttr)
238: return NULL;
239:
240: av = _selectAttribute(cfg, csSec, csAttr);
241: if (!av)
242: return NULL;
243:
244: return AIT_GET_STR(&av->cfg_val);
245: }
246:
247: /*
248: * cfg_loadAttribute() - Get guarded attribute, if not found item return *default value*
249: *
250: * @cfg = Config root
251: * @csSec = Config section //[{csSec}], if NULL unset in *default* section
252: * @csAttr = Config attribute //{csAttr} = ...
253: * @val = Return buffer for item Value //... = {val}
254: * @csDefValue = *Default Value* for return in //{val}, if not found item in config
255: * return: 0 item not found, -1 error or >0 number of copied bytes in //{val}
256: */
257: int
258: cfg_loadAttribute(cfg_root_t * __restrict cfg, const char *csSec, const char *csAttr,
259: ait_val_t * __restrict val, const char *csDefValue)
260: {
261: struct tagCfg *av;
262: int ret = 0;
263:
264: if (!cfg || !csAttr || !val) {
265: cfg_SetErr(EINVAL, "Invalid argument(s)");
266: return -1;
267: }
268:
269: av = _selectAttribute(cfg, csSec, csAttr);
270: if (!av) {
271: /* not found item */
272: if (csDefValue) {
273: AIT_SET_STR(val, csDefValue);
274: ret = AIT_LEN(val);
275: }
276: return ret;
277: }
278:
279: if (AIT_ISEMPTY(&av->cfg_val) || !*AIT_GET_LIKE(&av->cfg_val, char*)) {
280: /* empty value */
281: if (csDefValue) {
282: AIT_SET_STR(val, csDefValue);
283: ret = AIT_LEN(val);
284: }
285: } else {
286: /* copy value */
287: AIT_SET_STR(val, AIT_GET_STR(&av->cfg_val));
288: ret = AIT_LEN(val);
289: }
290:
291: return ret;
292: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>