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