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