1 | /*
|
---|
2 | * NTDLL wide-char functions
|
---|
3 | *
|
---|
4 | * Copyright 2000 Alexandre Julliard
|
---|
5 | * Copyright 2000 Jon Griffiths
|
---|
6 | */
|
---|
7 |
|
---|
8 | #include "config.h"
|
---|
9 |
|
---|
10 | #include <ctype.h>
|
---|
11 | #include <limits.h>
|
---|
12 | #include <stdlib.h>
|
---|
13 | #include <string.h>
|
---|
14 | #include <stdio.h>
|
---|
15 |
|
---|
16 | #ifdef __WIN32OS2__
|
---|
17 | #include <stdarg.h>
|
---|
18 | #include <heapstring.h>
|
---|
19 | #endif
|
---|
20 |
|
---|
21 | #include "windef.h"
|
---|
22 | #include "winbase.h"
|
---|
23 | #include "winnls.h"
|
---|
24 | #include "wine/unicode.h"
|
---|
25 | #include "heap.h"
|
---|
26 | #include "debugtools.h"
|
---|
27 |
|
---|
28 | DEFAULT_DEBUG_CHANNEL(ntdll);
|
---|
29 |
|
---|
30 |
|
---|
31 | /*********************************************************************
|
---|
32 | * _wcsicmp (NTDLL.@)
|
---|
33 | */
|
---|
34 | INT __cdecl NTDLL__wcsicmp( LPCWSTR str1, LPCWSTR str2 )
|
---|
35 | {
|
---|
36 | return strcmpiW( str1, str2 );
|
---|
37 | }
|
---|
38 |
|
---|
39 |
|
---|
40 | /*********************************************************************
|
---|
41 | * _wcslwr (NTDLL.@)
|
---|
42 | */
|
---|
43 | LPWSTR __cdecl NTDLL__wcslwr( LPWSTR str )
|
---|
44 | {
|
---|
45 | return strlwrW( str );
|
---|
46 | }
|
---|
47 |
|
---|
48 |
|
---|
49 | /*********************************************************************
|
---|
50 | * _wcsnicmp (NTDLL.@)
|
---|
51 | */
|
---|
52 | INT __cdecl NTDLL__wcsnicmp( LPCWSTR str1, LPCWSTR str2, INT n )
|
---|
53 | {
|
---|
54 | return strncmpiW( str1, str2, n );
|
---|
55 | }
|
---|
56 |
|
---|
57 |
|
---|
58 | /*********************************************************************
|
---|
59 | * _wcsupr (NTDLL.@)
|
---|
60 | */
|
---|
61 | LPWSTR __cdecl NTDLL__wcsupr( LPWSTR str )
|
---|
62 | {
|
---|
63 | return struprW( str );
|
---|
64 | }
|
---|
65 |
|
---|
66 |
|
---|
67 | /*********************************************************************
|
---|
68 | * towlower (NTDLL.@)
|
---|
69 | */
|
---|
70 | WCHAR __cdecl NTDLL_towlower( WCHAR ch )
|
---|
71 | {
|
---|
72 | return tolowerW(ch);
|
---|
73 | }
|
---|
74 |
|
---|
75 |
|
---|
76 | /*********************************************************************
|
---|
77 | * towupper (NTDLL.@)
|
---|
78 | */
|
---|
79 | WCHAR __cdecl NTDLL_towupper( WCHAR ch )
|
---|
80 | {
|
---|
81 | return toupperW(ch);
|
---|
82 | }
|
---|
83 |
|
---|
84 |
|
---|
85 | /***********************************************************************
|
---|
86 | * wcscat (NTDLL.@)
|
---|
87 | */
|
---|
88 | LPWSTR __cdecl NTDLL_wcscat( LPWSTR dst, LPCWSTR src )
|
---|
89 | {
|
---|
90 | return strcatW( dst, src );
|
---|
91 | }
|
---|
92 |
|
---|
93 |
|
---|
94 | /*********************************************************************
|
---|
95 | * wcschr (NTDLL.@)
|
---|
96 | */
|
---|
97 | LPWSTR __cdecl NTDLL_wcschr( LPCWSTR str, WCHAR ch )
|
---|
98 | {
|
---|
99 | return strchrW( str, ch );
|
---|
100 | }
|
---|
101 |
|
---|
102 |
|
---|
103 | /*********************************************************************
|
---|
104 | * wcscmp (NTDLL.@)
|
---|
105 | */
|
---|
106 | INT __cdecl NTDLL_wcscmp( LPCWSTR str1, LPCWSTR str2 )
|
---|
107 | {
|
---|
108 | return strcmpW( str1, str2 );
|
---|
109 | }
|
---|
110 |
|
---|
111 |
|
---|
112 | /***********************************************************************
|
---|
113 | * wcscpy (NTDLL.@)
|
---|
114 | */
|
---|
115 | LPWSTR __cdecl NTDLL_wcscpy( LPWSTR dst, LPCWSTR src )
|
---|
116 | {
|
---|
117 | return strcpyW( dst, src );
|
---|
118 | }
|
---|
119 |
|
---|
120 |
|
---|
121 | /*********************************************************************
|
---|
122 | * wcscspn (NTDLL.@)
|
---|
123 | */
|
---|
124 | INT __cdecl NTDLL_wcscspn( LPCWSTR str, LPCWSTR reject )
|
---|
125 | {
|
---|
126 | LPCWSTR start = str;
|
---|
127 | while (*str)
|
---|
128 | {
|
---|
129 | LPCWSTR p = reject;
|
---|
130 | while (*p && (*p != *str)) p++;
|
---|
131 | if (*p) break;
|
---|
132 | str++;
|
---|
133 | }
|
---|
134 | return str - start;
|
---|
135 | }
|
---|
136 |
|
---|
137 |
|
---|
138 | /***********************************************************************
|
---|
139 | * wcslen (NTDLL.@)
|
---|
140 | */
|
---|
141 | INT __cdecl NTDLL_wcslen( LPCWSTR str )
|
---|
142 | {
|
---|
143 | return strlenW( str );
|
---|
144 | }
|
---|
145 |
|
---|
146 |
|
---|
147 | /*********************************************************************
|
---|
148 | * wcsncat (NTDLL.@)
|
---|
149 | */
|
---|
150 | LPWSTR __cdecl NTDLL_wcsncat( LPWSTR s1, LPCWSTR s2, INT n )
|
---|
151 | {
|
---|
152 | LPWSTR ret = s1;
|
---|
153 | while (*s1) s1++;
|
---|
154 | while (n-- > 0) if (!(*s1++ = *s2++)) return ret;
|
---|
155 | *s1 = 0;
|
---|
156 | return ret;
|
---|
157 | }
|
---|
158 |
|
---|
159 |
|
---|
160 | /*********************************************************************
|
---|
161 | * wcsncmp (NTDLL.@)
|
---|
162 | */
|
---|
163 | INT __cdecl NTDLL_wcsncmp( LPCWSTR str1, LPCWSTR str2, INT n )
|
---|
164 | {
|
---|
165 | return strncmpW( str1, str2, n );
|
---|
166 | }
|
---|
167 |
|
---|
168 |
|
---|
169 | /*********************************************************************
|
---|
170 | * wcsncpy (NTDLL.@)
|
---|
171 | */
|
---|
172 | LPWSTR __cdecl NTDLL_wcsncpy( LPWSTR s1, LPCWSTR s2, INT n )
|
---|
173 | {
|
---|
174 | return strncpyW( s1, s2, n );
|
---|
175 | }
|
---|
176 |
|
---|
177 |
|
---|
178 | /*********************************************************************
|
---|
179 | * wcspbrk (NTDLL.@)
|
---|
180 | */
|
---|
181 | LPWSTR __cdecl NTDLL_wcspbrk( LPCWSTR str, LPCWSTR accept )
|
---|
182 | {
|
---|
183 | LPCWSTR p;
|
---|
184 | while (*str)
|
---|
185 | {
|
---|
186 | for (p = accept; *p; p++) if (*p == *str) return (LPWSTR)str;
|
---|
187 | str++;
|
---|
188 | }
|
---|
189 | return NULL;
|
---|
190 | }
|
---|
191 |
|
---|
192 |
|
---|
193 | /*********************************************************************
|
---|
194 | * wcsrchr (NTDLL.@)
|
---|
195 | */
|
---|
196 | LPWSTR __cdecl NTDLL_wcsrchr( LPWSTR str, WCHAR ch )
|
---|
197 | {
|
---|
198 | LPWSTR last = NULL;
|
---|
199 | while (*str)
|
---|
200 | {
|
---|
201 | if (*str == ch) last = str;
|
---|
202 | str++;
|
---|
203 | }
|
---|
204 | return last;
|
---|
205 | }
|
---|
206 |
|
---|
207 |
|
---|
208 | /*********************************************************************
|
---|
209 | * wcsspn (NTDLL.@)
|
---|
210 | */
|
---|
211 | INT __cdecl NTDLL_wcsspn( LPCWSTR str, LPCWSTR accept )
|
---|
212 | {
|
---|
213 | LPCWSTR start = str;
|
---|
214 | while (*str)
|
---|
215 | {
|
---|
216 | LPCWSTR p = accept;
|
---|
217 | while (*p && (*p != *str)) p++;
|
---|
218 | if (!*p) break;
|
---|
219 | str++;
|
---|
220 | }
|
---|
221 | return str - start;
|
---|
222 | }
|
---|
223 |
|
---|
224 |
|
---|
225 | /*********************************************************************
|
---|
226 | * wcsstr (NTDLL.@)
|
---|
227 | */
|
---|
228 | LPWSTR __cdecl NTDLL_wcsstr( LPCWSTR str, LPCWSTR sub )
|
---|
229 | {
|
---|
230 | return strstrW( str, sub );
|
---|
231 | }
|
---|
232 |
|
---|
233 |
|
---|
234 | /*********************************************************************
|
---|
235 | * wcstok (NTDLL.@)
|
---|
236 | */
|
---|
237 | LPWSTR __cdecl NTDLL_wcstok( LPWSTR str, LPCWSTR delim )
|
---|
238 | {
|
---|
239 | static LPWSTR next = NULL;
|
---|
240 | LPWSTR ret;
|
---|
241 |
|
---|
242 | if (!str)
|
---|
243 | if (!(str = next)) return NULL;
|
---|
244 |
|
---|
245 | while (*str && NTDLL_wcschr( delim, *str )) str++;
|
---|
246 | if (!*str) return NULL;
|
---|
247 | ret = str++;
|
---|
248 | while (*str && !NTDLL_wcschr( delim, *str )) str++;
|
---|
249 | if (*str) *str++ = 0;
|
---|
250 | next = str;
|
---|
251 | return ret;
|
---|
252 | }
|
---|
253 |
|
---|
254 |
|
---|
255 | /*********************************************************************
|
---|
256 | * wcstombs (NTDLL.@)
|
---|
257 | */
|
---|
258 | INT __cdecl NTDLL_wcstombs( LPSTR dst, LPCWSTR src, INT n )
|
---|
259 | {
|
---|
260 | INT ret;
|
---|
261 | if (n <= 0) return 0;
|
---|
262 | ret = WideCharToMultiByte( CP_ACP, 0, src, -1, dst, dst ? n : 0, NULL, NULL );
|
---|
263 | if (!ret) return n; /* overflow */
|
---|
264 | return ret - 1; /* do not count terminating NULL */
|
---|
265 | }
|
---|
266 |
|
---|
267 |
|
---|
268 | /*********************************************************************
|
---|
269 | * mbstowcs (NTDLL.@)
|
---|
270 | */
|
---|
271 | INT __cdecl NTDLL_mbstowcs( LPWSTR dst, LPCSTR src, INT n )
|
---|
272 | {
|
---|
273 | INT ret;
|
---|
274 | if (n <= 0) return 0;
|
---|
275 | ret = MultiByteToWideChar( CP_ACP, 0, src, -1, dst, dst ? n : 0 );
|
---|
276 | if (!ret) return n; /* overflow */
|
---|
277 | return ret - 1; /* do not count terminating NULL */
|
---|
278 | }
|
---|
279 |
|
---|
280 |
|
---|
281 | /*********************************************************************
|
---|
282 | * wcstol (NTDLL.@)
|
---|
283 | * Like strtol, but for wide character strings.
|
---|
284 | */
|
---|
285 | INT __cdecl NTDLL_wcstol(LPWSTR s,LPWSTR *end,INT base)
|
---|
286 | {
|
---|
287 | LPSTR sA = HEAP_strdupWtoA(GetProcessHeap(),0,s),endA;
|
---|
288 | INT ret = strtol(sA,&endA,base);
|
---|
289 |
|
---|
290 | HeapFree(GetProcessHeap(),0,sA);
|
---|
291 | if (end) *end = s+(endA-sA); /* pointer magic checked. */
|
---|
292 | return ret;
|
---|
293 | }
|
---|
294 |
|
---|
295 |
|
---|
296 | /*********************************************************************
|
---|
297 | * iswctype (NTDLL.@)
|
---|
298 | */
|
---|
299 | INT __cdecl NTDLL_iswctype( WCHAR wc, WCHAR wct )
|
---|
300 | {
|
---|
301 | return (get_char_typeW(wc) & 0xfff) & wct;
|
---|
302 | }
|
---|
303 |
|
---|
304 |
|
---|
305 | /*********************************************************************
|
---|
306 | * iswalpha (NTDLL.@)
|
---|
307 | */
|
---|
308 | INT __cdecl NTDLL_iswalpha( WCHAR wc )
|
---|
309 | {
|
---|
310 | return get_char_typeW(wc) & C1_ALPHA;
|
---|
311 | }
|
---|
312 |
|
---|
313 |
|
---|
314 | /*********************************************************************
|
---|
315 | * _ultow (NTDLL.@)
|
---|
316 | * Like _ultoa, but for wide character strings.
|
---|
317 | */
|
---|
318 | LPWSTR __cdecl _ultow(ULONG value, LPWSTR string, INT radix)
|
---|
319 | {
|
---|
320 | WCHAR tmp[33];
|
---|
321 | LPWSTR tp = tmp;
|
---|
322 | LPWSTR sp;
|
---|
323 | LONG i;
|
---|
324 | ULONG v = value;
|
---|
325 |
|
---|
326 | if (radix > 36 || radix <= 1)
|
---|
327 | return 0;
|
---|
328 |
|
---|
329 | while (v || tp == tmp)
|
---|
330 | {
|
---|
331 | i = v % radix;
|
---|
332 | v = v / radix;
|
---|
333 | if (i < 10)
|
---|
334 | *tp++ = i + '0';
|
---|
335 | else
|
---|
336 | *tp++ = i + 'a' - 10;
|
---|
337 | }
|
---|
338 |
|
---|
339 | sp = string;
|
---|
340 | while (tp > tmp)
|
---|
341 | *sp++ = *--tp;
|
---|
342 | *sp = 0;
|
---|
343 | return string;
|
---|
344 | }
|
---|
345 |
|
---|
346 | /*********************************************************************
|
---|
347 | * _wtol (NTDLL.@)
|
---|
348 | * Like atol, but for wide character strings.
|
---|
349 | */
|
---|
350 | LONG __cdecl _wtol(LPWSTR string)
|
---|
351 | {
|
---|
352 | char buffer[30];
|
---|
353 | NTDLL_wcstombs( buffer, string, sizeof(buffer) );
|
---|
354 | return atol( buffer );
|
---|
355 | }
|
---|
356 |
|
---|
357 | /*********************************************************************
|
---|
358 | * _wtoi (NTDLL.@)
|
---|
359 | */
|
---|
360 | INT __cdecl _wtoi(LPWSTR string)
|
---|
361 | {
|
---|
362 | return _wtol(string);
|
---|
363 | }
|
---|
364 |
|
---|
365 | /* INTERNAL: Wide char snprintf
|
---|
366 | * If you fix a bug in this function, fix it in msvcrt/wcs.c also!
|
---|
367 | */
|
---|
368 | static int __cdecl NTDLL_vsnwprintf(WCHAR *str, unsigned int len,
|
---|
369 | const WCHAR *format, va_list valist)
|
---|
370 | {
|
---|
371 | unsigned int written = 0;
|
---|
372 | const WCHAR *iter = format;
|
---|
373 | char bufa[256], fmtbufa[64], *fmta;
|
---|
374 |
|
---|
375 | TRACE("(%d,%s)\n",len,debugstr_w(format));
|
---|
376 |
|
---|
377 | while (*iter)
|
---|
378 | {
|
---|
379 | while (*iter && *iter != (WCHAR)L'%')
|
---|
380 | {
|
---|
381 | if (written++ >= len)
|
---|
382 | return -1;
|
---|
383 | *str++ = *iter++;
|
---|
384 | }
|
---|
385 | if (*iter == (WCHAR)L'%')
|
---|
386 | {
|
---|
387 | fmta = fmtbufa;
|
---|
388 | *fmta++ = *iter++;
|
---|
389 | while (*iter == (WCHAR)L'0' ||
|
---|
390 | *iter == (WCHAR)L'+' ||
|
---|
391 | *iter == (WCHAR)L'-' ||
|
---|
392 | *iter == (WCHAR)L' ' ||
|
---|
393 | *iter == (WCHAR)L'0' ||
|
---|
394 | *iter == (WCHAR)L'*' ||
|
---|
395 | *iter == (WCHAR)L'#')
|
---|
396 | {
|
---|
397 | if (*iter == (WCHAR)L'*')
|
---|
398 | {
|
---|
399 | char *buffiter = bufa;
|
---|
400 | int fieldlen = va_arg(valist, int);
|
---|
401 | sprintf(buffiter, "%d", fieldlen);
|
---|
402 | while (*buffiter)
|
---|
403 | *fmta++ = *buffiter++;
|
---|
404 | }
|
---|
405 | else
|
---|
406 | *fmta++ = *iter;
|
---|
407 | iter++;
|
---|
408 | }
|
---|
409 |
|
---|
410 | while (isdigit(*iter))
|
---|
411 | *fmta++ = *iter++;
|
---|
412 |
|
---|
413 | if (*iter == (WCHAR)L'.')
|
---|
414 | {
|
---|
415 | *fmta++ = *iter++;
|
---|
416 | if (*iter == (WCHAR)L'*')
|
---|
417 | {
|
---|
418 | char *buffiter = bufa;
|
---|
419 | int fieldlen = va_arg(valist, int);
|
---|
420 | sprintf(buffiter, "%d", fieldlen);
|
---|
421 | while (*buffiter)
|
---|
422 | *fmta++ = *buffiter++;
|
---|
423 | }
|
---|
424 | else
|
---|
425 | while (isdigit(*iter))
|
---|
426 | *fmta++ = *iter++;
|
---|
427 | }
|
---|
428 | if (*iter == (WCHAR)L'h' ||
|
---|
429 | *iter == (WCHAR)L'l')
|
---|
430 | {
|
---|
431 | *fmta++ = *iter++;
|
---|
432 | *fmta++ = *iter++;
|
---|
433 | }
|
---|
434 |
|
---|
435 | switch (*iter)
|
---|
436 | {
|
---|
437 | case (WCHAR)L's':
|
---|
438 | {
|
---|
439 | static const WCHAR none[] = { '(', 'n', 'u', 'l', 'l', ')', 0 };
|
---|
440 | const WCHAR *wstr = va_arg(valist, const WCHAR *);
|
---|
441 | const WCHAR *striter = wstr ? wstr : none;
|
---|
442 | while (*striter)
|
---|
443 | {
|
---|
444 | if (written++ >= len)
|
---|
445 | return -1;
|
---|
446 | *str++ = *striter++;
|
---|
447 | }
|
---|
448 | iter++;
|
---|
449 | break;
|
---|
450 | }
|
---|
451 |
|
---|
452 | case (WCHAR)L'c':
|
---|
453 | if (written++ >= len)
|
---|
454 | return -1;
|
---|
455 | *str++ = (WCHAR)va_arg(valist, int);
|
---|
456 | iter++;
|
---|
457 | break;
|
---|
458 |
|
---|
459 | default:
|
---|
460 | {
|
---|
461 | /* For non wc types, use system sprintf and append to wide char output */
|
---|
462 | /* FIXME: for unrecognised types, should ignore % when printing */
|
---|
463 | char *bufaiter = bufa;
|
---|
464 | if (*iter == (WCHAR)L'p')
|
---|
465 | sprintf(bufaiter, "%08lX", va_arg(valist, long));
|
---|
466 | else
|
---|
467 | {
|
---|
468 | *fmta++ = *iter;
|
---|
469 | *fmta = '\0';
|
---|
470 | if (*iter == (WCHAR)L'f')
|
---|
471 | sprintf(bufaiter, fmtbufa, va_arg(valist, double));
|
---|
472 | else
|
---|
473 | sprintf(bufaiter, fmtbufa, va_arg(valist, void *));
|
---|
474 | }
|
---|
475 | while (*bufaiter)
|
---|
476 | {
|
---|
477 | if (written++ >= len)
|
---|
478 | return -1;
|
---|
479 | *str++ = *bufaiter++;
|
---|
480 | }
|
---|
481 | iter++;
|
---|
482 | break;
|
---|
483 | }
|
---|
484 | }
|
---|
485 | }
|
---|
486 | }
|
---|
487 | if (written >= len)
|
---|
488 | return -1;
|
---|
489 | *str++ = (WCHAR)L'\0';
|
---|
490 | return (int)written;
|
---|
491 | }
|
---|
492 |
|
---|
493 |
|
---|
494 | /***********************************************************************
|
---|
495 | * _snwprintf (NTDLL.@)
|
---|
496 | */
|
---|
497 | int __cdecl _snwprintf(WCHAR *str, unsigned int len, const WCHAR *format, ...)
|
---|
498 | {
|
---|
499 | int retval;
|
---|
500 | va_list valist;
|
---|
501 | va_start(valist, format);
|
---|
502 | retval = NTDLL_vsnwprintf(str, len, format, valist);
|
---|
503 | va_end(valist);
|
---|
504 | return retval;
|
---|
505 | }
|
---|
506 |
|
---|
507 |
|
---|
508 | /***********************************************************************
|
---|
509 | * swprintf (NTDLL.@)
|
---|
510 | */
|
---|
511 | int __cdecl NTDLL_swprintf(WCHAR *str, const WCHAR *format, ...)
|
---|
512 | {
|
---|
513 | int retval;
|
---|
514 | va_list valist;
|
---|
515 | va_start(valist, format);
|
---|
516 | retval = NTDLL_vsnwprintf(str, INT_MAX, format, valist);
|
---|
517 | va_end(valist);
|
---|
518 | return retval;
|
---|
519 | }
|
---|