msvcrt: Implement _makepath_s.
[wine.git] / dlls / msvcrt / wcs.c
blob03ecc7fdcbed4328f3b14dabc91a38cdfad5b6eb
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 "wine/unicode.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
36 /*********************************************************************
37 * _wcsdup (MSVCRT.@)
39 MSVCRT_wchar_t* CDECL _wcsdup( const MSVCRT_wchar_t* str )
41 MSVCRT_wchar_t* ret = NULL;
42 if (str)
44 int size = (strlenW(str) + 1) * sizeof(MSVCRT_wchar_t);
45 ret = MSVCRT_malloc( size );
46 if (ret) memcpy( ret, str, size );
48 return ret;
51 /*********************************************************************
52 * _wcsicoll (MSVCRT.@)
54 INT CDECL _wcsicoll( const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2 )
56 /* FIXME: handle collates */
57 return strcmpiW( str1, str2 );
60 /*********************************************************************
61 * _wcsnset (MSVCRT.@)
63 MSVCRT_wchar_t* CDECL MSVCRT__wcsnset( MSVCRT_wchar_t* str, MSVCRT_wchar_t c, MSVCRT_size_t n )
65 MSVCRT_wchar_t* ret = str;
66 while ((n-- > 0) && *str) *str++ = c;
67 return ret;
70 /*********************************************************************
71 * _wcsrev (MSVCRT.@)
73 MSVCRT_wchar_t* CDECL _wcsrev( MSVCRT_wchar_t* str )
75 MSVCRT_wchar_t* ret = str;
76 MSVCRT_wchar_t* end = str + strlenW(str) - 1;
77 while (end > str)
79 MSVCRT_wchar_t t = *end;
80 *end-- = *str;
81 *str++ = t;
83 return ret;
86 /*********************************************************************
87 * _wcsset (MSVCRT.@)
89 MSVCRT_wchar_t* CDECL _wcsset( MSVCRT_wchar_t* str, MSVCRT_wchar_t c )
91 MSVCRT_wchar_t* ret = str;
92 while (*str) *str++ = c;
93 return ret;
96 /******************************************************************
97 * _wcsupr_s (MSVCRT.@)
100 INT CDECL MSVCRT__wcsupr_s( MSVCRT_wchar_t* str, MSVCRT_size_t n )
102 MSVCRT_wchar_t* ptr = str;
104 if (!str || !n)
106 if (str) *str = '\0';
107 *MSVCRT__errno() = MSVCRT_EINVAL;
108 return MSVCRT_EINVAL;
111 while (n--)
113 if (!*ptr) return 0;
114 *ptr = toupperW(*ptr);
115 ptr++;
118 /* MSDN claims that the function should return and set errno to
119 * ERANGE, which doesn't seem to be true based on the tests. */
120 *str = '\0';
121 *MSVCRT__errno() = MSVCRT_EINVAL;
122 return MSVCRT_EINVAL;
125 /*********************************************************************
126 * _wcstod_l - not exported in native msvcrt
128 double CDECL MSVCRT__wcstod_l(const MSVCRT_wchar_t* str, MSVCRT_wchar_t** end,
129 MSVCRT__locale_t locale)
131 unsigned __int64 d=0, hlp;
132 unsigned fpcontrol;
133 int exp=0, sign=1;
134 const MSVCRT_wchar_t *p;
135 double ret;
137 if(!str) {
138 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
139 *MSVCRT__errno() = MSVCRT_EINVAL;
140 return 0;
143 if(!locale)
144 locale = get_locale();
146 p = str;
147 while(isspaceW(*p))
148 p++;
150 if(*p == '-') {
151 sign = -1;
152 p++;
153 } else if(*p == '+')
154 p++;
156 while(isdigitW(*p)) {
157 hlp = d*10+*(p++)-'0';
158 if(d>MSVCRT_UI64_MAX/10 || hlp<d) {
159 exp++;
160 break;
161 } else
162 d = hlp;
164 while(isdigitW(*p)) {
165 exp++;
166 p++;
168 if(*p == *locale->locinfo->lconv->decimal_point)
169 p++;
171 while(isdigitW(*p)) {
172 hlp = d*10+*(p++)-'0';
173 if(d>MSVCRT_UI64_MAX/10 || hlp<d)
174 break;
176 d = hlp;
177 exp--;
179 while(isdigitW(*p))
180 p++;
182 if(p == str) {
183 if(end)
184 *end = (MSVCRT_wchar_t*)str;
185 return 0.0;
188 if(*p=='e' || *p=='E' || *p=='d' || *p=='D') {
189 int e=0, s=1;
191 p++;
192 if(*p == '-') {
193 s = -1;
194 p++;
195 } else if(*p == '+')
196 p++;
198 if(isdigitW(*p)) {
199 while(isdigitW(*p)) {
200 if(e>INT_MAX/10 || (e=e*10+*p-'0')<0)
201 e = INT_MAX;
202 p++;
204 e *= s;
206 if(exp<0 && e<0 && exp+e>=0) exp = INT_MIN;
207 else if(exp>0 && e>0 && exp+e<0) exp = INT_MAX;
208 else exp += e;
209 } else {
210 if(*p=='-' || *p=='+')
211 p--;
212 p--;
216 fpcontrol = _control87(0, 0);
217 _control87(MSVCRT__EM_DENORMAL|MSVCRT__EM_INVALID|MSVCRT__EM_ZERODIVIDE
218 |MSVCRT__EM_OVERFLOW|MSVCRT__EM_UNDERFLOW|MSVCRT__EM_INEXACT, 0xffffffff);
220 if(exp>0)
221 ret = (double)sign*d*pow(10, exp);
222 else
223 ret = (double)sign*d/pow(10, -exp);
225 _control87(fpcontrol, 0xffffffff);
227 if((d && ret==0.0) || isinf(ret))
228 *MSVCRT__errno() = MSVCRT_ERANGE;
230 if(end)
231 *end = (MSVCRT_wchar_t*)p;
233 return ret;
236 /*********************************************************************
237 * _wcstombs_l (MSVCRT.@)
239 MSVCRT_size_t CDECL MSVCRT__wcstombs_l(char *mbstr, const MSVCRT_wchar_t *wcstr,
240 MSVCRT_size_t count, MSVCRT__locale_t locale)
242 char default_char = '\0';
243 MSVCRT_size_t tmp = 0;
244 BOOL used_default;
246 if(!locale)
247 locale = get_locale();
249 if(!mbstr)
250 return WideCharToMultiByte(locale->locinfo->lc_codepage, WC_NO_BEST_FIT_CHARS,
251 wcstr, -1, NULL, 0, &default_char, &used_default)-1;
253 while(*wcstr) {
254 char buf[3];
255 MSVCRT_size_t i, size;
257 size = WideCharToMultiByte(locale->locinfo->lc_codepage, WC_NO_BEST_FIT_CHARS,
258 wcstr, 1, buf, 3, &default_char, &used_default);
259 if(used_default)
260 return -1;
261 if(tmp+size > count)
262 return tmp;
264 for(i=0; i<size; i++)
265 mbstr[tmp++] = buf[i];
266 wcstr++;
269 if(tmp < count)
270 mbstr[tmp] = '\0';
271 return tmp;
274 /*********************************************************************
275 * wcstombs (MSVCRT.@)
277 MSVCRT_size_t CDECL MSVCRT_wcstombs(char *mbstr, const MSVCRT_wchar_t *wcstr,
278 MSVCRT_size_t count)
280 return MSVCRT__wcstombs_l(mbstr, wcstr, count, NULL);
283 /*********************************************************************
284 * _wcstombs_s_l (MSVCRT.@)
286 MSVCRT_size_t CDECL MSVCRT__wcstombs_s_l(MSVCRT_size_t *ret, char *mbstr,
287 MSVCRT_size_t size, const MSVCRT_wchar_t *wcstr,
288 MSVCRT_size_t count, MSVCRT__locale_t locale)
290 MSVCRT_size_t conv;
292 if(!mbstr && !size) {
293 conv = MSVCRT__wcstombs_l(NULL, wcstr, 0, locale);
294 if(ret)
295 *ret = conv;
296 return 0;
299 if(!wcstr || !mbstr) {
300 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
301 if(mbstr && size)
302 mbstr[0] = '\0';
303 *MSVCRT__errno() = MSVCRT_EINVAL;
304 return MSVCRT_EINVAL;
307 if(count==MSVCRT__TRUNCATE || size<count)
308 conv = size;
309 else
310 conv = count;
312 conv = MSVCRT__wcstombs_l(mbstr, wcstr, conv, locale);
313 if(conv<size)
314 mbstr[conv++] = '\0';
315 else if(conv==size && (count==MSVCRT__TRUNCATE || mbstr[conv-1]=='\0'))
316 mbstr[conv-1] = '\0';
317 else {
318 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
319 if(size)
320 mbstr[0] = '\0';
321 *MSVCRT__errno() = MSVCRT_ERANGE;
322 return MSVCRT_ERANGE;
325 if(ret)
326 *ret = conv;
327 return 0;
330 /*********************************************************************
331 * wcstombs_s (MSVCRT.@)
333 MSVCRT_size_t CDECL MSVCRT_wcstombs_s(MSVCRT_size_t *ret, char *mbstr,
334 MSVCRT_size_t size, const MSVCRT_wchar_t *wcstr, MSVCRT_size_t count)
336 return MSVCRT__wcstombs_s_l(ret, mbstr, size, wcstr, count, NULL);
339 /*********************************************************************
340 * wcstod (MSVCRT.@)
342 double CDECL MSVCRT_wcstod(const MSVCRT_wchar_t* lpszStr, MSVCRT_wchar_t** end)
344 return MSVCRT__wcstod_l(lpszStr, end, NULL);
347 /*********************************************************************
348 * _wtof (MSVCRT.@)
350 double CDECL MSVCRT__wtof(const MSVCRT_wchar_t *str)
352 return MSVCRT__wcstod_l(str, NULL, NULL);
355 /*********************************************************************
356 * _wtof_l (MSVCRT.@)
358 double CDECL MSVCRT__wtof_l(const MSVCRT_wchar_t *str, MSVCRT__locale_t locale)
360 return MSVCRT__wcstod_l(str, NULL, locale);
363 typedef struct pf_output_t
365 int used;
366 int len;
367 BOOL unicode;
368 union {
369 LPWSTR W;
370 LPSTR A;
371 } buf;
372 } pf_output;
374 typedef struct pf_flags_t
376 char Sign, LeftAlign, Alternate, PadZero;
377 int FieldLength, Precision;
378 char IntegerLength, IntegerDouble;
379 char WideString;
380 char Format;
381 } pf_flags;
384 * writes a string of characters to the output
385 * returns -1 if the string doesn't fit in the output buffer
386 * return the length of the string if all characters were written
388 static inline int pf_output_stringW( pf_output *out, LPCWSTR str, int len )
390 int space = out->len - out->used;
392 if( len < 0 )
393 len = strlenW( str );
394 if( out->unicode )
396 LPWSTR p = out->buf.W + out->used;
398 if( space >= len )
400 if (out->buf.W) memcpy( p, str, len*sizeof(WCHAR) );
401 out->used += len;
402 return len;
404 if( space > 0 && out->buf.W )
405 memcpy( p, str, space*sizeof(WCHAR) );
406 out->used += len;
408 else
410 int n = WideCharToMultiByte( CP_ACP, 0, str, len, NULL, 0, NULL, NULL );
411 LPSTR p = out->buf.A + out->used;
413 if( space >= n )
415 if (out->buf.A) WideCharToMultiByte( CP_ACP, 0, str, len, p, n, NULL, NULL );
416 out->used += n;
417 return len;
419 if( space > 0 && out->buf.A )
420 WideCharToMultiByte( CP_ACP, 0, str, len, p, space, NULL, NULL );
421 out->used += n;
423 return -1;
426 static inline int pf_output_stringA( pf_output *out, LPCSTR str, int len )
428 int space = out->len - out->used;
430 if( len < 0 )
431 len = strlen( str );
432 if( !out->unicode )
434 LPSTR p = out->buf.A + out->used;
436 if( space >= len )
438 if (out->buf.A) memcpy( p, str, len );
439 out->used += len;
440 return len;
442 if( space > 0 && out->buf.A )
443 memcpy( p, str, space );
444 out->used += len;
446 else
448 int n = MultiByteToWideChar( CP_ACP, 0, str, len, NULL, 0 );
449 LPWSTR p = out->buf.W + out->used;
451 if( space >= n )
453 if (out->buf.W) MultiByteToWideChar( CP_ACP, 0, str, len, p, n );
454 out->used += n;
455 return len;
457 if( space > 0 && out->buf.W )
458 MultiByteToWideChar( CP_ACP, 0, str, len, p, space );
459 out->used += n;
461 return -1;
464 /* pf_fill: takes care of signs, alignment, zero and field padding */
465 static inline int pf_fill( pf_output *out, int len, pf_flags *flags, char left )
467 int i, r = 0;
469 if( flags->Sign && !( flags->Format == 'd' || flags->Format == 'i' ) )
470 flags->Sign = 0;
472 if( left && flags->Sign )
474 flags->FieldLength--;
475 if( flags->PadZero )
476 r = pf_output_stringA( out, &flags->Sign, 1 );
479 if( ( !left && flags->LeftAlign ) ||
480 ( left && !flags->LeftAlign ))
482 for( i=0; (i<(flags->FieldLength-len)) && (r>=0); i++ )
484 if( left && flags->PadZero )
485 r = pf_output_stringA( out, "0", 1 );
486 else
487 r = pf_output_stringA( out, " ", 1 );
491 if( left && flags->Sign && !flags->PadZero )
492 r = pf_output_stringA( out, &flags->Sign, 1 );
494 return r;
497 static inline int pf_output_format_W( pf_output *out, LPCWSTR str,
498 int len, pf_flags *flags )
500 int r = 0;
502 if( len < 0 )
503 len = strlenW( str );
505 if (flags->Precision >= 0 && flags->Precision < len)
506 len = flags->Precision;
508 r = pf_fill( out, len, flags, 1 );
510 if( r>=0 )
511 r = pf_output_stringW( out, str, len );
513 if( r>=0 )
514 r = pf_fill( out, len, flags, 0 );
516 return r;
519 static inline int pf_output_format_A( pf_output *out, LPCSTR str,
520 int len, pf_flags *flags )
522 int r = 0;
524 if( len < 0 )
525 len = strlen( str );
527 if (flags->Precision >= 0 && flags->Precision < len)
528 len = flags->Precision;
530 r = pf_fill( out, len, flags, 1 );
532 if( r>=0 )
533 r = pf_output_stringA( out, str, len );
535 if( r>=0 )
536 r = pf_fill( out, len, flags, 0 );
538 return r;
541 static int pf_handle_string_format( pf_output *out, const void* str, int len,
542 pf_flags *flags, BOOL capital_letter)
544 if(str == NULL) /* catch NULL pointer */
545 return pf_output_format_A( out, "(null)", -1, flags);
547 /* prefixes take priority over %c,%s vs. %C,%S, so we handle them first */
548 if(flags->WideString || flags->IntegerLength == 'l')
549 return pf_output_format_W( out, str, len, flags);
550 if(flags->IntegerLength == 'h')
551 return pf_output_format_A( out, str, len, flags);
553 /* %s,%c -> chars in ansi functions & wchars in unicode
554 * %S,%C -> wchars in ansi functions & chars in unicode */
555 if( capital_letter == out->unicode) /* either both TRUE or both FALSE */
556 return pf_output_format_A( out, str, len, flags);
557 else
558 return pf_output_format_W( out, str, len, flags);
561 static inline BOOL pf_is_integer_format( char fmt )
563 static const char float_fmts[] = "diouxX";
564 if (!fmt)
565 return FALSE;
566 return strchr( float_fmts, fmt ) ? TRUE : FALSE;
569 static inline BOOL pf_is_double_format( char fmt )
571 static const char float_fmts[] = "aeEfgG";
572 if (!fmt)
573 return FALSE;
574 return strchr( float_fmts, fmt ) ? TRUE : FALSE;
577 static inline BOOL pf_is_valid_format( char fmt )
579 static const char float_fmts[] = "acCdeEfgGinouxX";
580 if (!fmt)
581 return FALSE;
582 return strchr( float_fmts, fmt ) ? TRUE : FALSE;
585 static void pf_rebuild_format_string( char *p, pf_flags *flags )
587 *p++ = '%';
588 if( flags->Sign )
589 *p++ = flags->Sign;
590 if( flags->LeftAlign )
591 *p++ = flags->LeftAlign;
592 if( flags->Alternate )
593 *p++ = flags->Alternate;
594 if( flags->PadZero )
595 *p++ = flags->PadZero;
596 if( flags->FieldLength )
598 sprintf(p, "%d", flags->FieldLength);
599 p += strlen(p);
601 if( flags->Precision >= 0 )
603 sprintf(p, ".%d", flags->Precision);
604 p += strlen(p);
606 *p++ = flags->Format;
607 *p++ = 0;
610 /* pf_integer_conv: prints x to buf, including alternate formats and
611 additional precision digits, but not field characters or the sign */
612 static void pf_integer_conv( char *buf, int buf_len, pf_flags *flags,
613 LONGLONG x )
615 unsigned int base;
616 const char *digits;
618 int i, j, k;
619 char number[40], *tmp = number;
621 if( buf_len > sizeof number )
622 tmp = HeapAlloc( GetProcessHeap(), 0, buf_len );
624 base = 10;
625 if( flags->Format == 'o' )
626 base = 8;
627 else if( flags->Format == 'x' || flags->Format == 'X' )
628 base = 16;
630 if( flags->Format == 'X' )
631 digits = "0123456789ABCDEFX";
632 else
633 digits = "0123456789abcdefx";
635 if( x < 0 && ( flags->Format == 'd' || flags->Format == 'i' ) )
637 x = -x;
638 flags->Sign = '-';
641 /* Do conversion (backwards) */
642 i = 0;
643 if( x == 0 && flags->Precision )
644 tmp[i++] = '0';
645 else
646 while( x != 0 )
648 j = (ULONGLONG) x % base;
649 x = (ULONGLONG) x / base;
650 tmp[i++] = digits[j];
652 k = flags->Precision - i;
653 while( k-- > 0 )
654 tmp[i++] = '0';
655 if( flags->Alternate )
657 if( base == 16 )
659 tmp[i++] = digits[16];
660 tmp[i++] = '0';
662 else if( base == 8 && tmp[i-1] != '0' )
663 tmp[i++] = '0';
666 /* Reverse for buf */
667 j = 0;
668 while( i-- > 0 )
669 buf[j++] = tmp[i];
670 buf[j] = '\0';
672 /* Adjust precision so pf_fill won't truncate the number later */
673 flags->Precision = strlen( buf );
675 if( tmp != number )
676 HeapFree( GetProcessHeap(), 0, tmp );
678 return;
681 /* pf_fixup_exponent: convert a string containing a 2 digit exponent
682 to 3 digits, accounting for padding, in place. Needed to match
683 the native printf's which always use 3 digits. */
684 static void pf_fixup_exponent( char *buf )
686 char* tmp = buf;
688 while (tmp[0] && toupper(tmp[0]) != 'E')
689 tmp++;
691 if (tmp[0] && (tmp[1] == '+' || tmp[1] == '-') &&
692 isdigit(tmp[2]) && isdigit(tmp[3]))
694 char final;
696 if (isdigit(tmp[4]))
697 return; /* Exponent already 3 digits */
699 /* We have a 2 digit exponent. Prepend '0' to make it 3 */
700 tmp += 2;
701 final = tmp[2];
702 tmp[2] = tmp[1];
703 tmp[1] = tmp[0];
704 tmp[0] = '0';
705 if (final == '\0')
707 /* We didn't expand into trailing space, so this string isn't left
708 * justified. Terminate the string and strip a ' ' at the start of
709 * the string if there is one (as there may be if the string is
710 * right justified).
712 tmp[3] = '\0';
713 if (buf[0] == ' ')
714 memmove(buf, buf + 1, (tmp - buf) + 3);
716 /* Otherwise, we expanded into trailing space -> nothing to do */
720 /*********************************************************************
721 * pf_vsnprintf (INTERNAL)
723 * implements both A and W vsnprintf functions
725 static int pf_vsnprintf( pf_output *out, const WCHAR *format,
726 MSVCRT__locale_t locale, BOOL valid, __ms_va_list valist )
728 int r;
729 LPCWSTR q, p = format;
730 pf_flags flags;
732 if(!locale)
733 locale = get_locale();
735 TRACE("format is %s\n",debugstr_w(format));
736 while (*p)
738 q = strchrW( p, '%' );
740 /* there's no % characters left, output the rest of the string */
741 if( !q )
743 r = pf_output_stringW(out, p, -1);
744 if( r<0 )
745 return r;
746 p += r;
747 continue;
750 /* there's characters before the %, output them */
751 if( q != p )
753 r = pf_output_stringW(out, p, q - p);
754 if( r<0 )
755 return r;
756 p = q;
759 /* we must be at a % now, skip over it */
760 assert( *p == '%' );
761 p++;
763 /* output a single % character */
764 if( *p == '%' )
766 r = pf_output_stringW(out, p++, 1);
767 if( r<0 )
768 return r;
769 continue;
772 /* parse the flags */
773 memset( &flags, 0, sizeof flags );
774 while (*p)
776 if( *p == '+' || *p == ' ' )
778 if ( flags.Sign != '+' )
779 flags.Sign = *p;
781 else if( *p == '-' )
782 flags.LeftAlign = *p;
783 else if( *p == '0' )
784 flags.PadZero = *p;
785 else if( *p == '#' )
786 flags.Alternate = *p;
787 else
788 break;
789 p++;
792 /* deal with the field width specifier */
793 flags.FieldLength = 0;
794 if( *p == '*' )
796 flags.FieldLength = va_arg( valist, int );
797 if (flags.FieldLength < 0)
799 flags.LeftAlign = '-';
800 flags.FieldLength = -flags.FieldLength;
802 p++;
804 else while( isdigit(*p) )
806 flags.FieldLength *= 10;
807 flags.FieldLength += *p++ - '0';
810 /* deal with precision */
811 flags.Precision = -1;
812 if( *p == '.' )
814 flags.Precision = 0;
815 p++;
816 if( *p == '*' )
818 flags.Precision = va_arg( valist, int );
819 p++;
821 else while( isdigit(*p) )
823 flags.Precision *= 10;
824 flags.Precision += *p++ - '0';
828 /* deal with integer width modifier */
829 while( *p )
831 if( *p == 'h' || *p == 'l' || *p == 'L' )
833 flags.IntegerLength = *p;
834 p++;
836 else if( *p == 'I' )
838 if( *(p+1) == '6' && *(p+2) == '4' )
840 flags.IntegerDouble++;
841 p += 3;
843 else if( *(p+1) == '3' && *(p+2) == '2' )
844 p += 3;
845 else if( isdigit(*(p+1)) || *(p+1) == 0 )
846 break;
847 else
848 p++;
850 else if( *p == 'w' )
851 flags.WideString = *p++;
852 else if( *p == 'F' )
853 p++; /* ignore */
854 else
855 break;
858 flags.Format = *p;
859 r = 0;
861 if (flags.Format == '$')
863 FIXME("Positional parameters are not supported (%s)\n", wine_dbgstr_w(format));
864 return -1;
866 /* output a string */
867 if( flags.Format == 's' || flags.Format == 'S' )
868 r = pf_handle_string_format( out, va_arg(valist, const void*), -1,
869 &flags, (flags.Format == 'S') );
871 /* output a single character */
872 else if( flags.Format == 'c' || flags.Format == 'C' )
874 INT ch = va_arg( valist, int );
876 r = pf_handle_string_format( out, &ch, 1, &flags, (flags.Format == 'C') );
879 /* output a pointer */
880 else if( flags.Format == 'p' )
882 char pointer[32];
883 void *ptr = va_arg( valist, void * );
885 flags.PadZero = 0;
886 if( flags.Alternate )
887 sprintf(pointer, "0X%0*lX", 2 * (int)sizeof(ptr), (ULONG_PTR)ptr);
888 else
889 sprintf(pointer, "%0*lX", 2 * (int)sizeof(ptr), (ULONG_PTR)ptr);
890 r = pf_output_format_A( out, pointer, -1, &flags );
893 /* deal with %n */
894 else if( flags.Format == 'n' )
896 int *x = va_arg(valist, int *);
897 *x = out->used;
900 /* deal with 64-bit integers */
901 else if( pf_is_integer_format( flags.Format ) && flags.IntegerDouble )
903 char number[40], *x = number;
905 /* Estimate largest possible required buffer size:
906 * Chooses the larger of the field or precision
907 * Includes extra bytes: 1 byte for null, 1 byte for sign,
908 4 bytes for exponent, 2 bytes for alternate formats, 1 byte
909 for a decimal, and 1 byte for an additional float digit. */
910 int x_len = ((flags.FieldLength > flags.Precision) ?
911 flags.FieldLength : flags.Precision) + 10;
913 if( x_len >= sizeof number)
914 x = HeapAlloc( GetProcessHeap(), 0, x_len );
916 pf_integer_conv( x, x_len, &flags, va_arg(valist, LONGLONG) );
918 r = pf_output_format_A( out, x, -1, &flags );
919 if( x != number )
920 HeapFree( GetProcessHeap(), 0, x );
923 /* deal with integers and floats using libc's printf */
924 else if( pf_is_valid_format( flags.Format ) )
926 char fmt[20], number[40], *x = number, *decimal_point;
928 /* Estimate largest possible required buffer size:
929 * Chooses the larger of the field or precision
930 * Includes extra bytes: 1 byte for null, 1 byte for sign,
931 4 bytes for exponent, 2 bytes for alternate formats, 1 byte
932 for a decimal, and 1 byte for an additional float digit. */
933 int x_len = ((flags.FieldLength > flags.Precision) ?
934 flags.FieldLength : flags.Precision) + 10;
936 if( x_len >= sizeof number)
937 x = HeapAlloc( GetProcessHeap(), 0, x_len );
939 pf_rebuild_format_string( fmt, &flags );
941 if( pf_is_double_format( flags.Format ) )
943 sprintf( x, fmt, va_arg(valist, double) );
944 if (toupper(flags.Format) == 'E' || toupper(flags.Format) == 'G')
945 pf_fixup_exponent( x );
947 else
948 sprintf( x, fmt, va_arg(valist, int) );
950 decimal_point = strchr(x, '.');
951 if(decimal_point)
952 *decimal_point = *locale->locinfo->lconv->decimal_point;
954 r = pf_output_stringA( out, x, -1 );
955 if( x != number )
956 HeapFree( GetProcessHeap(), 0, x );
957 if(r < 0)
958 return r;
960 else
962 if( valid )
964 MSVCRT__invalid_parameter( NULL, NULL, NULL, 0, 0 );
965 *MSVCRT__errno() = MSVCRT_EINVAL;
966 return -1;
969 continue;
972 if( r<0 )
973 return r;
974 p++;
977 /* check we reached the end, and null terminate the string */
978 assert( *p == 0 );
979 pf_output_stringW( out, p, 1 );
981 return out->used - 1;
984 /*********************************************************************
985 * vsnprintf_internal (INTERNAL)
987 static inline int vsnprintf_internal( char *str, MSVCRT_size_t len, const char *format,
988 MSVCRT__locale_t locale, BOOL valid, __ms_va_list valist )
990 DWORD sz;
991 LPWSTR formatW = NULL;
992 pf_output out;
993 int r;
995 out.unicode = FALSE;
996 out.buf.A = str;
997 out.used = 0;
998 out.len = len;
1000 sz = MultiByteToWideChar( CP_ACP, 0, format, -1, NULL, 0 );
1001 formatW = HeapAlloc( GetProcessHeap(), 0, sz*sizeof(WCHAR) );
1002 MultiByteToWideChar( CP_ACP, 0, format, -1, formatW, sz );
1004 r = pf_vsnprintf( &out, formatW, locale, valid, valist );
1006 HeapFree( GetProcessHeap(), 0, formatW );
1008 return r;
1011 /*********************************************************************
1012 * _vsnprintf (MSVCRT.@)
1014 int CDECL MSVCRT_vsnprintf( char *str, MSVCRT_size_t len,
1015 const char *format, __ms_va_list valist )
1017 return vsnprintf_internal(str, len, format, NULL, FALSE, valist);
1020 /*********************************************************************
1021 * _vsnprintf_l (MSVCRT.@)
1023 int CDECL MSVCRT_vsnprintf_l( char *str, MSVCRT_size_t len, const char *format,
1024 MSVCRT__locale_t locale, __ms_va_list valist )
1026 return vsnprintf_internal(str, len, format, locale, FALSE, valist);
1029 /*********************************************************************
1030 * _vsnprintf_s_l (MSVCRT.@)
1032 int CDECL MSVCRT_vsnprintf_s_l( char *str, MSVCRT_size_t sizeOfBuffer,
1033 MSVCRT_size_t count, const char *format,
1034 MSVCRT__locale_t locale, __ms_va_list valist )
1036 int len, ret;
1038 if(sizeOfBuffer<count+1 || count==-1)
1039 len = sizeOfBuffer;
1040 else
1041 len = count+1;
1043 ret = vsnprintf_internal(str, len, format, locale, TRUE, valist);
1045 if(ret<0 || ret==len) {
1046 if(count!=MSVCRT__TRUNCATE && count>sizeOfBuffer) {
1047 MSVCRT__invalid_parameter( NULL, NULL, NULL, 0, 0 );
1048 *MSVCRT__errno() = MSVCRT_ERANGE;
1049 memset(str, 0, sizeOfBuffer);
1050 } else
1051 str[len-1] = '\0';
1053 return -1;
1056 return ret;
1059 /*********************************************************************
1060 * _vsnprintf_s (MSVCRT.@)
1062 int CDECL MSVCRT_vsnprintf_s( char *str, MSVCRT_size_t sizeOfBuffer,
1063 MSVCRT_size_t count, const char *format, __ms_va_list valist )
1065 return MSVCRT_vsnprintf_s_l(str,sizeOfBuffer, count, format, NULL, valist);
1068 /*********************************************************************
1069 * vsprintf (MSVCRT.@)
1071 int CDECL MSVCRT_vsprintf( char *str, const char *format, __ms_va_list valist)
1073 return MSVCRT_vsnprintf(str, INT_MAX, format, valist);
1076 /*********************************************************************
1077 * vsprintf_s (MSVCRT.@)
1079 int CDECL MSVCRT_vsprintf_s( char *str, MSVCRT_size_t num, const char *format, __ms_va_list valist)
1081 return MSVCRT_vsnprintf(str, num, format, valist);
1084 /*********************************************************************
1085 * _vscprintf (MSVCRT.@)
1087 int CDECL _vscprintf( const char *format, __ms_va_list valist )
1089 return MSVCRT_vsnprintf( NULL, INT_MAX, format, valist );
1092 /*********************************************************************
1093 * _snprintf (MSVCRT.@)
1095 int CDECL MSVCRT__snprintf(char *str, unsigned int len, const char *format, ...)
1097 int retval;
1098 __ms_va_list valist;
1099 __ms_va_start(valist, format);
1100 retval = MSVCRT_vsnprintf(str, len, format, valist);
1101 __ms_va_end(valist);
1102 return retval;
1105 /*********************************************************************
1106 * _snprintf_s (MSVCRT.@)
1108 int CDECL MSVCRT__snprintf_s(char *str, unsigned int len, unsigned int count,
1109 const char *format, ...)
1111 int retval;
1112 __ms_va_list valist;
1113 __ms_va_start(valist, format);
1114 retval = MSVCRT_vsnprintf_s_l(str, len, count, format, NULL, valist);
1115 __ms_va_end(valist);
1116 return retval;
1119 /*********************************************************************
1120 * vsnwprintf_internal (INTERNAL)
1122 static inline int vsnwprintf_internal(MSVCRT_wchar_t *str, MSVCRT_size_t len,
1123 const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, BOOL valid,
1124 __ms_va_list valist)
1126 pf_output out;
1128 out.unicode = TRUE;
1129 out.buf.W = str;
1130 out.used = 0;
1131 out.len = len;
1133 return pf_vsnprintf( &out, format, locale, valid, valist );
1136 /*********************************************************************
1137 * _vsnwprintf (MSVCRT.@)
1139 int CDECL MSVCRT_vsnwprintf(MSVCRT_wchar_t *str, MSVCRT_size_t len,
1140 const MSVCRT_wchar_t *format, __ms_va_list valist)
1142 return vsnwprintf_internal(str, len, format, NULL, FALSE, valist);
1145 /*********************************************************************
1146 * _vsnwprintf_l (MSVCRT.@)
1148 int CDECL MSVCRT_vsnwprintf_l(MSVCRT_wchar_t *str, MSVCRT_size_t len,
1149 const MSVCRT_wchar_t *format, MSVCRT__locale_t locale,
1150 __ms_va_list valist)
1152 return vsnwprintf_internal(str, len, format, locale, FALSE, valist);
1155 /*********************************************************************
1156 * _vsnwprintf_s_l (MSVCRT.@)
1158 int CDECL MSVCRT_vsnwprintf_s_l( MSVCRT_wchar_t *str, MSVCRT_size_t sizeOfBuffer,
1159 MSVCRT_size_t count, const MSVCRT_wchar_t *format,
1160 MSVCRT__locale_t locale, __ms_va_list valist)
1162 int len, ret;
1164 len = sizeOfBuffer;
1165 if(count!=-1 && len>count+1)
1166 len = count+1;
1168 ret = vsnwprintf_internal(str, len, format, locale, TRUE, valist);
1170 if(ret<0 || ret==len) {
1171 if(count!=MSVCRT__TRUNCATE && count>sizeOfBuffer) {
1172 MSVCRT__invalid_parameter( NULL, NULL, NULL, 0, 0 );
1173 *MSVCRT__errno() = MSVCRT_ERANGE;
1174 memset(str, 0, sizeOfBuffer*sizeof(MSVCRT_wchar_t));
1175 } else
1176 str[len-1] = '\0';
1178 return -1;
1181 return ret;
1184 /*********************************************************************
1185 * _vsnwprintf_s (MSVCRT.@)
1187 int CDECL MSVCRT_vsnwprintf_s(MSVCRT_wchar_t *str, MSVCRT_size_t sizeOfBuffer,
1188 MSVCRT_size_t count, const MSVCRT_wchar_t *format, __ms_va_list valist)
1190 return MSVCRT_vsnwprintf_s_l(str, sizeOfBuffer, count,
1191 format, NULL, valist);
1194 /*********************************************************************
1195 * _snwprintf (MSVCRT.@)
1197 int CDECL MSVCRT__snwprintf( MSVCRT_wchar_t *str, unsigned int len, const MSVCRT_wchar_t *format, ...)
1199 int retval;
1200 __ms_va_list valist;
1201 __ms_va_start(valist, format);
1202 retval = MSVCRT_vsnwprintf(str, len, format, valist);
1203 __ms_va_end(valist);
1204 return retval;
1207 /*********************************************************************
1208 * _snwprintf_s (MSVCRT.@)
1210 int CDECL MSVCRT__snwprintf_s( MSVCRT_wchar_t *str, unsigned int len, unsigned int count,
1211 const MSVCRT_wchar_t *format, ...)
1213 int retval;
1214 __ms_va_list valist;
1215 __ms_va_start(valist, format);
1216 retval = MSVCRT_vsnwprintf_s_l(str, len, count, format, NULL, valist);
1217 __ms_va_end(valist);
1218 return retval;
1221 /*********************************************************************
1222 * sprintf (MSVCRT.@)
1224 int CDECL MSVCRT_sprintf( char *str, const char *format, ... )
1226 __ms_va_list ap;
1227 int r;
1229 __ms_va_start( ap, format );
1230 r = MSVCRT_vsnprintf( str, INT_MAX, format, ap );
1231 __ms_va_end( ap );
1232 return r;
1235 /*********************************************************************
1236 * sprintf_s (MSVCRT.@)
1238 int CDECL MSVCRT_sprintf_s( char *str, MSVCRT_size_t num, const char *format, ... )
1240 __ms_va_list ap;
1241 int r;
1243 __ms_va_start( ap, format );
1244 r = MSVCRT_vsnprintf( str, num, format, ap );
1245 __ms_va_end( ap );
1246 return r;
1249 /*********************************************************************
1250 * _scwprintf (MSVCRT.@)
1252 int CDECL MSVCRT__scwprintf( const MSVCRT_wchar_t *format, ... )
1254 __ms_va_list ap;
1255 int r;
1257 __ms_va_start( ap, format );
1258 r = MSVCRT_vsnwprintf( NULL, INT_MAX, format, ap );
1259 __ms_va_end( ap );
1260 return r;
1263 /*********************************************************************
1264 * swprintf (MSVCRT.@)
1266 int CDECL MSVCRT_swprintf( MSVCRT_wchar_t *str, const MSVCRT_wchar_t *format, ... )
1268 __ms_va_list ap;
1269 int r;
1271 __ms_va_start( ap, format );
1272 r = MSVCRT_vsnwprintf( str, INT_MAX, format, ap );
1273 __ms_va_end( ap );
1274 return r;
1277 /*********************************************************************
1278 * swprintf_s (MSVCRT.@)
1280 int CDECL MSVCRT_swprintf_s(MSVCRT_wchar_t *str, MSVCRT_size_t numberOfElements,
1281 const MSVCRT_wchar_t *format, ... )
1283 __ms_va_list ap;
1284 int r;
1286 __ms_va_start(ap, format);
1287 r = MSVCRT_vsnwprintf_s(str, numberOfElements, INT_MAX, format, ap);
1288 __ms_va_end(ap);
1290 return r;
1293 /*********************************************************************
1294 * vswprintf (MSVCRT.@)
1296 int CDECL MSVCRT_vswprintf( MSVCRT_wchar_t* str, const MSVCRT_wchar_t* format, __ms_va_list args )
1298 return MSVCRT_vsnwprintf( str, INT_MAX, format, args );
1301 /*********************************************************************
1302 * _vscwprintf (MSVCRT.@)
1304 int CDECL _vscwprintf( const MSVCRT_wchar_t *format, __ms_va_list args )
1306 return MSVCRT_vsnwprintf( NULL, INT_MAX, format, args );
1309 /*********************************************************************
1310 * vswprintf_s (MSVCRT.@)
1312 int CDECL MSVCRT_vswprintf_s(MSVCRT_wchar_t* str, MSVCRT_size_t numberOfElements,
1313 const MSVCRT_wchar_t* format, __ms_va_list args)
1315 return MSVCRT_vsnwprintf_s(str, numberOfElements, INT_MAX, format, args );
1318 /*********************************************************************
1319 * _vswprintf_s_l (MSVCRT.@)
1321 int CDECL MSVCRT_vswprintf_s_l(MSVCRT_wchar_t* str, MSVCRT_size_t numberOfElements,
1322 const MSVCRT_wchar_t* format, MSVCRT__locale_t locale, __ms_va_list args)
1324 return MSVCRT_vsnwprintf_s_l(str, numberOfElements, INT_MAX,
1325 format, locale, args );
1328 /*********************************************************************
1329 * wcscoll (MSVCRT.@)
1331 int CDECL MSVCRT_wcscoll( const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2 )
1333 /* FIXME: handle collates */
1334 return strcmpW( str1, str2 );
1337 /*********************************************************************
1338 * wcspbrk (MSVCRT.@)
1340 MSVCRT_wchar_t* CDECL MSVCRT_wcspbrk( const MSVCRT_wchar_t* str, const MSVCRT_wchar_t* accept )
1342 const MSVCRT_wchar_t* p;
1343 while (*str)
1345 for (p = accept; *p; p++) if (*p == *str) return (MSVCRT_wchar_t*)str;
1346 str++;
1348 return NULL;
1351 /*********************************************************************
1352 * wcstok (MSVCRT.@)
1354 MSVCRT_wchar_t * CDECL MSVCRT_wcstok( MSVCRT_wchar_t *str, const MSVCRT_wchar_t *delim )
1356 thread_data_t *data = msvcrt_get_thread_data();
1357 MSVCRT_wchar_t *ret;
1359 if (!str)
1360 if (!(str = data->wcstok_next)) return NULL;
1362 while (*str && strchrW( delim, *str )) str++;
1363 if (!*str) return NULL;
1364 ret = str++;
1365 while (*str && !strchrW( delim, *str )) str++;
1366 if (*str) *str++ = 0;
1367 data->wcstok_next = str;
1368 return ret;
1372 /*********************************************************************
1373 * wctomb (MSVCRT.@)
1375 INT CDECL MSVCRT_wctomb( char *dst, MSVCRT_wchar_t ch )
1377 return WideCharToMultiByte( CP_ACP, 0, &ch, 1, dst, 6, NULL, NULL );
1380 /*********************************************************************
1381 * iswalnum (MSVCRT.@)
1383 INT CDECL MSVCRT_iswalnum( MSVCRT_wchar_t wc )
1385 return isalnumW( wc );
1388 /*********************************************************************
1389 * iswalpha (MSVCRT.@)
1391 INT CDECL MSVCRT_iswalpha( MSVCRT_wchar_t wc )
1393 return isalphaW( wc );
1396 /*********************************************************************
1397 * iswalpha_l (MSVCRT.@)
1399 INT CDECL MSVCRT__iswalpha_l( MSVCRT_wchar_t wc, MSVCRT__locale_t locale )
1401 return isalphaW( wc );
1404 /*********************************************************************
1405 * iswcntrl (MSVCRT.@)
1407 INT CDECL MSVCRT_iswcntrl( MSVCRT_wchar_t wc )
1409 return iscntrlW( wc );
1412 /*********************************************************************
1413 * iswdigit (MSVCRT.@)
1415 INT CDECL MSVCRT_iswdigit( MSVCRT_wchar_t wc )
1417 return isdigitW( wc );
1420 /*********************************************************************
1421 * iswgraph (MSVCRT.@)
1423 INT CDECL MSVCRT_iswgraph( MSVCRT_wchar_t wc )
1425 return isgraphW( wc );
1428 /*********************************************************************
1429 * iswlower (MSVCRT.@)
1431 INT CDECL MSVCRT_iswlower( MSVCRT_wchar_t wc )
1433 return islowerW( wc );
1436 /*********************************************************************
1437 * iswprint (MSVCRT.@)
1439 INT CDECL MSVCRT_iswprint( MSVCRT_wchar_t wc )
1441 return isprintW( wc );
1444 /*********************************************************************
1445 * iswpunct (MSVCRT.@)
1447 INT CDECL MSVCRT_iswpunct( MSVCRT_wchar_t wc )
1449 return ispunctW( wc );
1452 /*********************************************************************
1453 * iswspace (MSVCRT.@)
1455 INT CDECL MSVCRT_iswspace( MSVCRT_wchar_t wc )
1457 return isspaceW( wc );
1460 /*********************************************************************
1461 * iswupper (MSVCRT.@)
1463 INT CDECL MSVCRT_iswupper( MSVCRT_wchar_t wc )
1465 return isupperW( wc );
1468 /*********************************************************************
1469 * iswxdigit (MSVCRT.@)
1471 INT CDECL MSVCRT_iswxdigit( MSVCRT_wchar_t wc )
1473 return isxdigitW( wc );
1476 /*********************************************************************
1477 * wcscpy_s (MSVCRT.@)
1479 INT CDECL MSVCRT_wcscpy_s( MSVCRT_wchar_t* wcDest, MSVCRT_size_t numElement, const MSVCRT_wchar_t *wcSrc)
1481 MSVCRT_size_t size = 0;
1483 if(!wcDest || !numElement)
1484 return MSVCRT_EINVAL;
1486 wcDest[0] = 0;
1488 if(!wcSrc)
1490 return MSVCRT_EINVAL;
1493 size = strlenW(wcSrc) + 1;
1495 if(size > numElement)
1497 return MSVCRT_ERANGE;
1500 memcpy( wcDest, wcSrc, size*sizeof(WCHAR) );
1502 return 0;
1505 /******************************************************************
1506 * wcsncpy_s (MSVCRT.@)
1508 INT CDECL MSVCRT_wcsncpy_s( MSVCRT_wchar_t* wcDest, MSVCRT_size_t numElement, const MSVCRT_wchar_t *wcSrc,
1509 MSVCRT_size_t count )
1511 MSVCRT_size_t size = 0;
1513 if (!wcDest || !numElement)
1514 return MSVCRT_EINVAL;
1516 wcDest[0] = 0;
1518 if (!wcSrc)
1520 return MSVCRT_EINVAL;
1523 size = min(strlenW(wcSrc), count);
1525 if (size >= numElement)
1527 return MSVCRT_ERANGE;
1530 memcpy( wcDest, wcSrc, size*sizeof(WCHAR) );
1531 wcDest[size] = '\0';
1533 return 0;
1536 /******************************************************************
1537 * wcscat_s (MSVCRT.@)
1540 INT CDECL MSVCRT_wcscat_s(MSVCRT_wchar_t* dst, MSVCRT_size_t elem, const MSVCRT_wchar_t* src)
1542 MSVCRT_wchar_t* ptr = dst;
1544 if (!dst || elem == 0) return MSVCRT_EINVAL;
1545 if (!src)
1547 dst[0] = '\0';
1548 return MSVCRT_EINVAL;
1551 /* seek to end of dst string (or elem if no end of string is found */
1552 while (ptr < dst + elem && *ptr != '\0') ptr++;
1553 while (ptr < dst + elem)
1555 if ((*ptr++ = *src++) == '\0') return 0;
1557 /* not enough space */
1558 dst[0] = '\0';
1559 return MSVCRT_ERANGE;
1562 /*********************************************************************
1563 * _wcstoi64_l (MSVCRT.@)
1565 * FIXME: locale parameter is ignored
1567 __int64 CDECL MSVCRT__wcstoi64_l(const MSVCRT_wchar_t *nptr,
1568 MSVCRT_wchar_t **endptr, int base, MSVCRT__locale_t locale)
1570 BOOL negative = FALSE;
1571 __int64 ret = 0;
1573 TRACE("(%s %p %d %p)\n", debugstr_w(nptr), endptr, base, locale);
1575 if(!nptr || base<0 || base>36 || base==1) {
1576 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
1577 return 0;
1580 while(isspaceW(*nptr)) nptr++;
1582 if(*nptr == '-') {
1583 negative = TRUE;
1584 nptr++;
1585 } else if(*nptr == '+')
1586 nptr++;
1588 if((base==0 || base==16) && *nptr=='0' && tolowerW(*(nptr+1))=='x') {
1589 base = 16;
1590 nptr += 2;
1593 if(base == 0) {
1594 if(*nptr=='0')
1595 base = 8;
1596 else
1597 base = 10;
1600 while(*nptr) {
1601 char cur = tolowerW(*nptr);
1602 int v;
1604 if(isdigitW(cur)) {
1605 if(cur >= '0'+base)
1606 break;
1607 v = cur-'0';
1608 } else {
1609 if(cur<'a' || cur>='a'+base-10)
1610 break;
1611 v = cur-'a'+10;
1614 if(negative)
1615 v = -v;
1617 nptr++;
1619 if(!negative && (ret>MSVCRT_I64_MAX/base || ret*base>MSVCRT_I64_MAX-v)) {
1620 ret = MSVCRT_I64_MAX;
1621 *MSVCRT__errno() = MSVCRT_ERANGE;
1622 } else if(negative && (ret<MSVCRT_I64_MIN/base || ret*base<MSVCRT_I64_MIN-v)) {
1623 ret = MSVCRT_I64_MIN;
1624 *MSVCRT__errno() = MSVCRT_ERANGE;
1625 } else
1626 ret = ret*base + v;
1629 if(endptr)
1630 *endptr = (MSVCRT_wchar_t*)nptr;
1632 return ret;
1635 /*********************************************************************
1636 * _wcstoi64 (MSVCRT.@)
1638 __int64 CDECL MSVCRT__wcstoi64(const MSVCRT_wchar_t *nptr,
1639 MSVCRT_wchar_t **endptr, int base)
1641 return MSVCRT__wcstoi64_l(nptr, endptr, base, NULL);
1644 /*********************************************************************
1645 * _wcstoui64_l (MSVCRT.@)
1647 * FIXME: locale parameter is ignored
1649 unsigned __int64 CDECL MSVCRT__wcstoui64_l(const MSVCRT_wchar_t *nptr,
1650 MSVCRT_wchar_t **endptr, int base, MSVCRT__locale_t locale)
1652 BOOL negative = FALSE;
1653 unsigned __int64 ret = 0;
1655 TRACE("(%s %p %d %p)\n", debugstr_w(nptr), endptr, base, locale);
1657 if(!nptr || base<0 || base>36 || base==1) {
1658 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
1659 return 0;
1662 while(isspaceW(*nptr)) nptr++;
1664 if(*nptr == '-') {
1665 negative = TRUE;
1666 nptr++;
1667 } else if(*nptr == '+')
1668 nptr++;
1670 if((base==0 || base==16) && *nptr=='0' && tolowerW(*(nptr+1))=='x') {
1671 base = 16;
1672 nptr += 2;
1675 if(base == 0) {
1676 if(*nptr=='0')
1677 base = 8;
1678 else
1679 base = 10;
1682 while(*nptr) {
1683 char cur = tolowerW(*nptr);
1684 int v;
1686 if(isdigit(cur)) {
1687 if(cur >= '0'+base)
1688 break;
1689 v = *nptr-'0';
1690 } else {
1691 if(cur<'a' || cur>='a'+base-10)
1692 break;
1693 v = cur-'a'+10;
1696 nptr++;
1698 if(ret>MSVCRT_UI64_MAX/base || ret*base>MSVCRT_UI64_MAX-v) {
1699 ret = MSVCRT_UI64_MAX;
1700 *MSVCRT__errno() = MSVCRT_ERANGE;
1701 } else
1702 ret = ret*base + v;
1705 if(endptr)
1706 *endptr = (MSVCRT_wchar_t*)nptr;
1708 return negative ? -ret : ret;
1711 /*********************************************************************
1712 * _wcstoui64 (MSVCRT.@)
1714 unsigned __int64 CDECL MSVCRT__wcstoui64(const MSVCRT_wchar_t *nptr,
1715 MSVCRT_wchar_t **endptr, int base)
1717 return MSVCRT__wcstoui64_l(nptr, endptr, base, NULL);