msxml3: Add a trailing '\n' to an ERR() call.
[wine/multimedia.git] / dlls / msvcrt / string.c
blob11ad5a0c2d7e8f210c9c82949e34b1c5ccad21fe
1 /*
2 * MSVCRT string functions
4 * Copyright 1996,1998 Marcus Meissner
5 * Copyright 1996 Jukka Iivonen
6 * Copyright 1997,2000 Uwe Bonnes
7 * Copyright 2000 Jon Griffiths
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #define _ISOC99_SOURCE
25 #include "config.h"
26 #include "wine/port.h"
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <math.h>
31 #include <limits.h>
32 #include <errno.h>
33 #include "msvcrt.h"
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
38 /*********************************************************************
39 * _mbsdup (MSVCRT.@)
40 * _strdup (MSVCRT.@)
42 char* CDECL _strdup(const char* str)
44 if(str)
46 char * ret = MSVCRT_malloc(strlen(str)+1);
47 if (ret) strcpy( ret, str );
48 return ret;
50 else return 0;
53 /*********************************************************************
54 * _strlwr_s (MSVCRT.@)
56 int CDECL _strlwr_s(char *str, MSVCRT_size_t len)
58 char *ptr = str;
60 if (!str || !len)
62 *MSVCRT__errno() = MSVCRT_EINVAL;
63 return MSVCRT_EINVAL;
66 while (len && *ptr)
68 len--;
69 ptr++;
72 if (!len)
74 str[0] = '\0';
75 *MSVCRT__errno() = MSVCRT_EINVAL;
76 return MSVCRT_EINVAL;
79 while (*str)
81 *str = tolower(*str);
82 str++;
85 return 0;
88 /*********************************************************************
89 * _strnset (MSVCRT.@)
91 char* CDECL MSVCRT__strnset(char* str, int value, MSVCRT_size_t len)
93 if (len > 0 && str)
94 while (*str && len--)
95 *str++ = value;
96 return str;
99 /*********************************************************************
100 * _strrev (MSVCRT.@)
102 char* CDECL _strrev(char* str)
104 char * p1;
105 char * p2;
107 if (str && *str)
108 for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2)
110 *p1 ^= *p2;
111 *p2 ^= *p1;
112 *p1 ^= *p2;
115 return str;
118 /*********************************************************************
119 * _strset (MSVCRT.@)
121 char* CDECL _strset(char* str, int value)
123 char *ptr = str;
124 while (*ptr)
125 *ptr++ = value;
127 return str;
130 /*********************************************************************
131 * strtok (MSVCRT.@)
133 char * CDECL MSVCRT_strtok( char *str, const char *delim )
135 thread_data_t *data = msvcrt_get_thread_data();
136 char *ret;
138 if (!str)
139 if (!(str = data->strtok_next)) return NULL;
141 while (*str && strchr( delim, *str )) str++;
142 if (!*str) return NULL;
143 ret = str++;
144 while (*str && !strchr( delim, *str )) str++;
145 if (*str) *str++ = 0;
146 data->strtok_next = str;
147 return ret;
150 /*********************************************************************
151 * strtok_s (MSVCRT.@)
153 char * CDECL MSVCRT_strtok_s(char *str, const char *delim, char **ctx)
155 if (!MSVCRT_CHECK_PMT(delim != NULL) || !MSVCRT_CHECK_PMT(ctx != NULL) ||
156 !MSVCRT_CHECK_PMT(str != NULL || *ctx != NULL)) {
157 *MSVCRT__errno() = MSVCRT_EINVAL;
158 return NULL;
161 if(!str)
162 str = *ctx;
164 while(*str && strchr(delim, *str))
165 str++;
166 if(!*str)
167 return NULL;
169 *ctx = str+1;
170 while(**ctx && !strchr(delim, **ctx))
171 (*ctx)++;
172 if(**ctx)
173 *(*ctx)++ = 0;
175 return str;
178 /*********************************************************************
179 * _swab (MSVCRT.@)
181 void CDECL MSVCRT__swab(char* src, char* dst, int len)
183 if (len > 1)
185 len = (unsigned)len >> 1;
187 while (len--) {
188 char s0 = src[0];
189 char s1 = src[1];
190 *dst++ = s1;
191 *dst++ = s0;
192 src = src + 2;
197 /*********************************************************************
198 * strtod_l (MSVCRT.@)
200 double CDECL MSVCRT_strtod_l( const char *str, char **end, MSVCRT__locale_t locale)
202 unsigned __int64 d=0, hlp;
203 unsigned fpcontrol;
204 int exp=0, sign=1;
205 const char *p;
206 double ret;
207 BOOL found_digit = FALSE;
209 if (!MSVCRT_CHECK_PMT(str != NULL)) {
210 *MSVCRT__errno() = MSVCRT_EINVAL;
211 return 0;
214 if(!locale)
215 locale = get_locale();
217 /* FIXME: use *_l functions */
218 p = str;
219 while(isspace(*p))
220 p++;
222 if(*p == '-') {
223 sign = -1;
224 p++;
225 } else if(*p == '+')
226 p++;
228 while(isdigit(*p)) {
229 found_digit = TRUE;
230 hlp = d*10+*(p++)-'0';
231 if(d>MSVCRT_UI64_MAX/10 || hlp<d) {
232 exp++;
233 break;
234 } else
235 d = hlp;
237 while(isdigit(*p)) {
238 exp++;
239 p++;
242 if(*p == *locale->locinfo->lconv->decimal_point)
243 p++;
245 while(isdigit(*p)) {
246 found_digit = TRUE;
247 hlp = d*10+*(p++)-'0';
248 if(d>MSVCRT_UI64_MAX/10 || hlp<d)
249 break;
251 d = hlp;
252 exp--;
254 while(isdigit(*p))
255 p++;
257 if(!found_digit) {
258 if(end)
259 *end = (char*)str;
260 return 0.0;
263 if(*p=='e' || *p=='E' || *p=='d' || *p=='D') {
264 int e=0, s=1;
266 p++;
267 if(*p == '-') {
268 s = -1;
269 p++;
270 } else if(*p == '+')
271 p++;
273 if(isdigit(*p)) {
274 while(isdigit(*p)) {
275 if(e>INT_MAX/10 || (e=e*10+*p-'0')<0)
276 e = INT_MAX;
277 p++;
279 e *= s;
281 if(exp<0 && e<0 && exp+e>=0) exp = INT_MIN;
282 else if(exp>0 && e>0 && exp+e<0) exp = INT_MAX;
283 else exp += e;
284 } else {
285 if(*p=='-' || *p=='+')
286 p--;
287 p--;
291 fpcontrol = _control87(0, 0);
292 _control87(MSVCRT__EM_DENORMAL|MSVCRT__EM_INVALID|MSVCRT__EM_ZERODIVIDE
293 |MSVCRT__EM_OVERFLOW|MSVCRT__EM_UNDERFLOW|MSVCRT__EM_INEXACT, 0xffffffff);
295 if(exp>0)
296 ret = (double)sign*d*pow(10, exp);
297 else
298 ret = (double)sign*d/pow(10, -exp);
300 _control87(fpcontrol, 0xffffffff);
302 if((d && ret==0.0) || isinf(ret))
303 *MSVCRT__errno() = MSVCRT_ERANGE;
305 if(end)
306 *end = (char*)p;
308 return ret;
311 /*********************************************************************
312 * strtod (MSVCRT.@)
314 double CDECL MSVCRT_strtod( const char *str, char **end )
316 return MSVCRT_strtod_l( str, end, NULL );
319 /*********************************************************************
320 * atof (MSVCRT.@)
322 double CDECL MSVCRT_atof( const char *str )
324 return MSVCRT_strtod_l(str, NULL, NULL);
327 /*********************************************************************
328 * _atof_l (MSVCRT.@)
330 double CDECL MSVCRT__atof_l( const char *str, MSVCRT__locale_t locale)
332 return MSVCRT_strtod_l(str, NULL, locale);
335 /*********************************************************************
336 * strcoll (MSVCRT.@)
338 int CDECL MSVCRT_strcoll( const char* str1, const char* str2 )
340 /* FIXME: handle Windows locale */
341 return strcoll( str1, str2 );
344 /*********************************************************************
345 * strcpy_s (MSVCRT.@)
347 int CDECL MSVCRT_strcpy_s( char* dst, MSVCRT_size_t elem, const char* src )
349 MSVCRT_size_t i;
350 if(!elem) return MSVCRT_EINVAL;
351 if(!dst) return MSVCRT_EINVAL;
352 if(!src)
354 dst[0] = '\0';
355 return MSVCRT_EINVAL;
358 for(i = 0; i < elem; i++)
360 if((dst[i] = src[i]) == '\0') return 0;
362 dst[0] = '\0';
363 return MSVCRT_ERANGE;
366 /*********************************************************************
367 * strcat_s (MSVCRT.@)
369 int CDECL MSVCRT_strcat_s( char* dst, MSVCRT_size_t elem, const char* src )
371 MSVCRT_size_t i, j;
372 if(!dst) return MSVCRT_EINVAL;
373 if(elem == 0) return MSVCRT_EINVAL;
374 if(!src)
376 dst[0] = '\0';
377 return MSVCRT_EINVAL;
380 for(i = 0; i < elem; i++)
382 if(dst[i] == '\0')
384 for(j = 0; (j + i) < elem; j++)
386 if((dst[j + i] = src[j]) == '\0') return 0;
390 /* Set the first element to 0, not the first element after the skipped part */
391 dst[0] = '\0';
392 return MSVCRT_ERANGE;
395 /*********************************************************************
396 * strncat_s (MSVCRT.@)
398 int CDECL MSVCRT_strncat_s( char* dst, MSVCRT_size_t elem, const char* src, MSVCRT_size_t count )
400 MSVCRT_size_t i, j;
401 if(!MSVCRT_CHECK_PMT(dst != 0) || !MSVCRT_CHECK_PMT(elem != 0))
402 return MSVCRT_EINVAL;
403 if(!MSVCRT_CHECK_PMT(src != 0))
405 dst[0] = '\0';
406 return MSVCRT_EINVAL;
409 for(i = 0; i < elem; i++)
411 if(dst[i] == '\0')
413 for(j = 0; (j + i) < elem; j++)
415 if(count == MSVCRT__TRUNCATE && j + i == elem - 1)
417 dst[j + i] = '\0';
418 return MSVCRT_STRUNCATE;
420 if(j == count || (dst[j + i] = src[j]) == '\0')
422 dst[j + i] = '\0';
423 return 0;
428 /* Set the first element to 0, not the first element after the skipped part */
429 dst[0] = '\0';
430 return MSVCRT_ERANGE;
433 /*********************************************************************
434 * strxfrm (MSVCRT.@)
436 MSVCRT_size_t CDECL MSVCRT_strxfrm( char *dest, const char *src, MSVCRT_size_t len )
438 /* FIXME: handle Windows locale */
439 return strxfrm( dest, src, len );
442 /*********************************************************************
443 * _stricoll (MSVCRT.@)
445 int CDECL MSVCRT__stricoll( const char* str1, const char* str2 )
447 /* FIXME: handle collates */
448 TRACE("str1 %s str2 %s\n", debugstr_a(str1), debugstr_a(str2));
449 return lstrcmpiA( str1, str2 );
452 /********************************************************************
453 * _atoldbl (MSVCRT.@)
455 int CDECL MSVCRT__atoldbl(MSVCRT__LDOUBLE *value, const char *str)
457 /* FIXME needs error checking for huge/small values */
458 #ifdef HAVE_STRTOLD
459 TRACE("str %s value %p\n",str,value);
460 value->x = strtold(str,0);
461 #else
462 FIXME("stub, str %s value %p\n",str,value);
463 #endif
464 return 0;
467 /********************************************************************
468 * __STRINGTOLD (MSVCRT.@)
470 int CDECL __STRINGTOLD( MSVCRT__LDOUBLE *value, char **endptr, const char *str, int flags )
472 #ifdef HAVE_STRTOLD
473 FIXME("%p %p %s %x partial stub\n", value, endptr, str, flags );
474 value->x = strtold(str,endptr);
475 #else
476 FIXME("%p %p %s %x stub\n", value, endptr, str, flags );
477 #endif
478 return 0;
481 /******************************************************************
482 * strtol (MSVCRT.@)
484 MSVCRT_long CDECL MSVCRT_strtol(const char* nptr, char** end, int base)
486 /* wrapper to forward libc error code to msvcrt's error codes */
487 long ret;
489 errno = 0;
490 ret = strtol(nptr, end, base);
491 switch (errno)
493 case ERANGE: *MSVCRT__errno() = MSVCRT_ERANGE; break;
494 case EINVAL: *MSVCRT__errno() = MSVCRT_EINVAL; break;
495 default:
496 /* cope with the fact that we may use 64bit long integers on libc
497 * while msvcrt always uses 32bit long integers
499 if (ret > MSVCRT_LONG_MAX)
501 ret = MSVCRT_LONG_MAX;
502 *MSVCRT__errno() = MSVCRT_ERANGE;
504 else if (ret < -MSVCRT_LONG_MAX - 1)
506 ret = -MSVCRT_LONG_MAX - 1;
507 *MSVCRT__errno() = MSVCRT_ERANGE;
509 break;
512 return ret;
515 /******************************************************************
516 * strtoul (MSVCRT.@)
518 MSVCRT_ulong CDECL MSVCRT_strtoul(const char* nptr, char** end, int base)
520 /* wrapper to forward libc error code to msvcrt's error codes */
521 unsigned long ret;
523 errno = 0;
524 ret = strtoul(nptr, end, base);
525 switch (errno)
527 case ERANGE: *MSVCRT__errno() = MSVCRT_ERANGE; break;
528 case EINVAL: *MSVCRT__errno() = MSVCRT_EINVAL; break;
529 default:
530 /* cope with the fact that we may use 64bit long integers on libc
531 * while msvcrt always uses 32bit long integers
533 if (ret > MSVCRT_ULONG_MAX)
535 ret = MSVCRT_ULONG_MAX;
536 *MSVCRT__errno() = MSVCRT_ERANGE;
538 break;
541 return ret;
544 /******************************************************************
545 * strnlen (MSVCRT.@)
547 MSVCRT_size_t CDECL MSVCRT_strnlen(const char *s, MSVCRT_size_t maxlen)
549 MSVCRT_size_t i;
551 for(i=0; i<maxlen; i++)
552 if(!s[i]) break;
554 return i;
557 /*********************************************************************
558 * _strtoi64_l (MSVCRT.@)
560 * FIXME: locale parameter is ignored
562 __int64 CDECL MSVCRT_strtoi64_l(const char *nptr, char **endptr, int base, MSVCRT__locale_t locale)
564 BOOL negative = FALSE;
565 __int64 ret = 0;
567 TRACE("(%s %p %d %p)\n", nptr, endptr, base, locale);
569 if (!MSVCRT_CHECK_PMT(nptr != NULL) || !MSVCRT_CHECK_PMT(base == 0 || base >= 2) ||
570 !MSVCRT_CHECK_PMT(base <= 36)) {
571 return 0;
574 while(isspace(*nptr)) nptr++;
576 if(*nptr == '-') {
577 negative = TRUE;
578 nptr++;
579 } else if(*nptr == '+')
580 nptr++;
582 if((base==0 || base==16) && *nptr=='0' && tolower(*(nptr+1))=='x') {
583 base = 16;
584 nptr += 2;
587 if(base == 0) {
588 if(*nptr=='0')
589 base = 8;
590 else
591 base = 10;
594 while(*nptr) {
595 char cur = tolower(*nptr);
596 int v;
598 if(isdigit(cur)) {
599 if(cur >= '0'+base)
600 break;
601 v = cur-'0';
602 } else {
603 if(cur<'a' || cur>='a'+base-10)
604 break;
605 v = cur-'a'+10;
608 if(negative)
609 v = -v;
611 nptr++;
613 if(!negative && (ret>MSVCRT_I64_MAX/base || ret*base>MSVCRT_I64_MAX-v)) {
614 ret = MSVCRT_I64_MAX;
615 *MSVCRT__errno() = MSVCRT_ERANGE;
616 } else if(negative && (ret<MSVCRT_I64_MIN/base || ret*base<MSVCRT_I64_MIN-v)) {
617 ret = MSVCRT_I64_MIN;
618 *MSVCRT__errno() = MSVCRT_ERANGE;
619 } else
620 ret = ret*base + v;
623 if(endptr)
624 *endptr = (char*)nptr;
626 return ret;
629 /*********************************************************************
630 * _strtoi64 (MSVCRT.@)
632 __int64 CDECL MSVCRT_strtoi64(const char *nptr, char **endptr, int base)
634 return MSVCRT_strtoi64_l(nptr, endptr, base, NULL);
637 /*********************************************************************
638 * _strtoui64_l (MSVCRT.@)
640 * FIXME: locale parameter is ignored
642 unsigned __int64 CDECL MSVCRT_strtoui64_l(const char *nptr, char **endptr, int base, MSVCRT__locale_t locale)
644 BOOL negative = FALSE;
645 unsigned __int64 ret = 0;
647 TRACE("(%s %p %d %p)\n", nptr, endptr, base, locale);
649 if (!MSVCRT_CHECK_PMT(nptr != NULL) || !MSVCRT_CHECK_PMT(base == 0 || base >= 2) ||
650 !MSVCRT_CHECK_PMT(base <= 36)) {
651 return 0;
654 while(isspace(*nptr)) nptr++;
656 if(*nptr == '-') {
657 negative = TRUE;
658 nptr++;
659 } else if(*nptr == '+')
660 nptr++;
662 if((base==0 || base==16) && *nptr=='0' && tolower(*(nptr+1))=='x') {
663 base = 16;
664 nptr += 2;
667 if(base == 0) {
668 if(*nptr=='0')
669 base = 8;
670 else
671 base = 10;
674 while(*nptr) {
675 char cur = tolower(*nptr);
676 int v;
678 if(isdigit(cur)) {
679 if(cur >= '0'+base)
680 break;
681 v = *nptr-'0';
682 } else {
683 if(cur<'a' || cur>='a'+base-10)
684 break;
685 v = cur-'a'+10;
688 nptr++;
690 if(ret>MSVCRT_UI64_MAX/base || ret*base>MSVCRT_UI64_MAX-v) {
691 ret = MSVCRT_UI64_MAX;
692 *MSVCRT__errno() = MSVCRT_ERANGE;
693 } else
694 ret = ret*base + v;
697 if(endptr)
698 *endptr = (char*)nptr;
700 return negative ? -ret : ret;
703 /*********************************************************************
704 * _strtoui64 (MSVCRT.@)
706 unsigned __int64 CDECL MSVCRT_strtoui64(const char *nptr, char **endptr, int base)
708 return MSVCRT_strtoui64_l(nptr, endptr, base, NULL);
711 /*********************************************************************
712 * _ltoa_s (MSVCRT.@)
714 int CDECL _ltoa_s(MSVCRT_long value, char *str, MSVCRT_size_t size, int radix)
716 MSVCRT_ulong val;
717 unsigned int digit;
718 int is_negative;
719 char buffer[33], *pos;
720 size_t len;
722 if (!MSVCRT_CHECK_PMT(str != NULL) || !MSVCRT_CHECK_PMT(size > 0) ||
723 !MSVCRT_CHECK_PMT(radix >= 2) || !MSVCRT_CHECK_PMT(radix <= 36))
725 if (str && size)
726 str[0] = '\0';
728 *MSVCRT__errno() = MSVCRT_EINVAL;
729 return MSVCRT_EINVAL;
732 if (value < 0 && radix == 10)
734 is_negative = 1;
735 val = -value;
737 else
739 is_negative = 0;
740 val = value;
743 pos = buffer + 32;
744 *pos = '\0';
748 digit = val % radix;
749 val /= radix;
751 if (digit < 10)
752 *--pos = '0' + digit;
753 else
754 *--pos = 'a' + digit - 10;
756 while (val != 0);
758 if (is_negative)
759 *--pos = '-';
761 len = buffer + 33 - pos;
762 if (len > size)
764 size_t i;
765 char *p = str;
767 /* Copy the temporary buffer backwards up to the available number of
768 * characters. Don't copy the negative sign if present. */
770 if (is_negative)
772 p++;
773 size--;
776 for (pos = buffer + 31, i = 0; i < size; i++)
777 *p++ = *pos--;
779 str[0] = '\0';
780 MSVCRT_INVALID_PMT("str[size] is too small");
781 *MSVCRT__errno() = MSVCRT_ERANGE;
782 return MSVCRT_ERANGE;
785 memcpy(str, pos, len);
786 return 0;
789 /*********************************************************************
790 * _ltow_s (MSVCRT.@)
792 int CDECL _ltow_s(MSVCRT_long value, MSVCRT_wchar_t *str, MSVCRT_size_t size, int radix)
794 MSVCRT_ulong val;
795 unsigned int digit;
796 int is_negative;
797 MSVCRT_wchar_t buffer[33], *pos;
798 size_t len;
800 if (!MSVCRT_CHECK_PMT(str != NULL) || !MSVCRT_CHECK_PMT(size > 0) ||
801 !MSVCRT_CHECK_PMT(radix >= 2) || !MSVCRT_CHECK_PMT(radix <= 36))
803 if (str && size)
804 str[0] = '\0';
806 *MSVCRT__errno() = MSVCRT_EINVAL;
807 return MSVCRT_EINVAL;
810 if (value < 0 && radix == 10)
812 is_negative = 1;
813 val = -value;
815 else
817 is_negative = 0;
818 val = value;
821 pos = buffer + 32;
822 *pos = '\0';
826 digit = val % radix;
827 val /= radix;
829 if (digit < 10)
830 *--pos = '0' + digit;
831 else
832 *--pos = 'a' + digit - 10;
834 while (val != 0);
836 if (is_negative)
837 *--pos = '-';
839 len = buffer + 33 - pos;
840 if (len > size)
842 size_t i;
843 MSVCRT_wchar_t *p = str;
845 /* Copy the temporary buffer backwards up to the available number of
846 * characters. Don't copy the negative sign if present. */
848 if (is_negative)
850 p++;
851 size--;
854 for (pos = buffer + 31, i = 0; i < size; i++)
855 *p++ = *pos--;
857 MSVCRT_INVALID_PMT("str[size] is too small");
858 str[0] = '\0';
859 *MSVCRT__errno() = MSVCRT_ERANGE;
860 return MSVCRT_ERANGE;
863 memcpy(str, pos, len * sizeof(MSVCRT_wchar_t));
864 return 0;
867 /*********************************************************************
868 * _itoa_s (MSVCRT.@)
870 int CDECL _itoa_s(int value, char *str, MSVCRT_size_t size, int radix)
872 return _ltoa_s(value, str, size, radix);
875 /*********************************************************************
876 * _itow_s (MSVCRT.@)
878 int CDECL _itow_s(int value, MSVCRT_wchar_t *str, MSVCRT_size_t size, int radix)
880 return _ltow_s(value, str, size, radix);
883 /*********************************************************************
884 * _ui64toa_s (MSVCRT.@)
886 int CDECL MSVCRT__ui64toa_s(unsigned __int64 value, char *str,
887 MSVCRT_size_t size, int radix)
889 char buffer[65], *pos;
890 int digit;
892 if (!MSVCRT_CHECK_PMT(str != NULL) || !MSVCRT_CHECK_PMT(size > 0) ||
893 !MSVCRT_CHECK_PMT(radix>=2) || !MSVCRT_CHECK_PMT(radix<=36)) {
894 *MSVCRT__errno() = MSVCRT_EINVAL;
895 return MSVCRT_EINVAL;
898 pos = buffer+64;
899 *pos = '\0';
901 do {
902 digit = value%radix;
903 value /= radix;
905 if(digit < 10)
906 *--pos = '0'+digit;
907 else
908 *--pos = 'a'+digit-10;
909 }while(value != 0);
911 if(buffer-pos+65 > size) {
912 MSVCRT_INVALID_PMT("str[size] is too small");
913 *MSVCRT__errno() = MSVCRT_EINVAL;
914 return MSVCRT_EINVAL;
917 memcpy(str, pos, buffer-pos+65);
918 return 0;
921 /*********************************************************************
922 * _ultoa_s (MSVCRT.@)
924 int CDECL _ultoa_s(MSVCRT_ulong value, char *str, MSVCRT_size_t size, int radix)
926 MSVCRT_ulong digit;
927 char buffer[33], *pos;
928 size_t len;
930 if (!str || !size || radix < 2 || radix > 36)
932 if (str && size)
933 str[0] = '\0';
935 *MSVCRT__errno() = MSVCRT_EINVAL;
936 return MSVCRT_EINVAL;
939 pos = buffer + 32;
940 *pos = '\0';
944 digit = value % radix;
945 value /= radix;
947 if (digit < 10)
948 *--pos = '0' + digit;
949 else
950 *--pos = 'a' + digit - 10;
952 while (value != 0);
954 len = buffer + 33 - pos;
955 if (len > size)
957 size_t i;
958 char *p = str;
960 /* Copy the temporary buffer backwards up to the available number of
961 * characters. */
963 for (pos = buffer + 31, i = 0; i < size; i++)
964 *p++ = *pos--;
966 str[0] = '\0';
967 *MSVCRT__errno() = MSVCRT_ERANGE;
968 return MSVCRT_ERANGE;
971 memcpy(str, pos, len);
972 return 0;
975 /*********************************************************************
976 * _i64toa_s (MSVCRT.@)
978 int CDECL _i64toa_s(__int64 value, char *str, MSVCRT_size_t size, int radix)
980 unsigned __int64 val;
981 unsigned int digit;
982 int is_negative;
983 char buffer[65], *pos;
984 size_t len;
986 if (!MSVCRT_CHECK_PMT(str != NULL) || !MSVCRT_CHECK_PMT(size > 0) ||
987 !MSVCRT_CHECK_PMT(radix >= 2) || !MSVCRT_CHECK_PMT(radix <= 36))
989 if (str && size)
990 str[0] = '\0';
992 *MSVCRT__errno() = MSVCRT_EINVAL;
993 return MSVCRT_EINVAL;
996 if (value < 0 && radix == 10)
998 is_negative = 1;
999 val = -value;
1001 else
1003 is_negative = 0;
1004 val = value;
1007 pos = buffer + 64;
1008 *pos = '\0';
1012 digit = val % radix;
1013 val /= radix;
1015 if (digit < 10)
1016 *--pos = '0' + digit;
1017 else
1018 *--pos = 'a' + digit - 10;
1020 while (val != 0);
1022 if (is_negative)
1023 *--pos = '-';
1025 len = buffer + 65 - pos;
1026 if (len > size)
1028 size_t i;
1029 char *p = str;
1031 /* Copy the temporary buffer backwards up to the available number of
1032 * characters. Don't copy the negative sign if present. */
1034 if (is_negative)
1036 p++;
1037 size--;
1040 for (pos = buffer + 63, i = 0; i < size; i++)
1041 *p++ = *pos--;
1043 str[0] = '\0';
1044 MSVCRT_INVALID_PMT("str[size] is too small");
1045 *MSVCRT__errno() = MSVCRT_ERANGE;
1046 return MSVCRT_ERANGE;
1049 memcpy(str, pos, len);
1050 return 0;
1053 /*********************************************************************
1054 * _i64tow_s (MSVCRT.@)
1056 int CDECL _i64tow_s(__int64 value, MSVCRT_wchar_t *str, MSVCRT_size_t size, int radix)
1058 unsigned __int64 val;
1059 unsigned int digit;
1060 int is_negative;
1061 MSVCRT_wchar_t buffer[65], *pos;
1062 size_t len;
1064 if (!MSVCRT_CHECK_PMT(str != NULL) || !MSVCRT_CHECK_PMT(size > 0) ||
1065 !MSVCRT_CHECK_PMT(radix >= 2) || !MSVCRT_CHECK_PMT(radix <= 36))
1067 if (str && size)
1068 str[0] = '\0';
1070 *MSVCRT__errno() = MSVCRT_EINVAL;
1071 return MSVCRT_EINVAL;
1074 if (value < 0 && radix == 10)
1076 is_negative = 1;
1077 val = -value;
1079 else
1081 is_negative = 0;
1082 val = value;
1085 pos = buffer + 64;
1086 *pos = '\0';
1090 digit = val % radix;
1091 val /= radix;
1093 if (digit < 10)
1094 *--pos = '0' + digit;
1095 else
1096 *--pos = 'a' + digit - 10;
1098 while (val != 0);
1100 if (is_negative)
1101 *--pos = '-';
1103 len = buffer + 65 - pos;
1104 if (len > size)
1106 size_t i;
1107 MSVCRT_wchar_t *p = str;
1109 /* Copy the temporary buffer backwards up to the available number of
1110 * characters. Don't copy the negative sign if present. */
1112 if (is_negative)
1114 p++;
1115 size--;
1118 for (pos = buffer + 63, i = 0; i < size; i++)
1119 *p++ = *pos--;
1121 MSVCRT_INVALID_PMT("str[size] is too small");
1122 str[0] = '\0';
1123 *MSVCRT__errno() = MSVCRT_ERANGE;
1124 return MSVCRT_ERANGE;
1127 memcpy(str, pos, len * sizeof(MSVCRT_wchar_t));
1128 return 0;
1131 #define I10_OUTPUT_MAX_PREC 21
1132 /* Internal structure used by $I10_OUTPUT */
1133 struct _I10_OUTPUT_DATA {
1134 short pos;
1135 char sign;
1136 BYTE len;
1137 char str[I10_OUTPUT_MAX_PREC+1]; /* add space for '\0' */
1140 /*********************************************************************
1141 * $I10_OUTPUT (MSVCRT.@)
1142 * ld - long double to be printed to data
1143 * prec - precision of part, we're interested in
1144 * flag - 0 for first prec digits, 1 for fractional part
1145 * data - data to be populated
1147 * return value
1148 * 0 if given double is NaN or INF
1149 * 1 otherwise
1151 * FIXME
1152 * Native sets last byte of data->str to '0' or '9', I don't know what
1153 * it means. Current implementation sets it always to '0'.
1155 int CDECL MSVCRT_I10_OUTPUT(MSVCRT__LDOUBLE ld, int prec, int flag, struct _I10_OUTPUT_DATA *data)
1157 static const char inf_str[] = "1#INF";
1158 static const char nan_str[] = "1#QNAN";
1160 double d = ld.x;
1161 char format[8];
1162 char buf[I10_OUTPUT_MAX_PREC+9]; /* 9 = strlen("0.e+0000") + '\0' */
1163 char *p;
1165 TRACE("(%lf %d %x %p)\n", d, prec, flag, data);
1167 if(d<0) {
1168 data->sign = '-';
1169 d = -d;
1170 } else
1171 data->sign = ' ';
1173 if(isinf(d)) {
1174 data->pos = 1;
1175 data->len = 5;
1176 memcpy(data->str, inf_str, sizeof(inf_str));
1178 return 0;
1181 if(isnan(d)) {
1182 data->pos = 1;
1183 data->len = 6;
1184 memcpy(data->str, nan_str, sizeof(nan_str));
1186 return 0;
1189 if(flag&1) {
1190 int exp = 1+floor(log10(d));
1192 prec += exp;
1193 if(exp < 0)
1194 prec--;
1196 prec--;
1198 if(prec+1 > I10_OUTPUT_MAX_PREC)
1199 prec = I10_OUTPUT_MAX_PREC-1;
1200 else if(prec < 0) {
1201 d = 0.0;
1202 prec = 0;
1205 sprintf(format, "%%.%dle", prec);
1206 sprintf(buf, format, d);
1208 buf[1] = buf[0];
1209 data->pos = atoi(buf+prec+3);
1210 if(buf[1] != '0')
1211 data->pos++;
1213 for(p = buf+prec+1; p>buf+1 && *p=='0'; p--);
1214 data->len = p-buf;
1216 memcpy(data->str, buf+1, data->len);
1217 data->str[data->len] = '\0';
1219 if(buf[1]!='0' && prec-data->len+1>0)
1220 memcpy(data->str+data->len+1, buf+data->len+1, prec-data->len+1);
1222 return 1;
1224 #undef I10_OUTPUT_MAX_PREC