multibyte.h revision 1.2.2.2 1 1.2.2.2 nathanw /* $NetBSD: multibyte.h,v 1.2.2.2 2002/03/22 20:42:17 nathanw Exp $ */
2 1.2.2.2 nathanw
3 1.2.2.2 nathanw /*-
4 1.2.2.2 nathanw * Copyright (c)2002 Citrus Project,
5 1.2.2.2 nathanw * All rights reserved.
6 1.2.2.2 nathanw *
7 1.2.2.2 nathanw * Redistribution and use in source and binary forms, with or without
8 1.2.2.2 nathanw * modification, are permitted provided that the following conditions
9 1.2.2.2 nathanw * are met:
10 1.2.2.2 nathanw * 1. Redistributions of source code must retain the above copyright
11 1.2.2.2 nathanw * notice, this list of conditions and the following disclaimer.
12 1.2.2.2 nathanw * 2. Redistributions in binary form must reproduce the above copyright
13 1.2.2.2 nathanw * notice, this list of conditions and the following disclaimer in the
14 1.2.2.2 nathanw * documentation and/or other materials provided with the distribution.
15 1.2.2.2 nathanw *
16 1.2.2.2 nathanw * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 1.2.2.2 nathanw * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.2.2.2 nathanw * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.2.2.2 nathanw * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 1.2.2.2 nathanw * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.2.2.2 nathanw * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.2.2.2 nathanw * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.2.2.2 nathanw * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.2.2.2 nathanw * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.2.2.2 nathanw * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.2.2.2 nathanw * SUCH DAMAGE.
27 1.2.2.2 nathanw */
28 1.2.2.2 nathanw
29 1.2.2.2 nathanw #ifndef _MULTIBYTE_H_
30 1.2.2.2 nathanw #define _MULTIBYTE_H_
31 1.2.2.2 nathanw
32 1.2.2.2 nathanw /* mbstate_t private */
33 1.2.2.2 nathanw
34 1.2.2.2 nathanw #ifdef _BSD_MBSTATE_T_
35 1.2.2.2 nathanw typedef _BSD_MBSTATE_T_ mbstate_t;
36 1.2.2.2 nathanw #undef _BSD_MBSTATE_T_
37 1.2.2.2 nathanw #endif
38 1.2.2.2 nathanw
39 1.2.2.2 nathanw typedef struct _RuneStatePriv {
40 1.2.2.2 nathanw _RuneLocale *__runelocale;
41 1.2.2.2 nathanw char __private __attribute__((__aligned__));
42 1.2.2.2 nathanw } _RuneStatePriv;
43 1.2.2.2 nathanw
44 1.2.2.2 nathanw typedef union _RuneState {
45 1.2.2.2 nathanw mbstate_t __pad;
46 1.2.2.2 nathanw struct _RuneStatePriv __priv;
47 1.2.2.2 nathanw #define rs_runelocale __priv.__runelocale
48 1.2.2.2 nathanw #define rs_private __priv.__private
49 1.2.2.2 nathanw } _RuneState;
50 1.2.2.2 nathanw #define _PRIVSIZE (sizeof(mbstate_t)-offsetof(_RuneStatePriv, __private))
51 1.2.2.2 nathanw
52 1.2.2.2 nathanw
53 1.2.2.2 nathanw /* */
54 1.2.2.2 nathanw
55 1.2.2.2 nathanw static __inline _citrus_ctype_t
56 1.2.2.2 nathanw _to_cur_ctype(void)
57 1.2.2.2 nathanw {
58 1.2.2.2 nathanw return (_CurrentRuneLocale->rl_citrus_ctype);
59 1.2.2.2 nathanw }
60 1.2.2.2 nathanw
61 1.2.2.2 nathanw static __inline _RuneState *
62 1.2.2.2 nathanw _ps_to_runestate(mbstate_t *ps)
63 1.2.2.2 nathanw {
64 1.2.2.2 nathanw return (_RuneState *)(void *)ps;
65 1.2.2.2 nathanw }
66 1.2.2.2 nathanw
67 1.2.2.2 nathanw static __inline _RuneState const *
68 1.2.2.2 nathanw _ps_to_runestate_const(mbstate_t const *ps)
69 1.2.2.2 nathanw {
70 1.2.2.2 nathanw return (_RuneState const *)(void const *)ps;
71 1.2.2.2 nathanw }
72 1.2.2.2 nathanw
73 1.2.2.2 nathanw static __inline _RuneLocale *
74 1.2.2.2 nathanw _ps_to_runelocale(mbstate_t const *ps)
75 1.2.2.2 nathanw {
76 1.2.2.2 nathanw return _ps_to_runestate_const(ps)->rs_runelocale;
77 1.2.2.2 nathanw }
78 1.2.2.2 nathanw
79 1.2.2.2 nathanw static __inline _citrus_ctype_t
80 1.2.2.2 nathanw _ps_to_ctype(mbstate_t const *ps)
81 1.2.2.2 nathanw {
82 1.2.2.2 nathanw if (!ps)
83 1.2.2.2 nathanw return _to_cur_ctype();
84 1.2.2.2 nathanw
85 1.2.2.2 nathanw _DIAGASSERT(_ps_to_runelocale(ps) != NULL);
86 1.2.2.2 nathanw
87 1.2.2.2 nathanw return _ps_to_runelocale(ps)->rl_citrus_ctype;
88 1.2.2.2 nathanw }
89 1.2.2.2 nathanw
90 1.2.2.2 nathanw static __inline void *
91 1.2.2.2 nathanw _ps_to_private(mbstate_t *ps)
92 1.2.2.2 nathanw {
93 1.2.2.2 nathanw if (ps == NULL)
94 1.2.2.2 nathanw return NULL;
95 1.2.2.2 nathanw return (void *)&_ps_to_runestate(ps)->rs_private;
96 1.2.2.2 nathanw }
97 1.2.2.2 nathanw
98 1.2.2.2 nathanw static __inline void const *
99 1.2.2.2 nathanw _ps_to_private_const(mbstate_t const *ps)
100 1.2.2.2 nathanw {
101 1.2.2.2 nathanw if (ps == NULL)
102 1.2.2.2 nathanw return NULL;
103 1.2.2.2 nathanw return (void const *)&_ps_to_runestate_const(ps)->rs_private;
104 1.2.2.2 nathanw }
105 1.2.2.2 nathanw
106 1.2.2.2 nathanw static __inline void
107 1.2.2.2 nathanw _init_ps(_RuneLocale *rl, mbstate_t *ps)
108 1.2.2.2 nathanw {
109 1.2.2.2 nathanw size_t dum;
110 1.2.2.2 nathanw _ps_to_runestate(ps)->rs_runelocale = rl;
111 1.2.2.2 nathanw _citrus_ctype_mbrtowc(rl->rl_citrus_ctype, NULL, NULL, NULL,
112 1.2.2.2 nathanw _ps_to_private(ps), &dum);
113 1.2.2.2 nathanw }
114 1.2.2.2 nathanw
115 1.2.2.2 nathanw static __inline void
116 1.2.2.2 nathanw _fixup_ps(_RuneLocale *rl, mbstate_t *ps, int forceinit)
117 1.2.2.2 nathanw {
118 1.2.2.2 nathanw /* for future multi-locale facility */
119 1.2.2.2 nathanw _DIAGASSERT(rl != NULL);
120 1.2.2.2 nathanw
121 1.2.2.2 nathanw if (ps != NULL && (_ps_to_runelocale(ps) == NULL || forceinit)) {
122 1.2.2.2 nathanw _init_ps(rl, ps);
123 1.2.2.2 nathanw }
124 1.2.2.2 nathanw }
125 1.2.2.2 nathanw
126 1.2.2.2 nathanw #endif
127