wined3d: Duplicate GL textures for srgb switching.
[wine] / include / wine / unicode.h
1 /*
2  * Wine internal Unicode definitions
3  *
4  * Copyright 2000 Alexandre Julliard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #ifndef __WINE_WINE_UNICODE_H
22 #define __WINE_WINE_UNICODE_H
23
24 #include <stdarg.h>
25
26 #include <windef.h>
27 #include <winbase.h>
28 #include <winnls.h>
29
30 #ifdef __WINE_WINE_TEST_H
31 #error This file should not be used in Wine tests
32 #endif
33
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37
38 #ifndef WINE_UNICODE_API
39 # if defined(_MSC_VER) || defined(__MINGW32__)
40 #  define WINE_UNICODE_API DECLSPEC_IMPORT
41 # else
42 #  define WINE_UNICODE_API
43 # endif
44 #endif
45
46 #ifndef WINE_UNICODE_INLINE
47 #define WINE_UNICODE_INLINE extern inline
48 #endif
49
50 /* code page info common to SBCS and DBCS */
51 struct cp_info
52 {
53     unsigned int          codepage;          /* codepage id */
54     unsigned int          char_size;         /* char size (1 or 2 bytes) */
55     WCHAR                 def_char;          /* default char value (can be double-byte) */
56     WCHAR                 def_unicode_char;  /* default Unicode char value */
57     const char           *name;              /* code page name */
58 };
59
60 struct sbcs_table
61 {
62     struct cp_info        info;
63     const WCHAR          *cp2uni;            /* code page -> Unicode map */
64     const WCHAR          *cp2uni_glyphs;     /* code page -> Unicode map with glyph chars */
65     const unsigned char  *uni2cp_low;        /* Unicode -> code page map */
66     const unsigned short *uni2cp_high;
67 };
68
69 struct dbcs_table
70 {
71     struct cp_info        info;
72     const WCHAR          *cp2uni;            /* code page -> Unicode map */
73     const unsigned char  *cp2uni_leadbytes;
74     const unsigned short *uni2cp_low;        /* Unicode -> code page map */
75     const unsigned short *uni2cp_high;
76     unsigned char         lead_bytes[12];    /* lead bytes ranges */
77 };
78
79 union cptable
80 {
81     struct cp_info    info;
82     struct sbcs_table sbcs;
83     struct dbcs_table dbcs;
84 };
85
86 extern const union cptable *wine_cp_get_table( unsigned int codepage );
87 extern const union cptable *wine_cp_enum_table( unsigned int index );
88
89 extern int wine_cp_mbstowcs( const union cptable *table, int flags,
90                              const char *src, int srclen,
91                              WCHAR *dst, int dstlen );
92 extern int wine_cp_wcstombs( const union cptable *table, int flags,
93                              const WCHAR *src, int srclen,
94                              char *dst, int dstlen, const char *defchar, int *used );
95 extern int wine_cpsymbol_mbstowcs( const char *src, int srclen, WCHAR *dst, int dstlen );
96 extern int wine_cpsymbol_wcstombs( const WCHAR *src, int srclen, char *dst, int dstlen );
97 extern int wine_utf8_mbstowcs( int flags, const char *src, int srclen, WCHAR *dst, int dstlen );
98 extern int wine_utf8_wcstombs( int flags, const WCHAR *src, int srclen, char *dst, int dstlen );
99
100 extern int wine_compare_string( int flags, const WCHAR *str1, int len1, const WCHAR *str2, int len2 );
101 extern int wine_get_sortkey( int flags, const WCHAR *src, int srclen, char *dst, int dstlen );
102 extern int wine_fold_string( int flags, const WCHAR *src, int srclen , WCHAR *dst, int dstlen );
103
104 extern int strcmpiW( const WCHAR *str1, const WCHAR *str2 );
105 extern int strncmpiW( const WCHAR *str1, const WCHAR *str2, int n );
106 extern int memicmpW( const WCHAR *str1, const WCHAR *str2, int n );
107 extern WCHAR *strstrW( const WCHAR *str, const WCHAR *sub );
108 extern long int strtolW( const WCHAR *nptr, WCHAR **endptr, int base );
109 extern unsigned long int strtoulW( const WCHAR *nptr, WCHAR **endptr, int base );
110 extern int sprintfW( WCHAR *str, const WCHAR *format, ... );
111 extern int snprintfW( WCHAR *str, size_t len, const WCHAR *format, ... );
112 extern int vsprintfW( WCHAR *str, const WCHAR *format, va_list valist );
113 extern int vsnprintfW( WCHAR *str, size_t len, const WCHAR *format, va_list valist );
114
115 WINE_UNICODE_INLINE int wine_is_dbcs_leadbyte( const union cptable *table, unsigned char ch );
116 WINE_UNICODE_INLINE int wine_is_dbcs_leadbyte( const union cptable *table, unsigned char ch )
117 {
118     return (table->info.char_size == 2) && (table->dbcs.cp2uni_leadbytes[ch]);
119 }
120
121 WINE_UNICODE_INLINE WCHAR tolowerW( WCHAR ch );
122 WINE_UNICODE_INLINE WCHAR tolowerW( WCHAR ch )
123 {
124     extern WINE_UNICODE_API const WCHAR wine_casemap_lower[];
125     return ch + wine_casemap_lower[wine_casemap_lower[ch >> 8] + (ch & 0xff)];
126 }
127
128 WINE_UNICODE_INLINE WCHAR toupperW( WCHAR ch );
129 WINE_UNICODE_INLINE WCHAR toupperW( WCHAR ch )
130 {
131     extern WINE_UNICODE_API const WCHAR wine_casemap_upper[];
132     return ch + wine_casemap_upper[wine_casemap_upper[ch >> 8] + (ch & 0xff)];
133 }
134
135 /* the character type contains the C1_* flags in the low 12 bits */
136 /* and the C2_* type in the high 4 bits */
137 WINE_UNICODE_INLINE unsigned short get_char_typeW( WCHAR ch );
138 WINE_UNICODE_INLINE unsigned short get_char_typeW( WCHAR ch )
139 {
140     extern WINE_UNICODE_API const unsigned short wine_wctype_table[];
141     return wine_wctype_table[wine_wctype_table[ch >> 8] + (ch & 0xff)];
142 }
143
144 WINE_UNICODE_INLINE int iscntrlW( WCHAR wc );
145 WINE_UNICODE_INLINE int iscntrlW( WCHAR wc )
146 {
147     return get_char_typeW(wc) & C1_CNTRL;
148 }
149
150 WINE_UNICODE_INLINE int ispunctW( WCHAR wc );
151 WINE_UNICODE_INLINE int ispunctW( WCHAR wc )
152 {
153     return get_char_typeW(wc) & C1_PUNCT;
154 }
155
156 WINE_UNICODE_INLINE int isspaceW( WCHAR wc );
157 WINE_UNICODE_INLINE int isspaceW( WCHAR wc )
158 {
159     return get_char_typeW(wc) & C1_SPACE;
160 }
161
162 WINE_UNICODE_INLINE int isdigitW( WCHAR wc );
163 WINE_UNICODE_INLINE int isdigitW( WCHAR wc )
164 {
165     return get_char_typeW(wc) & C1_DIGIT;
166 }
167
168 WINE_UNICODE_INLINE int isxdigitW( WCHAR wc );
169 WINE_UNICODE_INLINE int isxdigitW( WCHAR wc )
170 {
171     return get_char_typeW(wc) & C1_XDIGIT;
172 }
173
174 WINE_UNICODE_INLINE int islowerW( WCHAR wc );
175 WINE_UNICODE_INLINE int islowerW( WCHAR wc )
176 {
177     return get_char_typeW(wc) & C1_LOWER;
178 }
179
180 WINE_UNICODE_INLINE int isupperW( WCHAR wc );
181 WINE_UNICODE_INLINE int isupperW( WCHAR wc )
182 {
183     return get_char_typeW(wc) & C1_UPPER;
184 }
185
186 WINE_UNICODE_INLINE int isalnumW( WCHAR wc );
187 WINE_UNICODE_INLINE int isalnumW( WCHAR wc )
188 {
189     return get_char_typeW(wc) & (C1_ALPHA|C1_DIGIT|C1_LOWER|C1_UPPER);
190 }
191
192 WINE_UNICODE_INLINE int isalphaW( WCHAR wc );
193 WINE_UNICODE_INLINE int isalphaW( WCHAR wc )
194 {
195     return get_char_typeW(wc) & (C1_ALPHA|C1_LOWER|C1_UPPER);
196 }
197
198 WINE_UNICODE_INLINE int isgraphW( WCHAR wc );
199 WINE_UNICODE_INLINE int isgraphW( WCHAR wc )
200 {
201     return get_char_typeW(wc) & (C1_ALPHA|C1_PUNCT|C1_DIGIT|C1_LOWER|C1_UPPER);
202 }
203
204 WINE_UNICODE_INLINE int isprintW( WCHAR wc );
205 WINE_UNICODE_INLINE int isprintW( WCHAR wc )
206 {
207     return get_char_typeW(wc) & (C1_ALPHA|C1_BLANK|C1_PUNCT|C1_DIGIT|C1_LOWER|C1_UPPER);
208 }
209
210 /* some useful string manipulation routines */
211
212 WINE_UNICODE_INLINE unsigned int strlenW( const WCHAR *str );
213 WINE_UNICODE_INLINE unsigned int strlenW( const WCHAR *str )
214 {
215     const WCHAR *s = str;
216     while (*s) s++;
217     return s - str;
218 }
219
220 WINE_UNICODE_INLINE WCHAR *strcpyW( WCHAR *dst, const WCHAR *src );
221 WINE_UNICODE_INLINE WCHAR *strcpyW( WCHAR *dst, const WCHAR *src )
222 {
223     WCHAR *p = dst;
224     while ((*p++ = *src++));
225     return dst;
226 }
227
228 /* strncpy doesn't do what you think, don't use it */
229 #define strncpyW(d,s,n) error do_not_use_strncpyW_use_lstrcpynW_or_memcpy_instead
230
231 WINE_UNICODE_INLINE int strcmpW( const WCHAR *str1, const WCHAR *str2 );
232 WINE_UNICODE_INLINE int strcmpW( const WCHAR *str1, const WCHAR *str2 )
233 {
234     while (*str1 && (*str1 == *str2)) { str1++; str2++; }
235     return *str1 - *str2;
236 }
237
238 WINE_UNICODE_INLINE int strncmpW( const WCHAR *str1, const WCHAR *str2, int n );
239 WINE_UNICODE_INLINE int strncmpW( const WCHAR *str1, const WCHAR *str2, int n )
240 {
241     if (n <= 0) return 0;
242     while ((--n > 0) && *str1 && (*str1 == *str2)) { str1++; str2++; }
243     return *str1 - *str2;
244 }
245
246 WINE_UNICODE_INLINE WCHAR *strcatW( WCHAR *dst, const WCHAR *src );
247 WINE_UNICODE_INLINE WCHAR *strcatW( WCHAR *dst, const WCHAR *src )
248 {
249     strcpyW( dst + strlenW(dst), src );
250     return dst;
251 }
252
253 WINE_UNICODE_INLINE WCHAR *strchrW( const WCHAR *str, WCHAR ch );
254 WINE_UNICODE_INLINE WCHAR *strchrW( const WCHAR *str, WCHAR ch )
255 {
256     do { if (*str == ch) return (WCHAR *)(ULONG_PTR)str; } while (*str++);
257     return NULL;
258 }
259
260 WINE_UNICODE_INLINE WCHAR *strrchrW( const WCHAR *str, WCHAR ch );
261 WINE_UNICODE_INLINE WCHAR *strrchrW( const WCHAR *str, WCHAR ch )
262 {
263     WCHAR *ret = NULL;
264     do { if (*str == ch) ret = (WCHAR *)(ULONG_PTR)str; } while (*str++);
265     return ret;
266 }
267
268 WINE_UNICODE_INLINE WCHAR *strpbrkW( const WCHAR *str, const WCHAR *accept );
269 WINE_UNICODE_INLINE WCHAR *strpbrkW( const WCHAR *str, const WCHAR *accept )
270 {
271     for ( ; *str; str++) if (strchrW( accept, *str )) return (WCHAR *)(ULONG_PTR)str;
272     return NULL;
273 }
274
275 WINE_UNICODE_INLINE size_t strspnW( const WCHAR *str, const WCHAR *accept );
276 WINE_UNICODE_INLINE size_t strspnW( const WCHAR *str, const WCHAR *accept )
277 {
278     const WCHAR *ptr;
279     for (ptr = str; *ptr; ptr++) if (!strchrW( accept, *ptr )) break;
280     return ptr - str;
281 }
282
283 WINE_UNICODE_INLINE size_t strcspnW( const WCHAR *str, const WCHAR *reject );
284 WINE_UNICODE_INLINE size_t strcspnW( const WCHAR *str, const WCHAR *reject )
285 {
286     const WCHAR *ptr;
287     for (ptr = str; *ptr; ptr++) if (strchrW( reject, *ptr )) break;
288     return ptr - str;
289 }
290
291 WINE_UNICODE_INLINE WCHAR *strlwrW( WCHAR *str );
292 WINE_UNICODE_INLINE WCHAR *strlwrW( WCHAR *str )
293 {
294     WCHAR *ret = str;
295     while ((*str = tolowerW(*str))) str++;
296     return ret;
297 }
298
299 WINE_UNICODE_INLINE WCHAR *struprW( WCHAR *str );
300 WINE_UNICODE_INLINE WCHAR *struprW( WCHAR *str )
301 {
302     WCHAR *ret = str;
303     while ((*str = toupperW(*str))) str++;
304     return ret;
305 }
306
307 WINE_UNICODE_INLINE WCHAR *memchrW( const WCHAR *ptr, WCHAR ch, size_t n );
308 WINE_UNICODE_INLINE WCHAR *memchrW( const WCHAR *ptr, WCHAR ch, size_t n )
309 {
310     const WCHAR *end;
311     for (end = ptr + n; ptr < end; ptr++) if (*ptr == ch) return (WCHAR *)(ULONG_PTR)ptr;
312     return NULL;
313 }
314
315 WINE_UNICODE_INLINE WCHAR *memrchrW( const WCHAR *ptr, WCHAR ch, size_t n );
316 WINE_UNICODE_INLINE WCHAR *memrchrW( const WCHAR *ptr, WCHAR ch, size_t n )
317 {
318     const WCHAR *end;
319     WCHAR *ret = NULL;
320     for (end = ptr + n; ptr < end; ptr++) if (*ptr == ch) ret = (WCHAR *)(ULONG_PTR)ptr;
321     return ret;
322 }
323
324 WINE_UNICODE_INLINE long int atolW( const WCHAR *str );
325 WINE_UNICODE_INLINE long int atolW( const WCHAR *str )
326 {
327     return strtolW( str, (WCHAR **)0, 10 );
328 }
329
330 WINE_UNICODE_INLINE int atoiW( const WCHAR *str );
331 WINE_UNICODE_INLINE int atoiW( const WCHAR *str )
332 {
333     return (int)atolW( str );
334 }
335
336 #undef WINE_UNICODE_INLINE
337
338 #ifdef __cplusplus
339 }
340 #endif
341
342 #endif  /* __WINE_WINE_UNICODE_H */