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