msvcrt: Added _stricoll_l implementation.
[wine/multimedia.git] / dlls / msvcrt / string.c
blob1e7e89097630524befe958ff6181dd79e4dfe9ca
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 "winnls.h"
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
39 /*********************************************************************
40 * _mbsdup (MSVCRT.@)
41 * _strdup (MSVCRT.@)
43 char* CDECL _strdup(const char* str)
45 if(str)
47 char * ret = MSVCRT_malloc(strlen(str)+1);
48 if (ret) strcpy( ret, str );
49 return ret;
51 else return 0;
54 /*********************************************************************
55 * _strlwr_s_l (MSVCRT.@)
57 int CDECL _strlwr_s_l(char *str, MSVCRT_size_t len, MSVCRT__locale_t locale)
59 char *ptr = str;
61 if(!locale)
62 locale = get_locale();
64 if (!str || !len)
66 *MSVCRT__errno() = MSVCRT_EINVAL;
67 return MSVCRT_EINVAL;
70 while (len && *ptr)
72 len--;
73 ptr++;
76 if (!len)
78 str[0] = '\0';
79 *MSVCRT__errno() = MSVCRT_EINVAL;
80 return MSVCRT_EINVAL;
83 while (*str)
85 *str = MSVCRT__tolower_l(*str, locale);
86 str++;
89 return 0;
92 /*********************************************************************
93 * _strlwr_s (MSVCRT.@)
95 int CDECL _strlwr_s(char *str, MSVCRT_size_t len)
97 return _strlwr_s_l(str, len, NULL);
100 /*********************************************************************
101 * _strlwr_l (MSVCRT.@)
103 int CDECL _strlwr_l(char *str, MSVCRT__locale_t locale)
105 return _strlwr_s_l(str, -1, locale);
108 /*********************************************************************
109 * _strlwr (MSVCRT.@)
111 int CDECL _strlwr(char *str)
113 return _strlwr_s_l(str, -1, NULL);
116 /*********************************************************************
117 * _strupr_s_l (MSVCRT.@)
119 int CDECL _strupr_s_l(char *str, MSVCRT_size_t len, MSVCRT__locale_t locale)
121 char *ptr = str;
123 if(!locale)
124 locale = get_locale();
126 if (!str || !len)
128 *MSVCRT__errno() = MSVCRT_EINVAL;
129 return MSVCRT_EINVAL;
132 while (len && *ptr)
134 len--;
135 ptr++;
138 if (!len)
140 str[0] = '\0';
141 *MSVCRT__errno() = MSVCRT_EINVAL;
142 return MSVCRT_EINVAL;
145 while (*str)
147 *str = MSVCRT__toupper_l(*str, locale);
148 str++;
151 return 0;
154 /*********************************************************************
155 * _strupr_s (MSVCRT.@)
157 int CDECL _strupr_s(char *str, MSVCRT_size_t len)
159 return _strupr_s_l(str, len, NULL);
162 /*********************************************************************
163 * _strupr_l (MSVCRT.@)
165 int CDECL _strupr_l(char *str, MSVCRT__locale_t locale)
167 return _strupr_s_l(str, -1, locale);
170 /*********************************************************************
171 * _strupr (MSVCRT.@)
173 int CDECL _strupr(char *str)
175 return _strupr_s_l(str, -1, NULL);
178 /*********************************************************************
179 * _strnset (MSVCRT.@)
181 char* CDECL MSVCRT__strnset(char* str, int value, MSVCRT_size_t len)
183 if (len > 0 && str)
184 while (*str && len--)
185 *str++ = value;
186 return str;
189 /*********************************************************************
190 * _strrev (MSVCRT.@)
192 char* CDECL _strrev(char* str)
194 char * p1;
195 char * p2;
197 if (str && *str)
198 for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2)
200 *p1 ^= *p2;
201 *p2 ^= *p1;
202 *p1 ^= *p2;
205 return str;
208 /*********************************************************************
209 * _strset (MSVCRT.@)
211 char* CDECL _strset(char* str, int value)
213 char *ptr = str;
214 while (*ptr)
215 *ptr++ = value;
217 return str;
220 /*********************************************************************
221 * strtok (MSVCRT.@)
223 char * CDECL MSVCRT_strtok( char *str, const char *delim )
225 thread_data_t *data = msvcrt_get_thread_data();
226 char *ret;
228 if (!str)
229 if (!(str = data->strtok_next)) return NULL;
231 while (*str && strchr( delim, *str )) str++;
232 if (!*str) return NULL;
233 ret = str++;
234 while (*str && !strchr( delim, *str )) str++;
235 if (*str) *str++ = 0;
236 data->strtok_next = str;
237 return ret;
240 /*********************************************************************
241 * strtok_s (MSVCRT.@)
243 char * CDECL MSVCRT_strtok_s(char *str, const char *delim, char **ctx)
245 if (!MSVCRT_CHECK_PMT(delim != NULL) || !MSVCRT_CHECK_PMT(ctx != NULL) ||
246 !MSVCRT_CHECK_PMT(str != NULL || *ctx != NULL)) {
247 *MSVCRT__errno() = MSVCRT_EINVAL;
248 return NULL;
251 if(!str)
252 str = *ctx;
254 while(*str && strchr(delim, *str))
255 str++;
256 if(!*str)
257 return NULL;
259 *ctx = str+1;
260 while(**ctx && !strchr(delim, **ctx))
261 (*ctx)++;
262 if(**ctx)
263 *(*ctx)++ = 0;
265 return str;
268 /*********************************************************************
269 * _swab (MSVCRT.@)
271 void CDECL MSVCRT__swab(char* src, char* dst, int len)
273 if (len > 1)
275 len = (unsigned)len >> 1;
277 while (len--) {
278 char s0 = src[0];
279 char s1 = src[1];
280 *dst++ = s1;
281 *dst++ = s0;
282 src = src + 2;
287 /*********************************************************************
288 * strtod_l (MSVCRT.@)
290 double CDECL MSVCRT_strtod_l( const char *str, char **end, MSVCRT__locale_t locale)
292 unsigned __int64 d=0, hlp;
293 unsigned fpcontrol;
294 int exp=0, sign=1;
295 const char *p;
296 double ret;
297 BOOL found_digit = FALSE;
299 if (!MSVCRT_CHECK_PMT(str != NULL)) {
300 *MSVCRT__errno() = MSVCRT_EINVAL;
301 return 0;
304 if(!locale)
305 locale = get_locale();
307 /* FIXME: use *_l functions */
308 p = str;
309 while(isspace(*p))
310 p++;
312 if(*p == '-') {
313 sign = -1;
314 p++;
315 } else if(*p == '+')
316 p++;
318 while(isdigit(*p)) {
319 found_digit = TRUE;
320 hlp = d*10+*(p++)-'0';
321 if(d>MSVCRT_UI64_MAX/10 || hlp<d) {
322 exp++;
323 break;
324 } else
325 d = hlp;
327 while(isdigit(*p)) {
328 exp++;
329 p++;
332 if(*p == *locale->locinfo->lconv->decimal_point)
333 p++;
335 while(isdigit(*p)) {
336 found_digit = TRUE;
337 hlp = d*10+*(p++)-'0';
338 if(d>MSVCRT_UI64_MAX/10 || hlp<d)
339 break;
341 d = hlp;
342 exp--;
344 while(isdigit(*p))
345 p++;
347 if(!found_digit) {
348 if(end)
349 *end = (char*)str;
350 return 0.0;
353 if(*p=='e' || *p=='E' || *p=='d' || *p=='D') {
354 int e=0, s=1;
356 p++;
357 if(*p == '-') {
358 s = -1;
359 p++;
360 } else if(*p == '+')
361 p++;
363 if(isdigit(*p)) {
364 while(isdigit(*p)) {
365 if(e>INT_MAX/10 || (e=e*10+*p-'0')<0)
366 e = INT_MAX;
367 p++;
369 e *= s;
371 if(exp<0 && e<0 && exp+e>=0) exp = INT_MIN;
372 else if(exp>0 && e>0 && exp+e<0) exp = INT_MAX;
373 else exp += e;
374 } else {
375 if(*p=='-' || *p=='+')
376 p--;
377 p--;
381 fpcontrol = _control87(0, 0);
382 _control87(MSVCRT__EM_DENORMAL|MSVCRT__EM_INVALID|MSVCRT__EM_ZERODIVIDE
383 |MSVCRT__EM_OVERFLOW|MSVCRT__EM_UNDERFLOW|MSVCRT__EM_INEXACT, 0xffffffff);
385 if(exp>0)
386 ret = (double)sign*d*pow(10, exp);
387 else
388 ret = (double)sign*d/pow(10, -exp);
390 _control87(fpcontrol, 0xffffffff);
392 if((d && ret==0.0) || isinf(ret))
393 *MSVCRT__errno() = MSVCRT_ERANGE;
395 if(end)
396 *end = (char*)p;
398 return ret;
401 /*********************************************************************
402 * strtod (MSVCRT.@)
404 double CDECL MSVCRT_strtod( const char *str, char **end )
406 return MSVCRT_strtod_l( str, end, NULL );
409 /*********************************************************************
410 * atof (MSVCRT.@)
412 double CDECL MSVCRT_atof( const char *str )
414 return MSVCRT_strtod_l(str, NULL, NULL);
417 /*********************************************************************
418 * _atof_l (MSVCRT.@)
420 double CDECL MSVCRT__atof_l( const char *str, MSVCRT__locale_t locale)
422 return MSVCRT_strtod_l(str, NULL, locale);
425 /*********************************************************************
426 * _atoflt_l (MSVCRT.@)
428 int CDECL MSVCRT__atoflt_l( MSVCRT__CRT_FLOAT *value, char *str, MSVCRT__locale_t locale)
430 unsigned __int64 d=0, hlp;
431 unsigned fpcontrol;
432 int exp=0, sign=1;
433 const char *p;
434 int ret=0;
435 BOOL found_digit = FALSE;
437 if(!locale)
438 locale = get_locale();
440 /* FIXME: use *_l functions */
441 p = str;
442 while(isspace(*p))
443 p++;
445 if(*p == '-') {
446 sign = -1;
447 p++;
448 } else if(*p == '+')
449 p++;
451 while(isdigit(*p)) {
452 found_digit = TRUE;
453 hlp = d*10+*(p++)-'0';
454 if(d>MSVCRT_UI64_MAX/10 || hlp<d) {
455 exp++;
456 break;
457 } else
458 d = hlp;
460 while(isdigit(*p)) {
461 exp++;
462 p++;
465 if(*p == *locale->locinfo->lconv->decimal_point)
466 p++;
468 while(isdigit(*p)) {
469 found_digit = TRUE;
470 hlp = d*10+*(p++)-'0';
471 if(d>MSVCRT_UI64_MAX/10 || hlp<d)
472 break;
474 d = hlp;
475 exp--;
477 while(isdigit(*p))
478 p++;
480 if(!found_digit) {
481 value->f = 0.0;
482 return 0;
485 if(*p=='e' || *p=='E' || *p=='d' || *p=='D') {
486 int e=0, s=1;
488 p++;
489 if(*p == '-') {
490 s = -1;
491 p++;
492 } else if(*p == '+')
493 p++;
495 if(isdigit(*p)) {
496 while(isdigit(*p)) {
497 if(e>INT_MAX/10 || (e=e*10+*p-'0')<0)
498 e = INT_MAX;
499 p++;
501 e *= s;
503 if(exp<0 && e<0 && exp+e>=0) exp = INT_MIN;
504 else if(exp>0 && e>0 && exp+e<0) exp = INT_MAX;
505 else exp += e;
506 } else {
507 if(*p=='-' || *p=='+')
508 p--;
509 p--;
513 fpcontrol = _control87(0, 0);
514 _control87(MSVCRT__EM_DENORMAL|MSVCRT__EM_INVALID|MSVCRT__EM_ZERODIVIDE
515 |MSVCRT__EM_OVERFLOW|MSVCRT__EM_UNDERFLOW|MSVCRT__EM_INEXACT, 0xffffffff);
517 if(exp>0)
518 value->f = (float)sign*d*powf(10, exp);
519 else
520 value->f = (float)sign*d/powf(10, -exp);
522 _control87(fpcontrol, 0xffffffff);
524 if((d && value->f==0.0) || isinf(value->f))
525 ret = exp > 0 ? MSVCRT__OVERFLOW : MSVCRT__UNDERFLOW;
527 return ret;
530 /*********************************************************************
531 * _strcoll_l (MSVCRT.@)
533 int CDECL MSVCRT_strcoll_l( const char* str1, const char* str2, MSVCRT__locale_t locale )
535 if(!locale)
536 locale = get_locale();
538 return CompareStringA(locale->locinfo->lc_handle[MSVCRT_LC_CTYPE], 0, str1, -1, str2, -1)-2;
541 /*********************************************************************
542 * strcoll (MSVCRT.@)
544 int CDECL MSVCRT_strcoll( const char* str1, const char* str2 )
546 return MSVCRT_strcoll_l(str1, str2, NULL);
549 /*********************************************************************
550 * _stricoll_l (MSVCRT.@)
552 int CDECL MSVCRT__stricoll_l( const char* str1, const char* str2, MSVCRT__locale_t locale )
554 if(!locale)
555 locale = get_locale();
557 return CompareStringA(locale->locinfo->lc_handle[MSVCRT_LC_CTYPE], NORM_IGNORECASE,
558 str1, -1, str2, -1)-2;
561 /*********************************************************************
562 * _stricoll (MSVCRT.@)
564 int CDECL MSVCRT__stricoll( const char* str1, const char* str2 )
566 return MSVCRT__stricoll_l(str1, str2, NULL);
569 /*********************************************************************
570 * strcpy_s (MSVCRT.@)
572 int CDECL MSVCRT_strcpy_s( char* dst, MSVCRT_size_t elem, const char* src )
574 MSVCRT_size_t i;
575 if(!elem) return MSVCRT_EINVAL;
576 if(!dst) return MSVCRT_EINVAL;
577 if(!src)
579 dst[0] = '\0';
580 return MSVCRT_EINVAL;
583 for(i = 0; i < elem; i++)
585 if((dst[i] = src[i]) == '\0') return 0;
587 dst[0] = '\0';
588 return MSVCRT_ERANGE;
591 /*********************************************************************
592 * strcat_s (MSVCRT.@)
594 int CDECL MSVCRT_strcat_s( char* dst, MSVCRT_size_t elem, const char* src )
596 MSVCRT_size_t i, j;
597 if(!dst) return MSVCRT_EINVAL;
598 if(elem == 0) return MSVCRT_EINVAL;
599 if(!src)
601 dst[0] = '\0';
602 return MSVCRT_EINVAL;
605 for(i = 0; i < elem; i++)
607 if(dst[i] == '\0')
609 for(j = 0; (j + i) < elem; j++)
611 if((dst[j + i] = src[j]) == '\0') return 0;
615 /* Set the first element to 0, not the first element after the skipped part */
616 dst[0] = '\0';
617 return MSVCRT_ERANGE;
620 /*********************************************************************
621 * strncat_s (MSVCRT.@)
623 int CDECL MSVCRT_strncat_s( char* dst, MSVCRT_size_t elem, const char* src, MSVCRT_size_t count )
625 MSVCRT_size_t i, j;
626 if(!MSVCRT_CHECK_PMT(dst != 0) || !MSVCRT_CHECK_PMT(elem != 0))
627 return MSVCRT_EINVAL;
628 if(!MSVCRT_CHECK_PMT(src != 0))
630 dst[0] = '\0';
631 return MSVCRT_EINVAL;
634 for(i = 0; i < elem; i++)
636 if(dst[i] == '\0')
638 for(j = 0; (j + i) < elem; j++)
640 if(count == MSVCRT__TRUNCATE && j + i == elem - 1)
642 dst[j + i] = '\0';
643 return MSVCRT_STRUNCATE;
645 if(j == count || (dst[j + i] = src[j]) == '\0')
647 dst[j + i] = '\0';
648 return 0;
653 /* Set the first element to 0, not the first element after the skipped part */
654 dst[0] = '\0';
655 return MSVCRT_ERANGE;
658 /*********************************************************************
659 * strxfrm (MSVCRT.@)
661 MSVCRT_size_t CDECL MSVCRT_strxfrm( char *dest, const char *src, MSVCRT_size_t len )
663 /* FIXME: handle Windows locale */
664 return strxfrm( dest, src, len );
667 /********************************************************************
668 * _atoldbl (MSVCRT.@)
670 int CDECL MSVCRT__atoldbl(MSVCRT__LDOUBLE *value, const char *str)
672 /* FIXME needs error checking for huge/small values */
673 #ifdef HAVE_STRTOLD
674 long double ld;
675 TRACE("str %s value %p\n",str,value);
676 ld = strtold(str,0);
677 memcpy(value, &ld, 10);
678 #else
679 FIXME("stub, str %s value %p\n",str,value);
680 #endif
681 return 0;
684 /********************************************************************
685 * __STRINGTOLD (MSVCRT.@)
687 int CDECL __STRINGTOLD( MSVCRT__LDOUBLE *value, char **endptr, const char *str, int flags )
689 #ifdef HAVE_STRTOLD
690 long double ld;
691 FIXME("%p %p %s %x partial stub\n", value, endptr, str, flags );
692 ld = strtold(str,0);
693 memcpy(value, &ld, 10);
694 #else
695 FIXME("%p %p %s %x stub\n", value, endptr, str, flags );
696 #endif
697 return 0;
700 /******************************************************************
701 * strtol (MSVCRT.@)
703 MSVCRT_long CDECL MSVCRT_strtol(const char* nptr, char** end, int base)
705 /* wrapper to forward libc error code to msvcrt's error codes */
706 long ret;
708 errno = 0;
709 ret = strtol(nptr, end, base);
710 switch (errno)
712 case ERANGE: *MSVCRT__errno() = MSVCRT_ERANGE; break;
713 case EINVAL: *MSVCRT__errno() = MSVCRT_EINVAL; break;
714 default:
715 /* cope with the fact that we may use 64bit long integers on libc
716 * while msvcrt always uses 32bit long integers
718 if (ret > MSVCRT_LONG_MAX)
720 ret = MSVCRT_LONG_MAX;
721 *MSVCRT__errno() = MSVCRT_ERANGE;
723 else if (ret < -MSVCRT_LONG_MAX - 1)
725 ret = -MSVCRT_LONG_MAX - 1;
726 *MSVCRT__errno() = MSVCRT_ERANGE;
728 break;
731 return ret;
734 /******************************************************************
735 * strtoul (MSVCRT.@)
737 MSVCRT_ulong CDECL MSVCRT_strtoul(const char* nptr, char** end, int base)
739 /* wrapper to forward libc error code to msvcrt's error codes */
740 unsigned long ret;
742 errno = 0;
743 ret = strtoul(nptr, end, base);
744 switch (errno)
746 case ERANGE: *MSVCRT__errno() = MSVCRT_ERANGE; break;
747 case EINVAL: *MSVCRT__errno() = MSVCRT_EINVAL; break;
748 default:
749 /* cope with the fact that we may use 64bit long integers on libc
750 * while msvcrt always uses 32bit long integers
752 if (ret > MSVCRT_ULONG_MAX)
754 ret = MSVCRT_ULONG_MAX;
755 *MSVCRT__errno() = MSVCRT_ERANGE;
757 break;
760 return ret;
763 /******************************************************************
764 * strnlen (MSVCRT.@)
766 MSVCRT_size_t CDECL MSVCRT_strnlen(const char *s, MSVCRT_size_t maxlen)
768 MSVCRT_size_t i;
770 for(i=0; i<maxlen; i++)
771 if(!s[i]) break;
773 return i;
776 /*********************************************************************
777 * _strtoi64_l (MSVCRT.@)
779 * FIXME: locale parameter is ignored
781 __int64 CDECL MSVCRT_strtoi64_l(const char *nptr, char **endptr, int base, MSVCRT__locale_t locale)
783 BOOL negative = FALSE;
784 __int64 ret = 0;
786 TRACE("(%s %p %d %p)\n", nptr, endptr, base, locale);
788 if (!MSVCRT_CHECK_PMT(nptr != NULL) || !MSVCRT_CHECK_PMT(base == 0 || base >= 2) ||
789 !MSVCRT_CHECK_PMT(base <= 36)) {
790 return 0;
793 while(isspace(*nptr)) nptr++;
795 if(*nptr == '-') {
796 negative = TRUE;
797 nptr++;
798 } else if(*nptr == '+')
799 nptr++;
801 if((base==0 || base==16) && *nptr=='0' && tolower(*(nptr+1))=='x') {
802 base = 16;
803 nptr += 2;
806 if(base == 0) {
807 if(*nptr=='0')
808 base = 8;
809 else
810 base = 10;
813 while(*nptr) {
814 char cur = tolower(*nptr);
815 int v;
817 if(isdigit(cur)) {
818 if(cur >= '0'+base)
819 break;
820 v = cur-'0';
821 } else {
822 if(cur<'a' || cur>='a'+base-10)
823 break;
824 v = cur-'a'+10;
827 if(negative)
828 v = -v;
830 nptr++;
832 if(!negative && (ret>MSVCRT_I64_MAX/base || ret*base>MSVCRT_I64_MAX-v)) {
833 ret = MSVCRT_I64_MAX;
834 *MSVCRT__errno() = MSVCRT_ERANGE;
835 } else if(negative && (ret<MSVCRT_I64_MIN/base || ret*base<MSVCRT_I64_MIN-v)) {
836 ret = MSVCRT_I64_MIN;
837 *MSVCRT__errno() = MSVCRT_ERANGE;
838 } else
839 ret = ret*base + v;
842 if(endptr)
843 *endptr = (char*)nptr;
845 return ret;
848 /*********************************************************************
849 * _strtoi64 (MSVCRT.@)
851 __int64 CDECL MSVCRT_strtoi64(const char *nptr, char **endptr, int base)
853 return MSVCRT_strtoi64_l(nptr, endptr, base, NULL);
856 /*********************************************************************
857 * _strtoui64_l (MSVCRT.@)
859 * FIXME: locale parameter is ignored
861 unsigned __int64 CDECL MSVCRT_strtoui64_l(const char *nptr, char **endptr, int base, MSVCRT__locale_t locale)
863 BOOL negative = FALSE;
864 unsigned __int64 ret = 0;
866 TRACE("(%s %p %d %p)\n", nptr, endptr, base, locale);
868 if (!MSVCRT_CHECK_PMT(nptr != NULL) || !MSVCRT_CHECK_PMT(base == 0 || base >= 2) ||
869 !MSVCRT_CHECK_PMT(base <= 36)) {
870 return 0;
873 while(isspace(*nptr)) nptr++;
875 if(*nptr == '-') {
876 negative = TRUE;
877 nptr++;
878 } else if(*nptr == '+')
879 nptr++;
881 if((base==0 || base==16) && *nptr=='0' && tolower(*(nptr+1))=='x') {
882 base = 16;
883 nptr += 2;
886 if(base == 0) {
887 if(*nptr=='0')
888 base = 8;
889 else
890 base = 10;
893 while(*nptr) {
894 char cur = tolower(*nptr);
895 int v;
897 if(isdigit(cur)) {
898 if(cur >= '0'+base)
899 break;
900 v = *nptr-'0';
901 } else {
902 if(cur<'a' || cur>='a'+base-10)
903 break;
904 v = cur-'a'+10;
907 nptr++;
909 if(ret>MSVCRT_UI64_MAX/base || ret*base>MSVCRT_UI64_MAX-v) {
910 ret = MSVCRT_UI64_MAX;
911 *MSVCRT__errno() = MSVCRT_ERANGE;
912 } else
913 ret = ret*base + v;
916 if(endptr)
917 *endptr = (char*)nptr;
919 return negative ? -ret : ret;
922 /*********************************************************************
923 * _strtoui64 (MSVCRT.@)
925 unsigned __int64 CDECL MSVCRT_strtoui64(const char *nptr, char **endptr, int base)
927 return MSVCRT_strtoui64_l(nptr, endptr, base, NULL);
930 /*********************************************************************
931 * _ltoa_s (MSVCRT.@)
933 int CDECL _ltoa_s(MSVCRT_long value, char *str, MSVCRT_size_t size, int radix)
935 MSVCRT_ulong val;
936 unsigned int digit;
937 int is_negative;
938 char buffer[33], *pos;
939 size_t len;
941 if (!MSVCRT_CHECK_PMT(str != NULL) || !MSVCRT_CHECK_PMT(size > 0) ||
942 !MSVCRT_CHECK_PMT(radix >= 2) || !MSVCRT_CHECK_PMT(radix <= 36))
944 if (str && size)
945 str[0] = '\0';
947 *MSVCRT__errno() = MSVCRT_EINVAL;
948 return MSVCRT_EINVAL;
951 if (value < 0 && radix == 10)
953 is_negative = 1;
954 val = -value;
956 else
958 is_negative = 0;
959 val = value;
962 pos = buffer + 32;
963 *pos = '\0';
967 digit = val % radix;
968 val /= radix;
970 if (digit < 10)
971 *--pos = '0' + digit;
972 else
973 *--pos = 'a' + digit - 10;
975 while (val != 0);
977 if (is_negative)
978 *--pos = '-';
980 len = buffer + 33 - pos;
981 if (len > size)
983 size_t i;
984 char *p = str;
986 /* Copy the temporary buffer backwards up to the available number of
987 * characters. Don't copy the negative sign if present. */
989 if (is_negative)
991 p++;
992 size--;
995 for (pos = buffer + 31, i = 0; i < size; i++)
996 *p++ = *pos--;
998 str[0] = '\0';
999 MSVCRT_INVALID_PMT("str[size] is too small");
1000 *MSVCRT__errno() = MSVCRT_ERANGE;
1001 return MSVCRT_ERANGE;
1004 memcpy(str, pos, len);
1005 return 0;
1008 /*********************************************************************
1009 * _ltow_s (MSVCRT.@)
1011 int CDECL _ltow_s(MSVCRT_long value, MSVCRT_wchar_t *str, MSVCRT_size_t size, int radix)
1013 MSVCRT_ulong val;
1014 unsigned int digit;
1015 int is_negative;
1016 MSVCRT_wchar_t buffer[33], *pos;
1017 size_t len;
1019 if (!MSVCRT_CHECK_PMT(str != NULL) || !MSVCRT_CHECK_PMT(size > 0) ||
1020 !MSVCRT_CHECK_PMT(radix >= 2) || !MSVCRT_CHECK_PMT(radix <= 36))
1022 if (str && size)
1023 str[0] = '\0';
1025 *MSVCRT__errno() = MSVCRT_EINVAL;
1026 return MSVCRT_EINVAL;
1029 if (value < 0 && radix == 10)
1031 is_negative = 1;
1032 val = -value;
1034 else
1036 is_negative = 0;
1037 val = value;
1040 pos = buffer + 32;
1041 *pos = '\0';
1045 digit = val % radix;
1046 val /= radix;
1048 if (digit < 10)
1049 *--pos = '0' + digit;
1050 else
1051 *--pos = 'a' + digit - 10;
1053 while (val != 0);
1055 if (is_negative)
1056 *--pos = '-';
1058 len = buffer + 33 - pos;
1059 if (len > size)
1061 size_t i;
1062 MSVCRT_wchar_t *p = str;
1064 /* Copy the temporary buffer backwards up to the available number of
1065 * characters. Don't copy the negative sign if present. */
1067 if (is_negative)
1069 p++;
1070 size--;
1073 for (pos = buffer + 31, i = 0; i < size; i++)
1074 *p++ = *pos--;
1076 MSVCRT_INVALID_PMT("str[size] is too small");
1077 str[0] = '\0';
1078 *MSVCRT__errno() = MSVCRT_ERANGE;
1079 return MSVCRT_ERANGE;
1082 memcpy(str, pos, len * sizeof(MSVCRT_wchar_t));
1083 return 0;
1086 /*********************************************************************
1087 * _itoa_s (MSVCRT.@)
1089 int CDECL _itoa_s(int value, char *str, MSVCRT_size_t size, int radix)
1091 return _ltoa_s(value, str, size, radix);
1094 /*********************************************************************
1095 * _itow_s (MSVCRT.@)
1097 int CDECL _itow_s(int value, MSVCRT_wchar_t *str, MSVCRT_size_t size, int radix)
1099 return _ltow_s(value, str, size, radix);
1102 /*********************************************************************
1103 * _ui64toa_s (MSVCRT.@)
1105 int CDECL MSVCRT__ui64toa_s(unsigned __int64 value, char *str,
1106 MSVCRT_size_t size, int radix)
1108 char buffer[65], *pos;
1109 int digit;
1111 if (!MSVCRT_CHECK_PMT(str != NULL) || !MSVCRT_CHECK_PMT(size > 0) ||
1112 !MSVCRT_CHECK_PMT(radix>=2) || !MSVCRT_CHECK_PMT(radix<=36)) {
1113 *MSVCRT__errno() = MSVCRT_EINVAL;
1114 return MSVCRT_EINVAL;
1117 pos = buffer+64;
1118 *pos = '\0';
1120 do {
1121 digit = value%radix;
1122 value /= radix;
1124 if(digit < 10)
1125 *--pos = '0'+digit;
1126 else
1127 *--pos = 'a'+digit-10;
1128 }while(value != 0);
1130 if(buffer-pos+65 > size) {
1131 MSVCRT_INVALID_PMT("str[size] is too small");
1132 *MSVCRT__errno() = MSVCRT_EINVAL;
1133 return MSVCRT_EINVAL;
1136 memcpy(str, pos, buffer-pos+65);
1137 return 0;
1140 /*********************************************************************
1141 * _ui64tow_s (MSVCRT.@)
1143 int CDECL MSVCRT__ui64tow_s( unsigned __int64 value, MSVCRT_wchar_t *str,
1144 MSVCRT_size_t size, int radix )
1146 MSVCRT_wchar_t buffer[65], *pos;
1147 int digit;
1149 if (!MSVCRT_CHECK_PMT(str != NULL) || !MSVCRT_CHECK_PMT(size > 0) ||
1150 !MSVCRT_CHECK_PMT(radix>=2) || !MSVCRT_CHECK_PMT(radix<=36)) {
1151 *MSVCRT__errno() = MSVCRT_EINVAL;
1152 return MSVCRT_EINVAL;
1155 pos = &buffer[64];
1156 *pos = '\0';
1158 do {
1159 digit = value % radix;
1160 value = value / radix;
1161 if (digit < 10)
1162 *--pos = '0' + digit;
1163 else
1164 *--pos = 'a' + digit - 10;
1165 } while (value != 0);
1167 if(buffer-pos+65 > size) {
1168 MSVCRT_INVALID_PMT("str[size] is too small");
1169 *MSVCRT__errno() = MSVCRT_EINVAL;
1170 return MSVCRT_EINVAL;
1173 memcpy(str, pos, buffer-pos+65);
1174 return 0;
1177 /*********************************************************************
1178 * _ultoa_s (MSVCRT.@)
1180 int CDECL _ultoa_s(MSVCRT_ulong value, char *str, MSVCRT_size_t size, int radix)
1182 MSVCRT_ulong digit;
1183 char buffer[33], *pos;
1184 size_t len;
1186 if (!str || !size || radix < 2 || radix > 36)
1188 if (str && size)
1189 str[0] = '\0';
1191 *MSVCRT__errno() = MSVCRT_EINVAL;
1192 return MSVCRT_EINVAL;
1195 pos = buffer + 32;
1196 *pos = '\0';
1200 digit = value % radix;
1201 value /= radix;
1203 if (digit < 10)
1204 *--pos = '0' + digit;
1205 else
1206 *--pos = 'a' + digit - 10;
1208 while (value != 0);
1210 len = buffer + 33 - pos;
1211 if (len > size)
1213 size_t i;
1214 char *p = str;
1216 /* Copy the temporary buffer backwards up to the available number of
1217 * characters. */
1219 for (pos = buffer + 31, i = 0; i < size; i++)
1220 *p++ = *pos--;
1222 str[0] = '\0';
1223 *MSVCRT__errno() = MSVCRT_ERANGE;
1224 return MSVCRT_ERANGE;
1227 memcpy(str, pos, len);
1228 return 0;
1231 /*********************************************************************
1232 * _i64toa_s (MSVCRT.@)
1234 int CDECL _i64toa_s(__int64 value, char *str, MSVCRT_size_t size, int radix)
1236 unsigned __int64 val;
1237 unsigned int digit;
1238 int is_negative;
1239 char buffer[65], *pos;
1240 size_t len;
1242 if (!MSVCRT_CHECK_PMT(str != NULL) || !MSVCRT_CHECK_PMT(size > 0) ||
1243 !MSVCRT_CHECK_PMT(radix >= 2) || !MSVCRT_CHECK_PMT(radix <= 36))
1245 if (str && size)
1246 str[0] = '\0';
1248 *MSVCRT__errno() = MSVCRT_EINVAL;
1249 return MSVCRT_EINVAL;
1252 if (value < 0 && radix == 10)
1254 is_negative = 1;
1255 val = -value;
1257 else
1259 is_negative = 0;
1260 val = value;
1263 pos = buffer + 64;
1264 *pos = '\0';
1268 digit = val % radix;
1269 val /= radix;
1271 if (digit < 10)
1272 *--pos = '0' + digit;
1273 else
1274 *--pos = 'a' + digit - 10;
1276 while (val != 0);
1278 if (is_negative)
1279 *--pos = '-';
1281 len = buffer + 65 - pos;
1282 if (len > size)
1284 size_t i;
1285 char *p = str;
1287 /* Copy the temporary buffer backwards up to the available number of
1288 * characters. Don't copy the negative sign if present. */
1290 if (is_negative)
1292 p++;
1293 size--;
1296 for (pos = buffer + 63, i = 0; i < size; i++)
1297 *p++ = *pos--;
1299 str[0] = '\0';
1300 MSVCRT_INVALID_PMT("str[size] is too small");
1301 *MSVCRT__errno() = MSVCRT_ERANGE;
1302 return MSVCRT_ERANGE;
1305 memcpy(str, pos, len);
1306 return 0;
1309 /*********************************************************************
1310 * _i64tow_s (MSVCRT.@)
1312 int CDECL _i64tow_s(__int64 value, MSVCRT_wchar_t *str, MSVCRT_size_t size, int radix)
1314 unsigned __int64 val;
1315 unsigned int digit;
1316 int is_negative;
1317 MSVCRT_wchar_t buffer[65], *pos;
1318 size_t len;
1320 if (!MSVCRT_CHECK_PMT(str != NULL) || !MSVCRT_CHECK_PMT(size > 0) ||
1321 !MSVCRT_CHECK_PMT(radix >= 2) || !MSVCRT_CHECK_PMT(radix <= 36))
1323 if (str && size)
1324 str[0] = '\0';
1326 *MSVCRT__errno() = MSVCRT_EINVAL;
1327 return MSVCRT_EINVAL;
1330 if (value < 0 && radix == 10)
1332 is_negative = 1;
1333 val = -value;
1335 else
1337 is_negative = 0;
1338 val = value;
1341 pos = buffer + 64;
1342 *pos = '\0';
1346 digit = val % radix;
1347 val /= radix;
1349 if (digit < 10)
1350 *--pos = '0' + digit;
1351 else
1352 *--pos = 'a' + digit - 10;
1354 while (val != 0);
1356 if (is_negative)
1357 *--pos = '-';
1359 len = buffer + 65 - pos;
1360 if (len > size)
1362 size_t i;
1363 MSVCRT_wchar_t *p = str;
1365 /* Copy the temporary buffer backwards up to the available number of
1366 * characters. Don't copy the negative sign if present. */
1368 if (is_negative)
1370 p++;
1371 size--;
1374 for (pos = buffer + 63, i = 0; i < size; i++)
1375 *p++ = *pos--;
1377 MSVCRT_INVALID_PMT("str[size] is too small");
1378 str[0] = '\0';
1379 *MSVCRT__errno() = MSVCRT_ERANGE;
1380 return MSVCRT_ERANGE;
1383 memcpy(str, pos, len * sizeof(MSVCRT_wchar_t));
1384 return 0;
1387 #define I10_OUTPUT_MAX_PREC 21
1388 /* Internal structure used by $I10_OUTPUT */
1389 struct _I10_OUTPUT_DATA {
1390 short pos;
1391 char sign;
1392 BYTE len;
1393 char str[I10_OUTPUT_MAX_PREC+1]; /* add space for '\0' */
1396 /*********************************************************************
1397 * $I10_OUTPUT (MSVCRT.@)
1398 * ld80 - long double (Intel 80 bit FP in 12 bytes) to be printed to data
1399 * prec - precision of part, we're interested in
1400 * flag - 0 for first prec digits, 1 for fractional part
1401 * data - data to be populated
1403 * return value
1404 * 0 if given double is NaN or INF
1405 * 1 otherwise
1407 * FIXME
1408 * Native sets last byte of data->str to '0' or '9', I don't know what
1409 * it means. Current implementation sets it always to '0'.
1411 int CDECL MSVCRT_I10_OUTPUT(MSVCRT__LDOUBLE ld80, int prec, int flag, struct _I10_OUTPUT_DATA *data)
1413 static const char inf_str[] = "1#INF";
1414 static const char nan_str[] = "1#QNAN";
1416 /* MS' long double type wants 12 bytes for Intel's 80 bit FP format.
1417 * Some UNIX have sizeof(long double) == 16, yet only 80 bit are used.
1418 * Assume long double uses 80 bit FP, never seen 128 bit FP. */
1419 long double ld = 0;
1420 double d;
1421 char format[8];
1422 char buf[I10_OUTPUT_MAX_PREC+9]; /* 9 = strlen("0.e+0000") + '\0' */
1423 char *p;
1425 memcpy(&ld, &ld80, 10);
1426 d = ld;
1427 TRACE("(%lf %d %x %p)\n", d, prec, flag, data);
1429 if(d<0) {
1430 data->sign = '-';
1431 d = -d;
1432 } else
1433 data->sign = ' ';
1435 if(isinf(d)) {
1436 data->pos = 1;
1437 data->len = 5;
1438 memcpy(data->str, inf_str, sizeof(inf_str));
1440 return 0;
1443 if(isnan(d)) {
1444 data->pos = 1;
1445 data->len = 6;
1446 memcpy(data->str, nan_str, sizeof(nan_str));
1448 return 0;
1451 if(flag&1) {
1452 int exp = 1+floor(log10(d));
1454 prec += exp;
1455 if(exp < 0)
1456 prec--;
1458 prec--;
1460 if(prec+1 > I10_OUTPUT_MAX_PREC)
1461 prec = I10_OUTPUT_MAX_PREC-1;
1462 else if(prec < 0) {
1463 d = 0.0;
1464 prec = 0;
1467 sprintf(format, "%%.%dle", prec);
1468 sprintf(buf, format, d);
1470 buf[1] = buf[0];
1471 data->pos = atoi(buf+prec+3);
1472 if(buf[1] != '0')
1473 data->pos++;
1475 for(p = buf+prec+1; p>buf+1 && *p=='0'; p--);
1476 data->len = p-buf;
1478 memcpy(data->str, buf+1, data->len);
1479 data->str[data->len] = '\0';
1481 if(buf[1]!='0' && prec-data->len+1>0)
1482 memcpy(data->str+data->len+1, buf+data->len+1, prec-data->len+1);
1484 return 1;
1486 #undef I10_OUTPUT_MAX_PREC