]> pd.if.org Git - pdclib/blob - internals/_PDCLIB_locale.h
PDCLIB-25 #resolve #comment Made ctype, wctype and codec data const.(This may be...
[pdclib] / internals / _PDCLIB_locale.h
1 #ifndef __PDCLIB_LOCALE_H
2 #define __PDCLIB_LOCALE_H __PDCLIB_LOCALE_H
3 #include <_PDCLIB_int.h>
4 #include <locale.h>
5 #include <wctype.h>
6 #include <threads.h>
7 #include <stdlib.h>
8
9 #define _PDCLIB_LOCALE_METHOD_TSS           't'
10 #define _PDCLIB_LOCALE_METHOD_THREAD_LOCAL  'T'
11
12 #if !defined(_PDCLIB_LOCALE_METHOD)
13     #error _PDCLIB_LOCALE_METHOD undefined: don't know where I'm storing the thread locale
14 #elif _PDCLIB_LOCALE_METHOD == _PDCLIB_LOCALE_METHOD_TSS
15     extern tss_t _PDCLIB_locale_tss;
16     static inline locale_t _PDCLIB_threadlocale( void )
17     {
18         locale_t l = tss_get(_PDCLIB_locale_tss);
19         if(l == NULL)
20             l = &_PDCLIB_global_locale;
21         return l;
22     }
23
24     static inline void _PDCLIB_setthreadlocale( locale_t l )
25     {
26         if(tss_set(_PDCLIB_locale_tss, l) != thrd_success)
27             abort();
28     }
29 #elif _PDCLIB_LOCALE_METHOD == _PDCLIB_LOCALE_METHOD_THREAD_LOCAL
30     extern thread_local locale_t _PDCLIB_locale_tls;
31     #define _PDCLIB_threadlocale() (_PDCLIB_locale_tls || &_PDCLIB_global_locale)
32     static inline locale_t _PDCLIB_threadlocale( void )
33     {
34         locale_t l = _PDCLIB_locale_tls;
35         if(l == NULL)
36             l = &_PDCLIB_global_locale;
37         return l;
38     }
39
40     static inline void _PDCLIB_setthreadlocale( locale_t l )
41     {
42         _PDCLIB_locale_tls = l;
43     }
44 #else
45     #error Locale TSS method unspecified
46 #endif
47
48 /* -------------------------------------------------------------------------- */
49 /* <ctype.h> lookup tables                                                    */
50 /* -------------------------------------------------------------------------- */
51
52 #define _PDCLIB_CTYPE_ALPHA   1
53 #define _PDCLIB_CTYPE_BLANK   2
54 #define _PDCLIB_CTYPE_CNTRL   4
55 #define _PDCLIB_CTYPE_GRAPH   8
56 #define _PDCLIB_CTYPE_PUNCT  16
57 #define _PDCLIB_CTYPE_SPACE  32
58 #define _PDCLIB_CTYPE_LOWER  64
59 #define _PDCLIB_CTYPE_UPPER 128
60 #define _PDCLIB_CTYPE_DIGIT 256
61 #define _PDCLIB_CTYPE_XDIGT 512
62
63 #define _PDCLIB_WCTRANS_TOLOWER 1
64 #define _PDCLIB_WCTRANS_TOUPPER 2
65
66 typedef struct _PDCLIB_ctype
67 {
68     _PDCLIB_uint16_t flags;
69     unsigned char upper;
70     unsigned char lower;
71     unsigned char collation;
72 } _PDCLIB_ctype_t;
73
74 typedef struct _PDCLIB_wcinfo
75 {
76     _PDCLIB_wint_t   num;
77     _PDCLIB_uint16_t flags;
78     _PDCLIB_wint_t   lower;
79     _PDCLIB_wint_t   upper;
80 } _PDCLIB_wcinfo_t;
81
82 struct _PDCLIB_locale {
83     const _PDCLIB_charcodec_t    _Codec;
84     struct lconv                 _Conv;
85
86     /* ctype / wctype */
87     /* XXX: Maybe re-evaluate constness of these later on? */
88     const _PDCLIB_wcinfo_t      *_WCType;
89     _PDCLIB_size_t               _WCTypeSize;
90     const _PDCLIB_ctype_t       *_CType; 
91
92     /* perror/strerror */
93     char                        *_ErrnoStr[_PDCLIB_ERRNO_MAX];
94 };
95
96 extern _PDCLIB_wcinfo_t _PDCLIB_wcinfo[];
97 extern size_t           _PDCLIB_wcinfo_size;
98
99 static inline int _PDCLIB_wcinfo_cmp( const void * _key, const void * _obj )
100 {
101     _PDCLIB_uint32_t * key = (_PDCLIB_uint32_t *) _key;
102     _PDCLIB_wcinfo_t * obj = (_PDCLIB_wcinfo_t *) _obj;
103     return *key - obj->num;
104 }
105
106 static inline _PDCLIB_wcinfo_t * _PDCLIB_wcgetinfo( locale_t l, _PDCLIB_uint32_t num )
107 {
108     _PDCLIB_wcinfo_t *info = (_PDCLIB_wcinfo_t*) 
109         bsearch( &num, l->_WCType, l->_WCTypeSize, 
110                  sizeof( l->_WCType[0] ), _PDCLIB_wcinfo_cmp );
111
112     return info;
113 }
114
115 static inline wint_t _PDCLIB_unpackwint( wint_t wc )
116 {
117     if( sizeof(_PDCLIB_wchar_t) == 2 && sizeof(_PDCLIB_wint_t) == 4 ) {
118         /* On UTF-16 platforms, as an extension accept a "packed surrogate"
119          * encoding. We accept the surrogate pairs either way
120          */
121
122         wint_t c = (wc & 0xF800F800);
123         if(c == (_PDCLIB_wint_t) 0xD800DC00) {
124             // MSW: Lead, LSW: Trail
125             wint_t lead  = wc >> 16 & 0x3FF;
126             wint_t trail = wc       & 0x3FF;
127             wc = lead << 10 | trail;
128         } else if(c == (_PDCLIB_wint_t) 0xDC00D800) {
129             // MSW: Trail, LSW: Lead
130             wint_t trail = wc >> 16 & 0x3FF;
131             wint_t lead  = wc       & 0x3FF;
132             wc = lead << 10 | trail;
133         }
134
135     }
136     return wc;
137 }
138
139 /* Internal xlocale-style WCType API */
140 int _PDCLIB_iswalnum_l( wint_t _Wc, locale_t l );
141 int _PDCLIB_iswalpha_l( wint_t _Wc, locale_t l );
142 int _PDCLIB_iswblank_l( wint_t _Wc, locale_t l );
143 int _PDCLIB_iswcntrl_l( wint_t _Wc, locale_t l );
144 int _PDCLIB_iswdigit_l( wint_t _Wc, locale_t l );
145 int _PDCLIB_iswgraph_l( wint_t _Wc, locale_t l );
146 int _PDCLIB_iswlower_l( wint_t _Wc, locale_t l );
147 int _PDCLIB_iswprint_l( wint_t _Wc, locale_t l );
148 int _PDCLIB_iswpunct_l( wint_t _Wc, locale_t l );
149 int _PDCLIB_iswspace_l( wint_t _Wc, locale_t l );
150 int _PDCLIB_iswupper_l( wint_t _Wc, locale_t l );
151 int _PDCLIB_iswxdigit_l( wint_t _Wc, locale_t l );
152 int _PDCLIB_iswctype_l( wint_t _Wc, wctype_t _Desc, locale_t l );
153 wint_t _PDCLIB_towlower_l( wint_t _Wc, locale_t l );
154 wint_t _PDCLIB_towupper_l( wint_t _Wc, locale_t l );
155 wint_t _PDCLIB_towctrans_l( wint_t _Wc, wctrans_t _Desc, locale_t l );
156
157 #endif