user32: Convert menu and string table resources to po files.
[wine.git] / dlls / msvcrt / wcs.c
blobb01e60b09fc0a16566cb07716509cd3d331b582f
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 * _wcsnicoll (MSVCRT.@)
63 INT CDECL _wcsnicoll( const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2, MSVCRT_size_t count )
65 /* FIXME: handle collates */
66 return strncmpiW( str1, str2, count );
69 /*********************************************************************
70 * _wcsnset (MSVCRT.@)
72 MSVCRT_wchar_t* CDECL MSVCRT__wcsnset( MSVCRT_wchar_t* str, MSVCRT_wchar_t c, MSVCRT_size_t n )
74 MSVCRT_wchar_t* ret = str;
75 while ((n-- > 0) && *str) *str++ = c;
76 return ret;
79 /*********************************************************************
80 * _wcsrev (MSVCRT.@)
82 MSVCRT_wchar_t* CDECL _wcsrev( MSVCRT_wchar_t* str )
84 MSVCRT_wchar_t* ret = str;
85 MSVCRT_wchar_t* end = str + strlenW(str) - 1;
86 while (end > str)
88 MSVCRT_wchar_t t = *end;
89 *end-- = *str;
90 *str++ = t;
92 return ret;
95 /*********************************************************************
96 * _wcsset (MSVCRT.@)
98 MSVCRT_wchar_t* CDECL _wcsset( MSVCRT_wchar_t* str, MSVCRT_wchar_t c )
100 MSVCRT_wchar_t* ret = str;
101 while (*str) *str++ = c;
102 return ret;
105 /******************************************************************
106 * _wcsupr_s (MSVCRT.@)
109 INT CDECL MSVCRT__wcsupr_s( MSVCRT_wchar_t* str, MSVCRT_size_t n )
111 MSVCRT_wchar_t* ptr = str;
113 if (!str || !n)
115 if (str) *str = '\0';
116 *MSVCRT__errno() = MSVCRT_EINVAL;
117 return MSVCRT_EINVAL;
120 while (n--)
122 if (!*ptr) return 0;
123 *ptr = toupperW(*ptr);
124 ptr++;
127 /* MSDN claims that the function should return and set errno to
128 * ERANGE, which doesn't seem to be true based on the tests. */
129 *str = '\0';
130 *MSVCRT__errno() = MSVCRT_EINVAL;
131 return MSVCRT_EINVAL;
134 /*********************************************************************
135 * _wcstod_l - not exported in native msvcrt
137 double CDECL MSVCRT__wcstod_l(const MSVCRT_wchar_t* str, MSVCRT_wchar_t** end,
138 MSVCRT__locale_t locale)
140 unsigned __int64 d=0, hlp;
141 unsigned fpcontrol;
142 int exp=0, sign=1;
143 const MSVCRT_wchar_t *p;
144 double ret;
145 BOOL found_digit = FALSE;
147 if (!MSVCRT_CHECK_PMT(str != NULL)) {
148 *MSVCRT__errno() = MSVCRT_EINVAL;
149 return 0;
152 if(!locale)
153 locale = get_locale();
155 p = str;
156 while(isspaceW(*p))
157 p++;
159 if(*p == '-') {
160 sign = -1;
161 p++;
162 } else if(*p == '+')
163 p++;
165 while(isdigitW(*p)) {
166 found_digit = TRUE;
167 hlp = d*10+*(p++)-'0';
168 if(d>MSVCRT_UI64_MAX/10 || hlp<d) {
169 exp++;
170 break;
171 } else
172 d = hlp;
174 while(isdigitW(*p)) {
175 exp++;
176 p++;
178 if(*p == *locale->locinfo->lconv->decimal_point)
179 p++;
181 while(isdigitW(*p)) {
182 found_digit = TRUE;
183 hlp = d*10+*(p++)-'0';
184 if(d>MSVCRT_UI64_MAX/10 || hlp<d)
185 break;
187 d = hlp;
188 exp--;
190 while(isdigitW(*p))
191 p++;
193 if(!found_digit) {
194 if(end)
195 *end = (MSVCRT_wchar_t*)str;
196 return 0.0;
199 if(*p=='e' || *p=='E' || *p=='d' || *p=='D') {
200 int e=0, s=1;
202 p++;
203 if(*p == '-') {
204 s = -1;
205 p++;
206 } else if(*p == '+')
207 p++;
209 if(isdigitW(*p)) {
210 while(isdigitW(*p)) {
211 if(e>INT_MAX/10 || (e=e*10+*p-'0')<0)
212 e = INT_MAX;
213 p++;
215 e *= s;
217 if(exp<0 && e<0 && exp+e>=0) exp = INT_MIN;
218 else if(exp>0 && e>0 && exp+e<0) exp = INT_MAX;
219 else exp += e;
220 } else {
221 if(*p=='-' || *p=='+')
222 p--;
223 p--;
227 fpcontrol = _control87(0, 0);
228 _control87(MSVCRT__EM_DENORMAL|MSVCRT__EM_INVALID|MSVCRT__EM_ZERODIVIDE
229 |MSVCRT__EM_OVERFLOW|MSVCRT__EM_UNDERFLOW|MSVCRT__EM_INEXACT, 0xffffffff);
231 if(exp>0)
232 ret = (double)sign*d*pow(10, exp);
233 else
234 ret = (double)sign*d/pow(10, -exp);
236 _control87(fpcontrol, 0xffffffff);
238 if((d && ret==0.0) || isinf(ret))
239 *MSVCRT__errno() = MSVCRT_ERANGE;
241 if(end)
242 *end = (MSVCRT_wchar_t*)p;
244 return ret;
247 /*********************************************************************
248 * _wcstombs_l (MSVCRT.@)
250 MSVCRT_size_t CDECL MSVCRT__wcstombs_l(char *mbstr, const MSVCRT_wchar_t *wcstr,
251 MSVCRT_size_t count, MSVCRT__locale_t locale)
253 char default_char = '\0';
254 MSVCRT_size_t tmp = 0;
255 BOOL used_default;
257 if(!locale)
258 locale = get_locale();
260 if(!mbstr)
261 return WideCharToMultiByte(locale->locinfo->lc_codepage, WC_NO_BEST_FIT_CHARS,
262 wcstr, -1, NULL, 0, &default_char, &used_default)-1;
264 while(*wcstr) {
265 char buf[3];
266 MSVCRT_size_t i, size;
268 size = WideCharToMultiByte(locale->locinfo->lc_codepage, WC_NO_BEST_FIT_CHARS,
269 wcstr, 1, buf, 3, &default_char, &used_default);
270 if(used_default)
271 return -1;
272 if(tmp+size > count)
273 return tmp;
275 for(i=0; i<size; i++)
276 mbstr[tmp++] = buf[i];
277 wcstr++;
280 if(tmp < count)
281 mbstr[tmp] = '\0';
282 return tmp;
285 /*********************************************************************
286 * wcstombs (MSVCRT.@)
288 MSVCRT_size_t CDECL MSVCRT_wcstombs(char *mbstr, const MSVCRT_wchar_t *wcstr,
289 MSVCRT_size_t count)
291 return MSVCRT__wcstombs_l(mbstr, wcstr, count, NULL);
294 /*********************************************************************
295 * _wcstombs_s_l (MSVCRT.@)
297 MSVCRT_size_t CDECL MSVCRT__wcstombs_s_l(MSVCRT_size_t *ret, char *mbstr,
298 MSVCRT_size_t size, const MSVCRT_wchar_t *wcstr,
299 MSVCRT_size_t count, MSVCRT__locale_t locale)
301 MSVCRT_size_t conv;
303 if(!mbstr && !size) {
304 conv = MSVCRT__wcstombs_l(NULL, wcstr, 0, locale);
305 if(ret)
306 *ret = conv;
307 return 0;
310 if (!MSVCRT_CHECK_PMT(wcstr != NULL) || !MSVCRT_CHECK_PMT(mbstr != NULL)) {
311 if(mbstr && size)
312 mbstr[0] = '\0';
313 *MSVCRT__errno() = MSVCRT_EINVAL;
314 return MSVCRT_EINVAL;
317 if(count==MSVCRT__TRUNCATE || size<count)
318 conv = size;
319 else
320 conv = count;
322 conv = MSVCRT__wcstombs_l(mbstr, wcstr, conv, locale);
323 if(conv<size)
324 mbstr[conv++] = '\0';
325 else if(conv==size && (count==MSVCRT__TRUNCATE || mbstr[conv-1]=='\0'))
326 mbstr[conv-1] = '\0';
327 else {
328 MSVCRT_INVALID_PMT("mbstr[size] is too small");
329 if(size)
330 mbstr[0] = '\0';
331 *MSVCRT__errno() = MSVCRT_ERANGE;
332 return MSVCRT_ERANGE;
335 if(ret)
336 *ret = conv;
337 return 0;
340 /*********************************************************************
341 * wcstombs_s (MSVCRT.@)
343 MSVCRT_size_t CDECL MSVCRT_wcstombs_s(MSVCRT_size_t *ret, char *mbstr,
344 MSVCRT_size_t size, const MSVCRT_wchar_t *wcstr, MSVCRT_size_t count)
346 return MSVCRT__wcstombs_s_l(ret, mbstr, size, wcstr, count, NULL);
349 /*********************************************************************
350 * wcstod (MSVCRT.@)
352 double CDECL MSVCRT_wcstod(const MSVCRT_wchar_t* lpszStr, MSVCRT_wchar_t** end)
354 return MSVCRT__wcstod_l(lpszStr, end, NULL);
357 /*********************************************************************
358 * _wtof (MSVCRT.@)
360 double CDECL MSVCRT__wtof(const MSVCRT_wchar_t *str)
362 return MSVCRT__wcstod_l(str, NULL, NULL);
365 /*********************************************************************
366 * _wtof_l (MSVCRT.@)
368 double CDECL MSVCRT__wtof_l(const MSVCRT_wchar_t *str, MSVCRT__locale_t locale)
370 return MSVCRT__wcstod_l(str, NULL, locale);
373 typedef struct pf_flags_t
375 char Sign, LeftAlign, Alternate, PadZero;
376 int FieldLength, Precision;
377 char IntegerLength, IntegerDouble;
378 char WideString;
379 char Format;
380 } pf_flags;
382 static inline BOOL pf_is_auto_grow(pf_output *out)
384 return (out->unicode) ? !!out->grow.W : !!out->grow.A;
387 static inline int pf_check_auto_grow(pf_output *out, unsigned delta)
389 if (pf_is_auto_grow(out) && out->used + delta > out->len)
391 out->len = max(out->len * 2, out->used + delta);
392 if (out->unicode)
394 if (out->buf.W != out->grow.W)
395 out->buf.W = MSVCRT_realloc(out->buf.W, out->len * sizeof(WCHAR));
396 else
397 out->buf.W = MSVCRT_malloc(out->len * sizeof(WCHAR));
398 if (!out->buf.W) return -1;
400 else
402 if (out->buf.A != out->grow.A)
403 out->buf.A = MSVCRT_realloc(out->buf.A, out->len * sizeof(char));
404 else
405 out->buf.A = MSVCRT_malloc(out->len * sizeof(char));
406 if (!out->buf.A) return -1;
409 return 0;
413 * writes a string of characters to the output
414 * returns -1 if the string doesn't fit in the output buffer
415 * return the length of the string if all characters were written
417 static inline int pf_output_stringW( pf_output *out, LPCWSTR str, int len )
419 int space;
421 if( len < 0 )
422 len = strlenW( str );
423 if( out->unicode )
425 LPWSTR p;
427 if(pf_check_auto_grow(out, len) == -1) return -1;
428 space = out->len - out->used;
429 p = out->buf.W + out->used;
430 if( space >= len )
432 if (out->buf.W) memcpy( p, str, len*sizeof(WCHAR) );
433 out->used += len;
434 return len;
436 if( space > 0 && out->buf.W )
437 memcpy( p, str, space*sizeof(WCHAR) );
438 out->used += len;
440 else
442 int n = WideCharToMultiByte( CP_ACP, 0, str, len, NULL, 0, NULL, NULL );
443 LPSTR p;
445 if(pf_check_auto_grow(out, n) == -1) return -1;
446 space = out->len - out->used;
447 p = out->buf.A + out->used;
448 if( space >= n )
450 if (out->buf.A) WideCharToMultiByte( CP_ACP, 0, str, len, p, n, NULL, NULL );
451 out->used += n;
452 return len;
454 if( space > 0 && out->buf.A )
455 WideCharToMultiByte( CP_ACP, 0, str, len, p, space, NULL, NULL );
456 out->used += n;
458 return -1;
461 static inline int pf_output_stringA( pf_output *out, LPCSTR str, int len )
463 int space;
465 if( len < 0 )
466 len = strlen( str );
467 if( !out->unicode )
469 LPSTR p;
471 if (pf_check_auto_grow(out, len) == -1) return -1;
472 p = out->buf.A + out->used;
473 space = out->len - out->used;
474 if( space >= len )
476 if (out->buf.A) memcpy( p, str, len );
477 out->used += len;
478 return len;
480 if( space > 0 && out->buf.A )
481 memcpy( p, str, space );
482 out->used += len;
484 else
486 int n = MultiByteToWideChar( CP_ACP, 0, str, len, NULL, 0 );
487 LPWSTR p;
489 if (pf_check_auto_grow(out, n) == -1) return -1;
490 p = out->buf.W + out->used;
491 space = out->len - out->used;
492 if( space >= n )
494 if (out->buf.W) MultiByteToWideChar( CP_ACP, 0, str, len, p, n );
495 out->used += n;
496 return len;
498 if( space > 0 && out->buf.W )
499 MultiByteToWideChar( CP_ACP, 0, str, len, p, space );
500 out->used += n;
502 return -1;
505 /* pf_fill: takes care of signs, alignment, zero and field padding */
506 static inline int pf_fill( pf_output *out, int len, pf_flags *flags, char left )
508 int i, r = 0;
510 if( flags->Sign && !( flags->Format == 'd' || flags->Format == 'i' ) )
511 flags->Sign = 0;
513 if( left && flags->Sign )
515 flags->FieldLength--;
516 if( flags->PadZero )
517 r = pf_output_stringA( out, &flags->Sign, 1 );
520 if( ( !left && flags->LeftAlign ) ||
521 ( left && !flags->LeftAlign ))
523 for( i=0; (i<(flags->FieldLength-len)) && (r>=0); i++ )
525 if( left && flags->PadZero )
526 r = pf_output_stringA( out, "0", 1 );
527 else
528 r = pf_output_stringA( out, " ", 1 );
532 if( left && flags->Sign && !flags->PadZero )
533 r = pf_output_stringA( out, &flags->Sign, 1 );
535 return r;
538 static inline int pf_output_format_W( pf_output *out, LPCWSTR str,
539 int len, pf_flags *flags )
541 int r = 0;
543 if( len < 0 )
544 len = strlenW( str );
546 if (flags->Precision >= 0 && flags->Precision < len)
547 len = flags->Precision;
549 r = pf_fill( out, len, flags, 1 );
551 if( r>=0 )
552 r = pf_output_stringW( out, str, len );
554 if( r>=0 )
555 r = pf_fill( out, len, flags, 0 );
557 return r;
560 static inline int pf_output_format_A( pf_output *out, LPCSTR str,
561 int len, pf_flags *flags )
563 int r = 0;
565 if( len < 0 )
566 len = strlen( str );
568 if (flags->Precision >= 0 && flags->Precision < len)
569 len = flags->Precision;
571 r = pf_fill( out, len, flags, 1 );
573 if( r>=0 )
574 r = pf_output_stringA( out, str, len );
576 if( r>=0 )
577 r = pf_fill( out, len, flags, 0 );
579 return r;
582 static int pf_handle_string_format( pf_output *out, const void* str, int len,
583 pf_flags *flags, BOOL capital_letter)
585 if(str == NULL) /* catch NULL pointer */
586 return pf_output_format_A( out, "(null)", -1, flags);
588 /* prefixes take priority over %c,%s vs. %C,%S, so we handle them first */
589 if(flags->WideString || flags->IntegerLength == 'l')
590 return pf_output_format_W( out, str, len, flags);
591 if(flags->IntegerLength == 'h')
592 return pf_output_format_A( out, str, len, flags);
594 /* %s,%c -> chars in ansi functions & wchars in unicode
595 * %S,%C -> wchars in ansi functions & chars in unicode */
596 if( capital_letter == out->unicode) /* either both TRUE or both FALSE */
597 return pf_output_format_A( out, str, len, flags);
598 else
599 return pf_output_format_W( out, str, len, flags);
602 static inline BOOL pf_is_integer_format( char fmt )
604 static const char float_fmts[] = "diouxX";
605 if (!fmt)
606 return FALSE;
607 return strchr( float_fmts, fmt ) ? TRUE : FALSE;
610 static inline BOOL pf_is_double_format( char fmt )
612 static const char float_fmts[] = "aeEfgG";
613 if (!fmt)
614 return FALSE;
615 return strchr( float_fmts, fmt ) ? TRUE : FALSE;
618 static inline BOOL pf_is_valid_format( char fmt )
620 static const char float_fmts[] = "acCdeEfgGinouxX";
621 if (!fmt)
622 return FALSE;
623 return strchr( float_fmts, fmt ) ? TRUE : FALSE;
626 static void pf_rebuild_format_string( char *p, pf_flags *flags )
628 *p++ = '%';
629 if( flags->Sign )
630 *p++ = flags->Sign;
631 if( flags->LeftAlign )
632 *p++ = flags->LeftAlign;
633 if( flags->Alternate )
634 *p++ = flags->Alternate;
635 if( flags->PadZero )
636 *p++ = flags->PadZero;
637 if( flags->FieldLength )
639 sprintf(p, "%d", flags->FieldLength);
640 p += strlen(p);
642 if( flags->Precision >= 0 )
644 sprintf(p, ".%d", flags->Precision);
645 p += strlen(p);
647 *p++ = flags->Format;
648 *p++ = 0;
651 /* pf_integer_conv: prints x to buf, including alternate formats and
652 additional precision digits, but not field characters or the sign */
653 static void pf_integer_conv( char *buf, int buf_len, pf_flags *flags,
654 LONGLONG x )
656 unsigned int base;
657 const char *digits;
659 int i, j, k;
660 char number[40], *tmp = number;
662 if( buf_len > sizeof number )
663 tmp = HeapAlloc( GetProcessHeap(), 0, buf_len );
665 base = 10;
666 if( flags->Format == 'o' )
667 base = 8;
668 else if( flags->Format == 'x' || flags->Format == 'X' )
669 base = 16;
671 if( flags->Format == 'X' )
672 digits = "0123456789ABCDEFX";
673 else
674 digits = "0123456789abcdefx";
676 if( x < 0 && ( flags->Format == 'd' || flags->Format == 'i' ) )
678 x = -x;
679 flags->Sign = '-';
682 /* Do conversion (backwards) */
683 i = 0;
684 if( x == 0 && flags->Precision )
685 tmp[i++] = '0';
686 else
687 while( x != 0 )
689 j = (ULONGLONG) x % base;
690 x = (ULONGLONG) x / base;
691 tmp[i++] = digits[j];
693 k = flags->Precision - i;
694 while( k-- > 0 )
695 tmp[i++] = '0';
696 if( flags->Alternate )
698 if( base == 16 )
700 tmp[i++] = digits[16];
701 tmp[i++] = '0';
703 else if( base == 8 && tmp[i-1] != '0' )
704 tmp[i++] = '0';
707 /* Reverse for buf */
708 j = 0;
709 while( i-- > 0 )
710 buf[j++] = tmp[i];
711 buf[j] = '\0';
713 /* Adjust precision so pf_fill won't truncate the number later */
714 flags->Precision = strlen( buf );
716 if( tmp != number )
717 HeapFree( GetProcessHeap(), 0, tmp );
719 return;
722 /* pf_fixup_exponent: convert a string containing a 2 digit exponent
723 to 3 digits, accounting for padding, in place. Needed to match
724 the native printf's which always use 3 digits. */
725 static void pf_fixup_exponent( char *buf )
727 char* tmp = buf;
729 while (tmp[0] && toupper(tmp[0]) != 'E')
730 tmp++;
732 if (tmp[0] && (tmp[1] == '+' || tmp[1] == '-') &&
733 isdigit(tmp[2]) && isdigit(tmp[3]))
735 char final;
737 if (isdigit(tmp[4]))
738 return; /* Exponent already 3 digits */
740 /* We have a 2 digit exponent. Prepend '0' to make it 3 */
741 tmp += 2;
742 final = tmp[2];
743 tmp[2] = tmp[1];
744 tmp[1] = tmp[0];
745 tmp[0] = '0';
746 if (final == '\0')
748 /* We didn't expand into trailing space, so this string isn't left
749 * justified. Terminate the string and strip a ' ' at the start of
750 * the string if there is one (as there may be if the string is
751 * right justified).
753 tmp[3] = '\0';
754 if (buf[0] == ' ')
755 memmove(buf, buf + 1, (tmp - buf) + 3);
757 /* Otherwise, we expanded into trailing space -> nothing to do */
761 /*********************************************************************
762 * pf_vsnprintf (INTERNAL)
764 * implements both A and W vsnprintf functions
766 int pf_vsnprintf( pf_output *out, const WCHAR *format,
767 MSVCRT__locale_t locale, BOOL valid, __ms_va_list valist )
769 int r;
770 LPCWSTR q, p = format;
771 pf_flags flags;
773 if(!locale)
774 locale = get_locale();
776 TRACE("format is %s\n",debugstr_w(format));
777 while (*p)
779 q = strchrW( p, '%' );
781 /* there's no % characters left, output the rest of the string */
782 if( !q )
784 r = pf_output_stringW(out, p, -1);
785 if( r<0 )
786 return r;
787 p += r;
788 continue;
791 /* there's characters before the %, output them */
792 if( q != p )
794 r = pf_output_stringW(out, p, q - p);
795 if( r<0 )
796 return r;
797 p = q;
800 /* we must be at a % now, skip over it */
801 assert( *p == '%' );
802 p++;
804 /* output a single % character */
805 if( *p == '%' )
807 r = pf_output_stringW(out, p++, 1);
808 if( r<0 )
809 return r;
810 continue;
813 /* parse the flags */
814 memset( &flags, 0, sizeof flags );
815 while (*p)
817 if( *p == '+' || *p == ' ' )
819 if ( flags.Sign != '+' )
820 flags.Sign = *p;
822 else if( *p == '-' )
823 flags.LeftAlign = *p;
824 else if( *p == '0' )
825 flags.PadZero = *p;
826 else if( *p == '#' )
827 flags.Alternate = *p;
828 else
829 break;
830 p++;
833 /* deal with the field width specifier */
834 flags.FieldLength = 0;
835 if( *p == '*' )
837 flags.FieldLength = va_arg( valist, int );
838 if (flags.FieldLength < 0)
840 flags.LeftAlign = '-';
841 flags.FieldLength = -flags.FieldLength;
843 p++;
845 else while( isdigit(*p) )
847 flags.FieldLength *= 10;
848 flags.FieldLength += *p++ - '0';
851 /* deal with precision */
852 flags.Precision = -1;
853 if( *p == '.' )
855 flags.Precision = 0;
856 p++;
857 if( *p == '*' )
859 flags.Precision = va_arg( valist, int );
860 p++;
862 else while( isdigit(*p) )
864 flags.Precision *= 10;
865 flags.Precision += *p++ - '0';
869 /* deal with integer width modifier */
870 while( *p )
872 if( *p == 'l' && *(p+1) == 'l' )
874 flags.IntegerDouble++;
875 p += 2;
877 else if( *p == 'h' || *p == 'l' || *p == 'L' )
879 flags.IntegerLength = *p;
880 p++;
882 else if( *p == 'I' )
884 if( *(p+1) == '6' && *(p+2) == '4' )
886 flags.IntegerDouble++;
887 p += 3;
889 else if( *(p+1) == '3' && *(p+2) == '2' )
890 p += 3;
891 else if( isdigit(*(p+1)) || *(p+1) == 0 )
892 break;
893 else
894 p++;
896 else if( *p == 'w' )
897 flags.WideString = *p++;
898 else if( *p == 'F' )
899 p++; /* ignore */
900 else
901 break;
904 flags.Format = *p;
905 r = 0;
907 if (flags.Format == '$')
909 FIXME("Positional parameters are not supported (%s)\n", wine_dbgstr_w(format));
910 return -1;
912 /* output a string */
913 if( flags.Format == 's' || flags.Format == 'S' )
914 r = pf_handle_string_format( out, va_arg(valist, const void*), -1,
915 &flags, (flags.Format == 'S') );
917 /* output a single character */
918 else if( flags.Format == 'c' || flags.Format == 'C' )
920 INT ch = va_arg( valist, int );
922 r = pf_handle_string_format( out, &ch, 1, &flags, (flags.Format == 'C') );
925 /* output a pointer */
926 else if( flags.Format == 'p' )
928 char pointer[32];
929 void *ptr = va_arg( valist, void * );
931 flags.PadZero = 0;
932 if( flags.Alternate )
933 sprintf(pointer, "0X%0*lX", 2 * (int)sizeof(ptr), (ULONG_PTR)ptr);
934 else
935 sprintf(pointer, "%0*lX", 2 * (int)sizeof(ptr), (ULONG_PTR)ptr);
936 r = pf_output_format_A( out, pointer, -1, &flags );
939 /* deal with %n */
940 else if( flags.Format == 'n' )
942 int *x = va_arg(valist, int *);
943 *x = out->used;
946 /* deal with 64-bit integers */
947 else if( pf_is_integer_format( flags.Format ) && flags.IntegerDouble )
949 char number[40], *x = number;
951 /* Estimate largest possible required buffer size:
952 * Chooses the larger of the field or precision
953 * Includes extra bytes: 1 byte for null, 1 byte for sign,
954 4 bytes for exponent, 2 bytes for alternate formats, 1 byte
955 for a decimal, and 1 byte for an additional float digit. */
956 int x_len = ((flags.FieldLength > flags.Precision) ?
957 flags.FieldLength : flags.Precision) + 10;
959 if( x_len >= sizeof number)
960 x = HeapAlloc( GetProcessHeap(), 0, x_len );
962 pf_integer_conv( x, x_len, &flags, va_arg(valist, LONGLONG) );
964 r = pf_output_format_A( out, x, -1, &flags );
965 if( x != number )
966 HeapFree( GetProcessHeap(), 0, x );
969 /* deal with integers and floats using libc's printf */
970 else if( pf_is_valid_format( flags.Format ) )
972 char fmt[20], number[40], *x = number, *decimal_point;
974 /* Estimate largest possible required buffer size:
975 * Chooses the larger of the field or precision
976 * Includes extra bytes: 1 byte for null, 1 byte for sign,
977 4 bytes for exponent, 2 bytes for alternate formats, 1 byte
978 for a decimal, and 1 byte for an additional float digit. */
979 int x_len = ((flags.FieldLength > flags.Precision) ?
980 flags.FieldLength : flags.Precision) + 10;
982 if( x_len >= sizeof number)
983 x = HeapAlloc( GetProcessHeap(), 0, x_len );
985 pf_rebuild_format_string( fmt, &flags );
987 if( pf_is_double_format( flags.Format ) )
989 sprintf( x, fmt, va_arg(valist, double) );
990 if (toupper(flags.Format) == 'E' || toupper(flags.Format) == 'G')
991 pf_fixup_exponent( x );
993 else
994 sprintf( x, fmt, va_arg(valist, int) );
996 decimal_point = strchr(x, '.');
997 if(decimal_point)
998 *decimal_point = *locale->locinfo->lconv->decimal_point;
1000 r = pf_output_stringA( out, x, -1 );
1001 if( x != number )
1002 HeapFree( GetProcessHeap(), 0, x );
1003 if(r < 0)
1004 return r;
1006 else
1008 if( valid )
1010 MSVCRT__invalid_parameter( NULL, NULL, NULL, 0, 0 );
1011 *MSVCRT__errno() = MSVCRT_EINVAL;
1012 return -1;
1015 continue;
1018 if( r<0 )
1019 return r;
1020 p++;
1023 /* check we reached the end, and null terminate the string */
1024 assert( *p == 0 );
1025 pf_output_stringW( out, p, 1 );
1027 return out->used - 1;
1030 /*********************************************************************
1031 * vsnprintf_internal (INTERNAL)
1033 static inline int vsnprintf_internal( char *str, MSVCRT_size_t len, const char *format,
1034 MSVCRT__locale_t locale, BOOL valid, __ms_va_list valist )
1036 DWORD sz;
1037 LPWSTR formatW = NULL;
1038 pf_output out;
1039 int r;
1041 out.unicode = FALSE;
1042 out.buf.A = str;
1043 out.grow.A = NULL;
1044 out.used = 0;
1045 out.len = len;
1047 sz = MultiByteToWideChar( CP_ACP, 0, format, -1, NULL, 0 );
1048 formatW = HeapAlloc( GetProcessHeap(), 0, sz*sizeof(WCHAR) );
1049 MultiByteToWideChar( CP_ACP, 0, format, -1, formatW, sz );
1051 r = pf_vsnprintf( &out, formatW, locale, valid, valist );
1053 HeapFree( GetProcessHeap(), 0, formatW );
1055 return r;
1058 /*********************************************************************
1059 * _vsnprintf (MSVCRT.@)
1061 int CDECL MSVCRT_vsnprintf( char *str, MSVCRT_size_t len,
1062 const char *format, __ms_va_list valist )
1064 return vsnprintf_internal(str, len, format, NULL, FALSE, valist);
1067 /*********************************************************************
1068 * _vsnprintf_l (MSVCRT.@)
1070 int CDECL MSVCRT_vsnprintf_l( char *str, MSVCRT_size_t len, const char *format,
1071 MSVCRT__locale_t locale, __ms_va_list valist )
1073 return vsnprintf_internal(str, len, format, locale, FALSE, valist);
1076 /*********************************************************************
1077 * _vsnprintf_s_l (MSVCRT.@)
1079 int CDECL MSVCRT_vsnprintf_s_l( char *str, MSVCRT_size_t sizeOfBuffer,
1080 MSVCRT_size_t count, const char *format,
1081 MSVCRT__locale_t locale, __ms_va_list valist )
1083 int len, ret;
1085 if(sizeOfBuffer<count+1 || count==-1)
1086 len = sizeOfBuffer;
1087 else
1088 len = count+1;
1090 ret = vsnprintf_internal(str, len, format, locale, TRUE, valist);
1092 if(ret<0 || ret==len) {
1093 if(count!=MSVCRT__TRUNCATE && count>sizeOfBuffer) {
1094 MSVCRT_INVALID_PMT("str[sizeOfBuffer] is too small");
1095 *MSVCRT__errno() = MSVCRT_ERANGE;
1096 memset(str, 0, sizeOfBuffer);
1097 } else
1098 str[len-1] = '\0';
1100 return -1;
1103 return ret;
1106 /*********************************************************************
1107 * _vsnprintf_s (MSVCRT.@)
1109 int CDECL MSVCRT_vsnprintf_s( char *str, MSVCRT_size_t sizeOfBuffer,
1110 MSVCRT_size_t count, const char *format, __ms_va_list valist )
1112 return MSVCRT_vsnprintf_s_l(str,sizeOfBuffer, count, format, NULL, valist);
1115 /*********************************************************************
1116 * vsprintf (MSVCRT.@)
1118 int CDECL MSVCRT_vsprintf( char *str, const char *format, __ms_va_list valist)
1120 return MSVCRT_vsnprintf(str, INT_MAX, format, valist);
1123 /*********************************************************************
1124 * vsprintf_s (MSVCRT.@)
1126 int CDECL MSVCRT_vsprintf_s( char *str, MSVCRT_size_t num, const char *format, __ms_va_list valist)
1128 return MSVCRT_vsnprintf(str, num, format, valist);
1131 /*********************************************************************
1132 * _vscprintf (MSVCRT.@)
1134 int CDECL _vscprintf( const char *format, __ms_va_list valist )
1136 return MSVCRT_vsnprintf( NULL, INT_MAX, format, valist );
1139 /*********************************************************************
1140 * _snprintf (MSVCRT.@)
1142 int CDECL MSVCRT__snprintf(char *str, unsigned int len, const char *format, ...)
1144 int retval;
1145 __ms_va_list valist;
1146 __ms_va_start(valist, format);
1147 retval = MSVCRT_vsnprintf(str, len, format, valist);
1148 __ms_va_end(valist);
1149 return retval;
1152 /*********************************************************************
1153 * _snprintf_s (MSVCRT.@)
1155 int CDECL MSVCRT__snprintf_s(char *str, unsigned int len, unsigned int count,
1156 const char *format, ...)
1158 int retval;
1159 __ms_va_list valist;
1160 __ms_va_start(valist, format);
1161 retval = MSVCRT_vsnprintf_s_l(str, len, count, format, NULL, valist);
1162 __ms_va_end(valist);
1163 return retval;
1166 /*********************************************************************
1167 * _scprintf (MSVCRT.@)
1169 int CDECL MSVCRT__scprintf(const char *format, ...)
1171 int retval;
1172 __ms_va_list valist;
1173 __ms_va_start(valist, format);
1174 retval = _vscprintf(format, valist);
1175 __ms_va_end(valist);
1176 return retval;
1179 /*********************************************************************
1180 * vsnwprintf_internal (INTERNAL)
1182 static inline int vsnwprintf_internal(MSVCRT_wchar_t *str, MSVCRT_size_t len,
1183 const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, BOOL valid,
1184 __ms_va_list valist)
1186 pf_output out;
1188 out.unicode = TRUE;
1189 out.buf.W = str;
1190 out.grow.W = NULL;
1191 out.used = 0;
1192 out.len = len;
1194 return pf_vsnprintf( &out, format, locale, valid, valist );
1197 /*********************************************************************
1198 * _vsnwprintf (MSVCRT.@)
1200 int CDECL MSVCRT_vsnwprintf(MSVCRT_wchar_t *str, MSVCRT_size_t len,
1201 const MSVCRT_wchar_t *format, __ms_va_list valist)
1203 return vsnwprintf_internal(str, len, format, NULL, FALSE, valist);
1206 /*********************************************************************
1207 * _vsnwprintf_l (MSVCRT.@)
1209 int CDECL MSVCRT_vsnwprintf_l(MSVCRT_wchar_t *str, MSVCRT_size_t len,
1210 const MSVCRT_wchar_t *format, MSVCRT__locale_t locale,
1211 __ms_va_list valist)
1213 return vsnwprintf_internal(str, len, format, locale, FALSE, valist);
1216 /*********************************************************************
1217 * _vsnwprintf_s_l (MSVCRT.@)
1219 int CDECL MSVCRT_vsnwprintf_s_l( MSVCRT_wchar_t *str, MSVCRT_size_t sizeOfBuffer,
1220 MSVCRT_size_t count, const MSVCRT_wchar_t *format,
1221 MSVCRT__locale_t locale, __ms_va_list valist)
1223 int len, ret;
1225 len = sizeOfBuffer;
1226 if(count!=-1 && len>count+1)
1227 len = count+1;
1229 ret = vsnwprintf_internal(str, len, format, locale, TRUE, valist);
1231 if(ret<0 || ret==len) {
1232 if(count!=MSVCRT__TRUNCATE && count>sizeOfBuffer) {
1233 MSVCRT_INVALID_PMT("str[sizeOfBuffer] is too small");
1234 *MSVCRT__errno() = MSVCRT_ERANGE;
1235 memset(str, 0, sizeOfBuffer*sizeof(MSVCRT_wchar_t));
1236 } else
1237 str[len-1] = '\0';
1239 return -1;
1242 return ret;
1245 /*********************************************************************
1246 * _vsnwprintf_s (MSVCRT.@)
1248 int CDECL MSVCRT_vsnwprintf_s(MSVCRT_wchar_t *str, MSVCRT_size_t sizeOfBuffer,
1249 MSVCRT_size_t count, const MSVCRT_wchar_t *format, __ms_va_list valist)
1251 return MSVCRT_vsnwprintf_s_l(str, sizeOfBuffer, count,
1252 format, NULL, valist);
1255 /*********************************************************************
1256 * _snwprintf (MSVCRT.@)
1258 int CDECL MSVCRT__snwprintf( MSVCRT_wchar_t *str, unsigned int len, const MSVCRT_wchar_t *format, ...)
1260 int retval;
1261 __ms_va_list valist;
1262 __ms_va_start(valist, format);
1263 retval = MSVCRT_vsnwprintf(str, len, format, valist);
1264 __ms_va_end(valist);
1265 return retval;
1268 /*********************************************************************
1269 * _snwprintf_s (MSVCRT.@)
1271 int CDECL MSVCRT__snwprintf_s( MSVCRT_wchar_t *str, unsigned int len, unsigned int count,
1272 const MSVCRT_wchar_t *format, ...)
1274 int retval;
1275 __ms_va_list valist;
1276 __ms_va_start(valist, format);
1277 retval = MSVCRT_vsnwprintf_s_l(str, len, count, format, NULL, valist);
1278 __ms_va_end(valist);
1279 return retval;
1282 /*********************************************************************
1283 * sprintf (MSVCRT.@)
1285 int CDECL MSVCRT_sprintf( char *str, const char *format, ... )
1287 __ms_va_list ap;
1288 int r;
1290 __ms_va_start( ap, format );
1291 r = MSVCRT_vsnprintf( str, INT_MAX, format, ap );
1292 __ms_va_end( ap );
1293 return r;
1296 /*********************************************************************
1297 * sprintf_s (MSVCRT.@)
1299 int CDECL MSVCRT_sprintf_s( char *str, MSVCRT_size_t num, const char *format, ... )
1301 __ms_va_list ap;
1302 int r;
1304 __ms_va_start( ap, format );
1305 r = MSVCRT_vsnprintf( str, num, format, ap );
1306 __ms_va_end( ap );
1307 return r;
1310 /*********************************************************************
1311 * _scwprintf (MSVCRT.@)
1313 int CDECL MSVCRT__scwprintf( const MSVCRT_wchar_t *format, ... )
1315 __ms_va_list ap;
1316 int r;
1318 __ms_va_start( ap, format );
1319 r = MSVCRT_vsnwprintf( NULL, INT_MAX, format, ap );
1320 __ms_va_end( ap );
1321 return r;
1324 /*********************************************************************
1325 * swprintf (MSVCRT.@)
1327 int CDECL MSVCRT_swprintf( MSVCRT_wchar_t *str, const MSVCRT_wchar_t *format, ... )
1329 __ms_va_list ap;
1330 int r;
1332 __ms_va_start( ap, format );
1333 r = MSVCRT_vsnwprintf( str, INT_MAX, format, ap );
1334 __ms_va_end( ap );
1335 return r;
1338 /*********************************************************************
1339 * swprintf_s (MSVCRT.@)
1341 int CDECL MSVCRT_swprintf_s(MSVCRT_wchar_t *str, MSVCRT_size_t numberOfElements,
1342 const MSVCRT_wchar_t *format, ... )
1344 __ms_va_list ap;
1345 int r;
1347 __ms_va_start(ap, format);
1348 r = MSVCRT_vsnwprintf_s(str, numberOfElements, INT_MAX, format, ap);
1349 __ms_va_end(ap);
1351 return r;
1354 /*********************************************************************
1355 * vswprintf (MSVCRT.@)
1357 int CDECL MSVCRT_vswprintf( MSVCRT_wchar_t* str, const MSVCRT_wchar_t* format, __ms_va_list args )
1359 return MSVCRT_vsnwprintf( str, INT_MAX, format, args );
1362 /*********************************************************************
1363 * _vscwprintf (MSVCRT.@)
1365 int CDECL _vscwprintf( const MSVCRT_wchar_t *format, __ms_va_list args )
1367 return MSVCRT_vsnwprintf( NULL, INT_MAX, format, args );
1370 /*********************************************************************
1371 * vswprintf_s (MSVCRT.@)
1373 int CDECL MSVCRT_vswprintf_s(MSVCRT_wchar_t* str, MSVCRT_size_t numberOfElements,
1374 const MSVCRT_wchar_t* format, __ms_va_list args)
1376 return MSVCRT_vsnwprintf_s(str, numberOfElements, INT_MAX, format, args );
1379 /*********************************************************************
1380 * _vswprintf_s_l (MSVCRT.@)
1382 int CDECL MSVCRT_vswprintf_s_l(MSVCRT_wchar_t* str, MSVCRT_size_t numberOfElements,
1383 const MSVCRT_wchar_t* format, MSVCRT__locale_t locale, __ms_va_list args)
1385 return MSVCRT_vsnwprintf_s_l(str, numberOfElements, INT_MAX,
1386 format, locale, args );
1389 /*********************************************************************
1390 * wcscoll (MSVCRT.@)
1392 int CDECL MSVCRT_wcscoll( const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2 )
1394 /* FIXME: handle collates */
1395 return strcmpW( str1, str2 );
1398 /*********************************************************************
1399 * wcspbrk (MSVCRT.@)
1401 MSVCRT_wchar_t* CDECL MSVCRT_wcspbrk( const MSVCRT_wchar_t* str, const MSVCRT_wchar_t* accept )
1403 const MSVCRT_wchar_t* p;
1404 while (*str)
1406 for (p = accept; *p; p++) if (*p == *str) return (MSVCRT_wchar_t*)str;
1407 str++;
1409 return NULL;
1412 /*********************************************************************
1413 * wcstok_s (MSVCRT.@)
1415 MSVCRT_wchar_t * CDECL wcstok_s( MSVCRT_wchar_t *str, const MSVCRT_wchar_t *delim,
1416 MSVCRT_wchar_t **next_token )
1418 MSVCRT_wchar_t *ret;
1420 if (!MSVCRT_CHECK_PMT(delim != NULL) || !MSVCRT_CHECK_PMT(next_token != NULL) ||
1421 !MSVCRT_CHECK_PMT(str != NULL || *next_token != NULL))
1423 *MSVCRT__errno() = MSVCRT_EINVAL;
1424 return NULL;
1426 if (!str) str = *next_token;
1428 while (*str && strchrW( delim, *str )) str++;
1429 if (!*str) return NULL;
1430 ret = str++;
1431 while (*str && !strchrW( delim, *str )) str++;
1432 if (*str) *str++ = 0;
1433 *next_token = str;
1434 return ret;
1437 /*********************************************************************
1438 * wcstok (MSVCRT.@)
1440 MSVCRT_wchar_t * CDECL MSVCRT_wcstok( MSVCRT_wchar_t *str, const MSVCRT_wchar_t *delim )
1442 return wcstok_s(str, delim, &msvcrt_get_thread_data()->wcstok_next);
1445 /*********************************************************************
1446 * wctomb (MSVCRT.@)
1448 INT CDECL MSVCRT_wctomb( char *dst, MSVCRT_wchar_t ch )
1450 return WideCharToMultiByte( CP_ACP, 0, &ch, 1, dst, 6, NULL, NULL );
1453 /*********************************************************************
1454 * iswalnum (MSVCRT.@)
1456 INT CDECL MSVCRT_iswalnum( MSVCRT_wchar_t wc )
1458 return isalnumW( wc );
1461 /*********************************************************************
1462 * iswalpha (MSVCRT.@)
1464 INT CDECL MSVCRT_iswalpha( MSVCRT_wchar_t wc )
1466 return isalphaW( wc );
1469 /*********************************************************************
1470 * iswalpha_l (MSVCRT.@)
1472 INT CDECL MSVCRT__iswalpha_l( MSVCRT_wchar_t wc, MSVCRT__locale_t locale )
1474 return isalphaW( wc );
1477 /*********************************************************************
1478 * iswcntrl (MSVCRT.@)
1480 INT CDECL MSVCRT_iswcntrl( MSVCRT_wchar_t wc )
1482 return iscntrlW( wc );
1485 /*********************************************************************
1486 * iswdigit (MSVCRT.@)
1488 INT CDECL MSVCRT_iswdigit( MSVCRT_wchar_t wc )
1490 return isdigitW( wc );
1493 /*********************************************************************
1494 * iswgraph (MSVCRT.@)
1496 INT CDECL MSVCRT_iswgraph( MSVCRT_wchar_t wc )
1498 return isgraphW( wc );
1501 /*********************************************************************
1502 * iswlower (MSVCRT.@)
1504 INT CDECL MSVCRT_iswlower( MSVCRT_wchar_t wc )
1506 return islowerW( wc );
1509 /*********************************************************************
1510 * iswprint (MSVCRT.@)
1512 INT CDECL MSVCRT_iswprint( MSVCRT_wchar_t wc )
1514 return isprintW( wc );
1517 /*********************************************************************
1518 * iswpunct (MSVCRT.@)
1520 INT CDECL MSVCRT_iswpunct( MSVCRT_wchar_t wc )
1522 return ispunctW( wc );
1525 /*********************************************************************
1526 * iswspace (MSVCRT.@)
1528 INT CDECL MSVCRT_iswspace( MSVCRT_wchar_t wc )
1530 return isspaceW( wc );
1533 /*********************************************************************
1534 * iswupper (MSVCRT.@)
1536 INT CDECL MSVCRT_iswupper( MSVCRT_wchar_t wc )
1538 return isupperW( wc );
1541 /*********************************************************************
1542 * iswxdigit (MSVCRT.@)
1544 INT CDECL MSVCRT_iswxdigit( MSVCRT_wchar_t wc )
1546 return isxdigitW( wc );
1549 /*********************************************************************
1550 * wcscpy_s (MSVCRT.@)
1552 INT CDECL MSVCRT_wcscpy_s( MSVCRT_wchar_t* wcDest, MSVCRT_size_t numElement, const MSVCRT_wchar_t *wcSrc)
1554 MSVCRT_size_t size = 0;
1556 if(!wcDest || !numElement)
1557 return MSVCRT_EINVAL;
1559 wcDest[0] = 0;
1561 if(!wcSrc)
1563 return MSVCRT_EINVAL;
1566 size = strlenW(wcSrc) + 1;
1568 if(size > numElement)
1570 return MSVCRT_ERANGE;
1573 memcpy( wcDest, wcSrc, size*sizeof(WCHAR) );
1575 return 0;
1578 /******************************************************************
1579 * wcsncpy_s (MSVCRT.@)
1581 INT CDECL MSVCRT_wcsncpy_s( MSVCRT_wchar_t* wcDest, MSVCRT_size_t numElement, const MSVCRT_wchar_t *wcSrc,
1582 MSVCRT_size_t count )
1584 MSVCRT_size_t size = 0;
1586 if (!wcDest || !numElement)
1587 return MSVCRT_EINVAL;
1589 wcDest[0] = 0;
1591 if (!wcSrc)
1593 return MSVCRT_EINVAL;
1596 size = min(strlenW(wcSrc), count);
1598 if (size >= numElement)
1600 return MSVCRT_ERANGE;
1603 memcpy( wcDest, wcSrc, size*sizeof(WCHAR) );
1604 wcDest[size] = '\0';
1606 return 0;
1609 /******************************************************************
1610 * wcscat_s (MSVCRT.@)
1613 INT CDECL MSVCRT_wcscat_s(MSVCRT_wchar_t* dst, MSVCRT_size_t elem, const MSVCRT_wchar_t* src)
1615 MSVCRT_wchar_t* ptr = dst;
1617 if (!dst || elem == 0) return MSVCRT_EINVAL;
1618 if (!src)
1620 dst[0] = '\0';
1621 return MSVCRT_EINVAL;
1624 /* seek to end of dst string (or elem if no end of string is found */
1625 while (ptr < dst + elem && *ptr != '\0') ptr++;
1626 while (ptr < dst + elem)
1628 if ((*ptr++ = *src++) == '\0') return 0;
1630 /* not enough space */
1631 dst[0] = '\0';
1632 return MSVCRT_ERANGE;
1635 /*********************************************************************
1636 * wcsncat_s (MSVCRT.@)
1639 INT CDECL MSVCRT_wcsncat_s(MSVCRT_wchar_t *dst, MSVCRT_size_t elem,
1640 const MSVCRT_wchar_t *src, MSVCRT_size_t count)
1642 MSVCRT_size_t srclen;
1643 MSVCRT_wchar_t dststart;
1644 INT ret = 0;
1646 if (!MSVCRT_CHECK_PMT(dst != NULL) || !MSVCRT_CHECK_PMT(elem > 0))
1648 *MSVCRT__errno() = MSVCRT_EINVAL;
1649 return MSVCRT_EINVAL;
1651 if (!MSVCRT_CHECK_PMT(src != NULL || count == 0))
1652 return MSVCRT_EINVAL;
1653 if (count == 0)
1654 return 0;
1656 for (dststart = 0; dststart < elem; dststart++)
1658 if (dst[dststart] == '\0')
1659 break;
1661 if (dststart == elem)
1663 MSVCRT_INVALID_PMT("dst[elem] is not NULL terminated\n");
1664 return MSVCRT_EINVAL;
1667 if (count == MSVCRT__TRUNCATE)
1669 srclen = strlenW(src);
1670 if (srclen >= (elem - dststart))
1672 srclen = elem - dststart - 1;
1673 ret = MSVCRT_STRUNCATE;
1676 else
1677 srclen = min(strlenW(src), count);
1678 if (srclen < (elem - dststart))
1680 memcpy(&dst[dststart], src, srclen*sizeof(MSVCRT_wchar_t));
1681 dst[srclen] = '\0';
1682 return ret;
1684 MSVCRT_INVALID_PMT("dst[elem] is too small");
1685 dst[0] = '\0';
1686 return MSVCRT_ERANGE;
1689 /*********************************************************************
1690 * _wcstoi64_l (MSVCRT.@)
1692 * FIXME: locale parameter is ignored
1694 __int64 CDECL MSVCRT__wcstoi64_l(const MSVCRT_wchar_t *nptr,
1695 MSVCRT_wchar_t **endptr, int base, MSVCRT__locale_t locale)
1697 BOOL negative = FALSE;
1698 __int64 ret = 0;
1700 TRACE("(%s %p %d %p)\n", debugstr_w(nptr), endptr, base, locale);
1702 if (!MSVCRT_CHECK_PMT(nptr != NULL) || !MSVCRT_CHECK_PMT(base == 0 || base >= 2) ||
1703 !MSVCRT_CHECK_PMT(base <= 36)) {
1704 *MSVCRT__errno() = MSVCRT_EINVAL;
1705 return 0;
1708 while(isspaceW(*nptr)) nptr++;
1710 if(*nptr == '-') {
1711 negative = TRUE;
1712 nptr++;
1713 } else if(*nptr == '+')
1714 nptr++;
1716 if((base==0 || base==16) && *nptr=='0' && tolowerW(*(nptr+1))=='x') {
1717 base = 16;
1718 nptr += 2;
1721 if(base == 0) {
1722 if(*nptr=='0')
1723 base = 8;
1724 else
1725 base = 10;
1728 while(*nptr) {
1729 char cur = tolowerW(*nptr);
1730 int v;
1732 if(isdigitW(cur)) {
1733 if(cur >= '0'+base)
1734 break;
1735 v = cur-'0';
1736 } else {
1737 if(cur<'a' || cur>='a'+base-10)
1738 break;
1739 v = cur-'a'+10;
1742 if(negative)
1743 v = -v;
1745 nptr++;
1747 if(!negative && (ret>MSVCRT_I64_MAX/base || ret*base>MSVCRT_I64_MAX-v)) {
1748 ret = MSVCRT_I64_MAX;
1749 *MSVCRT__errno() = MSVCRT_ERANGE;
1750 } else if(negative && (ret<MSVCRT_I64_MIN/base || ret*base<MSVCRT_I64_MIN-v)) {
1751 ret = MSVCRT_I64_MIN;
1752 *MSVCRT__errno() = MSVCRT_ERANGE;
1753 } else
1754 ret = ret*base + v;
1757 if(endptr)
1758 *endptr = (MSVCRT_wchar_t*)nptr;
1760 return ret;
1763 /*********************************************************************
1764 * _wcstoi64 (MSVCRT.@)
1766 __int64 CDECL MSVCRT__wcstoi64(const MSVCRT_wchar_t *nptr,
1767 MSVCRT_wchar_t **endptr, int base)
1769 return MSVCRT__wcstoi64_l(nptr, endptr, base, NULL);
1772 /*********************************************************************
1773 * _wcstoui64_l (MSVCRT.@)
1775 * FIXME: locale parameter is ignored
1777 unsigned __int64 CDECL MSVCRT__wcstoui64_l(const MSVCRT_wchar_t *nptr,
1778 MSVCRT_wchar_t **endptr, int base, MSVCRT__locale_t locale)
1780 BOOL negative = FALSE;
1781 unsigned __int64 ret = 0;
1783 TRACE("(%s %p %d %p)\n", debugstr_w(nptr), endptr, base, locale);
1785 if (!MSVCRT_CHECK_PMT(nptr != NULL) || !MSVCRT_CHECK_PMT(base == 0 || base >= 2) ||
1786 !MSVCRT_CHECK_PMT(base <= 36)) {
1787 *MSVCRT__errno() = MSVCRT_EINVAL;
1788 return 0;
1791 while(isspaceW(*nptr)) nptr++;
1793 if(*nptr == '-') {
1794 negative = TRUE;
1795 nptr++;
1796 } else if(*nptr == '+')
1797 nptr++;
1799 if((base==0 || base==16) && *nptr=='0' && tolowerW(*(nptr+1))=='x') {
1800 base = 16;
1801 nptr += 2;
1804 if(base == 0) {
1805 if(*nptr=='0')
1806 base = 8;
1807 else
1808 base = 10;
1811 while(*nptr) {
1812 char cur = tolowerW(*nptr);
1813 int v;
1815 if(isdigit(cur)) {
1816 if(cur >= '0'+base)
1817 break;
1818 v = *nptr-'0';
1819 } else {
1820 if(cur<'a' || cur>='a'+base-10)
1821 break;
1822 v = cur-'a'+10;
1825 nptr++;
1827 if(ret>MSVCRT_UI64_MAX/base || ret*base>MSVCRT_UI64_MAX-v) {
1828 ret = MSVCRT_UI64_MAX;
1829 *MSVCRT__errno() = MSVCRT_ERANGE;
1830 } else
1831 ret = ret*base + v;
1834 if(endptr)
1835 *endptr = (MSVCRT_wchar_t*)nptr;
1837 return negative ? -ret : ret;
1840 /*********************************************************************
1841 * _wcstoui64 (MSVCRT.@)
1843 unsigned __int64 CDECL MSVCRT__wcstoui64(const MSVCRT_wchar_t *nptr,
1844 MSVCRT_wchar_t **endptr, int base)
1846 return MSVCRT__wcstoui64_l(nptr, endptr, base, NULL);
1849 /******************************************************************
1850 * wcsnlen (MSVCRT.@)
1852 MSVCRT_size_t CDECL MSVCRT_wcsnlen(const MSVCRT_wchar_t *s, MSVCRT_size_t maxlen)
1854 MSVCRT_size_t i;
1856 for (i = 0; i < maxlen; i++)
1857 if (!s[i]) break;
1858 return i;