Annotation of libaitcfg/src/queue.c, revision 1.8
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.8 ! misho 6: * $Id: queue.c,v 1.7.2.3 2012/05/23 14:52:50 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.7 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.7 misho 49: static inline struct tagCfg *
50: _selectAttribute(cfg_root_t * __restrict cfg, const char *csSec, const char *csAttr)
1.1 misho 51: {
1.7 misho 52: struct tagCfg fav;
1.1 misho 53:
54: if (!cfg)
55: return NULL;
1.7 misho 56: else
57: memset(&fav, 0, sizeof fav);
1.1 misho 58:
1.7 misho 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);
1.1 misho 70: }
71:
1.7 misho 72: /* --------------------------------------------------------------- */
1.1 misho 73:
74: /*
1.7 misho 75: * cfg_findAttribute() - Find attribute position in config file
76: *
77: * @cfg = Config root
1.1 misho 78: * @csSec = Config section //[{csSec}]
79: * @csAttr = Config attribute //{csAttr} = ...
1.7 misho 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)
1.1 misho 84: {
1.7 misho 85: struct tagCfg *av, fav;
1.1 misho 86: register int cx = 0;
87:
1.7 misho 88: if (!cfg || !csAttr) {
89: cfg_SetErr(EINVAL, "Invalid argument(s)");
1.1 misho 90: return -1;
1.7 misho 91: } else
92: memset(&fav, 0, sizeof fav);
1.1 misho 93:
1.7 misho 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) {
1.1 misho 102: ++cx;
1.7 misho 103: if (!cfg_tree_cmp(&fav, av))
104: return cx;
1.1 misho 105: }
106:
107: return 0;
108: }
109:
110: /*
1.7 misho 111: * cfg_unsetAttribute() - Unset item from config and free resources
112: *
113: * @cfg = Config root
1.1 misho 114: * @csSec = Config section //[{csSec}], if NULL unset in *default* section
1.7 misho 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)
1.1 misho 120: {
1.7 misho 121: struct tagCfg *av;
1.1 misho 122:
123: if (!cfg || !csAttr)
124: return -1;
125:
1.7 misho 126: av = _selectAttribute(cfg, csSec, csAttr);
1.1 misho 127: if (!av)
128: return 0;
129:
1.7 misho 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);
1.8 ! misho 138: io_free(av);
1.7 misho 139: return 1;
1.1 misho 140: }
141:
142: /*
1.7 misho 143: * cfg_setAttribute() - Set item in config or adding new item if not exists
144: *
145: * @cfg = Config root
1.1 misho 146: * @csSec = Config section //[{csSec}], if NULL set in *default* section
1.7 misho 147: * @csAttr = Config attribute //{csAttr} = ...
1.1 misho 148: * @csVal = Config value //... = {csVal} to setup
1.7 misho 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)
1.1 misho 153: {
1.7 misho 154: struct tagCfg *av, *section;
1.1 misho 155:
156: if (!cfg || !csAttr)
157: return -1;
158:
1.7 misho 159: av = _selectAttribute(cfg, csSec, csAttr);
1.1 misho 160: if (!av) {
1.7 misho 161: /* adding new element */
162: section = _selectAttribute(cfg, csSec, NULL);
1.1 misho 163:
1.8 ! misho 164: av = io_malloc(sizeof(struct tagCfg));
1.1 misho 165: if (!av) {
166: LOGERR;
167: return -1;
168: } else {
1.7 misho 169: memset(av, 0, sizeof(struct tagCfg));
1.1 misho 170:
1.7 misho 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);
1.1 misho 177: }
1.7 misho 178:
1.1 misho 179: if (csSec && *csSec) {
1.7 misho 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);
1.1 misho 183: }
1.7 misho 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);
1.1 misho 192: return 2;
193: }
194:
1.7 misho 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);
1.1 misho 199: return 1;
200: }
201:
1.7 misho 202: /* Nothing happens ... found & values is equal! */
1.1 misho 203: return 0;
204: }
205:
206: /*
1.7 misho 207: * cfg_getAttribute() - Get item from config and return value from it
208: *
209: * @cfg = Config root
1.1 misho 210: * @csSec = Config section //[{csSec}], if NULL unset in *default* section
211: * @csAttr = Config attribute //{csAttr} = ..., if NULL unset as *any* attribute
1.7 misho 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)
1.1 misho 216: {
1.7 misho 217: struct tagCfg *av;
1.1 misho 218:
219: if (!cfg || !csAttr)
220: return NULL;
221:
1.7 misho 222: av = _selectAttribute(cfg, csSec, csAttr);
1.1 misho 223: if (!av)
224: return NULL;
225:
1.7 misho 226: return AIT_GET_STR(&av->cfg_val);
1.4 misho 227: }
228:
1.1 misho 229: /*
1.7 misho 230: * cfg_loadAttribute() - Get guarded attribute, if not found item return *default value*
231: *
232: * @cfg = Config root
1.1 misho 233: * @csSec = Config section //[{csSec}], if NULL unset in *default* section
1.7 misho 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)
1.1 misho 242: {
1.7 misho 243: struct tagCfg *av;
1.1 misho 244: int ret = 0;
245:
1.7 misho 246: if (!cfg || !csAttr || !val) {
247: cfg_SetErr(EINVAL, "Invalid argument(s)");
1.1 misho 248: return -1;
1.7 misho 249: }
1.1 misho 250:
1.7 misho 251: av = _selectAttribute(cfg, csSec, csAttr);
1.1 misho 252: if (!av) {
1.7 misho 253: /* not found item */
1.1 misho 254: if (csDefValue) {
1.7 misho 255: AIT_SET_STR(val, csDefValue);
256: ret = AIT_LEN(val);
1.8 ! misho 257: } else
! 258: AIT_INIT_VAL(val);
1.1 misho 259: return ret;
260: }
261:
1.7 misho 262: if (AIT_ISEMPTY(&av->cfg_val) || !*AIT_GET_LIKE(&av->cfg_val, char*)) {
263: /* empty value */
1.1 misho 264: if (csDefValue) {
1.7 misho 265: AIT_SET_STR(val, csDefValue);
266: ret = AIT_LEN(val);
1.8 ! misho 267: } else
! 268: AIT_INIT_VAL(val);
1.1 misho 269: } else {
1.7 misho 270: /* copy value */
271: AIT_SET_STR(val, AIT_GET_STR(&av->cfg_val));
272: ret = AIT_LEN(val);
1.1 misho 273: }
274:
275: return ret;
276: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>