kernel32: Update Italian translation.
[wine/testsucceed.git] / dlls / msvcrt / string.c
blob8c9939922231cef7d2d5a6e645b064de104612e0
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;
173 if(!str) {
174 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
175 *MSVCRT__errno() = MSVCRT_EINVAL;
176 return 0;
179 if(!locale)
180 locale = get_locale();
182 /* FIXME: use *_l functions */
183 p = str;
184 while(isspace(*p))
185 p++;
187 if(*p == '-') {
188 sign = -1;
189 p++;
190 } else if(*p == '+')
191 p++;
193 while(isdigit(*p)) {
194 hlp = d*10+*(p++)-'0';
195 if(d>MSVCRT_UI64_MAX/10 || hlp<d) {
196 exp++;
197 break;
198 } else
199 d = hlp;
201 while(isdigit(*p)) {
202 exp++;
203 p++;
206 if(*p == *locale->locinfo->lconv->decimal_point)
207 p++;
209 while(isdigit(*p)) {
210 hlp = d*10+*(p++)-'0';
211 if(d>MSVCRT_UI64_MAX/10 || hlp<d)
212 break;
214 d = hlp;
215 exp--;
217 while(isdigit(*p))
218 p++;
220 if(p == str) {
221 if(end)
222 *end = (char*)str;
223 return 0.0;
226 if(*p=='e' || *p=='E' || *p=='d' || *p=='D') {
227 int e=0, s=1;
229 p++;
230 if(*p == '-') {
231 s = -1;
232 p++;
233 } else if(*p == '+')
234 p++;
236 if(isdigit(*p)) {
237 while(isdigit(*p)) {
238 if(e>INT_MAX/10 || (e=e*10+*p-'0')<0)
239 e = INT_MAX;
240 p++;
242 e *= s;
244 if(exp<0 && e<0 && exp+e>=0) exp = INT_MIN;
245 else if(exp>0 && e>0 && exp+e<0) exp = INT_MAX;
246 else exp += e;
247 } else {
248 if(*p=='-' || *p=='+')
249 p--;
250 p--;
254 fpcontrol = _control87(0, 0);
255 _control87(MSVCRT__EM_DENORMAL|MSVCRT__EM_INVALID|MSVCRT__EM_ZERODIVIDE
256 |MSVCRT__EM_OVERFLOW|MSVCRT__EM_UNDERFLOW|MSVCRT__EM_INEXACT, 0xffffffff);
258 if(exp>0)
259 ret = (double)sign*d*pow(10, exp);
260 else
261 ret = (double)sign*d/pow(10, -exp);
263 _control87(fpcontrol, 0xffffffff);
265 if((d && ret==0.0) || isinf(ret))
266 *MSVCRT__errno() = MSVCRT_ERANGE;
268 if(end)
269 *end = (char*)p;
271 return ret;
274 /*********************************************************************
275 * strtod (MSVCRT.@)
277 double CDECL MSVCRT_strtod( const char *str, char **end )
279 return MSVCRT_strtod_l( str, end, NULL );
282 /*********************************************************************
283 * atof (MSVCRT.@)
285 double CDECL MSVCRT_atof( const char *str )
287 return MSVCRT_strtod_l(str, NULL, NULL);
290 /*********************************************************************
291 * _atof_l (MSVCRT.@)
293 double CDECL MSVCRT__atof_l( const char *str, MSVCRT__locale_t locale)
295 return MSVCRT_strtod_l(str, NULL, locale);
298 /*********************************************************************
299 * strcoll (MSVCRT.@)
301 int CDECL MSVCRT_strcoll( const char* str1, const char* str2 )
303 /* FIXME: handle Windows locale */
304 return strcoll( str1, str2 );
307 /*********************************************************************
308 * strcpy_s (MSVCRT.@)
310 int CDECL MSVCRT_strcpy_s( char* dst, MSVCRT_size_t elem, const char* src )
312 MSVCRT_size_t i;
313 if(!elem) return MSVCRT_EINVAL;
314 if(!dst) return MSVCRT_EINVAL;
315 if(!src)
317 dst[0] = '\0';
318 return MSVCRT_EINVAL;
321 for(i = 0; i < elem; i++)
323 if((dst[i] = src[i]) == '\0') return 0;
325 dst[0] = '\0';
326 return MSVCRT_ERANGE;
329 /*********************************************************************
330 * strcat_s (MSVCRT.@)
332 int CDECL MSVCRT_strcat_s( char* dst, MSVCRT_size_t elem, const char* src )
334 MSVCRT_size_t i, j;
335 if(!dst) return MSVCRT_EINVAL;
336 if(elem == 0) return MSVCRT_EINVAL;
337 if(!src)
339 dst[0] = '\0';
340 return MSVCRT_EINVAL;
343 for(i = 0; i < elem; i++)
345 if(dst[i] == '\0')
347 for(j = 0; (j + i) < elem; j++)
349 if((dst[j + i] = src[j]) == '\0') return 0;
353 /* Set the first element to 0, not the first element after the skipped part */
354 dst[0] = '\0';
355 return MSVCRT_ERANGE;
358 /*********************************************************************
359 * strxfrm (MSVCRT.@)
361 MSVCRT_size_t CDECL MSVCRT_strxfrm( char *dest, const char *src, MSVCRT_size_t len )
363 /* FIXME: handle Windows locale */
364 return strxfrm( dest, src, len );
367 /*********************************************************************
368 * _stricoll (MSVCRT.@)
370 int CDECL MSVCRT__stricoll( const char* str1, const char* str2 )
372 /* FIXME: handle collates */
373 TRACE("str1 %s str2 %s\n", debugstr_a(str1), debugstr_a(str2));
374 return lstrcmpiA( str1, str2 );
377 /********************************************************************
378 * _atoldbl (MSVCRT.@)
380 int CDECL MSVCRT__atoldbl(MSVCRT__LDOUBLE *value, const char *str)
382 /* FIXME needs error checking for huge/small values */
383 #ifdef HAVE_STRTOLD
384 TRACE("str %s value %p\n",str,value);
385 value->x = strtold(str,0);
386 #else
387 FIXME("stub, str %s value %p\n",str,value);
388 #endif
389 return 0;
392 /********************************************************************
393 * __STRINGTOLD (MSVCRT.@)
395 int CDECL __STRINGTOLD( MSVCRT__LDOUBLE *value, char **endptr, const char *str, int flags )
397 #ifdef HAVE_STRTOLD
398 FIXME("%p %p %s %x partial stub\n", value, endptr, str, flags );
399 value->x = strtold(str,endptr);
400 #else
401 FIXME("%p %p %s %x stub\n", value, endptr, str, flags );
402 #endif
403 return 0;
406 /******************************************************************
407 * strtol (MSVCRT.@)
409 MSVCRT_long CDECL MSVCRT_strtol(const char* nptr, char** end, int base)
411 /* wrapper to forward libc error code to msvcrt's error codes */
412 long ret;
414 errno = 0;
415 ret = strtol(nptr, end, base);
416 switch (errno)
418 case ERANGE: *MSVCRT__errno() = MSVCRT_ERANGE; break;
419 case EINVAL: *MSVCRT__errno() = MSVCRT_EINVAL; break;
420 default:
421 /* cope with the fact that we may use 64bit long integers on libc
422 * while msvcrt always uses 32bit long integers
424 if (ret > MSVCRT_LONG_MAX)
426 ret = MSVCRT_LONG_MAX;
427 *MSVCRT__errno() = MSVCRT_ERANGE;
429 else if (ret < -MSVCRT_LONG_MAX - 1)
431 ret = -MSVCRT_LONG_MAX - 1;
432 *MSVCRT__errno() = MSVCRT_ERANGE;
434 break;
437 return ret;
440 /******************************************************************
441 * strtoul (MSVCRT.@)
443 MSVCRT_ulong CDECL MSVCRT_strtoul(const char* nptr, char** end, int base)
445 /* wrapper to forward libc error code to msvcrt's error codes */
446 unsigned long ret;
448 errno = 0;
449 ret = strtoul(nptr, end, base);
450 switch (errno)
452 case ERANGE: *MSVCRT__errno() = MSVCRT_ERANGE; break;
453 case EINVAL: *MSVCRT__errno() = MSVCRT_EINVAL; break;
454 default:
455 /* cope with the fact that we may use 64bit long integers on libc
456 * while msvcrt always uses 32bit long integers
458 if (ret > MSVCRT_ULONG_MAX)
460 ret = MSVCRT_ULONG_MAX;
461 *MSVCRT__errno() = MSVCRT_ERANGE;
463 break;
466 return ret;
469 /******************************************************************
470 * strnlen (MSVCRT.@)
472 MSVCRT_size_t CDECL MSVCRT_strnlen(const char *s, MSVCRT_size_t maxlen)
474 MSVCRT_size_t i;
476 for(i=0; i<maxlen; i++)
477 if(!s[i]) break;
479 return i;
482 /*********************************************************************
483 * _strtoi64_l (MSVCRT.@)
485 * FIXME: locale parameter is ignored
487 __int64 CDECL MSVCRT_strtoi64_l(const char *nptr, char **endptr, int base, MSVCRT__locale_t locale)
489 BOOL negative = FALSE;
490 __int64 ret = 0;
492 TRACE("(%s %p %d %p)\n", nptr, endptr, base, locale);
494 if(!nptr || base<0 || base>36 || base==1) {
495 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
496 return 0;
499 while(isspace(*nptr)) nptr++;
501 if(*nptr == '-') {
502 negative = TRUE;
503 nptr++;
504 } else if(*nptr == '+')
505 nptr++;
507 if((base==0 || base==16) && *nptr=='0' && tolower(*(nptr+1))=='x') {
508 base = 16;
509 nptr += 2;
512 if(base == 0) {
513 if(*nptr=='0')
514 base = 8;
515 else
516 base = 10;
519 while(*nptr) {
520 char cur = tolower(*nptr);
521 int v;
523 if(isdigit(cur)) {
524 if(cur >= '0'+base)
525 break;
526 v = cur-'0';
527 } else {
528 if(cur<'a' || cur>='a'+base-10)
529 break;
530 v = cur-'a'+10;
533 if(negative)
534 v = -v;
536 nptr++;
538 if(!negative && (ret>MSVCRT_I64_MAX/base || ret*base>MSVCRT_I64_MAX-v)) {
539 ret = MSVCRT_I64_MAX;
540 *MSVCRT__errno() = MSVCRT_ERANGE;
541 } else if(negative && (ret<MSVCRT_I64_MIN/base || ret*base<MSVCRT_I64_MIN-v)) {
542 ret = MSVCRT_I64_MIN;
543 *MSVCRT__errno() = MSVCRT_ERANGE;
544 } else
545 ret = ret*base + v;
548 if(endptr)
549 *endptr = (char*)nptr;
551 return ret;
554 /*********************************************************************
555 * _strtoi64 (MSVCRT.@)
557 __int64 CDECL MSVCRT_strtoi64(const char *nptr, char **endptr, int base)
559 return MSVCRT_strtoi64_l(nptr, endptr, base, NULL);
562 /*********************************************************************
563 * _strtoui64_l (MSVCRT.@)
565 * FIXME: locale parameter is ignored
567 unsigned __int64 CDECL MSVCRT_strtoui64_l(const char *nptr, char **endptr, int base, MSVCRT__locale_t locale)
569 BOOL negative = FALSE;
570 unsigned __int64 ret = 0;
572 TRACE("(%s %p %d %p)\n", nptr, endptr, base, locale);
574 if(!nptr || base<0 || base>36 || base==1) {
575 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
576 return 0;
579 while(isspace(*nptr)) nptr++;
581 if(*nptr == '-') {
582 negative = TRUE;
583 nptr++;
584 } else if(*nptr == '+')
585 nptr++;
587 if((base==0 || base==16) && *nptr=='0' && tolower(*(nptr+1))=='x') {
588 base = 16;
589 nptr += 2;
592 if(base == 0) {
593 if(*nptr=='0')
594 base = 8;
595 else
596 base = 10;
599 while(*nptr) {
600 char cur = tolower(*nptr);
601 int v;
603 if(isdigit(cur)) {
604 if(cur >= '0'+base)
605 break;
606 v = *nptr-'0';
607 } else {
608 if(cur<'a' || cur>='a'+base-10)
609 break;
610 v = cur-'a'+10;
613 nptr++;
615 if(ret>MSVCRT_UI64_MAX/base || ret*base>MSVCRT_UI64_MAX-v) {
616 ret = MSVCRT_UI64_MAX;
617 *MSVCRT__errno() = MSVCRT_ERANGE;
618 } else
619 ret = ret*base + v;
622 if(endptr)
623 *endptr = (char*)nptr;
625 return negative ? -ret : ret;
628 /*********************************************************************
629 * _strtoui64 (MSVCRT.@)
631 unsigned __int64 CDECL MSVCRT_strtoui64(const char *nptr, char **endptr, int base)
633 return MSVCRT_strtoui64_l(nptr, endptr, base, NULL);
636 /*********************************************************************
637 * _ui64toa_s (MSVCRT.@)
639 int CDECL MSVCRT__ui64toa_s(unsigned __int64 value, char *str,
640 MSVCRT_size_t size, int radix)
642 char buffer[65], *pos;
643 int digit;
645 if(!str || radix<2 || radix>36) {
646 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
647 *MSVCRT__errno() = MSVCRT_EINVAL;
648 return MSVCRT_EINVAL;
651 pos = buffer+64;
652 *pos = '\0';
654 do {
655 digit = value%radix;
656 value /= radix;
658 if(digit < 10)
659 *--pos = '0'+digit;
660 else
661 *--pos = 'a'+digit-10;
662 }while(value != 0);
664 if(buffer-pos+65 > size) {
665 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
666 *MSVCRT__errno() = MSVCRT_EINVAL;
667 return MSVCRT_EINVAL;
670 memcpy(str, pos, buffer-pos+65);
671 return 0;
674 #define I10_OUTPUT_MAX_PREC 21
675 /* Internal structure used by $I10_OUTPUT */
676 struct _I10_OUTPUT_DATA {
677 short pos;
678 char sign;
679 BYTE len;
680 char str[I10_OUTPUT_MAX_PREC+1]; /* add space for '\0' */
683 /*********************************************************************
684 * $I10_OUTPUT (MSVCRT.@)
685 * ld - long double to be printed to data
686 * prec - precision of part, we're interested in
687 * flag - 0 for first prec digits, 1 for fractional part
688 * data - data to be populated
690 * return value
691 * 0 if given double is NaN or INF
692 * 1 otherwise
694 * FIXME
695 * Native sets last byte of data->str to '0' or '9', I don't know what
696 * it means. Current implementation sets it always to '0'.
698 int CDECL MSVCRT_I10_OUTPUT(MSVCRT__LDOUBLE ld, int prec, int flag, struct _I10_OUTPUT_DATA *data)
700 static const char inf_str[] = "1#INF";
701 static const char nan_str[] = "1#QNAN";
703 double d = ld.x;
704 char format[8];
705 char buf[I10_OUTPUT_MAX_PREC+9]; /* 9 = strlen("0.e+0000") + '\0' */
706 char *p;
708 TRACE("(%lf %d %x %p)\n", d, prec, flag, data);
710 if(d<0) {
711 data->sign = '-';
712 d = -d;
713 } else
714 data->sign = ' ';
716 if(isinf(d)) {
717 data->pos = 1;
718 data->len = 5;
719 memcpy(data->str, inf_str, sizeof(inf_str));
721 return 0;
724 if(isnan(d)) {
725 data->pos = 1;
726 data->len = 6;
727 memcpy(data->str, nan_str, sizeof(nan_str));
729 return 0;
732 if(flag&1) {
733 int exp = 1+floor(log10(d));
735 prec += exp;
736 if(exp < 0)
737 prec--;
739 prec--;
741 if(prec+1 > I10_OUTPUT_MAX_PREC)
742 prec = I10_OUTPUT_MAX_PREC-1;
743 else if(prec < 0) {
744 d = 0.0;
745 prec = 0;
748 sprintf(format, "%%.%dle", prec);
749 sprintf(buf, format, d);
751 buf[1] = buf[0];
752 data->pos = atoi(buf+prec+3);
753 if(buf[1] != '0')
754 data->pos++;
756 for(p = buf+prec+1; p>buf+1 && *p=='0'; p--);
757 data->len = p-buf;
759 memcpy(data->str, buf+1, data->len);
760 data->str[data->len] = '\0';
762 if(buf[1]!='0' && prec-data->len+1>0)
763 memcpy(data->str+data->len+1, buf+data->len+1, prec-data->len+1);
765 return 1;
767 #undef I10_OUTPUT_MAX_PREC