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