summaryrefslogtreecommitdiff
path: root/lib/libc/locale/multibyte.h
blob: dcf64f2f68447ec68767557a020b669ae6c6d56f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*	$NetBSD: multibyte.h,v 1.7 2020/06/02 01:30:31 joerg Exp $	*/

/*-
 * Copyright (c)2002 Citrus Project,
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#ifndef _MULTIBYTE_H_
#define _MULTIBYTE_H_

#include <stdalign.h>

/* mbstate_t private */

#ifdef _BSD_MBSTATE_T_
typedef	_BSD_MBSTATE_T_	mbstate_t;
#undef _BSD_MBSTATE_T_
#endif

typedef struct _RuneStatePriv {
	_RuneLocale	*__runelocale;
	char		__private[];
} _RuneStatePriv;

__CTASSERT(alignof(struct _RuneStatePriv) >= alignof(void *));
__CTASSERT(sizeof(_RuneStatePriv) % alignof(void *) == 0);
__CTASSERT(alignof(struct _RuneStatePriv) >= alignof(int));
__CTASSERT(sizeof(_RuneStatePriv) % alignof(int) == 0);

typedef union _RuneState {
	mbstate_t		__pad;
	struct _RuneStatePriv	__priv;
#define rs_runelocale		__priv.__runelocale
#define rs_private		__priv.__private
} _RuneState;
#define _PRIVSIZE	(sizeof(mbstate_t)-offsetof(_RuneStatePriv, __private))

#define _RUNE_LOCALE(loc) \
    ((_RuneLocale *)((loc)->part_impl[(size_t)LC_CTYPE]))

#define _CITRUS_CTYPE(loc) \
    (((_RuneLocale *)((loc)->part_impl[(size_t)LC_CTYPE]))->rl_citrus_ctype)

/* */

static __inline _RuneState *
_ps_to_runestate(mbstate_t *ps)
{
	return (_RuneState *)(void *)ps;
}

static __inline _RuneState const *
_ps_to_runestate_const(mbstate_t const *ps)
{
	return (_RuneState const *)(void const *)ps;
}

static __inline _RuneLocale *
_ps_to_runelocale(mbstate_t const *ps)
{
	return _ps_to_runestate_const(ps)->rs_runelocale;
}

static __inline _citrus_ctype_t
_ps_to_ctype(mbstate_t const *ps, locale_t loc)
{
	if (!ps)
		return _CITRUS_CTYPE(loc);

	_DIAGASSERT(_ps_to_runelocale(ps) != NULL);

	return _ps_to_runelocale(ps)->rl_citrus_ctype;
}

static __inline void *
_ps_to_private(mbstate_t *ps)
{
	if (ps == NULL)
		return NULL;
	return _ps_to_runestate(ps)->rs_private;
}

static __inline void const *
_ps_to_private_const(mbstate_t const *ps)
{
	if (ps == NULL)
		return NULL;
	return _ps_to_runestate_const(ps)->rs_private;
}

static __inline void
_init_ps(_RuneLocale *rl, mbstate_t *ps)
{
	size_t dum;
	_ps_to_runestate(ps)->rs_runelocale = rl;
	_citrus_ctype_mbrtowc(rl->rl_citrus_ctype, NULL, NULL, 0,
			      _ps_to_private(ps), &dum);
}

static __inline void
_fixup_ps(_RuneLocale *rl, mbstate_t *ps, int forceinit)
{
	/* for future multi-locale facility */
	_DIAGASSERT(rl != NULL);

	if (ps != NULL && (_ps_to_runelocale(ps) == NULL || forceinit)) {
		_init_ps(rl, ps);
	}
}

#endif /*_MULTIBYTE_H_*/