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