janitorial: Remove redundant NULL checks before calling HeapFree wrappers.
[wine/wine-kai.git] / dlls / msvcrt / string.c
blob6dfce9a5ecde40e91c0e1ab3624f087aeb8f7abe
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 #include <stdlib.h>
25 #include "msvcrt.h"
26 #include "wine/debug.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
30 /* INTERNAL: MSVCRT_malloc() based strndup */
31 char* msvcrt_strndup(const char* buf, unsigned int size)
33 char* ret;
34 unsigned int len = strlen(buf), max_len;
36 max_len = size <= len? size : len + 1;
38 ret = MSVCRT_malloc(max_len);
39 if (ret)
41 memcpy(ret,buf,max_len);
42 ret[max_len] = 0;
44 return ret;
47 /*********************************************************************
48 * _mbsdup (MSVCRT.@)
49 * _strdup (MSVCRT.@)
51 char* CDECL _strdup(const char* str)
53 if(str)
55 char * ret = MSVCRT_malloc(strlen(str)+1);
56 if (ret) strcpy( ret, str );
57 return ret;
59 else return 0;
62 /*********************************************************************
63 * _strnset (MSVCRT.@)
65 char* CDECL _strnset(char* str, int value, MSVCRT_size_t len)
67 if (len > 0 && str)
68 while (*str && len--)
69 *str++ = value;
70 return str;
73 /*********************************************************************
74 * _strrev (MSVCRT.@)
76 char* CDECL _strrev(char* str)
78 char * p1;
79 char * p2;
81 if (str && *str)
82 for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2)
84 *p1 ^= *p2;
85 *p2 ^= *p1;
86 *p1 ^= *p2;
89 return str;
92 /*********************************************************************
93 * _strset (MSVCRT.@)
95 char* CDECL _strset(char* str, int value)
97 char *ptr = str;
98 while (*ptr)
99 *ptr++ = value;
101 return str;
104 /*********************************************************************
105 * strtok (MSVCRT.@)
107 char * CDECL MSVCRT_strtok( char *str, const char *delim )
109 thread_data_t *data = msvcrt_get_thread_data();
110 char *ret;
112 if (!str)
113 if (!(str = data->strtok_next)) return NULL;
115 while (*str && strchr( delim, *str )) str++;
116 if (!*str) return NULL;
117 ret = str++;
118 while (*str && !strchr( delim, *str )) str++;
119 if (*str) *str++ = 0;
120 data->strtok_next = str;
121 return ret;
125 /*********************************************************************
126 * _swab (MSVCRT.@)
128 void CDECL MSVCRT__swab(char* src, char* dst, int len)
130 if (len > 1)
132 len = (unsigned)len >> 1;
134 while (len--) {
135 char s0 = src[0];
136 char s1 = src[1];
137 *dst++ = s1;
138 *dst++ = s0;
139 src = src + 2;
144 /*********************************************************************
145 * atof (MSVCRT.@)
147 double CDECL MSVCRT_atof( const char *str )
149 return atof( str );
152 /*********************************************************************
153 * strtod (MSVCRT.@)
155 double CDECL MSVCRT_strtod( const char *str, char **end )
157 return strtod( str, end );
160 /*********************************************************************
161 * strcoll (MSVCRT.@)
163 int CDECL MSVCRT_strcoll( const char* str1, const char* str2 )
165 /* FIXME: handle Windows locale */
166 return strcoll( str1, str2 );
169 /*********************************************************************
170 * strxfrm (MSVCRT.@)
172 MSVCRT_size_t CDECL MSVCRT_strxfrm( char *dest, const char *src, MSVCRT_size_t len )
174 /* FIXME: handle Windows locale */
175 return strxfrm( dest, src, len );
178 /*********************************************************************
179 * _stricoll (MSVCRT.@)
181 int CDECL MSVCRT__stricoll( const char* str1, const char* str2 )
183 /* FIXME: handle collates */
184 TRACE("str1 %s str2 %s\n", debugstr_a(str1), debugstr_a(str2));
185 return lstrcmpiA( str1, str2 );