Initial import of binutils 2.22 on the new vendor branch
[dragonfly.git] / lib / libc / locale / multibyte.h
1 /*      $NetBSD: src/lib/libc/locale/multibyte.h,v 1.3 2003/04/29 14:53:12 scw Exp $    */
2 /*      $DragonFly: src/lib/libc/locale/multibyte.h,v 1.1 2005/03/16 06:54:41 joerg Exp $ */
3
4 /*-
5  * Copyright (c)2002 Citrus Project,
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
16  *
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
27  * SUCH DAMAGE.
28  */
29
30 #ifndef _MULTIBYTE_H_
31 #define _MULTIBYTE_H_
32
33 /* mbstate_t private */
34
35 #ifndef _MBSTATE_T_DECLARED
36 #define _MBSTATE_T_DECLARED
37 typedef __mbstate_t     mbstate_t;
38 #endif
39
40 typedef struct _RuneStatePriv {
41         _RuneLocale     *__runelocale;
42         char            __private __attribute__((__aligned__));
43 } _RuneStatePriv;
44
45 typedef union _RuneState {
46         mbstate_t               __pad;
47         struct _RuneStatePriv   __priv;
48 #define rs_runelocale           __priv.__runelocale
49 #define rs_private              __priv.__private
50 } _RuneState;
51 #define _PRIVSIZE       (sizeof(mbstate_t)-offsetof(_RuneStatePriv, __private))
52
53 static __inline _citrus_ctype_t
54 _to_cur_ctype(void)
55 {
56         return(_CurrentRuneLocale->rl_citrus_ctype);
57 }
58
59 static __inline _RuneState *
60 _ps_to_runestate(mbstate_t *ps)
61 {
62         return((_RuneState *)(void *)ps);
63 }
64
65 static __inline const _RuneState *
66 _ps_to_runestate_const(const mbstate_t *ps)
67 {
68         return((const _RuneState *)(const void *)ps);
69 }
70
71 static __inline _RuneLocale *
72 _ps_to_runelocale(const mbstate_t *ps)
73 {
74         return(_ps_to_runestate_const(ps)->rs_runelocale);
75 }
76
77 static __inline _citrus_ctype_t
78 _ps_to_ctype(const mbstate_t *ps)
79 {
80         if (ps == NULL)
81                 return(_to_cur_ctype());
82
83         _DIAGASSERT(_ps_to_runelocale(ps) != NULL);
84
85         return(_ps_to_runelocale(ps)->rl_citrus_ctype);
86 }
87
88 static __inline void *
89 _ps_to_private(mbstate_t *ps)
90 {
91         if (ps == NULL)
92                 return(NULL);
93         return((void *)&_ps_to_runestate(ps)->rs_private);
94 }
95
96 static __inline const void *
97 _ps_to_private_const(const mbstate_t *ps)
98 {
99         if (ps == NULL)
100                 return(NULL);
101         return((const void *)&_ps_to_runestate_const(ps)->rs_private);
102 }
103
104 static __inline void
105 _init_ps(_RuneLocale *rl, mbstate_t *ps)
106 {
107         size_t dum;
108         _ps_to_runestate(ps)->rs_runelocale = rl;
109         _citrus_ctype_mbrtowc(rl->rl_citrus_ctype, NULL, NULL, 0,
110                               _ps_to_private(ps), &dum);
111 }
112
113 static __inline void
114 _fixup_ps(_RuneLocale *rl, mbstate_t *ps, int forceinit)
115 {
116         /* for future multi-locale facility */
117         _DIAGASSERT(rl != NULL);
118
119         if (ps != NULL && (_ps_to_runelocale(ps) == NULL || forceinit))
120                 _init_ps(rl, ps);
121 }
122
123 #endif