shdocvw: Update Italian translation.
[wine.git] / dlls / msvcrt / string.c
blobf44e6ebf7e9281a2d60014ef1e687334f918ffa3
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 * _strnset (MSVCRT.@)
56 char* CDECL MSVCRT__strnset(char* str, int value, MSVCRT_size_t len)
58 if (len > 0 && str)
59 while (*str && len--)
60 *str++ = value;
61 return str;
64 /*********************************************************************
65 * _strrev (MSVCRT.@)
67 char* CDECL _strrev(char* str)
69 char * p1;
70 char * p2;
72 if (str && *str)
73 for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2)
75 *p1 ^= *p2;
76 *p2 ^= *p1;
77 *p1 ^= *p2;
80 return str;
83 /*********************************************************************
84 * _strset (MSVCRT.@)
86 char* CDECL _strset(char* str, int value)
88 char *ptr = str;
89 while (*ptr)
90 *ptr++ = value;
92 return str;
95 /*********************************************************************
96 * strtok (MSVCRT.@)
98 char * CDECL MSVCRT_strtok( char *str, const char *delim )
100 thread_data_t *data = msvcrt_get_thread_data();
101 char *ret;
103 if (!str)
104 if (!(str = data->strtok_next)) return NULL;
106 while (*str && strchr( delim, *str )) str++;
107 if (!*str) return NULL;
108 ret = str++;
109 while (*str && !strchr( delim, *str )) str++;
110 if (*str) *str++ = 0;
111 data->strtok_next = str;
112 return ret;
115 /*********************************************************************
116 * strtok_s (MSVCRT.@)
118 char * CDECL MSVCRT_strtok_s(char *str, const char *delim, char **ctx)
120 if(!delim || !ctx || (!str && !*ctx)) {
121 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
122 *MSVCRT__errno() = MSVCRT_EINVAL;
123 return NULL;
126 if(!str)
127 str = *ctx;
129 while(*str && strchr(delim, *str))
130 str++;
131 if(!*str)
132 return NULL;
134 *ctx = str+1;
135 while(**ctx && !strchr(delim, **ctx))
136 (*ctx)++;
137 if(**ctx)
138 *(*ctx)++ = 0;
140 return str;
143 /*********************************************************************
144 * _swab (MSVCRT.@)
146 void CDECL MSVCRT__swab(char* src, char* dst, int len)
148 if (len > 1)
150 len = (unsigned)len >> 1;
152 while (len--) {
153 char s0 = src[0];
154 char s1 = src[1];
155 *dst++ = s1;
156 *dst++ = s0;
157 src = src + 2;
162 /*********************************************************************
163 * strtod_l (MSVCRT.@)
165 double CDECL MSVCRT_strtod_l( const char *str, char **end, MSVCRT__locale_t locale)
167 unsigned __int64 d=0, hlp;
168 unsigned fpcontrol;
169 int exp=0, sign=1;
170 const char *p;
171 double ret;
172 BOOL found_digit = FALSE;
174 if(!str) {
175 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
176 *MSVCRT__errno() = MSVCRT_EINVAL;
177 return 0;
180 if(!locale)
181 locale = get_locale();
183 /* FIXME: use *_l functions */
184 p = str;
185 while(isspace(*p))
186 p++;
188 if(*p == '-') {
189 sign = -1;
190 p++;
191 } else if(*p == '+')
192 p++;
194 while(isdigit(*p)) {
195 found_digit = TRUE;
196 hlp = d*10+*(p++)-'0';
197 if(d>MSVCRT_UI64_MAX/10 || hlp<d) {
198 exp++;
199 break;
200 } else
201 d = hlp;
203 while(isdigit(*p)) {
204 exp++;
205 p++;
208 if(*p == *locale->locinfo->lconv->decimal_point)
209 p++;
211 while(isdigit(*p)) {
212 found_digit = TRUE;
213 hlp = d*10+*(p++)-'0';
214 if(d>MSVCRT_UI64_MAX/10 || hlp<d)
215 break;
217 d = hlp;
218 exp--;
220 while(isdigit(*p))
221 p++;
223 if(!found_digit) {
224 if(end)
225 *end = (char*)str;
226 return 0.0;
229 if(*p=='e' || *p=='E' || *p=='d' || *p=='D') {
230 int e=0, s=1;
232 p++;
233 if(*p == '-') {
234 s = -1;
235 p++;
236 } else if(*p == '+')
237 p++;
239 if(isdigit(*p)) {
240 while(isdigit(*p)) {
241 if(e>INT_MAX/10 || (e=e*10+*p-'0')<0)
242 e = INT_MAX;
243 p++;
245 e *= s;
247 if(exp<0 && e<0 && exp+e>=0) exp = INT_MIN;
248 else if(exp>0 && e>0 && exp+e<0) exp = INT_MAX;
249 else exp += e;
250 } else {
251 if(*p=='-' || *p=='+')
252 p--;
253 p--;
257 fpcontrol = _control87(0, 0);
258 _control87(MSVCRT__EM_DENORMAL|MSVCRT__EM_INVALID|MSVCRT__EM_ZERODIVIDE
259 |MSVCRT__EM_OVERFLOW|MSVCRT__EM_UNDERFLOW|MSVCRT__EM_INEXACT, 0xffffffff);
261 if(exp>0)
262 ret = (double)sign*d*pow(10, exp);
263 else
264 ret = (double)sign*d/pow(10, -exp);
266 _control87(fpcontrol, 0xffffffff);
268 if((d && ret==0.0) || isinf(ret))
269 *MSVCRT__errno() = MSVCRT_ERANGE;
271 if(end)
272 *end = (char*)p;
274 return ret;
277 /*********************************************************************
278 * strtod (MSVCRT.@)
280 double CDECL MSVCRT_strtod( const char *str, char **end )
282 return MSVCRT_strtod_l( str, end, NULL );
285 /*********************************************************************
286 * atof (MSVCRT.@)
288 double CDECL MSVCRT_atof( const char *str )
290 return MSVCRT_strtod_l(str, NULL, NULL);
293 /*********************************************************************
294 * _atof_l (MSVCRT.@)
296 double CDECL MSVCRT__atof_l( const char *str, MSVCRT__locale_t locale)
298 return MSVCRT_strtod_l(str, NULL, locale);
301 /*********************************************************************
302 * strcoll (MSVCRT.@)
304 int CDECL MSVCRT_strcoll( const char* str1, const char* str2 )
306 /* FIXME: handle Windows locale */
307 return strcoll( str1, str2 );
310 /*********************************************************************
311 * strcpy_s (MSVCRT.@)
313 int CDECL MSVCRT_strcpy_s( char* dst, MSVCRT_size_t elem, const char* src )
315 MSVCRT_size_t i;
316 if(!elem) return MSVCRT_EINVAL;
317 if(!dst) return MSVCRT_EINVAL;
318 if(!src)
320 dst[0] = '\0';
321 return MSVCRT_EINVAL;
324 for(i = 0; i < elem; i++)
326 if((dst[i] = src[i]) == '\0') return 0;
328 dst[0] = '\0';
329 return MSVCRT_ERANGE;
332 /*********************************************************************
333 * strcat_s (MSVCRT.@)
335 int CDECL MSVCRT_strcat_s( char* dst, MSVCRT_size_t elem, const char* src )
337 MSVCRT_size_t i, j;
338 if(!dst) return MSVCRT_EINVAL;
339 if(elem == 0) return MSVCRT_EINVAL;
340 if(!src)
342 dst[0] = '\0';
343 return MSVCRT_EINVAL;
346 for(i = 0; i < elem; i++)
348 if(dst[i] == '\0')
350 for(j = 0; (j + i) < elem; j++)
352 if((dst[j + i] = src[j]) == '\0') return 0;
356 /* Set the first element to 0, not the first element after the skipped part */
357 dst[0] = '\0';
358 return MSVCRT_ERANGE;
361 /*********************************************************************
362 * strxfrm (MSVCRT.@)
364 MSVCRT_size_t CDECL MSVCRT_strxfrm( char *dest, const char *src, MSVCRT_size_t len )
366 /* FIXME: handle Windows locale */
367 return strxfrm( dest, src, len );
370 /*********************************************************************
371 * _stricoll (MSVCRT.@)
373 int CDECL MSVCRT__stricoll( const char* str1, const char* str2 )
375 /* FIXME: handle collates */
376 TRACE("str1 %s str2 %s\n", debugstr_a(str1), debugstr_a(str2));
377 return lstrcmpiA( str1, str2 );
380 /********************************************************************
381 * _atoldbl (MSVCRT.@)
383 int CDECL MSVCRT__atoldbl(MSVCRT__LDOUBLE *value, const char *str)
385 /* FIXME needs error checking for huge/small values */
386 #ifdef HAVE_STRTOLD
387 TRACE("str %s value %p\n",str,value);
388 value->x = strtold(str,0);
389 #else
390 FIXME("stub, str %s value %p\n",str,value);
391 #endif
392 return 0;
395 /********************************************************************
396 * __STRINGTOLD (MSVCRT.@)
398 int CDECL __STRINGTOLD( MSVCRT__LDOUBLE *value, char **endptr, const char *str, int flags )
400 #ifdef HAVE_STRTOLD
401 FIXME("%p %p %s %x partial stub\n", value, endptr, str, flags );
402 value->x = strtold(str,endptr);
403 #else
404 FIXME("%p %p %s %x stub\n", value, endptr, str, flags );
405 #endif
406 return 0;
409 /******************************************************************
410 * strtol (MSVCRT.@)
412 MSVCRT_long CDECL MSVCRT_strtol(const char* nptr, char** end, int base)
414 /* wrapper to forward libc error code to msvcrt's error codes */
415 long ret;
417 errno = 0;
418 ret = strtol(nptr, end, base);
419 switch (errno)
421 case ERANGE: *MSVCRT__errno() = MSVCRT_ERANGE; break;
422 case EINVAL: *MSVCRT__errno() = MSVCRT_EINVAL; break;
423 default:
424 /* cope with the fact that we may use 64bit long integers on libc
425 * while msvcrt always uses 32bit long integers
427 if (ret > MSVCRT_LONG_MAX)
429 ret = MSVCRT_LONG_MAX;
430 *MSVCRT__errno() = MSVCRT_ERANGE;
432 else if (ret < -MSVCRT_LONG_MAX - 1)
434 ret = -MSVCRT_LONG_MAX - 1;
435 *MSVCRT__errno() = MSVCRT_ERANGE;
437 break;
440 return ret;
443 /******************************************************************
444 * strtoul (MSVCRT.@)
446 MSVCRT_ulong CDECL MSVCRT_strtoul(const char* nptr, char** end, int base)
448 /* wrapper to forward libc error code to msvcrt's error codes */
449 unsigned long ret;
451 errno = 0;
452 ret = strtoul(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_ULONG_MAX)
463 ret = MSVCRT_ULONG_MAX;
464 *MSVCRT__errno() = MSVCRT_ERANGE;
466 break;
469 return ret;
472 /******************************************************************
473 * strnlen (MSVCRT.@)
475 MSVCRT_size_t CDECL MSVCRT_strnlen(const char *s, MSVCRT_size_t maxlen)
477 MSVCRT_size_t i;
479 for(i=0; i<maxlen; i++)
480 if(!s[i]) break;
482 return i;
485 /*********************************************************************
486 * _strtoi64_l (MSVCRT.@)
488 * FIXME: locale parameter is ignored
490 __int64 CDECL MSVCRT_strtoi64_l(const char *nptr, char **endptr, int base, MSVCRT__locale_t locale)
492 BOOL negative = FALSE;
493 __int64 ret = 0;
495 TRACE("(%s %p %d %p)\n", nptr, endptr, base, locale);
497 if(!nptr || base<0 || base>36 || base==1) {
498 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
499 return 0;
502 while(isspace(*nptr)) nptr++;
504 if(*nptr == '-') {
505 negative = TRUE;
506 nptr++;
507 } else if(*nptr == '+')
508 nptr++;
510 if((base==0 || base==16) && *nptr=='0' && tolower(*(nptr+1))=='x') {
511 base = 16;
512 nptr += 2;
515 if(base == 0) {
516 if(*nptr=='0')
517 base = 8;
518 else
519 base = 10;
522 while(*nptr) {
523 char cur = tolower(*nptr);
524 int v;
526 if(isdigit(cur)) {
527 if(cur >= '0'+base)
528 break;
529 v = cur-'0';
530 } else {
531 if(cur<'a' || cur>='a'+base-10)
532 break;
533 v = cur-'a'+10;
536 if(negative)
537 v = -v;
539 nptr++;
541 if(!negative && (ret>MSVCRT_I64_MAX/base || ret*base>MSVCRT_I64_MAX-v)) {
542 ret = MSVCRT_I64_MAX;
543 *MSVCRT__errno() = MSVCRT_ERANGE;
544 } else if(negative && (ret<MSVCRT_I64_MIN/base || ret*base<MSVCRT_I64_MIN-v)) {
545 ret = MSVCRT_I64_MIN;
546 *MSVCRT__errno() = MSVCRT_ERANGE;
547 } else
548 ret = ret*base + v;
551 if(endptr)
552 *endptr = (char*)nptr;
554 return ret;
557 /*********************************************************************
558 * _strtoi64 (MSVCRT.@)
560 __int64 CDECL MSVCRT_strtoi64(const char *nptr, char **endptr, int base)
562 return MSVCRT_strtoi64_l(nptr, endptr, base, NULL);
565 /*********************************************************************
566 * _strtoui64_l (MSVCRT.@)
568 * FIXME: locale parameter is ignored
570 unsigned __int64 CDECL MSVCRT_strtoui64_l(const char *nptr, char **endptr, int base, MSVCRT__locale_t locale)
572 BOOL negative = FALSE;
573 unsigned __int64 ret = 0;
575 TRACE("(%s %p %d %p)\n", nptr, endptr, base, locale);
577 if(!nptr || base<0 || base>36 || base==1) {
578 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
579 return 0;
582 while(isspace(*nptr)) nptr++;
584 if(*nptr == '-') {
585 negative = TRUE;
586 nptr++;
587 } else if(*nptr == '+')
588 nptr++;
590 if((base==0 || base==16) && *nptr=='0' && tolower(*(nptr+1))=='x') {
591 base = 16;
592 nptr += 2;
595 if(base == 0) {
596 if(*nptr=='0')
597 base = 8;
598 else
599 base = 10;
602 while(*nptr) {
603 char cur = tolower(*nptr);
604 int v;
606 if(isdigit(cur)) {
607 if(cur >= '0'+base)
608 break;
609 v = *nptr-'0';
610 } else {
611 if(cur<'a' || cur>='a'+base-10)
612 break;
613 v = cur-'a'+10;
616 nptr++;
618 if(ret>MSVCRT_UI64_MAX/base || ret*base>MSVCRT_UI64_MAX-v) {
619 ret = MSVCRT_UI64_MAX;
620 *MSVCRT__errno() = MSVCRT_ERANGE;
621 } else
622 ret = ret*base + v;
625 if(endptr)
626 *endptr = (char*)nptr;
628 return negative ? -ret : ret;
631 /*********************************************************************
632 * _strtoui64 (MSVCRT.@)
634 unsigned __int64 CDECL MSVCRT_strtoui64(const char *nptr, char **endptr, int base)
636 return MSVCRT_strtoui64_l(nptr, endptr, base, NULL);
639 /*********************************************************************
640 * _ui64toa_s (MSVCRT.@)
642 int CDECL MSVCRT__ui64toa_s(unsigned __int64 value, char *str,
643 MSVCRT_size_t size, int radix)
645 char buffer[65], *pos;
646 int digit;
648 if(!str || radix<2 || radix>36) {
649 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
650 *MSVCRT__errno() = MSVCRT_EINVAL;
651 return MSVCRT_EINVAL;
654 pos = buffer+64;
655 *pos = '\0';
657 do {
658 digit = value%radix;
659 value /= radix;
661 if(digit < 10)
662 *--pos = '0'+digit;
663 else
664 *--pos = 'a'+digit-10;
665 }while(value != 0);
667 if(buffer-pos+65 > size) {
668 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
669 *MSVCRT__errno() = MSVCRT_EINVAL;
670 return MSVCRT_EINVAL;
673 memcpy(str, pos, buffer-pos+65);
674 return 0;
677 #define I10_OUTPUT_MAX_PREC 21
678 /* Internal structure used by $I10_OUTPUT */
679 struct _I10_OUTPUT_DATA {
680 short pos;
681 char sign;
682 BYTE len;
683 char str[I10_OUTPUT_MAX_PREC+1]; /* add space for '\0' */
686 /*********************************************************************
687 * $I10_OUTPUT (MSVCRT.@)
688 * ld - long double to be printed to data
689 * prec - precision of part, we're interested in
690 * flag - 0 for first prec digits, 1 for fractional part
691 * data - data to be populated
693 * return value
694 * 0 if given double is NaN or INF
695 * 1 otherwise
697 * FIXME
698 * Native sets last byte of data->str to '0' or '9', I don't know what
699 * it means. Current implementation sets it always to '0'.
701 int CDECL MSVCRT_I10_OUTPUT(MSVCRT__LDOUBLE ld, int prec, int flag, struct _I10_OUTPUT_DATA *data)
703 static const char inf_str[] = "1#INF";
704 static const char nan_str[] = "1#QNAN";
706 double d = ld.x;
707 char format[8];
708 char buf[I10_OUTPUT_MAX_PREC+9]; /* 9 = strlen("0.e+0000") + '\0' */
709 char *p;
711 TRACE("(%lf %d %x %p)\n", d, prec, flag, data);
713 if(d<0) {
714 data->sign = '-';
715 d = -d;
716 } else
717 data->sign = ' ';
719 if(isinf(d)) {
720 data->pos = 1;
721 data->len = 5;
722 memcpy(data->str, inf_str, sizeof(inf_str));
724 return 0;
727 if(isnan(d)) {
728 data->pos = 1;
729 data->len = 6;
730 memcpy(data->str, nan_str, sizeof(nan_str));
732 return 0;
735 if(flag&1) {
736 int exp = 1+floor(log10(d));
738 prec += exp;
739 if(exp < 0)
740 prec--;
742 prec--;
744 if(prec+1 > I10_OUTPUT_MAX_PREC)
745 prec = I10_OUTPUT_MAX_PREC-1;
746 else if(prec < 0) {
747 d = 0.0;
748 prec = 0;
751 sprintf(format, "%%.%dle", prec);
752 sprintf(buf, format, d);
754 buf[1] = buf[0];
755 data->pos = atoi(buf+prec+3);
756 if(buf[1] != '0')
757 data->pos++;
759 for(p = buf+prec+1; p>buf+1 && *p=='0'; p--);
760 data->len = p-buf;
762 memcpy(data->str, buf+1, data->len);
763 data->str[data->len] = '\0';
765 if(buf[1]!='0' && prec-data->len+1>0)
766 memcpy(data->str+data->len+1, buf+data->len+1, prec-data->len+1);
768 return 1;
770 #undef I10_OUTPUT_MAX_PREC