Added InitializeCriticalSectionAndSpinCount prototype.
[wine.git] / memory / string.c
blob3ee704419263245e54319e143236bc2a8a02ac46
1 /*
2 * String functions
4 * Copyright 1993 Yngvi Sigurjonsson
5 * Copyright 1996 Alexandre Julliard
6 */
8 #include <ctype.h>
9 #include <string.h>
11 #include "windef.h"
12 #include "winbase.h"
13 #include "wine/winbase16.h"
14 #include "wine/exception.h"
15 #include "wine/unicode.h"
16 #include "winerror.h"
17 #include "winnls.h"
18 #include "debugtools.h"
20 DEFAULT_DEBUG_CHANNEL(string);
22 /* filter for page-fault exceptions */
23 static WINE_EXCEPTION_FILTER(page_fault)
25 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
26 return EXCEPTION_EXECUTE_HANDLER;
27 return EXCEPTION_CONTINUE_SEARCH;
31 /***********************************************************************
32 * hmemcpy16 (KERNEL.348)
34 void WINAPI hmemcpy16( LPVOID dst, LPCVOID src, LONG count )
36 memcpy( dst, src, count );
40 /***********************************************************************
41 * lstrcat16 (KERNEL.89)
43 SEGPTR WINAPI lstrcat16( SEGPTR dst, LPCSTR src )
45 /* Windows does not check for NULL pointers here, so we don't either */
46 strcat( MapSL(dst), src );
47 return dst;
51 /***********************************************************************
52 * lstrcatA (KERNEL32.599)
54 LPSTR WINAPI lstrcatA( LPSTR dst, LPCSTR src )
56 __TRY
58 strcat( dst, src );
60 __EXCEPT(page_fault)
62 SetLastError( ERROR_INVALID_PARAMETER );
63 return NULL;
65 __ENDTRY
66 return dst;
70 /***********************************************************************
71 * lstrcatW (KERNEL32.600)
73 LPWSTR WINAPI lstrcatW( LPWSTR dst, LPCWSTR src )
75 __TRY
77 strcatW( dst, src );
79 __EXCEPT(page_fault)
81 SetLastError( ERROR_INVALID_PARAMETER );
82 return NULL;
84 __ENDTRY
85 return dst;
89 /***********************************************************************
90 * lstrcatn16 (KERNEL.352)
92 SEGPTR WINAPI lstrcatn16( SEGPTR dst, LPCSTR src, INT16 n )
94 LPSTR p = MapSL(dst);
95 LPSTR start = p;
97 while (*p) p++;
98 if ((n -= (p - start)) <= 0) return dst;
99 lstrcpynA( p, src, n );
100 return dst;
104 /***********************************************************************
105 * lstrcmpA (KERNEL.602)
107 INT WINAPI lstrcmpA( LPCSTR str1, LPCSTR str2 )
109 return CompareStringA(LOCALE_SYSTEM_DEFAULT,0,str1,-1,str2,-1) - 2 ;
113 /***********************************************************************
114 * lstrcmpW (KERNEL.603)
115 * FIXME : should call CompareStringW, when it is implemented.
116 * This implementation is not "word sort", as it should.
118 INT WINAPI lstrcmpW( LPCWSTR str1, LPCWSTR str2 )
120 TRACE("%s and %s\n",
121 debugstr_w (str1), debugstr_w (str2));
122 if (!str1 || !str2) {
123 SetLastError(ERROR_INVALID_PARAMETER);
124 return 0;
126 while (*str1 && (*str1 == *str2)) { str1++; str2++; }
127 return (INT)(*str1 - *str2);
131 /***********************************************************************
132 * lstrcmpiA (KERNEL32.605)
134 INT WINAPI lstrcmpiA( LPCSTR str1, LPCSTR str2 )
135 { TRACE("strcmpi %s and %s\n",
136 debugstr_a (str1), debugstr_a (str2));
137 return CompareStringA(LOCALE_SYSTEM_DEFAULT,NORM_IGNORECASE,str1,-1,str2,-1)-2;
141 /***********************************************************************
142 * lstrcmpiW (KERNEL32.606)
144 INT WINAPI lstrcmpiW( LPCWSTR str1, LPCWSTR str2 )
146 if (!str1 || !str2) {
147 SetLastError(ERROR_INVALID_PARAMETER);
148 return 0;
150 return strcmpiW( str1, str2 );
154 /***********************************************************************
155 * lstrcpy16 (KERNEL.88)
157 SEGPTR WINAPI lstrcpy16( SEGPTR dst, LPCSTR src )
159 if (!lstrcpyA( MapSL(dst), src )) dst = 0;
160 return dst;
164 /***********************************************************************
165 * lstrcpyA (KERNEL32.608)
167 LPSTR WINAPI lstrcpyA( LPSTR dst, LPCSTR src )
169 __TRY
171 /* this is how Windows does it */
172 memmove( dst, src, strlen(src)+1 );
174 __EXCEPT(page_fault)
176 ERR("(%p, %p): page fault occurred ! Caused by bug ?\n", dst, src);
177 SetLastError( ERROR_INVALID_PARAMETER );
178 return NULL;
180 __ENDTRY
181 return dst;
185 /***********************************************************************
186 * lstrcpyW (KERNEL32.609)
188 LPWSTR WINAPI lstrcpyW( LPWSTR dst, LPCWSTR src )
190 __TRY
192 strcpyW( dst, src );
194 __EXCEPT(page_fault)
196 SetLastError( ERROR_INVALID_PARAMETER );
197 return NULL;
199 __ENDTRY
200 return dst;
204 /***********************************************************************
205 * lstrcpyn16 (KERNEL.353)
207 SEGPTR WINAPI lstrcpyn16( SEGPTR dst, LPCSTR src, INT16 n )
209 lstrcpynA( MapSL(dst), src, n );
210 return dst;
214 /***********************************************************************
215 * lstrcpynA (KERNEL32.611)
216 * Note: this function differs from the UNIX strncpy, it _always_ writes
217 * a terminating \0
219 LPSTR WINAPI lstrcpynA( LPSTR dst, LPCSTR src, INT n )
221 LPSTR p = dst;
222 TRACE("(%p, %s, %i)\n", dst, debugstr_an(src,n), n);
223 /* In real windows the whole function is protected by an exception handler
224 * that returns ERROR_INVALID_PARAMETER on faulty parameters
225 * We currently just check for NULL.
227 if (!dst || !src) {
228 SetLastError(ERROR_INVALID_PARAMETER);
229 return 0;
231 while ((n-- > 1) && *src) *p++ = *src++;
232 if (n >= 0) *p = 0;
233 return dst;
237 /***********************************************************************
238 * lstrcpynW (KERNEL32.612)
239 * Note: this function differs from the UNIX strncpy, it _always_ writes
240 * a terminating \0
242 LPWSTR WINAPI lstrcpynW( LPWSTR dst, LPCWSTR src, INT n )
244 LPWSTR p = dst;
245 TRACE("(%p, %s, %i)\n", dst, debugstr_wn(src,n), n);
246 /* In real windows the whole function is protected by an exception handler
247 * that returns ERROR_INVALID_PARAMETER on faulty parameters
248 * We currently just check for NULL.
250 if (!dst || !src) {
251 SetLastError(ERROR_INVALID_PARAMETER);
252 return 0;
254 while ((n-- > 1) && *src) *p++ = *src++;
255 if (n >= 0) *p = 0;
256 return dst;
260 /***********************************************************************
261 * lstrlen16 (KERNEL.90)
263 INT16 WINAPI lstrlen16( LPCSTR str )
265 return (INT16)lstrlenA( str );
269 /***********************************************************************
270 * lstrlenA (KERNEL32.614)
272 INT WINAPI lstrlenA( LPCSTR str )
274 INT ret;
275 __TRY
277 ret = strlen(str);
279 __EXCEPT(page_fault)
281 SetLastError( ERROR_INVALID_PARAMETER );
282 return 0;
284 __ENDTRY
285 return ret;
289 /***********************************************************************
290 * lstrlenW (KERNEL32.615)
292 INT WINAPI lstrlenW( LPCWSTR str )
294 INT ret;
295 __TRY
297 ret = strlenW(str);
299 __EXCEPT(page_fault)
301 SetLastError( ERROR_INVALID_PARAMETER );
302 return 0;
304 __ENDTRY
305 return ret;
309 /***********************************************************************
310 * UnicodeToAnsi (KERNEL.434)
312 INT16 WINAPI UnicodeToAnsi16( LPCWSTR src, LPSTR dst, INT16 codepage )
314 if ( codepage == -1 )
315 codepage = CP_ACP;
317 return WideCharToMultiByte( codepage, 0, src, -1, dst, 0x7fffffff, NULL, NULL );