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