- Update to r53061
[reactos.git] / include / host / 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 #include <string.h>
26 #include <typedefs.h>
27
28 // Definitions copied from <winnls.h>
29 // We only want to include host headers, so we define them manually
30 #define C1_UPPER 1
31 #define C1_LOWER 2
32 #define C1_DIGIT 4
33 #define C1_SPACE 8
34 #define C1_PUNCT 16
35 #define C1_CNTRL 32
36 #define C1_BLANK 64
37 #define C1_XDIGIT 128
38 #define C1_ALPHA 256
39 #define MB_COMPOSITE 2
40 #define MB_ERR_INVALID_CHARS 8
41 #define MB_USEGLYPHCHARS 0x04
42 #define WC_COMPOSITECHECK 512
43 #define WC_DISCARDNS 16
44 #define WC_DEFAULTCHAR 64
45 #define WC_NO_BEST_FIT_CHARS 1024
46 #define WC_ERR_INVALID_CHARS 0x0080
47
48 #ifdef __WINE_WINE_TEST_H
49 #error This file should not be used in Wine tests
50 #endif
51
52 #ifdef __cplusplus
53 extern "C" {
54 #endif
55
56 #ifndef WINE_UNICODE_API
57 #define WINE_UNICODE_API
58 #endif
59
60 #ifndef WINE_UNICODE_INLINE
61 #define WINE_UNICODE_INLINE static inline
62 #endif
63
64 /* code page info common to SBCS and DBCS */
65 struct cp_info
66 {
67 unsigned int codepage; /* codepage id */
68 unsigned int char_size; /* char size (1 or 2 bytes) */
69 WCHAR def_char; /* default char value (can be double-byte) */
70 WCHAR def_unicode_char; /* default Unicode char value */
71 const char *name; /* code page name */
72 };
73
74 struct sbcs_table
75 {
76 struct cp_info info;
77 const WCHAR *cp2uni; /* code page -> Unicode map */
78 const WCHAR *cp2uni_glyphs; /* code page -> Unicode map with glyph chars */
79 const unsigned char *uni2cp_low; /* Unicode -> code page map */
80 const unsigned short *uni2cp_high;
81 };
82
83 struct dbcs_table
84 {
85 struct cp_info info;
86 const WCHAR *cp2uni; /* code page -> Unicode map */
87 const unsigned char *cp2uni_leadbytes;
88 const unsigned short *uni2cp_low; /* Unicode -> code page map */
89 const unsigned short *uni2cp_high;
90 unsigned char lead_bytes[12]; /* lead bytes ranges */
91 };
92
93 union cptable
94 {
95 struct cp_info info;
96 struct sbcs_table sbcs;
97 struct dbcs_table dbcs;
98 };
99
100 extern const union cptable *wine_cp_get_table( unsigned int codepage );
101 extern const union cptable *wine_cp_enum_table( unsigned int index );
102
103 extern int wine_cp_mbstowcs( const union cptable *table, int flags,
104 const char *src, int srclen,
105 WCHAR *dst, int dstlen );
106 extern int wine_cp_wcstombs( const union cptable *table, int flags,
107 const WCHAR *src, int srclen,
108 char *dst, int dstlen, const char *defchar, int *used );
109 extern int wine_cpsymbol_mbstowcs( const char *src, int srclen, WCHAR *dst, int dstlen );
110 extern int wine_cpsymbol_wcstombs( const WCHAR *src, int srclen, char *dst, int dstlen );
111 extern int wine_utf8_mbstowcs( int flags, const char *src, int srclen, WCHAR *dst, int dstlen );
112 extern int wine_utf8_wcstombs( int flags, const WCHAR *src, int srclen, char *dst, int dstlen );
113
114 extern int wine_compare_string( int flags, const WCHAR *str1, int len1, const WCHAR *str2, int len2 );
115 extern int wine_get_sortkey( int flags, const WCHAR *src, int srclen, char *dst, int dstlen );
116 extern int wine_fold_string( int flags, const WCHAR *src, int srclen , WCHAR *dst, int dstlen );
117
118 extern int strcmpiW( const WCHAR *str1, const WCHAR *str2 );
119 extern int strncmpiW( const WCHAR *str1, const WCHAR *str2, int n );
120 extern int memicmpW( const WCHAR *str1, const WCHAR *str2, int n );
121 extern WCHAR *strstrW( const WCHAR *str, const WCHAR *sub );
122 extern long int strtolW( const WCHAR *nptr, WCHAR **endptr, int base );
123 extern unsigned long int strtoulW( const WCHAR *nptr, WCHAR **endptr, int base );
124 extern int sprintfW( WCHAR *str, const WCHAR *format, ... );
125 extern int snprintfW( WCHAR *str, size_t len, const WCHAR *format, ... );
126 extern int vsprintfW( WCHAR *str, const WCHAR *format, va_list valist );
127 extern int vsnprintfW( WCHAR *str, size_t len, const WCHAR *format, va_list valist );
128
129 WINE_UNICODE_INLINE int wine_is_dbcs_leadbyte( const union cptable *table, unsigned char ch )
130 {
131 return (table->info.char_size == 2) && (table->dbcs.cp2uni_leadbytes[ch]);
132 }
133
134 WINE_UNICODE_INLINE WCHAR tolowerW( WCHAR ch )
135 {
136 extern WINE_UNICODE_API const WCHAR wine_casemap_lower[];
137 return ch + wine_casemap_lower[wine_casemap_lower[ch >> 8] + (ch & 0xff)];
138 }
139
140 WINE_UNICODE_INLINE WCHAR toupperW( WCHAR ch )
141 {
142 extern WINE_UNICODE_API const WCHAR wine_casemap_upper[];
143 return ch + wine_casemap_upper[wine_casemap_upper[ch >> 8] + (ch & 0xff)];
144 }
145
146 /* the character type contains the C1_* flags in the low 12 bits */
147 /* and the C2_* type in the high 4 bits */
148 WINE_UNICODE_INLINE unsigned short get_char_typeW( WCHAR ch )
149 {
150 extern WINE_UNICODE_API const unsigned short wine_wctype_table[];
151 return wine_wctype_table[wine_wctype_table[ch >> 8] + (ch & 0xff)];
152 }
153
154 WINE_UNICODE_INLINE int iscntrlW( WCHAR wc )
155 {
156 return get_char_typeW(wc) & C1_CNTRL;
157 }
158
159 WINE_UNICODE_INLINE int ispunctW( WCHAR wc )
160 {
161 return get_char_typeW(wc) & C1_PUNCT;
162 }
163
164 WINE_UNICODE_INLINE int isspaceW( WCHAR wc )
165 {
166 return get_char_typeW(wc) & C1_SPACE;
167 }
168
169 WINE_UNICODE_INLINE int isdigitW( WCHAR wc )
170 {
171 return get_char_typeW(wc) & C1_DIGIT;
172 }
173
174 WINE_UNICODE_INLINE int isxdigitW( WCHAR wc )
175 {
176 return get_char_typeW(wc) & C1_XDIGIT;
177 }
178
179 WINE_UNICODE_INLINE int islowerW( WCHAR wc )
180 {
181 return get_char_typeW(wc) & C1_LOWER;
182 }
183
184 WINE_UNICODE_INLINE int isupperW( WCHAR wc )
185 {
186 return get_char_typeW(wc) & C1_UPPER;
187 }
188
189 WINE_UNICODE_INLINE int isalnumW( WCHAR wc )
190 {
191 return get_char_typeW(wc) & (C1_ALPHA|C1_DIGIT|C1_LOWER|C1_UPPER);
192 }
193
194 WINE_UNICODE_INLINE int isalphaW( WCHAR wc )
195 {
196 return get_char_typeW(wc) & (C1_ALPHA|C1_LOWER|C1_UPPER);
197 }
198
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 {
206 return get_char_typeW(wc) & (C1_ALPHA|C1_BLANK|C1_PUNCT|C1_DIGIT|C1_LOWER|C1_UPPER);
207 }
208
209 /* some useful string manipulation routines */
210
211 WINE_UNICODE_INLINE unsigned int strlenW( const WCHAR *str )
212 {
213 const WCHAR *s = str;
214 while (*s) s++;
215 return (unsigned int)(s - str);
216 }
217
218 WINE_UNICODE_INLINE WCHAR *strcpyW( WCHAR *dst, const WCHAR *src )
219 {
220 WCHAR *p = dst;
221 while ((*p++ = *src++));
222 return dst;
223 }
224
225 /* strncpy doesn't do what you think, don't use it */
226 #define strncpyW(d,s,n) error do_not_use_strncpyW_use_lstrcpynW_or_memcpy_instead
227
228 WINE_UNICODE_INLINE int strcmpW( const WCHAR *str1, const WCHAR *str2 )
229 {
230 while (*str1 && (*str1 == *str2)) { str1++; str2++; }
231 return *str1 - *str2;
232 }
233
234 WINE_UNICODE_INLINE int strncmpW( const WCHAR *str1, const WCHAR *str2, int n )
235 {
236 if (n <= 0) return 0;
237 while ((--n > 0) && *str1 && (*str1 == *str2)) { str1++; str2++; }
238 return *str1 - *str2;
239 }
240
241 WINE_UNICODE_INLINE WCHAR *strcatW( WCHAR *dst, const WCHAR *src )
242 {
243 strcpyW( dst + strlenW(dst), src );
244 return dst;
245 }
246
247 WINE_UNICODE_INLINE WCHAR *strchrW( const WCHAR *str, WCHAR ch )
248 {
249 do { if (*str == ch) return (WCHAR *)(ULONG_PTR)str; } while (*str++);
250 return NULL;
251 }
252
253 WINE_UNICODE_INLINE WCHAR *strrchrW( const WCHAR *str, WCHAR ch )
254 {
255 WCHAR *ret = NULL;
256 do { if (*str == ch) ret = (WCHAR *)(ULONG_PTR)str; } while (*str++);
257 return ret;
258 }
259
260 WINE_UNICODE_INLINE WCHAR *strpbrkW( const WCHAR *str, const WCHAR *accept )
261 {
262 for ( ; *str; str++) if (strchrW( accept, *str )) return (WCHAR *)(ULONG_PTR)str;
263 return NULL;
264 }
265
266 WINE_UNICODE_INLINE size_t strspnW( const WCHAR *str, const WCHAR *accept )
267 {
268 const WCHAR *ptr;
269 for (ptr = str; *ptr; ptr++) if (!strchrW( accept, *ptr )) break;
270 return ptr - str;
271 }
272
273 WINE_UNICODE_INLINE size_t strcspnW( const WCHAR *str, const WCHAR *reject )
274 {
275 const WCHAR *ptr;
276 for (ptr = str; *ptr; ptr++) if (strchrW( reject, *ptr )) break;
277 return ptr - str;
278 }
279
280 WINE_UNICODE_INLINE WCHAR *strlwrW( WCHAR *str )
281 {
282 WCHAR *ret = str;
283 while ((*str = tolowerW(*str))) str++;
284 return ret;
285 }
286
287 WINE_UNICODE_INLINE WCHAR *struprW( WCHAR *str )
288 {
289 WCHAR *ret = str;
290 while ((*str = toupperW(*str))) str++;
291 return ret;
292 }
293
294 WINE_UNICODE_INLINE WCHAR *memchrW( const WCHAR *ptr, WCHAR ch, size_t n )
295 {
296 const WCHAR *end;
297 for (end = ptr + n; ptr < end; ptr++) if (*ptr == ch) return (WCHAR *)(ULONG_PTR)ptr;
298 return NULL;
299 }
300
301 WINE_UNICODE_INLINE WCHAR *memrchrW( const WCHAR *ptr, WCHAR ch, size_t n )
302 {
303 const WCHAR *end;
304 WCHAR *ret = NULL;
305 for (end = ptr + n; ptr < end; ptr++) if (*ptr == ch) ret = (WCHAR *)(ULONG_PTR)ptr;
306 return ret;
307 }
308
309 WINE_UNICODE_INLINE long int atolW( const WCHAR *str )
310 {
311 return strtolW( str, (WCHAR **)0, 10 );
312 }
313
314 WINE_UNICODE_INLINE int atoiW( const WCHAR *str )
315 {
316 return (int)atolW( str );
317 }
318
319 #undef WINE_UNICODE_INLINE
320
321 #ifdef __cplusplus
322 }
323 #endif
324
325 #endif /* __WINE_WINE_UNICODE_H */