comdlg32: Fix some alignment issues in the Dutch translation.
[wine/hramrach.git] / dlls / msvcrt / string.c
blobe1d9557111f468b1b4e69ecec137f9eff45a4b2c
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 <math.h>
30 #include <limits.h>
31 #include <errno.h>
32 #include "msvcrt.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
37 /*********************************************************************
38 * _mbsdup (MSVCRT.@)
39 * _strdup (MSVCRT.@)
41 char* CDECL _strdup(const char* str)
43 if(str)
45 char * ret = MSVCRT_malloc(strlen(str)+1);
46 if (ret) strcpy( ret, str );
47 return ret;
49 else return 0;
52 /*********************************************************************
53 * _strnset (MSVCRT.@)
55 char* CDECL MSVCRT__strnset(char* str, int value, MSVCRT_size_t len)
57 if (len > 0 && str)
58 while (*str && len--)
59 *str++ = value;
60 return str;
63 /*********************************************************************
64 * _strrev (MSVCRT.@)
66 char* CDECL _strrev(char* str)
68 char * p1;
69 char * p2;
71 if (str && *str)
72 for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2)
74 *p1 ^= *p2;
75 *p2 ^= *p1;
76 *p1 ^= *p2;
79 return str;
82 /*********************************************************************
83 * _strset (MSVCRT.@)
85 char* CDECL _strset(char* str, int value)
87 char *ptr = str;
88 while (*ptr)
89 *ptr++ = value;
91 return str;
94 /*********************************************************************
95 * strtok (MSVCRT.@)
97 char * CDECL MSVCRT_strtok( char *str, const char *delim )
99 thread_data_t *data = msvcrt_get_thread_data();
100 char *ret;
102 if (!str)
103 if (!(str = data->strtok_next)) return NULL;
105 while (*str && strchr( delim, *str )) str++;
106 if (!*str) return NULL;
107 ret = str++;
108 while (*str && !strchr( delim, *str )) str++;
109 if (*str) *str++ = 0;
110 data->strtok_next = str;
111 return ret;
114 /*********************************************************************
115 * strtok_s (MSVCRT.@)
117 char * CDECL MSVCRT_strtok_s(char *str, const char *delim, char **ctx)
119 if(!delim || !ctx || (!str && !*ctx)) {
120 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
121 *MSVCRT__errno() = MSVCRT_EINVAL;
122 return NULL;
125 if(!str)
126 str = *ctx;
128 while(*str && strchr(delim, *str))
129 str++;
130 if(!*str)
131 return NULL;
133 *ctx = str+1;
134 while(**ctx && !strchr(delim, **ctx))
135 (*ctx)++;
136 if(**ctx)
137 *(*ctx)++ = 0;
139 return str;
142 /*********************************************************************
143 * _swab (MSVCRT.@)
145 void CDECL MSVCRT__swab(char* src, char* dst, int len)
147 if (len > 1)
149 len = (unsigned)len >> 1;
151 while (len--) {
152 char s0 = src[0];
153 char s1 = src[1];
154 *dst++ = s1;
155 *dst++ = s0;
156 src = src + 2;
161 /*********************************************************************
162 * strtod_l (MSVCRT.@)
164 double CDECL MSVCRT_strtod_l( const char *str, char **end, MSVCRT__locale_t locale)
166 unsigned __int64 d=0, hlp;
167 int exp=0, sign=1;
168 const char *p;
169 double ret;
171 if(!str) {
172 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
173 *MSVCRT__errno() = MSVCRT_EINVAL;
174 return 0;
177 if(!locale)
178 locale = get_locale();
180 /* FIXME: use *_l functions */
181 p = str;
182 while(isspace(*p))
183 p++;
185 if(*p == '-') {
186 sign = -1;
187 p++;
188 } else if(*p == '+')
189 p++;
191 while(isdigit(*p)) {
192 hlp = d*10+*(p++)-'0';
193 if(d>MSVCRT_UI64_MAX/10 || hlp<d) {
194 exp++;
195 break;
196 } else
197 d = hlp;
199 while(isdigit(*p)) {
200 exp++;
201 p++;
204 if(*p == *locale->locinfo->lconv->decimal_point)
205 p++;
207 while(isdigit(*p)) {
208 hlp = d*10+*(p++)-'0';
209 if(d>MSVCRT_UI64_MAX/10 || hlp<d)
210 break;
212 d = hlp;
213 exp--;
215 while(isdigit(*p))
216 p++;
218 if(p == str) {
219 if(end)
220 *end = (char*)str;
221 return 0.0;
224 if(*p=='e' || *p=='E' || *p=='d' || *p=='D') {
225 int e=0, s=1;
227 p++;
228 if(*p == '-') {
229 s = -1;
230 p++;
231 } else if(*p == '+')
232 p++;
234 if(isdigit(*p)) {
235 while(isdigit(*p)) {
236 if(e>INT_MAX/10 || (e=e*10+*p-'0')<0)
237 e = INT_MAX;
238 p++;
240 e *= s;
242 if(exp<0 && e<0 && exp+e>=0) exp = INT_MIN;
243 else if(exp>0 && e>0 && exp+e<0) exp = INT_MAX;
244 else exp += e;
245 } else {
246 if(*p=='-' || *p=='+')
247 p--;
248 p--;
252 if(exp>0)
253 ret = (double)sign*d*pow(10, exp);
254 else
255 ret = (double)sign*d/pow(10, -exp);
257 if((d && ret==0.0) || isinf(ret))
258 *MSVCRT__errno() = MSVCRT_ERANGE;
260 if(end)
261 *end = (char*)p;
263 return ret;
266 /*********************************************************************
267 * strtod (MSVCRT.@)
269 double CDECL MSVCRT_strtod( const char *str, char **end )
271 return MSVCRT_strtod_l( str, end, NULL );
274 /*********************************************************************
275 * atof (MSVCRT.@)
277 double CDECL MSVCRT_atof( const char *str )
279 return MSVCRT_strtod_l(str, NULL, NULL);
282 /*********************************************************************
283 * _atof_l (MSVCRT.@)
285 double CDECL MSVCRT__atof_l( const char *str, MSVCRT__locale_t locale)
287 return MSVCRT_strtod_l(str, NULL, locale);
290 /*********************************************************************
291 * strcoll (MSVCRT.@)
293 int CDECL MSVCRT_strcoll( const char* str1, const char* str2 )
295 /* FIXME: handle Windows locale */
296 return strcoll( str1, str2 );
299 /*********************************************************************
300 * strcpy_s (MSVCRT.@)
302 int CDECL MSVCRT_strcpy_s( char* dst, MSVCRT_size_t elem, const char* src )
304 MSVCRT_size_t i;
305 if(!elem) return MSVCRT_EINVAL;
306 if(!dst) return MSVCRT_EINVAL;
307 if(!src)
309 dst[0] = '\0';
310 return MSVCRT_EINVAL;
313 for(i = 0; i < elem; i++)
315 if((dst[i] = src[i]) == '\0') return 0;
317 dst[0] = '\0';
318 return MSVCRT_ERANGE;
321 /*********************************************************************
322 * strcat_s (MSVCRT.@)
324 int CDECL MSVCRT_strcat_s( char* dst, MSVCRT_size_t elem, const char* src )
326 MSVCRT_size_t i, j;
327 if(!dst) return MSVCRT_EINVAL;
328 if(elem == 0) return MSVCRT_EINVAL;
329 if(!src)
331 dst[0] = '\0';
332 return MSVCRT_EINVAL;
335 for(i = 0; i < elem; i++)
337 if(dst[i] == '\0')
339 for(j = 0; (j + i) < elem; j++)
341 if((dst[j + i] = src[j]) == '\0') return 0;
345 /* Set the first element to 0, not the first element after the skipped part */
346 dst[0] = '\0';
347 return MSVCRT_ERANGE;
350 /*********************************************************************
351 * strxfrm (MSVCRT.@)
353 MSVCRT_size_t CDECL MSVCRT_strxfrm( char *dest, const char *src, MSVCRT_size_t len )
355 /* FIXME: handle Windows locale */
356 return strxfrm( dest, src, len );
359 /*********************************************************************
360 * _stricoll (MSVCRT.@)
362 int CDECL MSVCRT__stricoll( const char* str1, const char* str2 )
364 /* FIXME: handle collates */
365 TRACE("str1 %s str2 %s\n", debugstr_a(str1), debugstr_a(str2));
366 return lstrcmpiA( str1, str2 );
369 /********************************************************************
370 * _atoldbl (MSVCRT.@)
372 int CDECL MSVCRT__atoldbl(MSVCRT__LDOUBLE *value, const char *str)
374 /* FIXME needs error checking for huge/small values */
375 #ifdef HAVE_STRTOLD
376 TRACE("str %s value %p\n",str,value);
377 value->x = strtold(str,0);
378 #else
379 FIXME("stub, str %s value %p\n",str,value);
380 #endif
381 return 0;
384 /********************************************************************
385 * __STRINGTOLD (MSVCRT.@)
387 int CDECL __STRINGTOLD( MSVCRT__LDOUBLE *value, char **endptr, const char *str, int flags )
389 #ifdef HAVE_STRTOLD
390 FIXME("%p %p %s %x partial stub\n", value, endptr, str, flags );
391 value->x = strtold(str,endptr);
392 #else
393 FIXME("%p %p %s %x stub\n", value, endptr, str, flags );
394 #endif
395 return 0;
398 /******************************************************************
399 * strtol (MSVCRT.@)
401 MSVCRT_long CDECL MSVCRT_strtol(const char* nptr, char** end, int base)
403 /* wrapper to forward libc error code to msvcrt's error codes */
404 long ret;
406 errno = 0;
407 ret = strtol(nptr, end, base);
408 switch (errno)
410 case ERANGE: *MSVCRT__errno() = MSVCRT_ERANGE; break;
411 case EINVAL: *MSVCRT__errno() = MSVCRT_EINVAL; break;
412 default:
413 /* cope with the fact that we may use 64bit long integers on libc
414 * while msvcrt always uses 32bit long integers
416 if (ret > MSVCRT_LONG_MAX)
418 ret = MSVCRT_LONG_MAX;
419 *MSVCRT__errno() = MSVCRT_ERANGE;
421 else if (ret < -MSVCRT_LONG_MAX - 1)
423 ret = -MSVCRT_LONG_MAX - 1;
424 *MSVCRT__errno() = MSVCRT_ERANGE;
426 break;
429 return ret;
432 /******************************************************************
433 * strtoul (MSVCRT.@)
435 MSVCRT_ulong CDECL MSVCRT_strtoul(const char* nptr, char** end, int base)
437 /* wrapper to forward libc error code to msvcrt's error codes */
438 unsigned long ret;
440 errno = 0;
441 ret = strtoul(nptr, end, base);
442 switch (errno)
444 case ERANGE: *MSVCRT__errno() = MSVCRT_ERANGE; break;
445 case EINVAL: *MSVCRT__errno() = MSVCRT_EINVAL; break;
446 default:
447 /* cope with the fact that we may use 64bit long integers on libc
448 * while msvcrt always uses 32bit long integers
450 if (ret > MSVCRT_ULONG_MAX)
452 ret = MSVCRT_ULONG_MAX;
453 *MSVCRT__errno() = MSVCRT_ERANGE;
455 break;
458 return ret;
461 /******************************************************************
462 * strnlen (MSVCRT.@)
464 MSVCRT_size_t CDECL MSVCRT_strnlen(const char *s, MSVCRT_size_t maxlen)
466 MSVCRT_size_t i;
468 for(i=0; i<maxlen; i++)
469 if(!s[i]) break;
471 return i;
474 /*********************************************************************
475 * _strtoi64_l (MSVCRT.@)
477 * FIXME: locale parameter is ignored
479 __int64 CDECL MSVCRT_strtoi64_l(const char *nptr, char **endptr, int base, MSVCRT__locale_t locale)
481 BOOL negative = FALSE;
482 __int64 ret = 0;
484 TRACE("(%s %p %d %p)\n", nptr, endptr, base, locale);
486 if(!nptr || base<0 || base>36 || base==1) {
487 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
488 return 0;
491 while(isspace(*nptr)) nptr++;
493 if(*nptr == '-') {
494 negative = TRUE;
495 nptr++;
496 } else if(*nptr == '+')
497 nptr++;
499 if((base==0 || base==16) && *nptr=='0' && tolower(*(nptr+1))=='x') {
500 base = 16;
501 nptr += 2;
504 if(base == 0) {
505 if(*nptr=='0')
506 base = 8;
507 else
508 base = 10;
511 while(*nptr) {
512 char cur = tolower(*nptr);
513 int v;
515 if(isdigit(cur)) {
516 if(cur >= '0'+base)
517 break;
518 v = cur-'0';
519 } else {
520 if(cur<'a' || cur>='a'+base-10)
521 break;
522 v = cur-'a'+10;
525 if(negative)
526 v = -v;
528 nptr++;
530 if(!negative && (ret>MSVCRT_I64_MAX/base || ret*base>MSVCRT_I64_MAX-v)) {
531 ret = MSVCRT_I64_MAX;
532 *MSVCRT__errno() = MSVCRT_ERANGE;
533 } else if(negative && (ret<MSVCRT_I64_MIN/base || ret*base<MSVCRT_I64_MIN-v)) {
534 ret = MSVCRT_I64_MIN;
535 *MSVCRT__errno() = MSVCRT_ERANGE;
536 } else
537 ret = ret*base + v;
540 if(endptr)
541 *endptr = (char*)nptr;
543 return ret;
546 /*********************************************************************
547 * _strtoi64 (MSVCRT.@)
549 __int64 CDECL MSVCRT_strtoi64(const char *nptr, char **endptr, int base)
551 return MSVCRT_strtoi64_l(nptr, endptr, base, NULL);
554 /*********************************************************************
555 * _strtoui64_l (MSVCRT.@)
557 * FIXME: locale parameter is ignored
559 unsigned __int64 CDECL MSVCRT_strtoui64_l(const char *nptr, char **endptr, int base, MSVCRT__locale_t locale)
561 BOOL negative = FALSE;
562 unsigned __int64 ret = 0;
564 TRACE("(%s %p %d %p)\n", nptr, endptr, base, locale);
566 if(!nptr || base<0 || base>36 || base==1) {
567 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
568 return 0;
571 while(isspace(*nptr)) nptr++;
573 if(*nptr == '-') {
574 negative = TRUE;
575 nptr++;
576 } else if(*nptr == '+')
577 nptr++;
579 if((base==0 || base==16) && *nptr=='0' && tolower(*(nptr+1))=='x') {
580 base = 16;
581 nptr += 2;
584 if(base == 0) {
585 if(*nptr=='0')
586 base = 8;
587 else
588 base = 10;
591 while(*nptr) {
592 char cur = tolower(*nptr);
593 int v;
595 if(isdigit(cur)) {
596 if(cur >= '0'+base)
597 break;
598 v = *nptr-'0';
599 } else {
600 if(cur<'a' || cur>='a'+base-10)
601 break;
602 v = cur-'a'+10;
605 nptr++;
607 if(ret>MSVCRT_UI64_MAX/base || ret*base>MSVCRT_UI64_MAX-v) {
608 ret = MSVCRT_UI64_MAX;
609 *MSVCRT__errno() = MSVCRT_ERANGE;
610 } else
611 ret = ret*base + v;
614 if(endptr)
615 *endptr = (char*)nptr;
617 return negative ? -ret : ret;
620 /*********************************************************************
621 * _strtoui64 (MSVCRT.@)
623 unsigned __int64 CDECL MSVCRT_strtoui64(const char *nptr, char **endptr, int base)
625 return MSVCRT_strtoui64_l(nptr, endptr, base, NULL);
628 /*********************************************************************
629 * _ui64toa_s (MSVCRT.@)
631 int CDECL MSVCRT__ui64toa_s(unsigned __int64 value, char *str,
632 MSVCRT_size_t size, int radix)
634 char buffer[65], *pos;
635 int digit;
637 if(!str || radix<2 || radix>36) {
638 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
639 *MSVCRT__errno() = MSVCRT_EINVAL;
640 return MSVCRT_EINVAL;
643 pos = buffer+64;
644 *pos = '\0';
646 do {
647 digit = value%radix;
648 value /= radix;
650 if(digit < 10)
651 *--pos = '0'+digit;
652 else
653 *--pos = 'a'+digit-10;
654 }while(value != 0);
656 if(buffer-pos+65 > size) {
657 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
658 *MSVCRT__errno() = MSVCRT_EINVAL;
659 return MSVCRT_EINVAL;
662 memcpy(str, pos, buffer-pos+65);
663 return 0;