1 /* $NetBSD: citrus_prop.c,v 1.3 2006/11/22 23:47:21 tnozaki Exp $ */
4 * Copyright (c)2006 Citrus Project,
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 #if defined(LIBC_SCCS) && !defined(lint)
32 __RCSID("$NetBSD: citrus_prop.c,v 1.3 2006/11/22 23:47:21 tnozaki Exp $");
33 #endif /* LIBC_SCCS and not lint */
44 #include "citrus_namespace.h"
45 #include "citrus_bcs.h"
46 #include "citrus_region.h"
47 #include "citrus_memstream.h"
48 #include "citrus_prop.h"
51 _citrus_prop_type_t type
;
57 } _citrus_prop_object_t
;
60 _citrus_prop_object_init(_citrus_prop_object_t
*obj
, _citrus_prop_type_t type
)
62 _DIAGASSERT(obj
!= NULL
);
65 memset(&obj
->u
, 0, sizeof(obj
->u
));
69 _citrus_prop_object_uninit(_citrus_prop_object_t
*obj
)
71 _DIAGASSERT(obj
!= NULL
);
73 if (obj
->type
== _CITRUS_PROP_STR
)
74 free(__UNCONST(obj
->u
.str
));
77 static const char *xdigit
= "0123456789ABCDEF";
79 #define _CITRUS_PROP_READ_UINT_COMMON(_func_, _type_, _max_) \
81 _citrus_prop_read_##_func_##_common(struct _memstream * __restrict ms, \
82 _type_ * __restrict result, int base) \
88 _DIAGASSERT(ms != NULL); \
89 _DIAGASSERT(result != NULL); \
92 cutoff = _max_ / base; \
93 cutlim = _max_ % base; \
95 ch = _memstream_getc(ms); \
96 p = strchr(xdigit, _bcs_toupper(ch)); \
97 if (p == NULL || (n = (p - xdigit)) >= base) \
99 if (acc > cutoff || (acc == cutoff && n > cutlim)) \
104 _memstream_ungetc(ms, ch); \
108 _CITRUS_PROP_READ_UINT_COMMON(chr
, int, UCHAR_MAX
)
109 _CITRUS_PROP_READ_UINT_COMMON(num
, uint64_t, UINT64_MAX
)
110 #undef _CITRUS_PROP_READ_UINT_COMMON
112 #define _CITRUS_PROP_READ_INT(_func_, _type_) \
114 _citrus_prop_read_##_func_(struct _memstream * __restrict ms, \
115 _citrus_prop_object_t * __restrict obj) \
119 _DIAGASSERT(ms != NULL); \
120 _DIAGASSERT(obj != NULL); \
122 _memstream_skip_ws(ms); \
123 ch = _memstream_getc(ms); \
129 ch = _memstream_getc(ms); \
134 ch = _memstream_getc(ms); \
135 if (ch == 'x' || ch == 'X') { \
136 ch = _memstream_getc(ms); \
137 if (_bcs_isxdigit(ch) == 0) { \
138 _memstream_ungetc(ms, ch); \
144 } else if (_bcs_isdigit(ch) == 0) \
146 _memstream_ungetc(ms, ch); \
147 return _citrus_prop_read_##_func_##_common \
148 (ms, &obj->u._func_, base); \
150 _CITRUS_PROP_READ_INT(chr
, int)
151 _CITRUS_PROP_READ_INT(num
, uint64_t)
152 #undef _CITRUS_PROP_READ_INT
155 _citrus_prop_read_character_common(struct _memstream
* __restrict ms
,
156 int * __restrict result
)
160 _DIAGASSERT(ms
!= NULL
);
161 _DIAGASSERT(result
!= NULL
);
163 ch
= _memstream_getc(ms
);
167 ch
= _memstream_getc(ms
);
170 case 'a': *result
= '\a'; break;
171 case 'b': *result
= '\b'; break;
172 case 'f': *result
= '\f'; break;
173 case 'n': *result
= '\n'; break;
174 case 'r': *result
= '\r'; break;
175 case 't': *result
= '\t'; break;
176 case 'v': *result
= '\v'; break;
178 case '0': case '1': case '2': case '3':
179 case '4': case '5': case '6': case '7':
180 _memstream_ungetc(ms
, ch
);
183 return _citrus_prop_read_chr_common(ms
, result
, base
);
194 _citrus_prop_read_character(struct _memstream
* __restrict ms
,
195 _citrus_prop_object_t
* __restrict obj
)
199 _DIAGASSERT(ms
!= NULL
);
200 _DIAGASSERT(obj
!= NULL
);
202 _memstream_skip_ws(ms
);
203 ch
= _memstream_getc(ms
);
205 _memstream_ungetc(ms
, ch
);
206 return _citrus_prop_read_chr(ms
, obj
);
208 errnum
= _citrus_prop_read_character_common(ms
, &ch
);
212 ch
= _memstream_getc(ms
);
219 _citrus_prop_read_bool(struct _memstream
* __restrict ms
,
220 _citrus_prop_object_t
* __restrict obj
)
222 _DIAGASSERT(ms
!= NULL
);
223 _DIAGASSERT(obj
!= NULL
);
225 _memstream_skip_ws(ms
);
226 switch (_bcs_tolower(_memstream_getc(ms
))) {
228 if (_bcs_tolower(_memstream_getc(ms
)) == 'r' &&
229 _bcs_tolower(_memstream_getc(ms
)) == 'u' &&
230 _bcs_tolower(_memstream_getc(ms
)) == 'e') {
236 if (_bcs_tolower(_memstream_getc(ms
)) == 'a' &&
237 _bcs_tolower(_memstream_getc(ms
)) == 'l' &&
238 _bcs_tolower(_memstream_getc(ms
)) == 's' &&
239 _bcs_tolower(_memstream_getc(ms
)) == 'e') {
248 _citrus_prop_read_str(struct _memstream
* __restrict ms
,
249 _citrus_prop_object_t
* __restrict obj
)
251 int errnum
, quot
, ch
;
253 #define _CITRUS_PROP_STR_BUFSIZ 512
256 _DIAGASSERT(ms
!= NULL
);
257 _DIAGASSERT(obj
!= NULL
);
259 m
= _CITRUS_PROP_STR_BUFSIZ
;
264 _memstream_skip_ws(ms
);
265 quot
= _memstream_getc(ms
);
270 _memstream_ungetc(ms
, quot
);
273 case '\"': case '\'':
282 m
= _CITRUS_PROP_STR_BUFSIZ
;
283 t
= realloc(s
, n
+ m
);
290 ch
= _memstream_getc(ms
);
291 if (quot
== ch
|| (quot
== EOF
&&
292 (ch
== ';' || _bcs_isspace(ch
)))) {
295 obj
->u
.str
= (const char *)s
;
298 _memstream_ungetc(ms
, ch
);
299 errnum
= _citrus_prop_read_character_common(ms
, &ch
);
307 #undef _CITRUS_PROP_STR_BUFSIZ
310 typedef int (*_citrus_prop_read_type_t
)(struct _memstream
* __restrict
,
311 _citrus_prop_object_t
* __restrict
);
313 static const _citrus_prop_read_type_t readers
[] = {
314 _citrus_prop_read_bool
,
315 _citrus_prop_read_str
,
316 _citrus_prop_read_character
,
317 _citrus_prop_read_num
,
321 _citrus_prop_read_symbol(struct _memstream
* __restrict ms
,
322 char * __restrict s
, size_t n
)
327 _DIAGASSERT(ms
!= NULL
);
328 _DIAGASSERT(s
!= NULL
);
331 for (m
= 0; m
< n
; ++m
) {
332 ch
= _memstream_getc(ms
);
333 if (ch
!= '_' && _bcs_isalnum(ch
) == 0)
337 ch
= _memstream_getc(ms
);
338 if (ch
== '_' || _bcs_isalnum(ch
) != 0)
342 _memstream_ungetc(ms
, ch
);
349 _citrus_prop_parse_element(struct _memstream
* __restrict ms
,
350 const _citrus_prop_hint_t
* __restrict hints
,
351 void ** __restrict context
)
354 #define _CITRUS_PROP_HINT_NAME_LEN_MAX 255
355 char name
[_CITRUS_PROP_HINT_NAME_LEN_MAX
+ 1];
356 const _citrus_prop_hint_t
*hint
;
357 _citrus_prop_object_t ostart
, oend
;
359 _DIAGASSERT(ms
!= NULL
);
360 _DIAGASSERT(hints
!= NULL
);
362 errnum
= _citrus_prop_read_symbol(ms
, name
, sizeof(name
));
365 for (hint
= hints
; hint
->name
!= NULL
; ++hint
) {
366 if (_citrus_bcs_strcasecmp(name
, hint
->name
) == 0)
372 _memstream_skip_ws(ms
);
373 ch
= _memstream_getc(ms
);
374 if (ch
!= '=' && ch
!= ':')
375 _memstream_ungetc(ms
, ch
);
377 _citrus_prop_object_init(&ostart
, hint
->type
);
378 _citrus_prop_object_init(&oend
, hint
->type
);
379 errnum
= (*readers
[hint
->type
])(ms
, &ostart
);
382 _memstream_skip_ws(ms
);
383 ch
= _memstream_getc(ms
);
384 switch (hint
->type
) {
385 case _CITRUS_PROP_BOOL
:
386 case _CITRUS_PROP_STR
:
391 errnum
= (*readers
[hint
->type
])(ms
, &oend
);
394 _memstream_skip_ws(ms
);
395 ch
= _memstream_getc(ms
);
397 #define CALL0(_func_) \
399 _DIAGASSERT(hint->cb._func_.func != NULL); \
400 errnum = (*hint->cb._func_.func)(context, \
401 hint->name, ostart.u._func_); \
402 } while (/*CONSTCOND*/0)
403 #define CALL1(_func_) \
405 _DIAGASSERT(hint->cb._func_.func != NULL); \
406 errnum = (*hint->cb._func_.func)(context, \
407 hint->name, ostart.u._func_, oend.u._func_);\
408 } while (/*CONSTCOND*/0)
409 switch (hint
->type
) {
410 case _CITRUS_PROP_BOOL
: CALL0(bool); break;
411 case _CITRUS_PROP_STR
: CALL0( str
); break;
412 case _CITRUS_PROP_CHR
: CALL1( chr
); break;
413 case _CITRUS_PROP_NUM
: CALL1( num
); break;
420 _citrus_prop_object_uninit(&ostart
);
421 _citrus_prop_object_uninit(&oend
);
426 _memstream_ungetc(ms
, ch
);
431 _citrus_prop_parse_variable(const _citrus_prop_hint_t
* __restrict hints
,
432 void * __restrict context
, const void *var
, size_t lenvar
)
434 struct _memstream ms
;
437 _DIAGASSERT(hints
!= NULL
);
439 _memstream_bind_ptr(&ms
, __UNCONST(var
), lenvar
);
441 _memstream_skip_ws(&ms
);
442 ch
= _memstream_getc(&ms
);
443 if (ch
== EOF
|| ch
== '\0')
445 _memstream_ungetc(&ms
, ch
);
446 errnum
= _citrus_prop_parse_element(
447 &ms
, hints
, (void **)&context
);