Merged msacm and msacm32 dlls.
[wine] / dlls / ntdll / wcstring.c
1 /*
2  * NTDLL wide-char functions
3  *
4  * Copyright 2000 Alexandre Julliard
5  */
6
7 #include "config.h"
8
9 #include <ctype.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #ifdef HAVE_WCTYPE_H
13 #include <wctype.h>
14 #endif
15
16 #include "windef.h"
17 #include "winbase.h"
18 #include "winnls.h"
19 #include "wine/unicode.h"
20 #include "heap.h"
21 #include "debugtools.h"
22
23 DEFAULT_DEBUG_CHANNEL(ntdll);
24
25
26 /*********************************************************************
27  *           NTDLL__wcsicmp    (NTDLL)
28  */
29 INT __cdecl NTDLL__wcsicmp( LPCWSTR str1, LPCWSTR str2 )
30 {
31     return strcmpiW( str1, str2 );
32 }
33
34
35 /*********************************************************************
36  *           NTDLL__wcslwr    (NTDLL)
37  */
38 LPWSTR __cdecl NTDLL__wcslwr( LPWSTR str )
39 {
40     return strlwrW( str );
41 }
42
43
44 /*********************************************************************
45  *           NTDLL__wcsnicmp    (NTDLL)
46  */
47 INT __cdecl NTDLL__wcsnicmp( LPCWSTR str1, LPCWSTR str2, INT n )
48 {
49     return strncmpiW( str1, str2, n );
50 }
51
52
53 /*********************************************************************
54  *           NTDLL__wcsupr    (NTDLL)
55  */
56 LPWSTR __cdecl NTDLL__wcsupr( LPWSTR str )
57 {
58     return struprW( str );
59 }
60
61
62 /*********************************************************************
63  *           NTDLL_towlower    (NTDLL)
64  */
65 WCHAR __cdecl NTDLL_towlower( WCHAR ch )
66 {
67     return tolowerW(ch);
68 }
69
70
71 /*********************************************************************
72  *           NTDLL_towupper    (NTDLL)
73  */
74 WCHAR __cdecl NTDLL_towupper( WCHAR ch )
75 {
76     return toupperW(ch);
77 }
78
79
80 /***********************************************************************
81  *           NTDLL_wcscat    (NTDLL)
82  */
83 LPWSTR __cdecl NTDLL_wcscat( LPWSTR dst, LPCWSTR src )
84 {
85     return strcatW( dst, src );
86 }
87
88
89 /*********************************************************************
90  *           NTDLL_wcschr    (NTDLL)
91  */
92 LPWSTR __cdecl NTDLL_wcschr( LPCWSTR str, WCHAR ch )
93 {
94     return strchrW( str, ch );
95 }
96
97
98 /*********************************************************************
99  *           NTDLL_wcscmp    (NTDLL)
100  */
101 INT __cdecl NTDLL_wcscmp( LPCWSTR str1, LPCWSTR str2 )
102 {
103     return strcmpW( str1, str2 );
104 }
105
106
107 /***********************************************************************
108  *           NTDLL_wcscpy    (NTDLL)
109  */
110 LPWSTR __cdecl NTDLL_wcscpy( LPWSTR dst, LPCWSTR src )
111 {
112     return strcpyW( dst, src );
113 }
114
115
116 /*********************************************************************
117  *           NTDLL_wcscspn    (NTDLL)
118  */
119 INT __cdecl NTDLL_wcscspn( LPCWSTR str, LPCWSTR reject )
120 {
121     LPCWSTR start = str;
122     while (*str)
123     {
124         LPCWSTR p = reject;
125         while (*p && (*p != *str)) p++;
126         if (*p) break;
127         str++;
128     }
129     return str - start;
130 }
131
132
133 /***********************************************************************
134  *           NTDLL_wcslen    (NTDLL)
135  */
136 INT __cdecl NTDLL_wcslen( LPCWSTR str )
137 {
138     return strlenW( str );
139 }
140
141
142 /*********************************************************************
143  *           NTDLL_wcsncat    (NTDLL)
144  */
145 LPWSTR __cdecl NTDLL_wcsncat( LPWSTR s1, LPCWSTR s2, INT n )
146 {
147     LPWSTR ret = s1;
148     while (*s1) s1++;
149     while (n-- > 0) if (!(*s1++ = *s2++)) return ret;
150     *s1 = 0;
151     return ret;
152 }
153
154
155 /*********************************************************************
156  *           NTDLL_wcsncmp    (NTDLL)
157  */
158 INT __cdecl NTDLL_wcsncmp( LPCWSTR str1, LPCWSTR str2, INT n )
159 {
160     return strncmpW( str1, str2, n );
161 }
162
163
164 /*********************************************************************
165  *           NTDLL_wcsncpy    (NTDLL)
166  */
167 LPWSTR __cdecl NTDLL_wcsncpy( LPWSTR s1, LPCWSTR s2, INT n )
168 {
169     LPWSTR ret = s1;
170     while (n-- > 0) if (!(*s1++ = *s2++)) break;
171     while (n-- > 0) *s1++ = 0;
172     return ret;
173 }
174
175
176 /*********************************************************************
177  *           NTDLL_wcspbrk    (NTDLL)
178  */
179 LPWSTR __cdecl NTDLL_wcspbrk( LPCWSTR str, LPCWSTR accept )
180 {
181     LPCWSTR p;
182     while (*str)
183     {
184         for (p = accept; *p; p++) if (*p == *str) return (LPWSTR)str;
185         str++;
186     }
187     return NULL;
188 }
189
190
191 /*********************************************************************
192  *           NTDLL_wcsrchr    (NTDLL)
193  */
194 LPWSTR __cdecl NTDLL_wcsrchr( LPWSTR str, WCHAR ch )
195 {
196     LPWSTR last = NULL;
197     while (*str)
198     {
199         if (*str == ch) last = str;
200         str++;
201     }
202     return last;
203 }
204
205
206 /*********************************************************************
207  *           NTDLL_wcsspn    (NTDLL)
208  */
209 INT __cdecl NTDLL_wcsspn( LPCWSTR str, LPCWSTR accept )
210 {
211     LPCWSTR start = str;
212     while (*str)
213     {
214         LPCWSTR p = accept;
215         while (*p && (*p != *str)) p++;
216         if (!*p) break;
217         str++;
218     }
219     return str - start;
220 }
221
222
223 /*********************************************************************
224  *           NTDLL_wcsstr    (NTDLL)
225  */
226 LPWSTR __cdecl NTDLL_wcsstr( LPCWSTR str, LPCWSTR sub )
227 {
228     return strstrW( str, sub );
229 }
230
231
232 /*********************************************************************
233  *           NTDLL_wcstok    (NTDLL)
234  */
235 LPWSTR __cdecl NTDLL_wcstok( LPWSTR str, LPCWSTR delim )
236 {
237     static LPWSTR next = NULL;
238     LPWSTR ret;
239
240     if (!str)
241         if (!(str = next)) return NULL;
242
243     while (*str && NTDLL_wcschr( delim, *str )) str++;
244     if (!*str) return NULL;
245     ret = str++;
246     while (*str && !NTDLL_wcschr( delim, *str )) str++;
247     if (*str) *str++ = 0;
248     next = str;
249     return ret;
250 }
251
252
253 /*********************************************************************
254  *           NTDLL_wcstombs    (NTDLL)
255  */
256 INT __cdecl NTDLL_wcstombs( LPSTR dst, LPCWSTR src, INT n )
257 {
258     INT ret;
259     if (n <= 0) return 0;
260     ret = WideCharToMultiByte( CP_ACP, 0, src, -1, dst, dst ? n : 0, NULL, NULL );
261     if (!ret) return n;  /* overflow */
262     return ret - 1;  /* do not count terminating NULL */
263 }
264
265
266 /*********************************************************************
267  *           NTDLL_mbstowcs    (NTDLL)
268  */
269 INT __cdecl NTDLL_mbstowcs( LPWSTR dst, LPCSTR src, INT n )
270 {
271     INT ret;
272     if (n <= 0) return 0;
273     ret = MultiByteToWideChar( CP_ACP, 0, src, -1, dst, dst ? n : 0 );
274     if (!ret) return n;  /* overflow */
275     return ret - 1;  /* do not count terminating NULL */
276 }
277
278
279 /*********************************************************************
280  *                  wcstol  (NTDLL)
281  * Like strtol, but for wide character strings.
282  */
283 INT __cdecl NTDLL_wcstol(LPWSTR s,LPWSTR *end,INT base)
284 {
285     LPSTR sA = HEAP_strdupWtoA(GetProcessHeap(),0,s),endA;
286     INT ret = strtol(sA,&endA,base);
287
288     HeapFree(GetProcessHeap(),0,sA);
289     if (end) *end = s+(endA-sA); /* pointer magic checked. */
290     return ret;
291 }
292
293
294 /*********************************************************************
295  *           NTDLL_iswctype    (NTDLL)
296  */
297 INT __cdecl NTDLL_iswctype( WCHAR wc, WCHAR wct )
298 {
299     INT res = 0;
300
301 #ifdef HAVE_WCTYPE_H
302 #undef iswupper
303 #undef iswlower
304 #undef iswdigit
305 #undef iswspace
306 #undef iswpunct
307 #undef iswcntrl
308 #undef iswxdigit
309 #undef iswalpha
310     if (wct & 0x0001) res |= iswupper(wc);
311     if (wct & 0x0002) res |= iswlower(wc);
312     if (wct & 0x0004) res |= iswdigit(wc);
313     if (wct & 0x0008) res |= iswspace(wc);
314     if (wct & 0x0010) res |= iswpunct(wc);
315     if (wct & 0x0020) res |= iswcntrl(wc);
316     if (wct & 0x0080) res |= iswxdigit(wc);
317     if (wct & 0x0100) res |= iswalpha(wc);
318 #else
319     if (wct & 0x0001) res |= isupper(LOBYTE(wc));
320     if (wct & 0x0002) res |= islower(LOBYTE(wc));
321     if (wct & 0x0004) res |= isdigit(LOBYTE(wc));
322     if (wct & 0x0008) res |= isspace(LOBYTE(wc));
323     if (wct & 0x0010) res |= ispunct(LOBYTE(wc));
324     if (wct & 0x0020) res |= iscntrl(LOBYTE(wc));
325     if (wct & 0x0080) res |= isxdigit(LOBYTE(wc));
326     if (wct & 0x0100) res |= isalpha(LOBYTE(wc));
327 #endif
328     if (wct & 0x0040)
329         FIXME(": iswctype(%04hx,_BLANK|...) requested\n",wc);
330     if (wct & 0x8000)
331         FIXME(": iswctype(%04hx,_LEADBYTE|...) requested\n",wc);
332     return res;
333 }
334
335
336 /*********************************************************************
337  *           NTDLL_iswalpha    (NTDLL)
338  */
339 INT __cdecl NTDLL_iswalpha( WCHAR wc )
340 {
341     return NTDLL_iswctype( wc, 0x0100 );
342 }