1 /* $FreeBSD: head/lib/libc/iconv/citrus_prop.c 281798 2015-04-20 22:09:50Z pfg $ */
2 /* $NetBSD: citrus_prop.c,v 1.4 2011/03/30 08:22:01 jruoho Exp $ */
5 * Copyright (c)2006 Citrus Project,
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
43 #include "citrus_namespace.h"
44 #include "citrus_bcs.h"
45 #include "citrus_region.h"
46 #include "citrus_memstream.h"
47 #include "citrus_prop.h"
50 _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
)
64 memset(&obj
->u
, 0, sizeof(obj
->u
));
68 _citrus_prop_object_uninit(_citrus_prop_object_t
*obj
)
71 if (obj
->type
== _CITRUS_PROP_STR
)
72 free(__DECONST(void *, obj
->u
.str
));
75 static const char *xdigit
= "0123456789ABCDEF";
77 #define _CITRUS_PROP_READ_UINT_COMMON(_func_, _type_, _max_) \
79 _citrus_prop_read_##_func_##_common(struct _memstream * __restrict ms, \
80 _type_ * __restrict result, int base) \
87 cutoff = _max_ / base; \
88 cutlim = _max_ % base; \
90 ch = _memstream_getc(ms); \
91 p = strchr(xdigit, _bcs_toupper(ch)); \
92 if (p == NULL || (n = (p - xdigit)) >= base) \
94 if (acc > cutoff || (acc == cutoff && n > cutlim)) \
99 _memstream_ungetc(ms, ch); \
103 _CITRUS_PROP_READ_UINT_COMMON(chr
, int, UCHAR_MAX
)
104 _CITRUS_PROP_READ_UINT_COMMON(num
, uint64_t, UINT64_MAX
)
105 #undef _CITRUS_PROP_READ_UINT_COMMON
107 #define _CITRUS_PROP_READ_INT(_func_, _type_) \
109 _citrus_prop_read_##_func_(struct _memstream * __restrict ms, \
110 _citrus_prop_object_t * __restrict obj) \
114 _memstream_skip_ws(ms); \
115 ch = _memstream_getc(ms); \
118 ch = _memstream_getc(ms); \
123 ch = _memstream_getc(ms); \
124 if (ch == 'x' || ch == 'X') { \
125 ch = _memstream_getc(ms); \
126 if (_bcs_isxdigit(ch) == 0) { \
127 _memstream_ungetc(ms, ch); \
133 } else if (_bcs_isdigit(ch) == 0) \
135 _memstream_ungetc(ms, ch); \
136 return (_citrus_prop_read_##_func_##_common \
137 (ms, &obj->u._func_, base)); \
139 _CITRUS_PROP_READ_INT(chr
, int)
140 _CITRUS_PROP_READ_INT(num
, uint64_t)
141 #undef _CITRUS_PROP_READ_INT
144 _citrus_prop_read_character_common(struct _memstream
* __restrict ms
,
145 int * __restrict result
)
149 ch
= _memstream_getc(ms
);
153 ch
= _memstream_getc(ms
);
177 case '0': case '1': case '2': case '3':
178 case '4': case '5': case '6': case '7':
179 _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 _memstream_skip_ws(ms
);
200 ch
= _memstream_getc(ms
);
202 _memstream_ungetc(ms
, ch
);
203 return (_citrus_prop_read_chr(ms
, obj
));
205 errnum
= _citrus_prop_read_character_common(ms
, &ch
);
209 ch
= _memstream_getc(ms
);
216 _citrus_prop_read_bool(struct _memstream
* __restrict ms
,
217 _citrus_prop_object_t
* __restrict obj
)
220 _memstream_skip_ws(ms
);
221 switch (_bcs_tolower(_memstream_getc(ms
))) {
223 if (_bcs_tolower(_memstream_getc(ms
)) == 'r' &&
224 _bcs_tolower(_memstream_getc(ms
)) == 'u' &&
225 _bcs_tolower(_memstream_getc(ms
)) == 'e') {
226 obj
->u
.boolean
= true;
231 if (_bcs_tolower(_memstream_getc(ms
)) == 'a' &&
232 _bcs_tolower(_memstream_getc(ms
)) == 'l' &&
233 _bcs_tolower(_memstream_getc(ms
)) == 's' &&
234 _bcs_tolower(_memstream_getc(ms
)) == 'e') {
235 obj
->u
.boolean
= false;
243 _citrus_prop_read_str(struct _memstream
* __restrict ms
,
244 _citrus_prop_object_t
* __restrict obj
)
246 int ch
, errnum
, quot
;
248 #define _CITRUS_PROP_STR_BUFSIZ 512
251 m
= _CITRUS_PROP_STR_BUFSIZ
;
256 _memstream_skip_ws(ms
);
257 quot
= _memstream_getc(ms
);
263 _memstream_ungetc(ms
, quot
);
266 case '\"': case '\'':
275 m
= _CITRUS_PROP_STR_BUFSIZ
;
276 t
= realloc(s
, n
+ m
);
283 ch
= _memstream_getc(ms
);
284 if (quot
== ch
|| (quot
== EOF
&&
285 (ch
== ';' || _bcs_isspace(ch
)))) {
288 obj
->u
.str
= (const char *)s
;
291 _memstream_ungetc(ms
, ch
);
292 errnum
= _citrus_prop_read_character_common(ms
, &ch
);
302 #undef _CITRUS_PROP_STR_BUFSIZ
305 typedef int (*_citrus_prop_read_type_t
)(struct _memstream
* __restrict
,
306 _citrus_prop_object_t
* __restrict
);
308 static const _citrus_prop_read_type_t readers
[] = {
309 _citrus_prop_read_bool
,
310 _citrus_prop_read_str
,
311 _citrus_prop_read_character
,
312 _citrus_prop_read_num
,
316 _citrus_prop_read_symbol(struct _memstream
* __restrict ms
,
317 char * __restrict s
, size_t n
)
322 for (m
= 0; m
< n
; ++m
) {
323 ch
= _memstream_getc(ms
);
324 if (ch
!= '_' && _bcs_isalnum(ch
) == 0)
328 ch
= _memstream_getc(ms
);
329 if (ch
== '_' || _bcs_isalnum(ch
) != 0)
333 _memstream_ungetc(ms
, ch
);
340 _citrus_prop_parse_element(struct _memstream
* __restrict ms
,
341 const _citrus_prop_hint_t
* __restrict hints
, void * __restrict context
)
344 #define _CITRUS_PROP_HINT_NAME_LEN_MAX 255
345 char name
[_CITRUS_PROP_HINT_NAME_LEN_MAX
+ 1];
346 const _citrus_prop_hint_t
*hint
;
347 _citrus_prop_object_t ostart
, oend
;
349 errnum
= _citrus_prop_read_symbol(ms
, name
, sizeof(name
));
352 for (hint
= hints
; hint
->name
!= NULL
; ++hint
)
353 if (_citrus_bcs_strcasecmp(name
, hint
->name
) == 0)
358 _memstream_skip_ws(ms
);
359 ch
= _memstream_getc(ms
);
360 if (ch
!= '=' && ch
!= ':')
361 _memstream_ungetc(ms
, ch
);
363 _citrus_prop_object_init(&ostart
, hint
->type
);
364 _citrus_prop_object_init(&oend
, hint
->type
);
365 errnum
= (*readers
[hint
->type
])(ms
, &ostart
);
368 _memstream_skip_ws(ms
);
369 ch
= _memstream_getc(ms
);
370 switch (hint
->type
) {
371 case _CITRUS_PROP_BOOL
:
373 case _CITRUS_PROP_STR
:
378 errnum
= (*readers
[hint
->type
])(ms
, &oend
);
381 _memstream_skip_ws(ms
);
382 ch
= _memstream_getc(ms
);
384 #define CALL0(_func_) \
386 errnum = (*hint->cb._func_.func)(context, \
387 hint->name, ostart.u._func_); \
389 #define CALL1(_func_) \
391 errnum = (*hint->cb._func_.func)(context, \
392 hint->name, ostart.u._func_, oend.u._func_);\
394 switch (hint
->type
) {
395 case _CITRUS_PROP_BOOL
:
398 case _CITRUS_PROP_STR
:
401 case _CITRUS_PROP_CHR
:
404 case _CITRUS_PROP_NUM
:
413 _citrus_prop_object_uninit(&ostart
);
414 _citrus_prop_object_uninit(&oend
);
419 _memstream_ungetc(ms
, ch
);
424 _citrus_prop_parse_variable(const _citrus_prop_hint_t
* __restrict hints
,
425 void * __restrict context
, const void *var
, size_t lenvar
)
427 struct _memstream ms
;
430 _memstream_bind_ptr(&ms
, __DECONST(void *, var
), lenvar
);
432 _memstream_skip_ws(&ms
);
433 ch
= _memstream_getc(&ms
);
434 if (ch
== EOF
|| ch
== '\0')
436 _memstream_ungetc(&ms
, ch
);
437 errnum
= _citrus_prop_parse_element(&ms
, hints
, context
);