2001-01-10 23:59:25 +00:00
|
|
|
/*
|
|
|
|
* MSVCRT string functions
|
|
|
|
*
|
|
|
|
* Copyright 1996,1998 Marcus Meissner
|
|
|
|
* Copyright 1996 Jukka Iivonen
|
|
|
|
* Copyright 1997,2000 Uwe Bonnes
|
|
|
|
* Copyright 2000 Jon Griffiths
|
2002-03-09 23:29:33 +00:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
2006-05-18 12:49:52 +00:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2001-01-10 23:59:25 +00:00
|
|
|
*/
|
|
|
|
|
2006-12-17 17:56:50 +00:00
|
|
|
#define _ISOC99_SOURCE
|
|
|
|
#include "config.h"
|
2010-04-26 10:33:28 +00:00
|
|
|
#include "wine/port.h"
|
2006-12-17 17:56:50 +00:00
|
|
|
|
2006-01-14 16:40:29 +00:00
|
|
|
#include <stdlib.h>
|
2010-04-26 10:33:28 +00:00
|
|
|
#include <math.h>
|
|
|
|
#include <limits.h>
|
2009-08-31 19:36:45 +00:00
|
|
|
#include <errno.h>
|
2001-04-23 18:20:55 +00:00
|
|
|
#include "msvcrt.h"
|
2002-01-22 00:57:16 +00:00
|
|
|
#include "wine/debug.h"
|
|
|
|
|
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
|
2001-01-10 23:59:25 +00:00
|
|
|
|
|
|
|
/*********************************************************************
|
2001-07-11 18:56:41 +00:00
|
|
|
* _mbsdup (MSVCRT.@)
|
2001-01-10 23:59:25 +00:00
|
|
|
* _strdup (MSVCRT.@)
|
|
|
|
*/
|
2006-06-13 09:21:19 +00:00
|
|
|
char* CDECL _strdup(const char* str)
|
2001-01-10 23:59:25 +00:00
|
|
|
{
|
2006-06-21 08:55:11 +00:00
|
|
|
if(str)
|
|
|
|
{
|
|
|
|
char * ret = MSVCRT_malloc(strlen(str)+1);
|
|
|
|
if (ret) strcpy( ret, str );
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
else return 0;
|
2001-01-10 23:59:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _strnset (MSVCRT.@)
|
|
|
|
*/
|
2010-03-28 22:00:26 +00:00
|
|
|
char* CDECL MSVCRT__strnset(char* str, int value, MSVCRT_size_t len)
|
2001-01-10 23:59:25 +00:00
|
|
|
{
|
|
|
|
if (len > 0 && str)
|
|
|
|
while (*str && len--)
|
|
|
|
*str++ = value;
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _strrev (MSVCRT.@)
|
|
|
|
*/
|
2006-06-13 09:21:19 +00:00
|
|
|
char* CDECL _strrev(char* str)
|
2001-01-10 23:59:25 +00:00
|
|
|
{
|
|
|
|
char * p1;
|
|
|
|
char * p2;
|
|
|
|
|
|
|
|
if (str && *str)
|
|
|
|
for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2)
|
|
|
|
{
|
|
|
|
*p1 ^= *p2;
|
|
|
|
*p2 ^= *p1;
|
|
|
|
*p1 ^= *p2;
|
|
|
|
}
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _strset (MSVCRT.@)
|
|
|
|
*/
|
2006-06-13 09:21:19 +00:00
|
|
|
char* CDECL _strset(char* str, int value)
|
2001-01-10 23:59:25 +00:00
|
|
|
{
|
|
|
|
char *ptr = str;
|
|
|
|
while (*ptr)
|
|
|
|
*ptr++ = value;
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2006-01-14 16:24:49 +00:00
|
|
|
/*********************************************************************
|
|
|
|
* strtok (MSVCRT.@)
|
|
|
|
*/
|
2006-06-13 09:21:19 +00:00
|
|
|
char * CDECL MSVCRT_strtok( char *str, const char *delim )
|
2006-01-14 16:24:49 +00:00
|
|
|
{
|
|
|
|
thread_data_t *data = msvcrt_get_thread_data();
|
|
|
|
char *ret;
|
|
|
|
|
|
|
|
if (!str)
|
|
|
|
if (!(str = data->strtok_next)) return NULL;
|
|
|
|
|
|
|
|
while (*str && strchr( delim, *str )) str++;
|
|
|
|
if (!*str) return NULL;
|
|
|
|
ret = str++;
|
|
|
|
while (*str && !strchr( delim, *str )) str++;
|
|
|
|
if (*str) *str++ = 0;
|
|
|
|
data->strtok_next = str;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-04-26 12:09:59 +00:00
|
|
|
/*********************************************************************
|
|
|
|
* strtok_s (MSVCRT.@)
|
|
|
|
*/
|
|
|
|
char * CDECL MSVCRT_strtok_s(char *str, const char *delim, char **ctx)
|
|
|
|
{
|
|
|
|
if(!delim || !ctx || (!str && !*ctx)) {
|
|
|
|
MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
|
|
|
|
*MSVCRT__errno() = MSVCRT_EINVAL;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!str)
|
|
|
|
str = *ctx;
|
|
|
|
|
|
|
|
while(*str && strchr(delim, *str))
|
|
|
|
str++;
|
|
|
|
if(!*str)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
*ctx = str+1;
|
|
|
|
while(**ctx && !strchr(delim, **ctx))
|
|
|
|
(*ctx)++;
|
|
|
|
if(**ctx)
|
|
|
|
*(*ctx)++ = 0;
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
2006-01-14 16:24:49 +00:00
|
|
|
|
2001-01-10 23:59:25 +00:00
|
|
|
/*********************************************************************
|
|
|
|
* _swab (MSVCRT.@)
|
|
|
|
*/
|
2006-06-13 09:21:19 +00:00
|
|
|
void CDECL MSVCRT__swab(char* src, char* dst, int len)
|
2001-01-10 23:59:25 +00:00
|
|
|
{
|
|
|
|
if (len > 1)
|
|
|
|
{
|
|
|
|
len = (unsigned)len >> 1;
|
|
|
|
|
|
|
|
while (len--) {
|
2005-08-19 10:01:42 +00:00
|
|
|
char s0 = src[0];
|
|
|
|
char s1 = src[1];
|
|
|
|
*dst++ = s1;
|
|
|
|
*dst++ = s0;
|
|
|
|
src = src + 2;
|
2001-01-10 23:59:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-01-14 16:40:29 +00:00
|
|
|
|
2010-04-21 09:26:22 +00:00
|
|
|
/*********************************************************************
|
|
|
|
* strtod_l (MSVCRT.@)
|
|
|
|
*/
|
|
|
|
double CDECL MSVCRT_strtod_l( const char *str, char **end, MSVCRT__locale_t locale)
|
|
|
|
{
|
2010-04-26 10:33:28 +00:00
|
|
|
unsigned __int64 d=0, hlp;
|
|
|
|
int exp=0, sign=1;
|
|
|
|
const char *p;
|
2010-04-21 09:26:22 +00:00
|
|
|
double ret;
|
|
|
|
|
|
|
|
if(!str) {
|
|
|
|
MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
|
|
|
|
*MSVCRT__errno() = MSVCRT_EINVAL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-04-26 10:33:28 +00:00
|
|
|
if(!locale)
|
|
|
|
locale = get_locale();
|
|
|
|
|
2010-04-21 09:26:22 +00:00
|
|
|
/* FIXME: use *_l functions */
|
|
|
|
p = str;
|
|
|
|
while(isspace(*p))
|
|
|
|
p++;
|
2010-04-26 10:33:28 +00:00
|
|
|
|
|
|
|
if(*p == '-') {
|
|
|
|
sign = -1;
|
2010-04-21 09:26:22 +00:00
|
|
|
p++;
|
2010-04-26 10:33:28 +00:00
|
|
|
} else if(*p == '+')
|
2010-04-21 09:26:22 +00:00
|
|
|
p++;
|
2010-04-26 10:33:28 +00:00
|
|
|
|
|
|
|
while(isdigit(*p)) {
|
|
|
|
hlp = d*10+*(p++)-'0';
|
|
|
|
if(d>MSVCRT_UI64_MAX/10 || hlp<d) {
|
|
|
|
exp++;
|
|
|
|
break;
|
|
|
|
} else
|
|
|
|
d = hlp;
|
|
|
|
}
|
|
|
|
while(isdigit(*p)) {
|
|
|
|
exp++;
|
2010-04-21 09:26:22 +00:00
|
|
|
p++;
|
|
|
|
}
|
2010-04-26 10:33:28 +00:00
|
|
|
|
|
|
|
if(*p == *locale->locinfo->lconv->decimal_point)
|
2010-04-21 09:26:22 +00:00
|
|
|
p++;
|
|
|
|
|
2010-04-26 10:33:28 +00:00
|
|
|
while(isdigit(*p)) {
|
|
|
|
hlp = d*10+*(p++)-'0';
|
|
|
|
if(d>MSVCRT_UI64_MAX/10 || hlp<d)
|
|
|
|
break;
|
2010-04-21 09:26:22 +00:00
|
|
|
|
2010-04-26 10:33:28 +00:00
|
|
|
d = hlp;
|
|
|
|
exp--;
|
|
|
|
}
|
|
|
|
while(isdigit(*p))
|
|
|
|
p++;
|
2010-04-21 09:26:22 +00:00
|
|
|
|
2010-04-26 10:33:28 +00:00
|
|
|
if(p == str) {
|
2010-04-21 09:26:22 +00:00
|
|
|
if(end)
|
2010-04-26 10:33:28 +00:00
|
|
|
*end = (char*)str;
|
|
|
|
return 0.0;
|
|
|
|
}
|
2010-04-21 09:26:22 +00:00
|
|
|
|
2010-04-26 10:33:28 +00:00
|
|
|
if(*p=='e' || *p=='E' || *p=='d' || *p=='D') {
|
|
|
|
int e=0, s=1;
|
2010-04-21 09:26:22 +00:00
|
|
|
|
2010-04-26 10:33:28 +00:00
|
|
|
p++;
|
|
|
|
if(*p == '-') {
|
|
|
|
s = -1;
|
|
|
|
p++;
|
|
|
|
} else if(*p == '+')
|
|
|
|
p++;
|
2010-04-21 09:26:22 +00:00
|
|
|
|
2010-04-26 10:33:28 +00:00
|
|
|
if(isdigit(*p)) {
|
|
|
|
while(isdigit(*p)) {
|
|
|
|
if(e>INT_MAX/10 || (e=e*10+*p-'0')<0)
|
|
|
|
e = INT_MAX;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
e *= s;
|
2010-04-21 09:26:22 +00:00
|
|
|
|
2010-04-26 10:33:28 +00:00
|
|
|
if(exp<0 && e<0 && exp+e>=0) exp = INT_MIN;
|
|
|
|
else if(exp>0 && e>0 && exp+e<0) exp = INT_MAX;
|
|
|
|
else exp += e;
|
|
|
|
} else {
|
|
|
|
if(*p=='-' || *p=='+')
|
|
|
|
p--;
|
|
|
|
p--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(exp>0)
|
|
|
|
ret = (double)sign*d*pow(10, exp);
|
|
|
|
else
|
|
|
|
ret = (double)sign*d/pow(10, -exp);
|
|
|
|
|
|
|
|
if((d && ret==0.0) || isinf(ret))
|
|
|
|
*MSVCRT__errno() = MSVCRT_ERANGE;
|
|
|
|
|
|
|
|
if(end)
|
|
|
|
*end = (char*)p;
|
|
|
|
|
|
|
|
return ret;
|
2010-04-21 09:26:22 +00:00
|
|
|
}
|
|
|
|
|
2006-01-14 16:40:29 +00:00
|
|
|
/*********************************************************************
|
|
|
|
* strtod (MSVCRT.@)
|
|
|
|
*/
|
2006-06-13 09:21:19 +00:00
|
|
|
double CDECL MSVCRT_strtod( const char *str, char **end )
|
2006-01-14 16:40:29 +00:00
|
|
|
{
|
2010-04-21 09:26:22 +00:00
|
|
|
return MSVCRT_strtod_l( str, end, NULL );
|
2006-01-14 16:40:29 +00:00
|
|
|
}
|
|
|
|
|
2010-04-26 10:33:49 +00:00
|
|
|
/*********************************************************************
|
|
|
|
* atof (MSVCRT.@)
|
|
|
|
*/
|
|
|
|
double CDECL MSVCRT_atof( const char *str )
|
|
|
|
{
|
|
|
|
return MSVCRT_strtod_l(str, NULL, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _atof_l (MSVCRT.@)
|
|
|
|
*/
|
|
|
|
double CDECL MSVCRT__atof_l( const char *str, MSVCRT__locale_t locale)
|
|
|
|
{
|
|
|
|
return MSVCRT_strtod_l(str, NULL, locale);
|
|
|
|
}
|
|
|
|
|
2006-01-14 16:40:29 +00:00
|
|
|
/*********************************************************************
|
|
|
|
* strcoll (MSVCRT.@)
|
|
|
|
*/
|
2006-06-13 09:21:19 +00:00
|
|
|
int CDECL MSVCRT_strcoll( const char* str1, const char* str2 )
|
2006-01-14 16:40:29 +00:00
|
|
|
{
|
|
|
|
/* FIXME: handle Windows locale */
|
|
|
|
return strcoll( str1, str2 );
|
|
|
|
}
|
|
|
|
|
2007-11-15 15:11:26 +00:00
|
|
|
/*********************************************************************
|
|
|
|
* strcpy_s (MSVCRT.@)
|
|
|
|
*/
|
|
|
|
int CDECL MSVCRT_strcpy_s( char* dst, MSVCRT_size_t elem, const char* src )
|
|
|
|
{
|
|
|
|
MSVCRT_size_t i;
|
2008-02-27 11:26:15 +00:00
|
|
|
if(!elem) return MSVCRT_EINVAL;
|
|
|
|
if(!dst) return MSVCRT_EINVAL;
|
2007-11-15 15:11:26 +00:00
|
|
|
if(!src)
|
|
|
|
{
|
|
|
|
dst[0] = '\0';
|
2008-02-27 11:26:15 +00:00
|
|
|
return MSVCRT_EINVAL;
|
2007-11-15 15:11:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for(i = 0; i < elem; i++)
|
|
|
|
{
|
|
|
|
if((dst[i] = src[i]) == '\0') return 0;
|
|
|
|
}
|
|
|
|
dst[0] = '\0';
|
2008-02-27 11:26:15 +00:00
|
|
|
return MSVCRT_ERANGE;
|
2007-11-15 15:11:26 +00:00
|
|
|
}
|
|
|
|
|
2007-11-16 10:36:03 +00:00
|
|
|
/*********************************************************************
|
|
|
|
* strcat_s (MSVCRT.@)
|
|
|
|
*/
|
|
|
|
int CDECL MSVCRT_strcat_s( char* dst, MSVCRT_size_t elem, const char* src )
|
|
|
|
{
|
|
|
|
MSVCRT_size_t i, j;
|
2008-02-27 11:26:15 +00:00
|
|
|
if(!dst) return MSVCRT_EINVAL;
|
|
|
|
if(elem == 0) return MSVCRT_EINVAL;
|
2007-11-16 10:36:03 +00:00
|
|
|
if(!src)
|
|
|
|
{
|
|
|
|
dst[0] = '\0';
|
2008-02-27 11:26:15 +00:00
|
|
|
return MSVCRT_EINVAL;
|
2007-11-16 10:36:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for(i = 0; i < elem; i++)
|
|
|
|
{
|
|
|
|
if(dst[i] == '\0')
|
|
|
|
{
|
|
|
|
for(j = 0; (j + i) < elem; j++)
|
|
|
|
{
|
|
|
|
if((dst[j + i] = src[j]) == '\0') return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Set the first element to 0, not the first element after the skipped part */
|
|
|
|
dst[0] = '\0';
|
2008-02-27 11:26:15 +00:00
|
|
|
return MSVCRT_ERANGE;
|
2007-11-16 10:36:03 +00:00
|
|
|
}
|
|
|
|
|
2006-01-14 16:40:29 +00:00
|
|
|
/*********************************************************************
|
|
|
|
* strxfrm (MSVCRT.@)
|
|
|
|
*/
|
2006-06-13 09:21:19 +00:00
|
|
|
MSVCRT_size_t CDECL MSVCRT_strxfrm( char *dest, const char *src, MSVCRT_size_t len )
|
2006-01-14 16:40:29 +00:00
|
|
|
{
|
|
|
|
/* FIXME: handle Windows locale */
|
|
|
|
return strxfrm( dest, src, len );
|
|
|
|
}
|
|
|
|
|
2005-05-23 10:27:58 +00:00
|
|
|
/*********************************************************************
|
|
|
|
* _stricoll (MSVCRT.@)
|
|
|
|
*/
|
2006-06-13 09:21:19 +00:00
|
|
|
int CDECL MSVCRT__stricoll( const char* str1, const char* str2 )
|
2005-05-23 10:27:58 +00:00
|
|
|
{
|
|
|
|
/* FIXME: handle collates */
|
|
|
|
TRACE("str1 %s str2 %s\n", debugstr_a(str1), debugstr_a(str2));
|
|
|
|
return lstrcmpiA( str1, str2 );
|
|
|
|
}
|
2006-12-17 17:56:50 +00:00
|
|
|
|
|
|
|
/********************************************************************
|
|
|
|
* _atoldbl (MSVCRT.@)
|
|
|
|
*/
|
2007-09-19 15:52:40 +00:00
|
|
|
int CDECL MSVCRT__atoldbl(MSVCRT__LDOUBLE *value, const char *str)
|
2006-12-17 17:56:50 +00:00
|
|
|
{
|
|
|
|
/* FIXME needs error checking for huge/small values */
|
|
|
|
#ifdef HAVE_STRTOLD
|
|
|
|
TRACE("str %s value %p\n",str,value);
|
|
|
|
value->x = strtold(str,0);
|
|
|
|
#else
|
|
|
|
FIXME("stub, str %s value %p\n",str,value);
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
2007-09-19 15:52:40 +00:00
|
|
|
|
|
|
|
/********************************************************************
|
|
|
|
* __STRINGTOLD (MSVCRT.@)
|
|
|
|
*/
|
|
|
|
int CDECL __STRINGTOLD( MSVCRT__LDOUBLE *value, char **endptr, const char *str, int flags )
|
|
|
|
{
|
|
|
|
#ifdef HAVE_STRTOLD
|
|
|
|
FIXME("%p %p %s %x partial stub\n", value, endptr, str, flags );
|
|
|
|
value->x = strtold(str,endptr);
|
|
|
|
#else
|
|
|
|
FIXME("%p %p %s %x stub\n", value, endptr, str, flags );
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
2009-08-31 19:36:45 +00:00
|
|
|
|
|
|
|
/******************************************************************
|
|
|
|
* strtol (MSVCRT.@)
|
|
|
|
*/
|
2009-09-01 20:34:50 +00:00
|
|
|
MSVCRT_long CDECL MSVCRT_strtol(const char* nptr, char** end, int base)
|
2009-08-31 19:36:45 +00:00
|
|
|
{
|
|
|
|
/* wrapper to forward libc error code to msvcrt's error codes */
|
|
|
|
long ret;
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
ret = strtol(nptr, end, base);
|
|
|
|
switch (errno)
|
|
|
|
{
|
|
|
|
case ERANGE: *MSVCRT__errno() = MSVCRT_ERANGE; break;
|
|
|
|
case EINVAL: *MSVCRT__errno() = MSVCRT_EINVAL; break;
|
|
|
|
default:
|
|
|
|
/* cope with the fact that we may use 64bit long integers on libc
|
|
|
|
* while msvcrt always uses 32bit long integers
|
|
|
|
*/
|
|
|
|
if (ret > MSVCRT_LONG_MAX)
|
|
|
|
{
|
|
|
|
ret = MSVCRT_LONG_MAX;
|
|
|
|
*MSVCRT__errno() = MSVCRT_ERANGE;
|
|
|
|
}
|
|
|
|
else if (ret < -MSVCRT_LONG_MAX - 1)
|
|
|
|
{
|
|
|
|
ret = -MSVCRT_LONG_MAX - 1;
|
|
|
|
*MSVCRT__errno() = MSVCRT_ERANGE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************
|
|
|
|
* strtoul (MSVCRT.@)
|
|
|
|
*/
|
2009-09-01 20:34:50 +00:00
|
|
|
MSVCRT_ulong CDECL MSVCRT_strtoul(const char* nptr, char** end, int base)
|
2009-08-31 19:36:45 +00:00
|
|
|
{
|
|
|
|
/* wrapper to forward libc error code to msvcrt's error codes */
|
|
|
|
unsigned long ret;
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
ret = strtoul(nptr, end, base);
|
|
|
|
switch (errno)
|
|
|
|
{
|
|
|
|
case ERANGE: *MSVCRT__errno() = MSVCRT_ERANGE; break;
|
|
|
|
case EINVAL: *MSVCRT__errno() = MSVCRT_EINVAL; break;
|
|
|
|
default:
|
|
|
|
/* cope with the fact that we may use 64bit long integers on libc
|
|
|
|
* while msvcrt always uses 32bit long integers
|
|
|
|
*/
|
|
|
|
if (ret > MSVCRT_ULONG_MAX)
|
|
|
|
{
|
|
|
|
ret = MSVCRT_ULONG_MAX;
|
|
|
|
*MSVCRT__errno() = MSVCRT_ERANGE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2010-03-24 21:39:28 +00:00
|
|
|
|
|
|
|
/******************************************************************
|
|
|
|
* strnlen (MSVCRT.@)
|
|
|
|
*/
|
|
|
|
MSVCRT_size_t CDECL MSVCRT_strnlen(const char *s, MSVCRT_size_t maxlen)
|
|
|
|
{
|
|
|
|
MSVCRT_size_t i;
|
|
|
|
|
|
|
|
for(i=0; i<maxlen; i++)
|
|
|
|
if(!s[i]) break;
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
2010-03-28 22:04:03 +00:00
|
|
|
|
|
|
|
/*********************************************************************
|
2010-04-28 15:44:51 +00:00
|
|
|
* _strtoi64_l (MSVCRT.@)
|
2010-03-28 22:04:03 +00:00
|
|
|
*
|
|
|
|
* FIXME: locale parameter is ignored
|
|
|
|
*/
|
|
|
|
__int64 CDECL MSVCRT_strtoi64_l(const char *nptr, char **endptr, int base, MSVCRT__locale_t locale)
|
|
|
|
{
|
|
|
|
BOOL negative = FALSE;
|
|
|
|
__int64 ret = 0;
|
|
|
|
|
|
|
|
TRACE("(%s %p %d %p)\n", nptr, endptr, base, locale);
|
|
|
|
|
|
|
|
if(!nptr || base<0 || base>36 || base==1) {
|
2010-04-14 12:34:27 +00:00
|
|
|
MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
|
2010-03-28 22:04:03 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
while(isspace(*nptr)) nptr++;
|
|
|
|
|
|
|
|
if(*nptr == '-') {
|
|
|
|
negative = TRUE;
|
|
|
|
nptr++;
|
|
|
|
} else if(*nptr == '+')
|
|
|
|
nptr++;
|
|
|
|
|
|
|
|
if((base==0 || base==16) && *nptr=='0' && tolower(*(nptr+1))=='x') {
|
|
|
|
base = 16;
|
|
|
|
nptr += 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(base == 0) {
|
|
|
|
if(*nptr=='0')
|
|
|
|
base = 8;
|
|
|
|
else
|
|
|
|
base = 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
while(*nptr) {
|
|
|
|
char cur = tolower(*nptr);
|
|
|
|
int v;
|
|
|
|
|
|
|
|
if(isdigit(cur)) {
|
|
|
|
if(cur >= '0'+base)
|
|
|
|
break;
|
|
|
|
v = cur-'0';
|
|
|
|
} else {
|
|
|
|
if(cur<'a' || cur>='a'+base-10)
|
|
|
|
break;
|
|
|
|
v = cur-'a'+10;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(negative)
|
|
|
|
v = -v;
|
|
|
|
|
|
|
|
nptr++;
|
|
|
|
|
|
|
|
if(!negative && (ret>MSVCRT_I64_MAX/base || ret*base>MSVCRT_I64_MAX-v)) {
|
|
|
|
ret = MSVCRT_I64_MAX;
|
2010-04-27 06:42:32 +00:00
|
|
|
*MSVCRT__errno() = MSVCRT_ERANGE;
|
2010-03-28 22:04:03 +00:00
|
|
|
} else if(negative && (ret<MSVCRT_I64_MIN/base || ret*base<MSVCRT_I64_MIN-v)) {
|
|
|
|
ret = MSVCRT_I64_MIN;
|
2010-04-27 06:42:32 +00:00
|
|
|
*MSVCRT__errno() = MSVCRT_ERANGE;
|
2010-03-28 22:04:03 +00:00
|
|
|
} else
|
|
|
|
ret = ret*base + v;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(endptr)
|
|
|
|
*endptr = (char*)nptr;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
2010-04-28 15:44:51 +00:00
|
|
|
* _strtoi64 (MSVCRT.@)
|
2010-03-28 22:04:03 +00:00
|
|
|
*/
|
|
|
|
__int64 CDECL MSVCRT_strtoi64(const char *nptr, char **endptr, int base)
|
|
|
|
{
|
|
|
|
return MSVCRT_strtoi64_l(nptr, endptr, base, NULL);
|
|
|
|
}
|
2010-03-28 22:04:32 +00:00
|
|
|
|
|
|
|
/*********************************************************************
|
2010-04-28 15:44:51 +00:00
|
|
|
* _strtoui64_l (MSVCRT.@)
|
2010-03-28 22:04:32 +00:00
|
|
|
*
|
|
|
|
* FIXME: locale parameter is ignored
|
|
|
|
*/
|
|
|
|
unsigned __int64 CDECL MSVCRT_strtoui64_l(const char *nptr, char **endptr, int base, MSVCRT__locale_t locale)
|
|
|
|
{
|
|
|
|
BOOL negative = FALSE;
|
|
|
|
unsigned __int64 ret = 0;
|
|
|
|
|
|
|
|
TRACE("(%s %p %d %p)\n", nptr, endptr, base, locale);
|
|
|
|
|
|
|
|
if(!nptr || base<0 || base>36 || base==1) {
|
2010-04-14 12:34:27 +00:00
|
|
|
MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
|
2010-03-28 22:04:32 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
while(isspace(*nptr)) nptr++;
|
|
|
|
|
|
|
|
if(*nptr == '-') {
|
|
|
|
negative = TRUE;
|
|
|
|
nptr++;
|
|
|
|
} else if(*nptr == '+')
|
|
|
|
nptr++;
|
|
|
|
|
|
|
|
if((base==0 || base==16) && *nptr=='0' && tolower(*(nptr+1))=='x') {
|
|
|
|
base = 16;
|
|
|
|
nptr += 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(base == 0) {
|
|
|
|
if(*nptr=='0')
|
|
|
|
base = 8;
|
|
|
|
else
|
|
|
|
base = 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
while(*nptr) {
|
|
|
|
char cur = tolower(*nptr);
|
|
|
|
int v;
|
|
|
|
|
|
|
|
if(isdigit(cur)) {
|
|
|
|
if(cur >= '0'+base)
|
|
|
|
break;
|
|
|
|
v = *nptr-'0';
|
|
|
|
} else {
|
|
|
|
if(cur<'a' || cur>='a'+base-10)
|
|
|
|
break;
|
|
|
|
v = cur-'a'+10;
|
|
|
|
}
|
|
|
|
|
|
|
|
nptr++;
|
|
|
|
|
|
|
|
if(ret>MSVCRT_UI64_MAX/base || ret*base>MSVCRT_UI64_MAX-v) {
|
|
|
|
ret = MSVCRT_UI64_MAX;
|
2010-04-27 06:42:32 +00:00
|
|
|
*MSVCRT__errno() = MSVCRT_ERANGE;
|
2010-03-28 22:04:32 +00:00
|
|
|
} else
|
|
|
|
ret = ret*base + v;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(endptr)
|
|
|
|
*endptr = (char*)nptr;
|
|
|
|
|
|
|
|
return negative ? -ret : ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
2010-04-28 15:44:51 +00:00
|
|
|
* _strtoui64 (MSVCRT.@)
|
2010-03-28 22:04:32 +00:00
|
|
|
*/
|
|
|
|
unsigned __int64 CDECL MSVCRT_strtoui64(const char *nptr, char **endptr, int base)
|
|
|
|
{
|
|
|
|
return MSVCRT_strtoui64_l(nptr, endptr, base, NULL);
|
|
|
|
}
|
2010-05-05 11:37:33 +00:00
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _ui64toa_s (MSVCRT.@)
|
|
|
|
*/
|
|
|
|
int CDECL MSVCRT__ui64toa_s(unsigned __int64 value, char *str,
|
|
|
|
MSVCRT_size_t size, int radix)
|
|
|
|
{
|
|
|
|
char buffer[65], *pos;
|
|
|
|
int digit;
|
|
|
|
|
|
|
|
if(!str || radix<2 || radix>36) {
|
|
|
|
MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
|
|
|
|
*MSVCRT__errno() = MSVCRT_EINVAL;
|
|
|
|
return MSVCRT_EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
pos = buffer+64;
|
|
|
|
*pos = '\0';
|
|
|
|
|
|
|
|
do {
|
|
|
|
digit = value%radix;
|
|
|
|
value /= radix;
|
|
|
|
|
|
|
|
if(digit < 10)
|
|
|
|
*--pos = '0'+digit;
|
|
|
|
else
|
|
|
|
*--pos = 'a'+digit-10;
|
|
|
|
}while(value != 0);
|
|
|
|
|
|
|
|
if(buffer-pos+65 > size) {
|
|
|
|
MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
|
|
|
|
*MSVCRT__errno() = MSVCRT_EINVAL;
|
|
|
|
return MSVCRT_EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(str, pos, buffer-pos+65);
|
|
|
|
return 0;
|
|
|
|
}
|