string.c 6.04 KB
Newer Older
1 2 3 4 5 6 7
/*
 * MSVCRT string functions
 *
 * Copyright 1996,1998 Marcus Meissner
 * Copyright 1996 Jukka Iivonen
 * Copyright 1997,2000 Uwe Bonnes
 * Copyright 2000 Jon Griffiths
8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * 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
21
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 23
 */

Louis Lenders's avatar
Louis Lenders committed
24 25 26
#define _ISOC99_SOURCE
#include "config.h"

27
#include <stdlib.h>
28
#include "msvcrt.h"
29 30 31
#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
32 33

/*********************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
34
 *		_mbsdup (MSVCRT.@)
35 36
 *		_strdup (MSVCRT.@)
 */
37
char* CDECL _strdup(const char* str)
38
{
39 40 41 42 43 44 45
    if(str)
    {
      char * ret = MSVCRT_malloc(strlen(str)+1);
      if (ret) strcpy( ret, str );
      return ret;
    }
    else return 0;
46 47 48 49 50
}

/*********************************************************************
 *		_strnset (MSVCRT.@)
 */
51
char* CDECL _strnset(char* str, int value, MSVCRT_size_t len)
52 53 54 55 56 57 58 59 60 61
{
  if (len > 0 && str)
    while (*str && len--)
      *str++ = value;
  return str;
}

/*********************************************************************
 *		_strrev (MSVCRT.@)
 */
62
char* CDECL _strrev(char* str)
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
{
  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.@)
 */
81
char* CDECL _strset(char* str, int value)
82 83 84 85 86 87 88 89
{
  char *ptr = str;
  while (*ptr)
    *ptr++ = value;

  return str;
}

90 91 92
/*********************************************************************
 *		strtok  (MSVCRT.@)
 */
93
char * CDECL MSVCRT_strtok( char *str, const char *delim )
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
{
    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;
}


111 112 113
/*********************************************************************
 *		_swab (MSVCRT.@)
 */
114
void CDECL MSVCRT__swab(char* src, char* dst, int len)
115 116 117 118 119 120
{
  if (len > 1)
  {
    len = (unsigned)len >> 1;

    while (len--) {
121 122 123 124 125
      char s0 = src[0];
      char s1 = src[1];
      *dst++ = s1;
      *dst++ = s0;
      src = src + 2;
126 127 128
    }
  }
}
129 130 131 132

/*********************************************************************
 *		atof  (MSVCRT.@)
 */
133
double CDECL MSVCRT_atof( const char *str )
134 135 136 137 138 139 140
{
    return atof( str );
}

/*********************************************************************
 *		strtod  (MSVCRT.@)
 */
141
double CDECL MSVCRT_strtod( const char *str, char **end )
142 143 144 145 146 147 148
{
    return strtod( str, end );
}

/*********************************************************************
 *		strcoll (MSVCRT.@)
 */
149
int CDECL MSVCRT_strcoll( const char* str1, const char* str2 )
150 151 152 153 154
{
    /* FIXME: handle Windows locale */
    return strcoll( str1, str2 );
}

155 156 157 158 159 160
/*********************************************************************
 *      strcpy_s (MSVCRT.@)
 */
int CDECL MSVCRT_strcpy_s( char* dst, MSVCRT_size_t elem, const char* src )
{
    MSVCRT_size_t i;
161 162
    if(!elem) return MSVCRT_EINVAL;
    if(!dst) return MSVCRT_EINVAL;
163 164 165
    if(!src)
    {
        dst[0] = '\0';
166
        return MSVCRT_EINVAL;
167 168 169 170 171 172 173
    }

    for(i = 0; i < elem; i++)
    {
        if((dst[i] = src[i]) == '\0') return 0;
    }
    dst[0] = '\0';
174
    return MSVCRT_ERANGE;
175 176
}

177 178 179 180 181 182
/*********************************************************************
 *      strcat_s (MSVCRT.@)
 */
int CDECL MSVCRT_strcat_s( char* dst, MSVCRT_size_t elem, const char* src )
{
    MSVCRT_size_t i, j;
183 184
    if(!dst) return MSVCRT_EINVAL;
    if(elem == 0) return MSVCRT_EINVAL;
185 186 187
    if(!src)
    {
        dst[0] = '\0';
188
        return MSVCRT_EINVAL;
189 190 191 192 193 194 195 196 197 198 199 200 201 202
    }

    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';
203
    return MSVCRT_ERANGE;
204 205
}

206 207 208
/*********************************************************************
 *		strxfrm (MSVCRT.@)
 */
209
MSVCRT_size_t CDECL MSVCRT_strxfrm( char *dest, const char *src, MSVCRT_size_t len )
210 211 212 213 214
{
    /* FIXME: handle Windows locale */
    return strxfrm( dest, src, len );
}

215 216 217
/*********************************************************************
 *		_stricoll (MSVCRT.@)
 */
218
int CDECL MSVCRT__stricoll( const char* str1, const char* str2 )
219 220 221 222 223
{
  /* FIXME: handle collates */
  TRACE("str1 %s str2 %s\n", debugstr_a(str1), debugstr_a(str2));
  return lstrcmpiA( str1, str2 );
}
Louis Lenders's avatar
Louis Lenders committed
224 225 226 227

/********************************************************************
 *		_atoldbl (MSVCRT.@)
 */
228
int CDECL MSVCRT__atoldbl(MSVCRT__LDOUBLE *value, const char *str)
Louis Lenders's avatar
Louis Lenders committed
229 230 231 232 233 234 235 236 237 238
{
  /* 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;
}
239 240 241 242 243 244 245 246 247 248 249 250 251 252

/********************************************************************
 *		__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;
}