multibyte.h revision 1.6.20.1 1 /* $NetBSD: multibyte.h,v 1.6.20.1 2017/07/21 20:22:29 perseant Exp $ */
2
3 /*-
4 * Copyright (c)2002 Citrus Project,
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
15 *
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
26 * SUCH DAMAGE.
27 */
28
29 #ifndef _MULTIBYTE_H_
30 #define _MULTIBYTE_H_
31
32 #include "setlocale_local.h"
33 /*#include "runetype_local.h"*/
34
35 /* mbstate_t private */
36
37 #ifdef _BSD_MBSTATE_T_
38 typedef _BSD_MBSTATE_T_ mbstate_t;
39 #undef _BSD_MBSTATE_T_
40 #endif
41
42 typedef struct _RuneStatePriv {
43 _RuneLocale *__runelocale;
44 char __private __attribute__((__aligned__));
45 } _RuneStatePriv;
46
47 typedef union _RuneState {
48 mbstate_t __pad;
49 struct _RuneStatePriv __priv;
50 #define rs_runelocale __priv.__runelocale
51 #define rs_private __priv.__private
52 } _RuneState;
53 #define _PRIVSIZE (sizeof(mbstate_t)-offsetof(_RuneStatePriv, __private))
54
55 #ifndef _RUNE_LOCALE
56 #define _RUNE_LOCALE(loc) \
57 ((_RuneLocale *)((loc)->part_impl[(size_t)LC_CTYPE]))
58 #endif
59
60 #define _CITRUS_CTYPE(loc) \
61 (((_RuneLocale *)((loc)->part_impl[(size_t)LC_CTYPE]))->rl_citrus_ctype)
62
63 /* */
64
65 static __inline _RuneState *
66 _ps_to_runestate(mbstate_t *ps)
67 {
68 return (_RuneState *)(void *)ps;
69 }
70
71 static __inline _RuneState const *
72 _ps_to_runestate_const(mbstate_t const *ps)
73 {
74 return (_RuneState const *)(void const *)ps;
75 }
76
77 static __inline _RuneLocale *
78 _ps_to_runelocale(mbstate_t const *ps)
79 {
80 return _ps_to_runestate_const(ps)->rs_runelocale;
81 }
82
83 static __inline _citrus_ctype_t
84 _ps_to_ctype(mbstate_t const *ps, locale_t loc)
85 {
86 if (!ps)
87 return _CITRUS_CTYPE(loc);
88
89 _DIAGASSERT(_ps_to_runelocale(ps) != NULL);
90
91 return _ps_to_runelocale(ps)->rl_citrus_ctype;
92 }
93
94 static __inline void *
95 _ps_to_private(mbstate_t *ps)
96 {
97 if (ps == NULL)
98 return NULL;
99 return (void *)&_ps_to_runestate(ps)->rs_private;
100 }
101
102 static __inline void const *
103 _ps_to_private_const(mbstate_t const *ps)
104 {
105 if (ps == NULL)
106 return NULL;
107 return (void const *)&_ps_to_runestate_const(ps)->rs_private;
108 }
109
110 static __inline void
111 _init_ps(_RuneLocale *rl, mbstate_t *ps)
112 {
113 size_t dum;
114 _ps_to_runestate(ps)->rs_runelocale = rl;
115 _citrus_ctype_mbrtowc(rl->rl_citrus_ctype, NULL, NULL, 0,
116 _ps_to_private(ps), &dum);
117 }
118
119 static __inline void
120 _fixup_ps(_RuneLocale *rl, mbstate_t *ps, int forceinit)
121 {
122 /* for future multi-locale facility */
123 _DIAGASSERT(rl != NULL);
124
125 if (ps != NULL && (_ps_to_runelocale(ps) == NULL || forceinit)) {
126 _init_ps(rl, ps);
127 }
128 }
129
130 #endif /*_MULTIBYTE_H_*/
131