avifil32: Convert string table resources to po files.
[wine.git] / dlls / msvcrt / string.c
blob6be223742f5cadd872ad90648488c7d86cd34767
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 * _ui64tow_s (MSVCRT.@)
924 int CDECL MSVCRT__ui64tow_s( unsigned __int64 value, MSVCRT_wchar_t *str,
925 MSVCRT_size_t size, int radix )
927 MSVCRT_wchar_t buffer[65], *pos;
928 int digit;
930 if (!MSVCRT_CHECK_PMT(str != NULL) || !MSVCRT_CHECK_PMT(size > 0) ||
931 !MSVCRT_CHECK_PMT(radix>=2) || !MSVCRT_CHECK_PMT(radix<=36)) {
932 *MSVCRT__errno() = MSVCRT_EINVAL;
933 return MSVCRT_EINVAL;
936 pos = &buffer[64];
937 *pos = '\0';
939 do {
940 digit = value % radix;
941 value = value / radix;
942 if (digit < 10)
943 *--pos = '0' + digit;
944 else
945 *--pos = 'a' + digit - 10;
946 } while (value != 0);
948 if(buffer-pos+65 > size) {
949 MSVCRT_INVALID_PMT("str[size] is too small");
950 *MSVCRT__errno() = MSVCRT_EINVAL;
951 return MSVCRT_EINVAL;
954 memcpy(str, pos, buffer-pos+65);
955 return 0;
958 /*********************************************************************
959 * _ultoa_s (MSVCRT.@)
961 int CDECL _ultoa_s(MSVCRT_ulong value, char *str, MSVCRT_size_t size, int radix)
963 MSVCRT_ulong digit;
964 char buffer[33], *pos;
965 size_t len;
967 if (!str || !size || radix < 2 || radix > 36)
969 if (str && size)
970 str[0] = '\0';
972 *MSVCRT__errno() = MSVCRT_EINVAL;
973 return MSVCRT_EINVAL;
976 pos = buffer + 32;
977 *pos = '\0';
981 digit = value % radix;
982 value /= radix;
984 if (digit < 10)
985 *--pos = '0' + digit;
986 else
987 *--pos = 'a' + digit - 10;
989 while (value != 0);
991 len = buffer + 33 - pos;
992 if (len > size)
994 size_t i;
995 char *p = str;
997 /* Copy the temporary buffer backwards up to the available number of
998 * characters. */
1000 for (pos = buffer + 31, i = 0; i < size; i++)
1001 *p++ = *pos--;
1003 str[0] = '\0';
1004 *MSVCRT__errno() = MSVCRT_ERANGE;
1005 return MSVCRT_ERANGE;
1008 memcpy(str, pos, len);
1009 return 0;
1012 /*********************************************************************
1013 * _i64toa_s (MSVCRT.@)
1015 int CDECL _i64toa_s(__int64 value, char *str, MSVCRT_size_t size, int radix)
1017 unsigned __int64 val;
1018 unsigned int digit;
1019 int is_negative;
1020 char buffer[65], *pos;
1021 size_t len;
1023 if (!MSVCRT_CHECK_PMT(str != NULL) || !MSVCRT_CHECK_PMT(size > 0) ||
1024 !MSVCRT_CHECK_PMT(radix >= 2) || !MSVCRT_CHECK_PMT(radix <= 36))
1026 if (str && size)
1027 str[0] = '\0';
1029 *MSVCRT__errno() = MSVCRT_EINVAL;
1030 return MSVCRT_EINVAL;
1033 if (value < 0 && radix == 10)
1035 is_negative = 1;
1036 val = -value;
1038 else
1040 is_negative = 0;
1041 val = value;
1044 pos = buffer + 64;
1045 *pos = '\0';
1049 digit = val % radix;
1050 val /= radix;
1052 if (digit < 10)
1053 *--pos = '0' + digit;
1054 else
1055 *--pos = 'a' + digit - 10;
1057 while (val != 0);
1059 if (is_negative)
1060 *--pos = '-';
1062 len = buffer + 65 - pos;
1063 if (len > size)
1065 size_t i;
1066 char *p = str;
1068 /* Copy the temporary buffer backwards up to the available number of
1069 * characters. Don't copy the negative sign if present. */
1071 if (is_negative)
1073 p++;
1074 size--;
1077 for (pos = buffer + 63, i = 0; i < size; i++)
1078 *p++ = *pos--;
1080 str[0] = '\0';
1081 MSVCRT_INVALID_PMT("str[size] is too small");
1082 *MSVCRT__errno() = MSVCRT_ERANGE;
1083 return MSVCRT_ERANGE;
1086 memcpy(str, pos, len);
1087 return 0;
1090 /*********************************************************************
1091 * _i64tow_s (MSVCRT.@)
1093 int CDECL _i64tow_s(__int64 value, MSVCRT_wchar_t *str, MSVCRT_size_t size, int radix)
1095 unsigned __int64 val;
1096 unsigned int digit;
1097 int is_negative;
1098 MSVCRT_wchar_t buffer[65], *pos;
1099 size_t len;
1101 if (!MSVCRT_CHECK_PMT(str != NULL) || !MSVCRT_CHECK_PMT(size > 0) ||
1102 !MSVCRT_CHECK_PMT(radix >= 2) || !MSVCRT_CHECK_PMT(radix <= 36))
1104 if (str && size)
1105 str[0] = '\0';
1107 *MSVCRT__errno() = MSVCRT_EINVAL;
1108 return MSVCRT_EINVAL;
1111 if (value < 0 && radix == 10)
1113 is_negative = 1;
1114 val = -value;
1116 else
1118 is_negative = 0;
1119 val = value;
1122 pos = buffer + 64;
1123 *pos = '\0';
1127 digit = val % radix;
1128 val /= radix;
1130 if (digit < 10)
1131 *--pos = '0' + digit;
1132 else
1133 *--pos = 'a' + digit - 10;
1135 while (val != 0);
1137 if (is_negative)
1138 *--pos = '-';
1140 len = buffer + 65 - pos;
1141 if (len > size)
1143 size_t i;
1144 MSVCRT_wchar_t *p = str;
1146 /* Copy the temporary buffer backwards up to the available number of
1147 * characters. Don't copy the negative sign if present. */
1149 if (is_negative)
1151 p++;
1152 size--;
1155 for (pos = buffer + 63, i = 0; i < size; i++)
1156 *p++ = *pos--;
1158 MSVCRT_INVALID_PMT("str[size] is too small");
1159 str[0] = '\0';
1160 *MSVCRT__errno() = MSVCRT_ERANGE;
1161 return MSVCRT_ERANGE;
1164 memcpy(str, pos, len * sizeof(MSVCRT_wchar_t));
1165 return 0;
1168 #define I10_OUTPUT_MAX_PREC 21
1169 /* Internal structure used by $I10_OUTPUT */
1170 struct _I10_OUTPUT_DATA {
1171 short pos;
1172 char sign;
1173 BYTE len;
1174 char str[I10_OUTPUT_MAX_PREC+1]; /* add space for '\0' */
1177 /*********************************************************************
1178 * $I10_OUTPUT (MSVCRT.@)
1179 * ld - long double to be printed to data
1180 * prec - precision of part, we're interested in
1181 * flag - 0 for first prec digits, 1 for fractional part
1182 * data - data to be populated
1184 * return value
1185 * 0 if given double is NaN or INF
1186 * 1 otherwise
1188 * FIXME
1189 * Native sets last byte of data->str to '0' or '9', I don't know what
1190 * it means. Current implementation sets it always to '0'.
1192 int CDECL MSVCRT_I10_OUTPUT(MSVCRT__LDOUBLE ld, int prec, int flag, struct _I10_OUTPUT_DATA *data)
1194 static const char inf_str[] = "1#INF";
1195 static const char nan_str[] = "1#QNAN";
1197 double d = ld.x;
1198 char format[8];
1199 char buf[I10_OUTPUT_MAX_PREC+9]; /* 9 = strlen("0.e+0000") + '\0' */
1200 char *p;
1202 TRACE("(%lf %d %x %p)\n", d, prec, flag, data);
1204 if(d<0) {
1205 data->sign = '-';
1206 d = -d;
1207 } else
1208 data->sign = ' ';
1210 if(isinf(d)) {
1211 data->pos = 1;
1212 data->len = 5;
1213 memcpy(data->str, inf_str, sizeof(inf_str));
1215 return 0;
1218 if(isnan(d)) {
1219 data->pos = 1;
1220 data->len = 6;
1221 memcpy(data->str, nan_str, sizeof(nan_str));
1223 return 0;
1226 if(flag&1) {
1227 int exp = 1+floor(log10(d));
1229 prec += exp;
1230 if(exp < 0)
1231 prec--;
1233 prec--;
1235 if(prec+1 > I10_OUTPUT_MAX_PREC)
1236 prec = I10_OUTPUT_MAX_PREC-1;
1237 else if(prec < 0) {
1238 d = 0.0;
1239 prec = 0;
1242 sprintf(format, "%%.%dle", prec);
1243 sprintf(buf, format, d);
1245 buf[1] = buf[0];
1246 data->pos = atoi(buf+prec+3);
1247 if(buf[1] != '0')
1248 data->pos++;
1250 for(p = buf+prec+1; p>buf+1 && *p=='0'; p--);
1251 data->len = p-buf;
1253 memcpy(data->str, buf+1, data->len);
1254 data->str[data->len] = '\0';
1256 if(buf[1]!='0' && prec-data->len+1>0)
1257 memcpy(data->str+data->len+1, buf+data->len+1, prec-data->len+1);
1259 return 1;
1261 #undef I10_OUTPUT_MAX_PREC