1*84d9c625SLionel Sambuc /* $NetBSD: multibyte.h,v 1.6 2013/08/18 20:03:48 joerg Exp $ */
22fe8fb19SBen Gras
32fe8fb19SBen Gras /*-
42fe8fb19SBen Gras * Copyright (c)2002 Citrus Project,
52fe8fb19SBen Gras * All rights reserved.
62fe8fb19SBen Gras *
72fe8fb19SBen Gras * Redistribution and use in source and binary forms, with or without
82fe8fb19SBen Gras * modification, are permitted provided that the following conditions
92fe8fb19SBen Gras * are met:
102fe8fb19SBen Gras * 1. Redistributions of source code must retain the above copyright
112fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer.
122fe8fb19SBen Gras * 2. Redistributions in binary form must reproduce the above copyright
132fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer in the
142fe8fb19SBen Gras * documentation and/or other materials provided with the distribution.
152fe8fb19SBen Gras *
162fe8fb19SBen Gras * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
172fe8fb19SBen Gras * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
182fe8fb19SBen Gras * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
192fe8fb19SBen Gras * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
202fe8fb19SBen Gras * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
212fe8fb19SBen Gras * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
222fe8fb19SBen Gras * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
232fe8fb19SBen Gras * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
242fe8fb19SBen Gras * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
252fe8fb19SBen Gras * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
262fe8fb19SBen Gras * SUCH DAMAGE.
272fe8fb19SBen Gras */
282fe8fb19SBen Gras
292fe8fb19SBen Gras #ifndef _MULTIBYTE_H_
302fe8fb19SBen Gras #define _MULTIBYTE_H_
312fe8fb19SBen Gras
322fe8fb19SBen Gras /* mbstate_t private */
332fe8fb19SBen Gras
342fe8fb19SBen Gras #ifdef _BSD_MBSTATE_T_
352fe8fb19SBen Gras typedef _BSD_MBSTATE_T_ mbstate_t;
362fe8fb19SBen Gras #undef _BSD_MBSTATE_T_
372fe8fb19SBen Gras #endif
382fe8fb19SBen Gras
392fe8fb19SBen Gras typedef struct _RuneStatePriv {
402fe8fb19SBen Gras _RuneLocale *__runelocale;
412fe8fb19SBen Gras char __private __attribute__((__aligned__));
422fe8fb19SBen Gras } _RuneStatePriv;
432fe8fb19SBen Gras
442fe8fb19SBen Gras typedef union _RuneState {
452fe8fb19SBen Gras mbstate_t __pad;
462fe8fb19SBen Gras struct _RuneStatePriv __priv;
472fe8fb19SBen Gras #define rs_runelocale __priv.__runelocale
482fe8fb19SBen Gras #define rs_private __priv.__private
492fe8fb19SBen Gras } _RuneState;
502fe8fb19SBen Gras #define _PRIVSIZE (sizeof(mbstate_t)-offsetof(_RuneStatePriv, __private))
512fe8fb19SBen Gras
52*84d9c625SLionel Sambuc #define _RUNE_LOCALE(loc) \
53*84d9c625SLionel Sambuc ((_RuneLocale *)((loc)->part_impl[(size_t)LC_CTYPE]))
54*84d9c625SLionel Sambuc
55*84d9c625SLionel Sambuc #define _CITRUS_CTYPE(loc) \
56*84d9c625SLionel Sambuc (((_RuneLocale *)((loc)->part_impl[(size_t)LC_CTYPE]))->rl_citrus_ctype)
572fe8fb19SBen Gras
582fe8fb19SBen Gras /* */
592fe8fb19SBen Gras
602fe8fb19SBen Gras static __inline _RuneState *
_ps_to_runestate(mbstate_t * ps)612fe8fb19SBen Gras _ps_to_runestate(mbstate_t *ps)
622fe8fb19SBen Gras {
632fe8fb19SBen Gras return (_RuneState *)(void *)ps;
642fe8fb19SBen Gras }
652fe8fb19SBen Gras
662fe8fb19SBen Gras static __inline _RuneState const *
_ps_to_runestate_const(mbstate_t const * ps)672fe8fb19SBen Gras _ps_to_runestate_const(mbstate_t const *ps)
682fe8fb19SBen Gras {
692fe8fb19SBen Gras return (_RuneState const *)(void const *)ps;
702fe8fb19SBen Gras }
712fe8fb19SBen Gras
722fe8fb19SBen Gras static __inline _RuneLocale *
_ps_to_runelocale(mbstate_t const * ps)732fe8fb19SBen Gras _ps_to_runelocale(mbstate_t const *ps)
742fe8fb19SBen Gras {
752fe8fb19SBen Gras return _ps_to_runestate_const(ps)->rs_runelocale;
762fe8fb19SBen Gras }
772fe8fb19SBen Gras
782fe8fb19SBen Gras static __inline _citrus_ctype_t
_ps_to_ctype(mbstate_t const * ps,locale_t loc)79*84d9c625SLionel Sambuc _ps_to_ctype(mbstate_t const *ps, locale_t loc)
802fe8fb19SBen Gras {
812fe8fb19SBen Gras if (!ps)
82*84d9c625SLionel Sambuc return _CITRUS_CTYPE(loc);
832fe8fb19SBen Gras
842fe8fb19SBen Gras _DIAGASSERT(_ps_to_runelocale(ps) != NULL);
852fe8fb19SBen Gras
862fe8fb19SBen Gras return _ps_to_runelocale(ps)->rl_citrus_ctype;
872fe8fb19SBen Gras }
882fe8fb19SBen Gras
892fe8fb19SBen Gras static __inline void *
_ps_to_private(mbstate_t * ps)902fe8fb19SBen Gras _ps_to_private(mbstate_t *ps)
912fe8fb19SBen Gras {
922fe8fb19SBen Gras if (ps == NULL)
932fe8fb19SBen Gras return NULL;
942fe8fb19SBen Gras return (void *)&_ps_to_runestate(ps)->rs_private;
952fe8fb19SBen Gras }
962fe8fb19SBen Gras
972fe8fb19SBen Gras static __inline void const *
_ps_to_private_const(mbstate_t const * ps)982fe8fb19SBen Gras _ps_to_private_const(mbstate_t const *ps)
992fe8fb19SBen Gras {
1002fe8fb19SBen Gras if (ps == NULL)
1012fe8fb19SBen Gras return NULL;
1022fe8fb19SBen Gras return (void const *)&_ps_to_runestate_const(ps)->rs_private;
1032fe8fb19SBen Gras }
1042fe8fb19SBen Gras
1052fe8fb19SBen Gras static __inline void
_init_ps(_RuneLocale * rl,mbstate_t * ps)1062fe8fb19SBen Gras _init_ps(_RuneLocale *rl, mbstate_t *ps)
1072fe8fb19SBen Gras {
1082fe8fb19SBen Gras size_t dum;
1092fe8fb19SBen Gras _ps_to_runestate(ps)->rs_runelocale = rl;
1102fe8fb19SBen Gras _citrus_ctype_mbrtowc(rl->rl_citrus_ctype, NULL, NULL, 0,
1112fe8fb19SBen Gras _ps_to_private(ps), &dum);
1122fe8fb19SBen Gras }
1132fe8fb19SBen Gras
1142fe8fb19SBen Gras static __inline void
_fixup_ps(_RuneLocale * rl,mbstate_t * ps,int forceinit)1152fe8fb19SBen Gras _fixup_ps(_RuneLocale *rl, mbstate_t *ps, int forceinit)
1162fe8fb19SBen Gras {
1172fe8fb19SBen Gras /* for future multi-locale facility */
1182fe8fb19SBen Gras _DIAGASSERT(rl != NULL);
1192fe8fb19SBen Gras
1202fe8fb19SBen Gras if (ps != NULL && (_ps_to_runelocale(ps) == NULL || forceinit)) {
1212fe8fb19SBen Gras _init_ps(rl, ps);
1222fe8fb19SBen Gras }
1232fe8fb19SBen Gras }
1242fe8fb19SBen Gras
1252fe8fb19SBen Gras #endif /*_MULTIBYTE_H_*/
126