Release 1.3.7.
[wine/gsoc-2012-control.git] / dlls / msvcrt / string.c
blobea3ca1b634897b935e6481f042f733773ae64f99
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 * strxfrm (MSVCRT.@)
398 MSVCRT_size_t CDECL MSVCRT_strxfrm( char *dest, const char *src, MSVCRT_size_t len )
400 /* FIXME: handle Windows locale */
401 return strxfrm( dest, src, len );
404 /*********************************************************************
405 * _stricoll (MSVCRT.@)
407 int CDECL MSVCRT__stricoll( const char* str1, const char* str2 )
409 /* FIXME: handle collates */
410 TRACE("str1 %s str2 %s\n", debugstr_a(str1), debugstr_a(str2));
411 return lstrcmpiA( str1, str2 );
414 /********************************************************************
415 * _atoldbl (MSVCRT.@)
417 int CDECL MSVCRT__atoldbl(MSVCRT__LDOUBLE *value, const char *str)
419 /* FIXME needs error checking for huge/small values */
420 #ifdef HAVE_STRTOLD
421 TRACE("str %s value %p\n",str,value);
422 value->x = strtold(str,0);
423 #else
424 FIXME("stub, str %s value %p\n",str,value);
425 #endif
426 return 0;
429 /********************************************************************
430 * __STRINGTOLD (MSVCRT.@)
432 int CDECL __STRINGTOLD( MSVCRT__LDOUBLE *value, char **endptr, const char *str, int flags )
434 #ifdef HAVE_STRTOLD
435 FIXME("%p %p %s %x partial stub\n", value, endptr, str, flags );
436 value->x = strtold(str,endptr);
437 #else
438 FIXME("%p %p %s %x stub\n", value, endptr, str, flags );
439 #endif
440 return 0;
443 /******************************************************************
444 * strtol (MSVCRT.@)
446 MSVCRT_long CDECL MSVCRT_strtol(const char* nptr, char** end, int base)
448 /* wrapper to forward libc error code to msvcrt's error codes */
449 long ret;
451 errno = 0;
452 ret = strtol(nptr, end, base);
453 switch (errno)
455 case ERANGE: *MSVCRT__errno() = MSVCRT_ERANGE; break;
456 case EINVAL: *MSVCRT__errno() = MSVCRT_EINVAL; break;
457 default:
458 /* cope with the fact that we may use 64bit long integers on libc
459 * while msvcrt always uses 32bit long integers
461 if (ret > MSVCRT_LONG_MAX)
463 ret = MSVCRT_LONG_MAX;
464 *MSVCRT__errno() = MSVCRT_ERANGE;
466 else if (ret < -MSVCRT_LONG_MAX - 1)
468 ret = -MSVCRT_LONG_MAX - 1;
469 *MSVCRT__errno() = MSVCRT_ERANGE;
471 break;
474 return ret;
477 /******************************************************************
478 * strtoul (MSVCRT.@)
480 MSVCRT_ulong CDECL MSVCRT_strtoul(const char* nptr, char** end, int base)
482 /* wrapper to forward libc error code to msvcrt's error codes */
483 unsigned long ret;
485 errno = 0;
486 ret = strtoul(nptr, end, base);
487 switch (errno)
489 case ERANGE: *MSVCRT__errno() = MSVCRT_ERANGE; break;
490 case EINVAL: *MSVCRT__errno() = MSVCRT_EINVAL; break;
491 default:
492 /* cope with the fact that we may use 64bit long integers on libc
493 * while msvcrt always uses 32bit long integers
495 if (ret > MSVCRT_ULONG_MAX)
497 ret = MSVCRT_ULONG_MAX;
498 *MSVCRT__errno() = MSVCRT_ERANGE;
500 break;
503 return ret;
506 /******************************************************************
507 * strnlen (MSVCRT.@)
509 MSVCRT_size_t CDECL MSVCRT_strnlen(const char *s, MSVCRT_size_t maxlen)
511 MSVCRT_size_t i;
513 for(i=0; i<maxlen; i++)
514 if(!s[i]) break;
516 return i;
519 /*********************************************************************
520 * _strtoi64_l (MSVCRT.@)
522 * FIXME: locale parameter is ignored
524 __int64 CDECL MSVCRT_strtoi64_l(const char *nptr, char **endptr, int base, MSVCRT__locale_t locale)
526 BOOL negative = FALSE;
527 __int64 ret = 0;
529 TRACE("(%s %p %d %p)\n", nptr, endptr, base, locale);
531 if (!MSVCRT_CHECK_PMT(nptr != NULL) || !MSVCRT_CHECK_PMT(base == 0 || base >= 2) ||
532 !MSVCRT_CHECK_PMT(base <= 36)) {
533 return 0;
536 while(isspace(*nptr)) nptr++;
538 if(*nptr == '-') {
539 negative = TRUE;
540 nptr++;
541 } else if(*nptr == '+')
542 nptr++;
544 if((base==0 || base==16) && *nptr=='0' && tolower(*(nptr+1))=='x') {
545 base = 16;
546 nptr += 2;
549 if(base == 0) {
550 if(*nptr=='0')
551 base = 8;
552 else
553 base = 10;
556 while(*nptr) {
557 char cur = tolower(*nptr);
558 int v;
560 if(isdigit(cur)) {
561 if(cur >= '0'+base)
562 break;
563 v = cur-'0';
564 } else {
565 if(cur<'a' || cur>='a'+base-10)
566 break;
567 v = cur-'a'+10;
570 if(negative)
571 v = -v;
573 nptr++;
575 if(!negative && (ret>MSVCRT_I64_MAX/base || ret*base>MSVCRT_I64_MAX-v)) {
576 ret = MSVCRT_I64_MAX;
577 *MSVCRT__errno() = MSVCRT_ERANGE;
578 } else if(negative && (ret<MSVCRT_I64_MIN/base || ret*base<MSVCRT_I64_MIN-v)) {
579 ret = MSVCRT_I64_MIN;
580 *MSVCRT__errno() = MSVCRT_ERANGE;
581 } else
582 ret = ret*base + v;
585 if(endptr)
586 *endptr = (char*)nptr;
588 return ret;
591 /*********************************************************************
592 * _strtoi64 (MSVCRT.@)
594 __int64 CDECL MSVCRT_strtoi64(const char *nptr, char **endptr, int base)
596 return MSVCRT_strtoi64_l(nptr, endptr, base, NULL);
599 /*********************************************************************
600 * _strtoui64_l (MSVCRT.@)
602 * FIXME: locale parameter is ignored
604 unsigned __int64 CDECL MSVCRT_strtoui64_l(const char *nptr, char **endptr, int base, MSVCRT__locale_t locale)
606 BOOL negative = FALSE;
607 unsigned __int64 ret = 0;
609 TRACE("(%s %p %d %p)\n", nptr, endptr, base, locale);
611 if (!MSVCRT_CHECK_PMT(nptr != NULL) || !MSVCRT_CHECK_PMT(base == 0 || base >= 2) ||
612 !MSVCRT_CHECK_PMT(base <= 36)) {
613 return 0;
616 while(isspace(*nptr)) nptr++;
618 if(*nptr == '-') {
619 negative = TRUE;
620 nptr++;
621 } else if(*nptr == '+')
622 nptr++;
624 if((base==0 || base==16) && *nptr=='0' && tolower(*(nptr+1))=='x') {
625 base = 16;
626 nptr += 2;
629 if(base == 0) {
630 if(*nptr=='0')
631 base = 8;
632 else
633 base = 10;
636 while(*nptr) {
637 char cur = tolower(*nptr);
638 int v;
640 if(isdigit(cur)) {
641 if(cur >= '0'+base)
642 break;
643 v = *nptr-'0';
644 } else {
645 if(cur<'a' || cur>='a'+base-10)
646 break;
647 v = cur-'a'+10;
650 nptr++;
652 if(ret>MSVCRT_UI64_MAX/base || ret*base>MSVCRT_UI64_MAX-v) {
653 ret = MSVCRT_UI64_MAX;
654 *MSVCRT__errno() = MSVCRT_ERANGE;
655 } else
656 ret = ret*base + v;
659 if(endptr)
660 *endptr = (char*)nptr;
662 return negative ? -ret : ret;
665 /*********************************************************************
666 * _strtoui64 (MSVCRT.@)
668 unsigned __int64 CDECL MSVCRT_strtoui64(const char *nptr, char **endptr, int base)
670 return MSVCRT_strtoui64_l(nptr, endptr, base, NULL);
673 /*********************************************************************
674 * _itoa_s (MSVCRT.@)
676 int CDECL _itoa_s(int value, char *str, MSVCRT_size_t size, int radix)
678 unsigned int val, digit;
679 int is_negative;
680 char buffer[33], *pos;
681 size_t len;
683 if (!MSVCRT_CHECK_PMT(str != NULL) || !MSVCRT_CHECK_PMT(size > 0) ||
684 !MSVCRT_CHECK_PMT(radix >= 2) || !MSVCRT_CHECK_PMT(radix <= 36))
686 if (str && size)
687 str[0] = '\0';
689 *MSVCRT__errno() = MSVCRT_EINVAL;
690 return MSVCRT_EINVAL;
693 if (value < 0 && radix == 10)
695 is_negative = 1;
696 val = -value;
698 else
700 is_negative = 0;
701 val = value;
704 pos = buffer + 32;
705 *pos = '\0';
709 digit = val % radix;
710 val /= radix;
712 if (digit < 10)
713 *--pos = '0' + digit;
714 else
715 *--pos = 'a' + digit - 10;
717 while (val != 0);
719 if (is_negative)
720 *--pos = '-';
722 len = buffer + 33 - pos;
723 if (len > size)
725 size_t i;
726 char *p = str;
728 /* Copy the temporary buffer backwards up to the available number of
729 * characters. Don't copy the negative sign if present. */
731 if (is_negative)
733 p++;
734 size--;
737 for (pos = buffer + 31, i = 0; i < size; i++)
738 *p++ = *pos--;
740 str[0] = '\0';
741 MSVCRT_INVALID_PMT("str[size] is too small");
742 *MSVCRT__errno() = MSVCRT_ERANGE;
743 return MSVCRT_ERANGE;
746 memcpy(str, pos, len);
747 return 0;
750 /*********************************************************************
751 * _itow_s (MSVCRT.@)
753 int CDECL _itow_s(int value, MSVCRT_wchar_t *str, MSVCRT_size_t size, int radix)
755 unsigned int val, digit;
756 int is_negative;
757 MSVCRT_wchar_t buffer[33], *pos;
758 size_t len;
760 if (!MSVCRT_CHECK_PMT(str != NULL) || !MSVCRT_CHECK_PMT(size > 0) ||
761 !MSVCRT_CHECK_PMT(radix >= 2) || !MSVCRT_CHECK_PMT(radix <= 36))
763 if (str && size)
764 str[0] = '\0';
766 *MSVCRT__errno() = MSVCRT_EINVAL;
767 return MSVCRT_EINVAL;
770 if (value < 0 && radix == 10)
772 is_negative = 1;
773 val = -value;
775 else
777 is_negative = 0;
778 val = value;
781 pos = buffer + 32;
782 *pos = '\0';
786 digit = val % radix;
787 val /= radix;
789 if (digit < 10)
790 *--pos = '0' + digit;
791 else
792 *--pos = 'a' + digit - 10;
794 while (val != 0);
796 if (is_negative)
797 *--pos = '-';
799 len = buffer + 33 - pos;
800 if (len > size)
802 size_t i;
803 MSVCRT_wchar_t *p = str;
805 /* Copy the temporary buffer backwards up to the available number of
806 * characters. Don't copy the negative sign if present. */
808 if (is_negative)
810 p++;
811 size--;
814 for (pos = buffer + 31, i = 0; i < size; i++)
815 *p++ = *pos--;
817 MSVCRT_INVALID_PMT("str[size] is too small");
818 str[0] = '\0';
819 *MSVCRT__errno() = MSVCRT_ERANGE;
820 return MSVCRT_ERANGE;
823 memcpy(str, pos, len * sizeof(MSVCRT_wchar_t));
824 return 0;
827 /*********************************************************************
828 * _ui64toa_s (MSVCRT.@)
830 int CDECL MSVCRT__ui64toa_s(unsigned __int64 value, char *str,
831 MSVCRT_size_t size, int radix)
833 char buffer[65], *pos;
834 int digit;
836 if (!MSVCRT_CHECK_PMT(str != NULL) || !MSVCRT_CHECK_PMT(size > 0) ||
837 !MSVCRT_CHECK_PMT(radix>=2) || !MSVCRT_CHECK_PMT(radix<=36)) {
838 *MSVCRT__errno() = MSVCRT_EINVAL;
839 return MSVCRT_EINVAL;
842 pos = buffer+64;
843 *pos = '\0';
845 do {
846 digit = value%radix;
847 value /= radix;
849 if(digit < 10)
850 *--pos = '0'+digit;
851 else
852 *--pos = 'a'+digit-10;
853 }while(value != 0);
855 if(buffer-pos+65 > size) {
856 MSVCRT_INVALID_PMT("str[size] is too small");
857 *MSVCRT__errno() = MSVCRT_EINVAL;
858 return MSVCRT_EINVAL;
861 memcpy(str, pos, buffer-pos+65);
862 return 0;
865 /*********************************************************************
866 * _ultoa_s (MSVCRT.@)
868 int CDECL _ultoa_s(MSVCRT_ulong value, char *str, MSVCRT_size_t size, int radix)
870 MSVCRT_ulong digit;
871 char buffer[33], *pos;
872 size_t len;
874 if (!str || !size || radix < 2 || radix > 36)
876 if (str && size)
877 str[0] = '\0';
879 *MSVCRT__errno() = MSVCRT_EINVAL;
880 return MSVCRT_EINVAL;
883 pos = buffer + 32;
884 *pos = '\0';
888 digit = value % radix;
889 value /= radix;
891 if (digit < 10)
892 *--pos = '0' + digit;
893 else
894 *--pos = 'a' + digit - 10;
896 while (value != 0);
898 len = buffer + 33 - pos;
899 if (len > size)
901 size_t i;
902 char *p = str;
904 /* Copy the temporary buffer backwards up to the available number of
905 * characters. */
907 for (pos = buffer + 31, i = 0; i < size; i++)
908 *p++ = *pos--;
910 str[0] = '\0';
911 *MSVCRT__errno() = MSVCRT_ERANGE;
912 return MSVCRT_ERANGE;
915 memcpy(str, pos, len);
916 return 0;
919 #define I10_OUTPUT_MAX_PREC 21
920 /* Internal structure used by $I10_OUTPUT */
921 struct _I10_OUTPUT_DATA {
922 short pos;
923 char sign;
924 BYTE len;
925 char str[I10_OUTPUT_MAX_PREC+1]; /* add space for '\0' */
928 /*********************************************************************
929 * $I10_OUTPUT (MSVCRT.@)
930 * ld - long double to be printed to data
931 * prec - precision of part, we're interested in
932 * flag - 0 for first prec digits, 1 for fractional part
933 * data - data to be populated
935 * return value
936 * 0 if given double is NaN or INF
937 * 1 otherwise
939 * FIXME
940 * Native sets last byte of data->str to '0' or '9', I don't know what
941 * it means. Current implementation sets it always to '0'.
943 int CDECL MSVCRT_I10_OUTPUT(MSVCRT__LDOUBLE ld, int prec, int flag, struct _I10_OUTPUT_DATA *data)
945 static const char inf_str[] = "1#INF";
946 static const char nan_str[] = "1#QNAN";
948 double d = ld.x;
949 char format[8];
950 char buf[I10_OUTPUT_MAX_PREC+9]; /* 9 = strlen("0.e+0000") + '\0' */
951 char *p;
953 TRACE("(%lf %d %x %p)\n", d, prec, flag, data);
955 if(d<0) {
956 data->sign = '-';
957 d = -d;
958 } else
959 data->sign = ' ';
961 if(isinf(d)) {
962 data->pos = 1;
963 data->len = 5;
964 memcpy(data->str, inf_str, sizeof(inf_str));
966 return 0;
969 if(isnan(d)) {
970 data->pos = 1;
971 data->len = 6;
972 memcpy(data->str, nan_str, sizeof(nan_str));
974 return 0;
977 if(flag&1) {
978 int exp = 1+floor(log10(d));
980 prec += exp;
981 if(exp < 0)
982 prec--;
984 prec--;
986 if(prec+1 > I10_OUTPUT_MAX_PREC)
987 prec = I10_OUTPUT_MAX_PREC-1;
988 else if(prec < 0) {
989 d = 0.0;
990 prec = 0;
993 sprintf(format, "%%.%dle", prec);
994 sprintf(buf, format, d);
996 buf[1] = buf[0];
997 data->pos = atoi(buf+prec+3);
998 if(buf[1] != '0')
999 data->pos++;
1001 for(p = buf+prec+1; p>buf+1 && *p=='0'; p--);
1002 data->len = p-buf;
1004 memcpy(data->str, buf+1, data->len);
1005 data->str[data->len] = '\0';
1007 if(buf[1]!='0' && prec-data->len+1>0)
1008 memcpy(data->str+data->len+1, buf+data->len+1, prec-data->len+1);
1010 return 1;
1012 #undef I10_OUTPUT_MAX_PREC