makedep: Add phony install targets for fonts also for the ttf case.
[wine/multimedia.git] / dlls / msvcrt / wcs.c
blob1b00b41c2198a65a219b18acdd5bc543559c2359
1 /*
2 * msvcrt.dll wide-char functions
4 * Copyright 1999 Alexandre Julliard
5 * Copyright 2000 Jon Griffiths
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <limits.h>
25 #include <stdio.h>
26 #include <math.h>
27 #include <assert.h>
28 #include "msvcrt.h"
29 #include "winnls.h"
30 #include "wtypes.h"
31 #include "wine/unicode.h"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
36 static BOOL n_format_enabled = TRUE;
38 #include "printf.h"
39 #define PRINTF_WIDE
40 #include "printf.h"
41 #undef PRINTF_WIDE
43 /* _get_printf_count_output - not exported in native msvcrt */
44 int CDECL MSVCRT__get_printf_count_output( void )
46 return n_format_enabled ? 1 : 0;
49 /* _set_printf_count_output - not exported in native msvcrt */
50 int CDECL MSVCRT__set_printf_count_output( int enable )
52 BOOL old = n_format_enabled;
53 n_format_enabled = enable != 0;
54 return old ? 1 : 0;
57 /*********************************************************************
58 * _wcsdup (MSVCRT.@)
60 MSVCRT_wchar_t* CDECL MSVCRT__wcsdup( const MSVCRT_wchar_t* str )
62 MSVCRT_wchar_t* ret = NULL;
63 if (str)
65 int size = (strlenW(str) + 1) * sizeof(MSVCRT_wchar_t);
66 ret = MSVCRT_malloc( size );
67 if (ret) memcpy( ret, str, size );
69 return ret;
72 INT CDECL MSVCRT__wcsicmp_l(const MSVCRT_wchar_t *str1, const MSVCRT_wchar_t *str2, MSVCRT__locale_t locale)
74 if(!MSVCRT_CHECK_PMT(str1 != NULL) || !MSVCRT_CHECK_PMT(str2 != NULL))
75 return MSVCRT__NLSCMPERROR;
77 return strcmpiW(str1, str2);
80 /*********************************************************************
81 * _wcsicmp (MSVCRT.@)
83 INT CDECL MSVCRT__wcsicmp( const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2 )
85 return strcmpiW( str1, str2 );
88 /*********************************************************************
89 * _wcsnicmp (MSVCRT.@)
91 INT CDECL MSVCRT__wcsnicmp(const MSVCRT_wchar_t *str1, const MSVCRT_wchar_t *str2, INT n)
93 return strncmpiW(str1, str2, n);
96 /*********************************************************************
97 * _wcsicoll_l (MSVCRT.@)
99 int CDECL MSVCRT__wcsicoll_l(const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2, MSVCRT__locale_t locale)
101 MSVCRT_pthreadlocinfo locinfo;
103 if(!locale)
104 locinfo = get_locinfo();
105 else
106 locinfo = locale->locinfo;
108 if(!locinfo->lc_handle[MSVCRT_LC_COLLATE])
109 return strcmpiW(str1, str2);
110 return CompareStringW(locinfo->lc_handle[MSVCRT_LC_COLLATE], NORM_IGNORECASE,
111 str1, -1, str2, -1)-CSTR_EQUAL;
114 /*********************************************************************
115 * _wcsicoll (MSVCRT.@)
117 INT CDECL MSVCRT__wcsicoll( const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2 )
119 return MSVCRT__wcsicoll_l(str1, str2, NULL);
122 /*********************************************************************
123 * _wcsnicoll_l (MSVCRT.@)
125 int CDECL MSVCRT__wcsnicoll_l(const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2,
126 MSVCRT_size_t count, MSVCRT__locale_t locale)
128 MSVCRT_pthreadlocinfo locinfo;
130 if(!locale)
131 locinfo = get_locinfo();
132 else
133 locinfo = locale->locinfo;
135 if(!locinfo->lc_handle[MSVCRT_LC_COLLATE])
136 return strncmpiW(str1, str2, count);
137 return CompareStringW(locinfo->lc_handle[MSVCRT_LC_COLLATE], NORM_IGNORECASE,
138 str1, count, str2, count)-CSTR_EQUAL;
141 /*********************************************************************
142 * _wcsnicoll (MSVCRT.@)
144 INT CDECL MSVCRT__wcsnicoll( const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2, MSVCRT_size_t count )
146 return MSVCRT__wcsnicoll_l(str1, str2, count, NULL);
149 /*********************************************************************
150 * _wcsnset (MSVCRT.@)
152 MSVCRT_wchar_t* CDECL MSVCRT__wcsnset( MSVCRT_wchar_t* str, MSVCRT_wchar_t c, MSVCRT_size_t n )
154 MSVCRT_wchar_t* ret = str;
155 while ((n-- > 0) && *str) *str++ = c;
156 return ret;
159 /*********************************************************************
160 * _wcsrev (MSVCRT.@)
162 MSVCRT_wchar_t* CDECL MSVCRT__wcsrev( MSVCRT_wchar_t* str )
164 MSVCRT_wchar_t* ret = str;
165 MSVCRT_wchar_t* end = str + strlenW(str) - 1;
166 while (end > str)
168 MSVCRT_wchar_t t = *end;
169 *end-- = *str;
170 *str++ = t;
172 return ret;
175 /*********************************************************************
176 * _wcsset (MSVCRT.@)
178 MSVCRT_wchar_t* CDECL MSVCRT__wcsset( MSVCRT_wchar_t* str, MSVCRT_wchar_t c )
180 MSVCRT_wchar_t* ret = str;
181 while (*str) *str++ = c;
182 return ret;
185 /******************************************************************
186 * _wcsupr_s_l (MSVCRT.@)
188 int CDECL MSVCRT__wcsupr_s_l( MSVCRT_wchar_t* str, MSVCRT_size_t n,
189 MSVCRT__locale_t locale )
191 MSVCRT_wchar_t* ptr = str;
193 if (!str || !n)
195 if (str) *str = '\0';
196 *MSVCRT__errno() = MSVCRT_EINVAL;
197 return MSVCRT_EINVAL;
200 while (n--)
202 if (!*ptr) return 0;
203 /* FIXME: add locale support */
204 *ptr = toupperW(*ptr);
205 ptr++;
208 /* MSDN claims that the function should return and set errno to
209 * ERANGE, which doesn't seem to be true based on the tests. */
210 *str = '\0';
211 *MSVCRT__errno() = MSVCRT_EINVAL;
212 return MSVCRT_EINVAL;
215 /******************************************************************
216 * _wcsupr_s (MSVCRT.@)
219 INT CDECL MSVCRT__wcsupr_s( MSVCRT_wchar_t* str, MSVCRT_size_t n )
221 return MSVCRT__wcsupr_s_l( str, n, NULL );
224 /******************************************************************
225 * _wcslwr_s (MSVCRT.@)
227 int CDECL MSVCRT__wcslwr_s( MSVCRT_wchar_t* str, MSVCRT_size_t n )
229 MSVCRT_wchar_t* ptr = str;
231 if (!str || !n)
233 if (str) *str = '\0';
234 *MSVCRT__errno() = MSVCRT_EINVAL;
235 return MSVCRT_EINVAL;
238 while (n--)
240 if (!*ptr) return 0;
241 *ptr = tolowerW(*ptr);
242 ptr++;
245 /* MSDN claims that the function should return and set errno to
246 * ERANGE, which doesn't seem to be true based on the tests. */
247 *str = '\0';
248 *MSVCRT__errno() = MSVCRT_EINVAL;
249 return MSVCRT_EINVAL;
252 /*********************************************************************
253 * _wcsncoll_l (MSVCRT.@)
255 int CDECL MSVCRT__wcsncoll_l(const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2,
256 MSVCRT_size_t count, MSVCRT__locale_t locale)
258 MSVCRT_pthreadlocinfo locinfo;
260 if(!locale)
261 locinfo = get_locinfo();
262 else
263 locinfo = locale->locinfo;
265 if(!locinfo->lc_handle[MSVCRT_LC_COLLATE])
266 return strncmpW(str1, str2, count);
267 return CompareStringW(locinfo->lc_handle[MSVCRT_LC_COLLATE], 0, str1, count, str2, count)-CSTR_EQUAL;
270 /*********************************************************************
271 * _wcsncoll (MSVCRT.@)
273 int CDECL MSVCRT__wcsncoll(const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2, MSVCRT_size_t count)
275 return MSVCRT__wcsncoll_l(str1, str2, count, NULL);
278 /*********************************************************************
279 * _wcstod_l (MSVCRT.@)
281 double CDECL MSVCRT__wcstod_l(const MSVCRT_wchar_t* str, MSVCRT_wchar_t** end,
282 MSVCRT__locale_t locale)
284 MSVCRT_pthreadlocinfo locinfo;
285 unsigned __int64 d=0, hlp;
286 unsigned fpcontrol;
287 int exp=0, sign=1;
288 const MSVCRT_wchar_t *p;
289 double ret;
290 long double lret=1, expcnt = 10;
291 BOOL found_digit = FALSE, negexp;
293 if (!MSVCRT_CHECK_PMT(str != NULL)) return 0;
295 if(!locale)
296 locinfo = get_locinfo();
297 else
298 locinfo = locale->locinfo;
300 p = str;
301 while(isspaceW(*p))
302 p++;
304 if(*p == '-') {
305 sign = -1;
306 p++;
307 } else if(*p == '+')
308 p++;
310 while(isdigitW(*p)) {
311 found_digit = TRUE;
312 hlp = d*10+*(p++)-'0';
313 if(d>MSVCRT_UI64_MAX/10 || hlp<d) {
314 exp++;
315 break;
316 } else
317 d = hlp;
319 while(isdigitW(*p)) {
320 exp++;
321 p++;
323 if(*p == *locinfo->lconv->decimal_point)
324 p++;
326 while(isdigitW(*p)) {
327 found_digit = TRUE;
328 hlp = d*10+*(p++)-'0';
329 if(d>MSVCRT_UI64_MAX/10 || hlp<d)
330 break;
332 d = hlp;
333 exp--;
335 while(isdigitW(*p))
336 p++;
338 if(!found_digit) {
339 if(end)
340 *end = (MSVCRT_wchar_t*)str;
341 return 0.0;
344 if(*p=='e' || *p=='E' || *p=='d' || *p=='D') {
345 int e=0, s=1;
347 p++;
348 if(*p == '-') {
349 s = -1;
350 p++;
351 } else if(*p == '+')
352 p++;
354 if(isdigitW(*p)) {
355 while(isdigitW(*p)) {
356 if(e>INT_MAX/10 || (e=e*10+*p-'0')<0)
357 e = INT_MAX;
358 p++;
360 e *= s;
362 if(exp<0 && e<0 && exp+e>=0) exp = INT_MIN;
363 else if(exp>0 && e>0 && exp+e<0) exp = INT_MAX;
364 else exp += e;
365 } else {
366 if(*p=='-' || *p=='+')
367 p--;
368 p--;
372 fpcontrol = _control87(0, 0);
373 _control87(MSVCRT__EM_DENORMAL|MSVCRT__EM_INVALID|MSVCRT__EM_ZERODIVIDE
374 |MSVCRT__EM_OVERFLOW|MSVCRT__EM_UNDERFLOW|MSVCRT__EM_INEXACT, 0xffffffff);
376 negexp = (exp < 0);
377 if(negexp)
378 exp = -exp;
379 while(exp) {
380 if(exp & 1)
381 lret *= expcnt;
382 exp /= 2;
383 expcnt = expcnt*expcnt;
385 ret = (long double)sign * (negexp ? d/lret : d*lret);
387 _control87(fpcontrol, 0xffffffff);
389 if((d && ret==0.0) || isinf(ret))
390 *MSVCRT__errno() = MSVCRT_ERANGE;
392 if(end)
393 *end = (MSVCRT_wchar_t*)p;
395 return ret;
398 /*********************************************************************
399 * wcsrtombs_l (INTERNAL)
401 static MSVCRT_size_t MSVCRT_wcsrtombs_l(char *mbstr, const MSVCRT_wchar_t **wcstr,
402 MSVCRT_size_t count, MSVCRT__locale_t locale)
404 MSVCRT_pthreadlocinfo locinfo;
405 MSVCRT_size_t tmp = 0;
406 BOOL used_default;
408 if(!locale)
409 locinfo = get_locinfo();
410 else
411 locinfo = locale->locinfo;
413 if(!mbstr) {
414 tmp = WideCharToMultiByte(locinfo->lc_codepage, WC_NO_BEST_FIT_CHARS,
415 *wcstr, -1, NULL, 0, NULL, &used_default)-1;
416 if(!tmp || used_default) {
417 *MSVCRT__errno() = MSVCRT_EILSEQ;
418 return -1;
420 return tmp;
423 while(**wcstr) {
424 char buf[3];
425 MSVCRT_size_t i, size;
427 size = WideCharToMultiByte(locinfo->lc_codepage, WC_NO_BEST_FIT_CHARS,
428 *wcstr, 1, buf, 3, NULL, &used_default);
429 if(!size || used_default) {
430 *MSVCRT__errno() = MSVCRT_EILSEQ;
431 return -1;
433 if(tmp+size > count)
434 return tmp;
436 for(i=0; i<size; i++)
437 mbstr[tmp++] = buf[i];
438 (*wcstr)++;
441 if(tmp < count) {
442 mbstr[tmp] = '\0';
443 *wcstr = NULL;
445 return tmp;
448 /*********************************************************************
449 * _wcstombs_l (MSVCRT.@)
451 MSVCRT_size_t CDECL MSVCRT__wcstombs_l(char *mbstr, const MSVCRT_wchar_t *wcstr,
452 MSVCRT_size_t count, MSVCRT__locale_t locale)
454 return MSVCRT_wcsrtombs_l(mbstr, &wcstr, count, locale);
457 /*********************************************************************
458 * wcstombs (MSVCRT.@)
460 MSVCRT_size_t CDECL MSVCRT_wcstombs(char *mbstr, const MSVCRT_wchar_t *wcstr,
461 MSVCRT_size_t count)
463 return MSVCRT_wcsrtombs_l(mbstr, &wcstr, count, NULL);
466 /*********************************************************************
467 * wcsrtombs (MSVCRT.@)
469 MSVCRT_size_t CDECL MSVCRT_wcsrtombs(char *mbstr, const MSVCRT_wchar_t **wcstr,
470 MSVCRT_size_t count, MSVCRT_mbstate_t *mbstate)
472 if(mbstate)
473 *mbstate = 0;
475 return MSVCRT_wcsrtombs_l(mbstr, wcstr, count, NULL);
478 /*********************************************************************
479 * MSVCRT_wcsrtombs_s_l (INTERNAL)
481 static int MSVCRT_wcsrtombs_s_l(MSVCRT_size_t *ret, char *mbstr,
482 MSVCRT_size_t size, const MSVCRT_wchar_t **wcstr,
483 MSVCRT_size_t count, MSVCRT__locale_t locale)
485 MSVCRT_size_t conv;
487 if(!mbstr && !size && wcstr) {
488 conv = MSVCRT_wcsrtombs_l(NULL, wcstr, 0, locale);
489 if(conv == -1)
490 return *MSVCRT__errno();
491 if(ret)
492 *ret = conv+1;
493 return 0;
496 if (!MSVCRT_CHECK_PMT(mbstr != NULL)) return MSVCRT_EINVAL;
497 if (size) mbstr[0] = '\0';
498 if (!MSVCRT_CHECK_PMT(wcstr != NULL)) return MSVCRT_EINVAL;
499 if (!MSVCRT_CHECK_PMT(*wcstr != NULL)) return MSVCRT_EINVAL;
501 if(count==MSVCRT__TRUNCATE || size<count)
502 conv = size;
503 else
504 conv = count;
506 conv = MSVCRT_wcsrtombs_l(mbstr, wcstr, conv, locale);
507 if(conv == -1) {
508 if(size)
509 mbstr[0] = '\0';
510 return *MSVCRT__errno();
511 }else if(conv < size)
512 mbstr[conv++] = '\0';
513 else if(conv==size && (count==MSVCRT__TRUNCATE || mbstr[conv-1]=='\0'))
514 mbstr[conv-1] = '\0';
515 else {
516 MSVCRT_INVALID_PMT("mbstr[size] is too small", MSVCRT_ERANGE);
517 if(size)
518 mbstr[0] = '\0';
519 return MSVCRT_ERANGE;
522 if(ret)
523 *ret = conv;
524 return 0;
527 /*********************************************************************
528 * _wcstombs_s_l (MSVCRT.@)
530 int CDECL MSVCRT__wcstombs_s_l(MSVCRT_size_t *ret, char *mbstr,
531 MSVCRT_size_t size, const MSVCRT_wchar_t *wcstr,
532 MSVCRT_size_t count, MSVCRT__locale_t locale)
534 return MSVCRT_wcsrtombs_s_l(ret, mbstr, size, &wcstr,count, locale);
537 /*********************************************************************
538 * wcstombs_s (MSVCRT.@)
540 int CDECL MSVCRT_wcstombs_s(MSVCRT_size_t *ret, char *mbstr,
541 MSVCRT_size_t size, const MSVCRT_wchar_t *wcstr, MSVCRT_size_t count)
543 return MSVCRT_wcsrtombs_s_l(ret, mbstr, size, &wcstr, count, NULL);
546 /*********************************************************************
547 * wcsrtombs_s (MSVCRT.@)
549 int CDECL MSVCRT_wcsrtombs_s(MSVCRT_size_t *ret, char *mbstr, MSVCRT_size_t size,
550 const MSVCRT_wchar_t **wcstr, MSVCRT_size_t count, MSVCRT_mbstate_t *mbstate)
552 if(mbstate)
553 *mbstate = 0;
555 return MSVCRT_wcsrtombs_s_l(ret, mbstr, size, wcstr, count, NULL);
558 /*********************************************************************
559 * wcstod (MSVCRT.@)
561 double CDECL MSVCRT_wcstod(const MSVCRT_wchar_t* lpszStr, MSVCRT_wchar_t** end)
563 return MSVCRT__wcstod_l(lpszStr, end, NULL);
566 /*********************************************************************
567 * _wtof (MSVCRT.@)
569 double CDECL MSVCRT__wtof(const MSVCRT_wchar_t *str)
571 return MSVCRT__wcstod_l(str, NULL, NULL);
574 /*********************************************************************
575 * _wtof_l (MSVCRT.@)
577 double CDECL MSVCRT__wtof_l(const MSVCRT_wchar_t *str, MSVCRT__locale_t locale)
579 return MSVCRT__wcstod_l(str, NULL, locale);
582 /*********************************************************************
583 * arg_clbk_valist (INTERNAL)
585 printf_arg arg_clbk_valist(void *ctx, int arg_pos, int type, __ms_va_list *valist)
587 printf_arg ret;
589 if(type == VT_I8)
590 ret.get_longlong = va_arg(*valist, LONGLONG);
591 else if(type == VT_INT)
592 ret.get_int = va_arg(*valist, int);
593 else if(type == VT_R8)
594 ret.get_double = va_arg(*valist, double);
595 else if(type == VT_PTR)
596 ret.get_ptr = va_arg(*valist, void*);
597 else {
598 ERR("Incorrect type\n");
599 ret.get_int = 0;
602 return ret;
605 /*********************************************************************
606 * arg_clbk_positional (INTERNAL)
608 static printf_arg arg_clbk_positional(void *ctx, int pos, int type, __ms_va_list *valist)
610 printf_arg *args = ctx;
611 return args[pos];
614 /*********************************************************************
615 * _vsnprintf (MSVCRT.@)
617 int CDECL MSVCRT_vsnprintf( char *str, MSVCRT_size_t len,
618 const char *format, __ms_va_list valist )
620 static const char nullbyte = '\0';
621 struct _str_ctx_a ctx = {len, str};
622 int ret;
624 ret = pf_printf_a(puts_clbk_str_a, &ctx, format, NULL, FALSE, FALSE,
625 arg_clbk_valist, NULL, &valist);
626 puts_clbk_str_a(&ctx, 1, &nullbyte);
627 return ret;
630 /*********************************************************************
631 * _vsnprintf_l (MSVCRT.@)
633 int CDECL MSVCRT_vsnprintf_l( char *str, MSVCRT_size_t len, const char *format,
634 MSVCRT__locale_t locale, __ms_va_list valist )
636 static const char nullbyte = '\0';
637 struct _str_ctx_a ctx = {len, str};
638 int ret;
640 ret = pf_printf_a(puts_clbk_str_a, &ctx, format, locale, FALSE, FALSE,
641 arg_clbk_valist, NULL, &valist);
642 puts_clbk_str_a(&ctx, 1, &nullbyte);
643 return ret;
646 /*********************************************************************
647 * _vsprintf_l (MSVCRT.@)
649 int CDECL MSVCRT_vsprintf_l( char *str, const char *format,
650 MSVCRT__locale_t locale, __ms_va_list valist )
652 return MSVCRT_vsnprintf_l(str, INT_MAX, format, locale, valist);
655 /*********************************************************************
656 * _sprintf_l (MSVCRT.@)
658 int CDECL MSVCRT_sprintf_l(char *str, const char *format,
659 MSVCRT__locale_t locale, ...)
661 int retval;
662 __ms_va_list valist;
663 __ms_va_start(valist, locale);
664 retval = MSVCRT_vsnprintf_l(str, INT_MAX, format, locale, valist);
665 __ms_va_end(valist);
666 return retval;
669 /*********************************************************************
670 * _vsnprintf_s_l (MSVCRT.@)
672 int CDECL MSVCRT_vsnprintf_s_l( char *str, MSVCRT_size_t sizeOfBuffer,
673 MSVCRT_size_t count, const char *format,
674 MSVCRT__locale_t locale, __ms_va_list valist )
676 static const char nullbyte = '\0';
677 struct _str_ctx_a ctx;
678 int len, ret;
680 if(sizeOfBuffer<count+1 || count==-1)
681 len = sizeOfBuffer;
682 else
683 len = count+1;
685 ctx.len = len;
686 ctx.buf = str;
687 ret = pf_printf_a(puts_clbk_str_a, &ctx, format, locale, FALSE, TRUE,
688 arg_clbk_valist, NULL, &valist);
689 puts_clbk_str_a(&ctx, 1, &nullbyte);
691 if(ret<0 || ret==len) {
692 if(count!=MSVCRT__TRUNCATE && count>sizeOfBuffer) {
693 MSVCRT_INVALID_PMT("str[sizeOfBuffer] is too small", MSVCRT_ERANGE);
694 memset(str, 0, sizeOfBuffer);
695 } else
696 str[len-1] = '\0';
698 return -1;
701 return ret;
704 /*********************************************************************
705 * _vsprintf_s_l (MSVCRT.@)
707 int CDECL MSVCRT_vsprintf_s_l( char *str, MSVCRT_size_t count, const char *format,
708 MSVCRT__locale_t locale, __ms_va_list valist )
710 return MSVCRT_vsnprintf_s_l(str, INT_MAX, count, format, locale, valist);
713 /*********************************************************************
714 * _sprintf_s_l (MSVCRT.@)
716 int CDECL MSVCRT_sprintf_s_l( char *str, MSVCRT_size_t count, const char *format,
717 MSVCRT__locale_t locale, ...)
719 int retval;
720 __ms_va_list valist;
721 __ms_va_start(valist, locale);
722 retval = MSVCRT_vsnprintf_s_l(str, INT_MAX, count, format, locale, valist);
723 __ms_va_end(valist);
724 return retval;
727 /*********************************************************************
728 * _vsnprintf_s (MSVCRT.@)
730 int CDECL MSVCRT_vsnprintf_s( char *str, MSVCRT_size_t sizeOfBuffer,
731 MSVCRT_size_t count, const char *format, __ms_va_list valist )
733 return MSVCRT_vsnprintf_s_l(str,sizeOfBuffer, count, format, NULL, valist);
736 /*********************************************************************
737 * vsprintf (MSVCRT.@)
739 int CDECL MSVCRT_vsprintf( char *str, const char *format, __ms_va_list valist)
741 return MSVCRT_vsnprintf(str, INT_MAX, format, valist);
744 /*********************************************************************
745 * vsprintf_s (MSVCRT.@)
747 int CDECL MSVCRT_vsprintf_s( char *str, MSVCRT_size_t num, const char *format, __ms_va_list valist)
749 return MSVCRT_vsnprintf(str, num, format, valist);
752 /*********************************************************************
753 * _vscprintf (MSVCRT.@)
755 int CDECL MSVCRT__vscprintf( const char *format, __ms_va_list valist )
757 return MSVCRT_vsnprintf( NULL, INT_MAX, format, valist );
760 /*********************************************************************
761 * _snprintf (MSVCRT.@)
763 int CDECL MSVCRT__snprintf(char *str, unsigned int len, const char *format, ...)
765 int retval;
766 __ms_va_list valist;
767 __ms_va_start(valist, format);
768 retval = MSVCRT_vsnprintf(str, len, format, valist);
769 __ms_va_end(valist);
770 return retval;
773 /*********************************************************************
774 * _snprintf_s (MSVCRT.@)
776 int CDECL MSVCRT__snprintf_s(char *str, unsigned int len, unsigned int count,
777 const char *format, ...)
779 int retval;
780 __ms_va_list valist;
781 __ms_va_start(valist, format);
782 retval = MSVCRT_vsnprintf_s_l(str, len, count, format, NULL, valist);
783 __ms_va_end(valist);
784 return retval;
787 /*********************************************************************
788 * _scprintf (MSVCRT.@)
790 int CDECL MSVCRT__scprintf(const char *format, ...)
792 int retval;
793 __ms_va_list valist;
794 __ms_va_start(valist, format);
795 retval = MSVCRT__vscprintf(format, valist);
796 __ms_va_end(valist);
797 return retval;
800 /*********************************************************************
801 * _vsnwprintf (MSVCRT.@)
803 int CDECL MSVCRT_vsnwprintf(MSVCRT_wchar_t *str, MSVCRT_size_t len,
804 const MSVCRT_wchar_t *format, __ms_va_list valist)
806 static const MSVCRT_wchar_t nullbyte = '\0';
807 struct _str_ctx_w ctx = {len, str};
808 int ret;
810 ret = pf_printf_w(puts_clbk_str_w, &ctx, format, NULL, FALSE, FALSE,
811 arg_clbk_valist, NULL, &valist);
812 puts_clbk_str_w(&ctx, 1, &nullbyte);
813 return ret;
816 /*********************************************************************
817 * _vsnwprintf_l (MSVCRT.@)
819 int CDECL MSVCRT_vsnwprintf_l(MSVCRT_wchar_t *str, MSVCRT_size_t len,
820 const MSVCRT_wchar_t *format, MSVCRT__locale_t locale,
821 __ms_va_list valist)
823 static const MSVCRT_wchar_t nullbyte = '\0';
824 struct _str_ctx_w ctx = {len, str};
825 int ret;
827 ret = pf_printf_w(puts_clbk_str_w, &ctx, format, locale, FALSE, FALSE,
828 arg_clbk_valist, NULL, &valist);
829 puts_clbk_str_w(&ctx, 1, &nullbyte);
830 return ret;
833 /*********************************************************************
834 * _vswprintf_p_l (MSVCRT.@)
836 int CDECL MSVCRT_vswprintf_p_l(MSVCRT_wchar_t *buffer, MSVCRT_size_t length,
837 const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, __ms_va_list args)
839 static const MSVCRT_wchar_t nullbyte = '\0';
840 printf_arg args_ctx[MSVCRT__ARGMAX+1];
841 struct _str_ctx_w puts_ctx = {length, buffer};
842 int ret;
844 memset(args_ctx, 0, sizeof(args_ctx));
846 ret = create_positional_ctx_w(args_ctx, format, args);
847 if(ret < 0) {
848 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
849 *MSVCRT__errno() = MSVCRT_EINVAL;
850 return ret;
851 } else if(ret == 0)
852 ret = pf_printf_w(puts_clbk_str_w, &puts_ctx, format, locale, FALSE, TRUE,
853 arg_clbk_valist, NULL, &args);
854 else
855 ret = pf_printf_w(puts_clbk_str_w, &puts_ctx, format, locale, TRUE, TRUE,
856 arg_clbk_positional, args_ctx, NULL);
858 puts_clbk_str_w(&puts_ctx, 1, &nullbyte);
859 return ret;
862 /*********************************************************************
863 * _vsnwprintf_s_l (MSVCRT.@)
865 int CDECL MSVCRT_vsnwprintf_s_l( MSVCRT_wchar_t *str, MSVCRT_size_t sizeOfBuffer,
866 MSVCRT_size_t count, const MSVCRT_wchar_t *format,
867 MSVCRT__locale_t locale, __ms_va_list valist)
869 static const MSVCRT_wchar_t nullbyte = '\0';
870 struct _str_ctx_w ctx;
871 int len, ret;
873 len = sizeOfBuffer;
874 if(count!=-1 && len>count+1)
875 len = count+1;
877 ctx.len = len;
878 ctx.buf = str;
879 ret = pf_printf_w(puts_clbk_str_w, &ctx, format, locale, FALSE, TRUE,
880 arg_clbk_valist, NULL, &valist);
881 puts_clbk_str_w(&ctx, 1, &nullbyte);
883 if(ret<0 || ret==len) {
884 if(count!=MSVCRT__TRUNCATE && count>sizeOfBuffer) {
885 MSVCRT_INVALID_PMT("str[sizeOfBuffer] is too small", MSVCRT_ERANGE);
886 memset(str, 0, sizeOfBuffer*sizeof(MSVCRT_wchar_t));
887 } else
888 str[len-1] = '\0';
890 return -1;
893 return ret;
896 /*********************************************************************
897 * _vsnwprintf_s (MSVCRT.@)
899 int CDECL MSVCRT_vsnwprintf_s(MSVCRT_wchar_t *str, MSVCRT_size_t sizeOfBuffer,
900 MSVCRT_size_t count, const MSVCRT_wchar_t *format, __ms_va_list valist)
902 return MSVCRT_vsnwprintf_s_l(str, sizeOfBuffer, count,
903 format, NULL, valist);
906 /*********************************************************************
907 * _snwprintf (MSVCRT.@)
909 int CDECL MSVCRT__snwprintf( MSVCRT_wchar_t *str, unsigned int len, const MSVCRT_wchar_t *format, ...)
911 int retval;
912 __ms_va_list valist;
913 __ms_va_start(valist, format);
914 retval = MSVCRT_vsnwprintf(str, len, format, valist);
915 __ms_va_end(valist);
916 return retval;
919 /*********************************************************************
920 * _snwprintf_l (MSVCRT.@)
922 int CDECL MSVCRT__snwprintf_l( MSVCRT_wchar_t *str, unsigned int len, const MSVCRT_wchar_t *format,
923 MSVCRT__locale_t locale, ...)
925 int retval;
926 __ms_va_list valist;
927 __ms_va_start(valist, locale);
928 retval = MSVCRT_vsnwprintf_l(str, len, format, locale, valist);
929 __ms_va_end(valist);
930 return retval;
933 /*********************************************************************
934 * _snwprintf_s (MSVCRT.@)
936 int CDECL MSVCRT__snwprintf_s( MSVCRT_wchar_t *str, unsigned int len, unsigned int count,
937 const MSVCRT_wchar_t *format, ...)
939 int retval;
940 __ms_va_list valist;
941 __ms_va_start(valist, format);
942 retval = MSVCRT_vsnwprintf_s_l(str, len, count, format, NULL, valist);
943 __ms_va_end(valist);
944 return retval;
947 /*********************************************************************
948 * _snwprintf_s_l (MSVCRT.@)
950 int CDECL MSVCRT__snwprintf_s_l( MSVCRT_wchar_t *str, unsigned int len, unsigned int count,
951 const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, ... )
953 int retval;
954 __ms_va_list valist;
955 __ms_va_start(valist, locale);
956 retval = MSVCRT_vsnwprintf_s_l(str, len, count, format, locale, valist);
957 __ms_va_end(valist);
958 return retval;
961 /*********************************************************************
962 * sprintf (MSVCRT.@)
964 int CDECL MSVCRT_sprintf( char *str, const char *format, ... )
966 __ms_va_list ap;
967 int r;
969 __ms_va_start( ap, format );
970 r = MSVCRT_vsnprintf( str, INT_MAX, format, ap );
971 __ms_va_end( ap );
972 return r;
975 /*********************************************************************
976 * sprintf_s (MSVCRT.@)
978 int CDECL MSVCRT_sprintf_s( char *str, MSVCRT_size_t num, const char *format, ... )
980 __ms_va_list ap;
981 int r;
983 __ms_va_start( ap, format );
984 r = MSVCRT_vsnprintf( str, num, format, ap );
985 __ms_va_end( ap );
986 return r;
989 /*********************************************************************
990 * _scwprintf (MSVCRT.@)
992 int CDECL MSVCRT__scwprintf( const MSVCRT_wchar_t *format, ... )
994 __ms_va_list ap;
995 int r;
997 __ms_va_start( ap, format );
998 r = MSVCRT_vsnwprintf( NULL, INT_MAX, format, ap );
999 __ms_va_end( ap );
1000 return r;
1003 /*********************************************************************
1004 * swprintf (MSVCRT.@)
1006 int CDECL MSVCRT_swprintf( MSVCRT_wchar_t *str, const MSVCRT_wchar_t *format, ... )
1008 __ms_va_list ap;
1009 int r;
1011 __ms_va_start( ap, format );
1012 r = MSVCRT_vsnwprintf( str, INT_MAX, format, ap );
1013 __ms_va_end( ap );
1014 return r;
1017 /*********************************************************************
1018 * swprintf_s (MSVCRT.@)
1020 int CDECL MSVCRT_swprintf_s(MSVCRT_wchar_t *str, MSVCRT_size_t numberOfElements,
1021 const MSVCRT_wchar_t *format, ... )
1023 __ms_va_list ap;
1024 int r;
1026 __ms_va_start(ap, format);
1027 r = MSVCRT_vsnwprintf_s(str, numberOfElements, INT_MAX, format, ap);
1028 __ms_va_end(ap);
1030 return r;
1033 /*********************************************************************
1034 * _swprintf_s_l (MSVCRT.@)
1036 int CDECL MSVCRT__swprintf_s_l(MSVCRT_wchar_t *str, MSVCRT_size_t numberOfElements,
1037 const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, ... )
1039 __ms_va_list ap;
1040 int r;
1042 __ms_va_start(ap, locale);
1043 r = MSVCRT_vsnwprintf_s_l(str, numberOfElements, INT_MAX, format, locale, ap);
1044 __ms_va_end(ap);
1046 return r;
1049 /*********************************************************************
1050 * _vswprintf (MSVCRT.@)
1052 int CDECL MSVCRT_vswprintf( MSVCRT_wchar_t* str, const MSVCRT_wchar_t* format, __ms_va_list args )
1054 return MSVCRT_vsnwprintf( str, INT_MAX, format, args );
1057 /*********************************************************************
1058 * _vswprintf (MSVCRT.@)
1060 int CDECL MSVCRT_vswprintf_l( MSVCRT_wchar_t* str, const MSVCRT_wchar_t* format,
1061 MSVCRT__locale_t locale, __ms_va_list args )
1063 return MSVCRT_vsnwprintf_l( str, INT_MAX, format, locale, args );
1066 /*********************************************************************
1067 * _vscwprintf (MSVCRT.@)
1069 int CDECL MSVCRT__vscwprintf( const MSVCRT_wchar_t *format, __ms_va_list args )
1071 return MSVCRT_vsnwprintf( NULL, INT_MAX, format, args );
1074 /*********************************************************************
1075 * _vscwprintf_l (MSVCRT.@)
1077 int CDECL MSVCRT__vscwprintf_l( const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, __ms_va_list args )
1079 return MSVCRT_vsnwprintf_l( NULL, INT_MAX, format, locale, args );
1082 /*********************************************************************
1083 * _vscwprintf_p_l (MSVCRT.@)
1085 int CDECL MSVCRT__vscwprintf_p_l( const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, __ms_va_list args )
1087 return MSVCRT_vswprintf_p_l( NULL, INT_MAX, format, locale, args );
1090 /*********************************************************************
1091 * vswprintf_s (MSVCRT.@)
1093 int CDECL MSVCRT_vswprintf_s(MSVCRT_wchar_t* str, MSVCRT_size_t numberOfElements,
1094 const MSVCRT_wchar_t* format, __ms_va_list args)
1096 return MSVCRT_vsnwprintf_s(str, numberOfElements, INT_MAX, format, args );
1099 /*********************************************************************
1100 * _vswprintf_s_l (MSVCRT.@)
1102 int CDECL MSVCRT_vswprintf_s_l(MSVCRT_wchar_t* str, MSVCRT_size_t numberOfElements,
1103 const MSVCRT_wchar_t* format, MSVCRT__locale_t locale, __ms_va_list args)
1105 return MSVCRT_vsnwprintf_s_l(str, numberOfElements, INT_MAX,
1106 format, locale, args );
1109 /*********************************************************************
1110 * _vsprintf_p_l (MSVCRT.@)
1112 int CDECL MSVCRT_vsprintf_p_l(char *buffer, MSVCRT_size_t length, const char *format,
1113 MSVCRT__locale_t locale, __ms_va_list args)
1115 static const char nullbyte = '\0';
1116 printf_arg args_ctx[MSVCRT__ARGMAX+1];
1117 struct _str_ctx_a puts_ctx = {length, buffer};
1118 int ret;
1120 memset(args_ctx, 0, sizeof(args_ctx));
1122 ret = create_positional_ctx_a(args_ctx, format, args);
1123 if(ret < 0) {
1124 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
1125 *MSVCRT__errno() = MSVCRT_EINVAL;
1126 return ret;
1127 } else if(ret == 0)
1128 ret = pf_printf_a(puts_clbk_str_a, &puts_ctx, format, locale, FALSE, TRUE,
1129 arg_clbk_valist, NULL, &args);
1130 else
1131 ret = pf_printf_a(puts_clbk_str_a, &puts_ctx, format, locale, TRUE, TRUE,
1132 arg_clbk_positional, args_ctx, NULL);
1134 puts_clbk_str_a(&puts_ctx, 1, &nullbyte);
1135 return ret;
1138 /*********************************************************************
1139 * _vsprintf_p (MSVCRT.@)
1141 int CDECL MSVCRT_vsprintf_p(char *buffer, MSVCRT_size_t length,
1142 const char *format, __ms_va_list args)
1144 return MSVCRT_vsprintf_p_l(buffer, length, format, NULL, args);
1147 /*********************************************************************
1148 * _sprintf_p_l (MSVCRT.@)
1150 int CDECL MSVCRT_sprintf_p_l(char *buffer, MSVCRT_size_t length,
1151 const char *format, MSVCRT__locale_t locale, ...)
1153 __ms_va_list valist;
1154 int r;
1156 __ms_va_start(valist, locale);
1157 r = MSVCRT_vsprintf_p_l(buffer, length, format, locale, valist);
1158 __ms_va_end(valist);
1160 return r;
1163 /*********************************************************************
1164 * _swprintf_p_l (MSVCRT.@)
1166 int CDECL MSVCRT_swprintf_p_l(MSVCRT_wchar_t *buffer, MSVCRT_size_t length,
1167 const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, ...)
1169 __ms_va_list valist;
1170 int r;
1172 __ms_va_start(valist, locale);
1173 r = MSVCRT_vswprintf_p_l(buffer, length, format, locale, valist);
1174 __ms_va_end(valist);
1176 return r;
1179 /*********************************************************************
1180 * _wcscoll_l (MSVCRT.@)
1182 int CDECL MSVCRT__wcscoll_l(const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2, MSVCRT__locale_t locale)
1184 MSVCRT_pthreadlocinfo locinfo;
1186 if(!locale)
1187 locinfo = get_locinfo();
1188 else
1189 locinfo = locale->locinfo;
1191 if(!locinfo->lc_handle[MSVCRT_LC_COLLATE])
1192 return strcmpW(str1, str2);
1193 return CompareStringW(locinfo->lc_handle[MSVCRT_LC_COLLATE], 0, str1, -1, str2, -1)-CSTR_EQUAL;
1196 /*********************************************************************
1197 * wcscoll (MSVCRT.@)
1199 int CDECL MSVCRT_wcscoll( const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2 )
1201 return MSVCRT__wcscoll_l(str1, str2, NULL);
1204 /*********************************************************************
1205 * wcspbrk (MSVCRT.@)
1207 MSVCRT_wchar_t* CDECL MSVCRT_wcspbrk( const MSVCRT_wchar_t* str, const MSVCRT_wchar_t* accept )
1209 const MSVCRT_wchar_t* p;
1211 while (*str)
1213 for (p = accept; *p; p++) if (*p == *str) return (MSVCRT_wchar_t*)str;
1214 str++;
1216 return NULL;
1219 /*********************************************************************
1220 * wcstok_s (MSVCRT.@)
1222 MSVCRT_wchar_t * CDECL wcstok_s( MSVCRT_wchar_t *str, const MSVCRT_wchar_t *delim,
1223 MSVCRT_wchar_t **next_token )
1225 MSVCRT_wchar_t *ret;
1227 if (!MSVCRT_CHECK_PMT(delim != NULL)) return NULL;
1228 if (!MSVCRT_CHECK_PMT(next_token != NULL)) return NULL;
1229 if (!MSVCRT_CHECK_PMT(str != NULL || *next_token != NULL)) return NULL;
1231 if (!str) str = *next_token;
1233 while (*str && strchrW( delim, *str )) str++;
1234 if (!*str) return NULL;
1235 ret = str++;
1236 while (*str && !strchrW( delim, *str )) str++;
1237 if (*str) *str++ = 0;
1238 *next_token = str;
1239 return ret;
1242 /*********************************************************************
1243 * wcstok (MSVCRT.@)
1245 MSVCRT_wchar_t * CDECL MSVCRT_wcstok( MSVCRT_wchar_t *str, const MSVCRT_wchar_t *delim )
1247 return wcstok_s(str, delim, &msvcrt_get_thread_data()->wcstok_next);
1250 /*********************************************************************
1251 * _wctomb_s_l (MSVCRT.@)
1253 int CDECL MSVCRT__wctomb_s_l(int *len, char *mbchar, MSVCRT_size_t size,
1254 MSVCRT_wchar_t wch, MSVCRT__locale_t locale)
1256 MSVCRT_pthreadlocinfo locinfo;
1257 BOOL error;
1258 int mblen;
1260 if(!mbchar && size>0) {
1261 if(len)
1262 *len = 0;
1263 return 0;
1266 if(len)
1267 *len = -1;
1269 if(!MSVCRT_CHECK_PMT(size <= INT_MAX))
1270 return MSVCRT_EINVAL;
1272 if(!locale)
1273 locinfo = get_locinfo();
1274 else
1275 locinfo = locale->locinfo;
1277 if(!locinfo->lc_codepage) {
1278 if(wch > 0xff) {
1279 if(mbchar && size>0)
1280 memset(mbchar, 0, size);
1281 *MSVCRT__errno() = MSVCRT_EILSEQ;
1282 return MSVCRT_EILSEQ;
1285 if(!MSVCRT_CHECK_PMT_ERR(size >= 1, MSVCRT_ERANGE))
1286 return MSVCRT_ERANGE;
1288 *mbchar = wch;
1289 if(len)
1290 *len = 1;
1291 return 0;
1294 mblen = WideCharToMultiByte(locinfo->lc_codepage, 0, &wch, 1, mbchar, size, NULL, &error);
1295 if(!mblen || error) {
1296 if(!mblen && GetLastError()==ERROR_INSUFFICIENT_BUFFER) {
1297 if(mbchar && size>0)
1298 memset(mbchar, 0, size);
1300 MSVCRT_INVALID_PMT("insufficient buffer size", MSVCRT_ERANGE);
1301 return MSVCRT_ERANGE;
1304 *MSVCRT__errno() = MSVCRT_EILSEQ;
1305 return MSVCRT_EILSEQ;
1308 if(len)
1309 *len = mblen;
1310 return 0;
1313 /*********************************************************************
1314 * wctomb_s (MSVCRT.@)
1316 int CDECL MSVCRT_wctomb_s(int *len, char *mbchar, MSVCRT_size_t size, MSVCRT_wchar_t wch)
1318 return MSVCRT__wctomb_s_l(len, mbchar, size, wch, NULL);
1321 /*********************************************************************
1322 * _wctomb_l (MSVCRT.@)
1324 int CDECL MSVCRT__wctomb_l(char *dst, MSVCRT_wchar_t ch, MSVCRT__locale_t locale)
1326 int len;
1328 MSVCRT__wctomb_s_l(&len, dst, dst ? 6 : 0, ch, locale);
1329 return len;
1332 /*********************************************************************
1333 * wctomb (MSVCRT.@)
1335 INT CDECL MSVCRT_wctomb( char *dst, MSVCRT_wchar_t ch )
1337 return MSVCRT__wctomb_l(dst, ch, NULL);
1340 /*********************************************************************
1341 * wctob (MSVCRT.@)
1343 INT CDECL MSVCRT_wctob( MSVCRT_wint_t wchar )
1345 char out;
1346 BOOL error;
1347 UINT codepage = get_locinfo()->lc_codepage;
1349 if(!codepage) {
1350 if (wchar < 0xff)
1351 return (signed char)wchar;
1352 else
1353 return MSVCRT_EOF;
1354 } else if(WideCharToMultiByte( codepage, 0, &wchar, 1, &out, 1, NULL, &error ) && !error)
1355 return (INT)out;
1356 return MSVCRT_EOF;
1359 /*********************************************************************
1360 * wcrtomb (MSVCRT.@)
1362 MSVCRT_size_t CDECL MSVCRT_wcrtomb( char *dst, MSVCRT_wchar_t ch, MSVCRT_mbstate_t *s)
1364 if(s)
1365 *s = 0;
1366 return MSVCRT_wctomb(dst, ch);
1369 /*********************************************************************
1370 * iswalnum (MSVCRT.@)
1372 INT CDECL MSVCRT_iswalnum( MSVCRT_wchar_t wc )
1374 return isalnumW( wc );
1377 /*********************************************************************
1378 * iswalpha (MSVCRT.@)
1380 INT CDECL MSVCRT_iswalpha( MSVCRT_wchar_t wc )
1382 return isalphaW( wc );
1385 /*********************************************************************
1386 * iswalpha_l (MSVCRT.@)
1388 INT CDECL MSVCRT__iswalpha_l( MSVCRT_wchar_t wc, MSVCRT__locale_t locale )
1390 return isalphaW( wc );
1393 /*********************************************************************
1394 * iswcntrl (MSVCRT.@)
1396 INT CDECL MSVCRT_iswcntrl( MSVCRT_wchar_t wc )
1398 return iscntrlW( wc );
1401 /*********************************************************************
1402 * iswdigit (MSVCRT.@)
1404 INT CDECL MSVCRT_iswdigit( MSVCRT_wchar_t wc )
1406 return isdigitW( wc );
1409 /*********************************************************************
1410 * _iswdigit_l (MSVCRT.@)
1412 INT CDECL MSVCRT__iswdigit_l( MSVCRT_wchar_t wc, MSVCRT__locale_t locale )
1414 return isdigitW( wc );
1417 /*********************************************************************
1418 * iswgraph (MSVCRT.@)
1420 INT CDECL MSVCRT_iswgraph( MSVCRT_wchar_t wc )
1422 return isgraphW( wc );
1425 /*********************************************************************
1426 * iswlower (MSVCRT.@)
1428 INT CDECL MSVCRT_iswlower( MSVCRT_wchar_t wc )
1430 return islowerW( wc );
1433 /*********************************************************************
1434 * iswprint (MSVCRT.@)
1436 INT CDECL MSVCRT_iswprint( MSVCRT_wchar_t wc )
1438 return isprintW( wc );
1441 /*********************************************************************
1442 * iswpunct (MSVCRT.@)
1444 INT CDECL MSVCRT_iswpunct( MSVCRT_wchar_t wc )
1446 return ispunctW( wc );
1449 /*********************************************************************
1450 * iswspace (MSVCRT.@)
1452 INT CDECL MSVCRT_iswspace( MSVCRT_wchar_t wc )
1454 return isspaceW( wc );
1457 /*********************************************************************
1458 * iswupper (MSVCRT.@)
1460 INT CDECL MSVCRT_iswupper( MSVCRT_wchar_t wc )
1462 return isupperW( wc );
1465 /*********************************************************************
1466 * iswxdigit (MSVCRT.@)
1468 INT CDECL MSVCRT_iswxdigit( MSVCRT_wchar_t wc )
1470 return isxdigitW( wc );
1473 /*********************************************************************
1474 * wcscpy_s (MSVCRT.@)
1476 INT CDECL MSVCRT_wcscpy_s( MSVCRT_wchar_t* wcDest, MSVCRT_size_t numElement, const MSVCRT_wchar_t *wcSrc)
1478 MSVCRT_size_t size = 0;
1480 if(!MSVCRT_CHECK_PMT(wcDest)) return MSVCRT_EINVAL;
1481 if(!MSVCRT_CHECK_PMT(numElement)) return MSVCRT_EINVAL;
1483 wcDest[0] = 0;
1485 if(!MSVCRT_CHECK_PMT(wcSrc)) return MSVCRT_EINVAL;
1487 size = strlenW(wcSrc) + 1;
1489 if(!MSVCRT_CHECK_PMT_ERR(size <= numElement, MSVCRT_ERANGE))
1490 return MSVCRT_ERANGE;
1492 memcpy( wcDest, wcSrc, size*sizeof(WCHAR) );
1494 return 0;
1497 /******************************************************************
1498 * wcsncpy (MSVCRT.@)
1500 MSVCRT_wchar_t* __cdecl MSVCRT_wcsncpy( MSVCRT_wchar_t* s1,
1501 const MSVCRT_wchar_t *s2, MSVCRT_size_t n )
1503 MSVCRT_size_t i;
1505 for(i=0; i<n; i++)
1506 if(!(s1[i] = s2[i])) break;
1507 for(; i<n; i++)
1508 s1[i] = 0;
1509 return s1;
1512 /******************************************************************
1513 * wcsncpy_s (MSVCRT.@)
1515 INT CDECL MSVCRT_wcsncpy_s( MSVCRT_wchar_t* wcDest, MSVCRT_size_t numElement, const MSVCRT_wchar_t *wcSrc,
1516 MSVCRT_size_t count )
1518 WCHAR *p = wcDest;
1519 BOOL truncate = (count == MSVCRT__TRUNCATE);
1521 if(!wcDest && !numElement && !count)
1522 return 0;
1524 if (!wcDest || !numElement)
1525 return MSVCRT_EINVAL;
1527 if (!wcSrc)
1529 *wcDest = 0;
1530 return count ? MSVCRT_EINVAL : 0;
1533 while (numElement && count && *wcSrc)
1535 *p++ = *wcSrc++;
1536 numElement--;
1537 count--;
1539 if (!numElement && truncate)
1541 *(p-1) = 0;
1542 return MSVCRT_STRUNCATE;
1544 else if (!numElement)
1546 *wcDest = 0;
1547 return MSVCRT_ERANGE;
1550 *p = 0;
1551 return 0;
1554 /******************************************************************
1555 * wcscat_s (MSVCRT.@)
1558 INT CDECL MSVCRT_wcscat_s(MSVCRT_wchar_t* dst, MSVCRT_size_t elem, const MSVCRT_wchar_t* src)
1560 MSVCRT_wchar_t* ptr = dst;
1562 if (!dst || elem == 0) return MSVCRT_EINVAL;
1563 if (!src)
1565 dst[0] = '\0';
1566 return MSVCRT_EINVAL;
1569 /* seek to end of dst string (or elem if no end of string is found */
1570 while (ptr < dst + elem && *ptr != '\0') ptr++;
1571 while (ptr < dst + elem)
1573 if ((*ptr++ = *src++) == '\0') return 0;
1575 /* not enough space */
1576 dst[0] = '\0';
1577 return MSVCRT_ERANGE;
1580 /*********************************************************************
1581 * wcsncat_s (MSVCRT.@)
1584 INT CDECL MSVCRT_wcsncat_s(MSVCRT_wchar_t *dst, MSVCRT_size_t elem,
1585 const MSVCRT_wchar_t *src, MSVCRT_size_t count)
1587 MSVCRT_size_t srclen;
1588 MSVCRT_wchar_t dststart;
1589 INT ret = 0;
1591 if (!MSVCRT_CHECK_PMT(dst != NULL)) return MSVCRT_EINVAL;
1592 if (!MSVCRT_CHECK_PMT(elem > 0)) return MSVCRT_EINVAL;
1593 if (!MSVCRT_CHECK_PMT(src != NULL || count == 0)) return MSVCRT_EINVAL;
1595 if (count == 0)
1596 return 0;
1598 for (dststart = 0; dststart < elem; dststart++)
1600 if (dst[dststart] == '\0')
1601 break;
1603 if (dststart == elem)
1605 MSVCRT_INVALID_PMT("dst[elem] is not NULL terminated\n", MSVCRT_EINVAL);
1606 return MSVCRT_EINVAL;
1609 if (count == MSVCRT__TRUNCATE)
1611 srclen = strlenW(src);
1612 if (srclen >= (elem - dststart))
1614 srclen = elem - dststart - 1;
1615 ret = MSVCRT_STRUNCATE;
1618 else
1619 srclen = min(strlenW(src), count);
1620 if (srclen < (elem - dststart))
1622 memcpy(&dst[dststart], src, srclen*sizeof(MSVCRT_wchar_t));
1623 dst[dststart+srclen] = '\0';
1624 return ret;
1626 MSVCRT_INVALID_PMT("dst[elem] is too small", MSVCRT_ERANGE);
1627 dst[0] = '\0';
1628 return MSVCRT_ERANGE;
1631 /*********************************************************************
1632 * _wcstoi64_l (MSVCRT.@)
1634 * FIXME: locale parameter is ignored
1636 __int64 CDECL MSVCRT__wcstoi64_l(const MSVCRT_wchar_t *nptr,
1637 MSVCRT_wchar_t **endptr, int base, MSVCRT__locale_t locale)
1639 BOOL negative = FALSE;
1640 __int64 ret = 0;
1642 TRACE("(%s %p %d %p)\n", debugstr_w(nptr), endptr, base, locale);
1644 if (!MSVCRT_CHECK_PMT(nptr != NULL)) return 0;
1645 if (!MSVCRT_CHECK_PMT(base == 0 || base >= 2)) return 0;
1646 if (!MSVCRT_CHECK_PMT(base <= 36)) return 0;
1648 while(isspaceW(*nptr)) nptr++;
1650 if(*nptr == '-') {
1651 negative = TRUE;
1652 nptr++;
1653 } else if(*nptr == '+')
1654 nptr++;
1656 if((base==0 || base==16) && *nptr=='0' && tolowerW(*(nptr+1))=='x') {
1657 base = 16;
1658 nptr += 2;
1661 if(base == 0) {
1662 if(*nptr=='0')
1663 base = 8;
1664 else
1665 base = 10;
1668 while(*nptr) {
1669 MSVCRT_wchar_t cur = tolowerW(*nptr);
1670 int v;
1672 if(isdigitW(cur)) {
1673 if(cur >= '0'+base)
1674 break;
1675 v = cur-'0';
1676 } else {
1677 if(cur<'a' || cur>='a'+base-10)
1678 break;
1679 v = cur-'a'+10;
1682 if(negative)
1683 v = -v;
1685 nptr++;
1687 if(!negative && (ret>MSVCRT_I64_MAX/base || ret*base>MSVCRT_I64_MAX-v)) {
1688 ret = MSVCRT_I64_MAX;
1689 *MSVCRT__errno() = MSVCRT_ERANGE;
1690 } else if(negative && (ret<MSVCRT_I64_MIN/base || ret*base<MSVCRT_I64_MIN-v)) {
1691 ret = MSVCRT_I64_MIN;
1692 *MSVCRT__errno() = MSVCRT_ERANGE;
1693 } else
1694 ret = ret*base + v;
1697 if(endptr)
1698 *endptr = (MSVCRT_wchar_t*)nptr;
1700 return ret;
1703 /*********************************************************************
1704 * _wcstoi64 (MSVCRT.@)
1706 __int64 CDECL MSVCRT__wcstoi64(const MSVCRT_wchar_t *nptr,
1707 MSVCRT_wchar_t **endptr, int base)
1709 return MSVCRT__wcstoi64_l(nptr, endptr, base, NULL);
1712 /*********************************************************************
1713 * _wcstol_l (MSVCRT.@)
1715 MSVCRT_long CDECL MSVCRT__wcstol_l(const MSVCRT_wchar_t *s,
1716 MSVCRT_wchar_t **end, int base, MSVCRT__locale_t locale)
1718 __int64 ret = MSVCRT__wcstoi64_l(s, end, base, locale);
1720 if(ret > MSVCRT_LONG_MAX) {
1721 ret = MSVCRT_LONG_MAX;
1722 *MSVCRT__errno() = MSVCRT_ERANGE;
1723 }else if(ret < MSVCRT_LONG_MIN) {
1724 ret = MSVCRT_LONG_MIN;
1725 *MSVCRT__errno() = MSVCRT_ERANGE;
1727 return ret;
1730 /*********************************************************************
1731 * _wtoi_l (MSVCRT.@)
1733 int __cdecl MSVCRT__wtoi_l(const MSVCRT_wchar_t *str, MSVCRT__locale_t locale)
1735 __int64 ret = MSVCRT__wcstoi64_l(str, NULL, 10, locale);
1737 if(ret > INT_MAX) {
1738 ret = INT_MAX;
1739 *MSVCRT__errno() = MSVCRT_ERANGE;
1740 } else if(ret < INT_MIN) {
1741 ret = INT_MIN;
1742 *MSVCRT__errno() = MSVCRT_ERANGE;
1744 return ret;
1747 /*********************************************************************
1748 * _wtoi (MSVCRT.@)
1750 int __cdecl MSVCRT__wtoi(const MSVCRT_wchar_t *str)
1752 return MSVCRT__wtoi_l(str, NULL);
1755 /*********************************************************************
1756 * _wtol_l (MSVCRT.@)
1758 MSVCRT_long __cdecl MSVCRT__wtol_l(const MSVCRT_wchar_t *str, MSVCRT__locale_t locale)
1760 __int64 ret = MSVCRT__wcstoi64_l(str, NULL, 10, locale);
1762 if(ret > MSVCRT_LONG_MAX) {
1763 ret = MSVCRT_LONG_MAX;
1764 *MSVCRT__errno() = MSVCRT_ERANGE;
1765 } else if(ret < MSVCRT_LONG_MIN) {
1766 ret = MSVCRT_LONG_MIN;
1767 *MSVCRT__errno() = MSVCRT_ERANGE;
1769 return ret;
1772 /*********************************************************************
1773 * _wtol (MSVCRT.@)
1775 MSVCRT_long __cdecl MSVCRT__wtol(const MSVCRT_wchar_t *str)
1777 return MSVCRT__wtol_l(str, NULL);
1780 /*********************************************************************
1781 * _wcstoui64_l (MSVCRT.@)
1783 * FIXME: locale parameter is ignored
1785 unsigned __int64 CDECL MSVCRT__wcstoui64_l(const MSVCRT_wchar_t *nptr,
1786 MSVCRT_wchar_t **endptr, int base, MSVCRT__locale_t locale)
1788 BOOL negative = FALSE;
1789 unsigned __int64 ret = 0;
1791 TRACE("(%s %p %d %p)\n", debugstr_w(nptr), endptr, base, locale);
1793 if (!MSVCRT_CHECK_PMT(nptr != NULL)) return 0;
1794 if (!MSVCRT_CHECK_PMT(base == 0 || base >= 2)) return 0;
1795 if (!MSVCRT_CHECK_PMT(base <= 36)) return 0;
1797 while(isspaceW(*nptr)) nptr++;
1799 if(*nptr == '-') {
1800 negative = TRUE;
1801 nptr++;
1802 } else if(*nptr == '+')
1803 nptr++;
1805 if((base==0 || base==16) && *nptr=='0' && tolowerW(*(nptr+1))=='x') {
1806 base = 16;
1807 nptr += 2;
1810 if(base == 0) {
1811 if(*nptr=='0')
1812 base = 8;
1813 else
1814 base = 10;
1817 while(*nptr) {
1818 MSVCRT_wchar_t cur = tolowerW(*nptr);
1819 int v;
1821 if(isdigitW(cur)) {
1822 if(cur >= '0'+base)
1823 break;
1824 v = *nptr-'0';
1825 } else {
1826 if(cur<'a' || cur>='a'+base-10)
1827 break;
1828 v = cur-'a'+10;
1831 nptr++;
1833 if(ret>MSVCRT_UI64_MAX/base || ret*base>MSVCRT_UI64_MAX-v) {
1834 ret = MSVCRT_UI64_MAX;
1835 *MSVCRT__errno() = MSVCRT_ERANGE;
1836 } else
1837 ret = ret*base + v;
1840 if(endptr)
1841 *endptr = (MSVCRT_wchar_t*)nptr;
1843 return negative ? -ret : ret;
1846 /*********************************************************************
1847 * _wcstoui64 (MSVCRT.@)
1849 unsigned __int64 CDECL MSVCRT__wcstoui64(const MSVCRT_wchar_t *nptr,
1850 MSVCRT_wchar_t **endptr, int base)
1852 return MSVCRT__wcstoui64_l(nptr, endptr, base, NULL);
1855 /*********************************************************************
1856 * _wcstoul_l (MSVCRT.@)
1858 MSVCRT_ulong __cdecl MSVCRT__wcstoul_l(const MSVCRT_wchar_t *s,
1859 MSVCRT_wchar_t **end, int base, MSVCRT__locale_t locale)
1861 __int64 ret = MSVCRT__wcstoui64_l(s, end, base, locale);
1863 if(ret > MSVCRT_ULONG_MAX) {
1864 ret = MSVCRT_ULONG_MAX;
1865 *MSVCRT__errno() = MSVCRT_ERANGE;
1867 return ret;
1870 /*********************************************************************
1871 * wcstoul (MSVCRT.@)
1873 MSVCRT_ulong __cdecl MSVCRT_wcstoul(const MSVCRT_wchar_t *s, MSVCRT_wchar_t **end, int base)
1875 return MSVCRT__wcstoul_l(s, end, base, NULL);
1878 /******************************************************************
1879 * wcsnlen (MSVCRT.@)
1881 MSVCRT_size_t CDECL MSVCRT_wcsnlen(const MSVCRT_wchar_t *s, MSVCRT_size_t maxlen)
1883 MSVCRT_size_t i;
1885 for (i = 0; i < maxlen; i++)
1886 if (!s[i]) break;
1887 return i;
1890 /*********************************************************************
1891 * _towupper_l (MSVCRT.@)
1893 int CDECL MSVCRT__towupper_l(MSVCRT_wint_t c, MSVCRT__locale_t locale)
1895 return toupperW(c);
1898 /*********************************************************************
1899 * towupper (MSVCRT.@)
1901 int CDECL MSVCRT_towupper(MSVCRT_wint_t c)
1903 return MSVCRT__towupper_l(c, NULL);
1906 /*********************************************************************
1907 * _towlower_l (MSVCRT.@)
1909 int CDECL MSVCRT__towlower_l(MSVCRT_wint_t c, MSVCRT__locale_t locale)
1911 return tolowerW(c);
1914 /*********************************************************************
1915 * towlower (MSVCRT.@)
1917 int CDECL MSVCRT_towlower(MSVCRT_wint_t c)
1919 return MSVCRT__towlower_l(c, NULL);
1922 /*********************************************************************
1923 * wcschr (MSVCRT.@)
1925 MSVCRT_wchar_t* CDECL MSVCRT_wcschr(const MSVCRT_wchar_t *str, MSVCRT_wchar_t ch)
1927 return strchrW(str, ch);
1930 /***********************************************************************
1931 * wcslen (MSVCRT.@)
1933 int CDECL MSVCRT_wcslen(const MSVCRT_wchar_t *str)
1935 return strlenW(str);
1938 /*********************************************************************
1939 * wcsstr (MSVCRT.@)
1941 MSVCRT_wchar_t* CDECL MSVCRT_wcsstr(const MSVCRT_wchar_t *str, const MSVCRT_wchar_t *sub)
1943 return strstrW(str, sub);
1946 /*********************************************************************
1947 * _wtoi64_l (MSVCRT.@)
1949 __int64 CDECL _wtoi64_l(const MSVCRT_wchar_t *str, MSVCRT__locale_t locale)
1951 ULONGLONG RunningTotal = 0;
1952 BOOL bMinus = FALSE;
1954 while (isspaceW(*str)) {
1955 str++;
1956 } /* while */
1958 if (*str == '+') {
1959 str++;
1960 } else if (*str == '-') {
1961 bMinus = TRUE;
1962 str++;
1963 } /* if */
1965 while (*str >= '0' && *str <= '9') {
1966 RunningTotal = RunningTotal * 10 + *str - '0';
1967 str++;
1968 } /* while */
1970 return bMinus ? -RunningTotal : RunningTotal;
1973 /*********************************************************************
1974 * _wtoi64 (MSVCRT.@)
1976 __int64 CDECL _wtoi64(const MSVCRT_wchar_t *str)
1978 return _wtoi64_l(str, NULL);
1981 /*********************************************************************
1982 * wcsncmp (MSVCRT.@)
1984 int CDECL MSVCRT_wcsncmp(const MSVCRT_wchar_t *str1, const MSVCRT_wchar_t *str2, int n)
1986 return strncmpW(str1, str2, n);