Fixed header dependencies to be fully compatible with the Windows
[wine/multimedia.git] / memory / string.c
blob57df2e829fa84f9ac31455bf5d8fbaba432c982c
1 /*
2 * String functions
4 * Copyright 1993 Yngvi Sigurjonsson
5 * Copyright 1996 Alexandre Julliard
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <ctype.h>
23 #include <stdarg.h>
24 #include <string.h>
26 #include "ntstatus.h"
27 #include "windef.h"
28 #include "winbase.h"
29 #include "wine/winbase16.h"
30 #include "wine/exception.h"
31 #include "wine/unicode.h"
32 #include "winerror.h"
33 #include "winnls.h"
34 #include "excpt.h"
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(string);
39 /* filter for page-fault exceptions */
40 static WINE_EXCEPTION_FILTER(page_fault)
42 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
43 return EXCEPTION_EXECUTE_HANDLER;
44 return EXCEPTION_CONTINUE_SEARCH;
48 /***********************************************************************
49 * hmemcpy (KERNEL.348)
51 void WINAPI hmemcpy16( LPVOID dst, LPCVOID src, LONG count )
53 memcpy( dst, src, count );
57 /***********************************************************************
58 * lstrcat (KERNEL.89)
60 SEGPTR WINAPI lstrcat16( SEGPTR dst, LPCSTR src )
62 /* Windows does not check for NULL pointers here, so we don't either */
63 strcat( MapSL(dst), src );
64 return dst;
68 /***********************************************************************
69 * lstrcat (KERNEL32.@)
70 * lstrcatA (KERNEL32.@)
72 LPSTR WINAPI lstrcatA( LPSTR dst, LPCSTR src )
74 __TRY
76 strcat( dst, src );
78 __EXCEPT(page_fault)
80 SetLastError( ERROR_INVALID_PARAMETER );
81 return NULL;
83 __ENDTRY
84 return dst;
88 /***********************************************************************
89 * lstrcatW (KERNEL32.@)
91 LPWSTR WINAPI lstrcatW( LPWSTR dst, LPCWSTR src )
93 __TRY
95 strcatW( dst, src );
97 __EXCEPT(page_fault)
99 SetLastError( ERROR_INVALID_PARAMETER );
100 return NULL;
102 __ENDTRY
103 return dst;
107 /***********************************************************************
108 * lstrcatn (KERNEL.352)
110 SEGPTR WINAPI lstrcatn16( SEGPTR dst, LPCSTR src, INT16 n )
112 LPSTR p = MapSL(dst);
113 LPSTR start = p;
115 while (*p) p++;
116 if ((n -= (p - start)) <= 0) return dst;
117 lstrcpynA( p, src, n );
118 return dst;
122 /***********************************************************************
123 * lstrcpy (KERNEL.88)
125 SEGPTR WINAPI lstrcpy16( SEGPTR dst, LPCSTR src )
127 if (!lstrcpyA( MapSL(dst), src )) dst = 0;
128 return dst;
132 /***********************************************************************
133 * lstrcpy (KERNEL32.@)
134 * lstrcpyA (KERNEL32.@)
136 LPSTR WINAPI lstrcpyA( LPSTR dst, LPCSTR src )
138 __TRY
140 /* this is how Windows does it */
141 memmove( dst, src, strlen(src)+1 );
143 __EXCEPT(page_fault)
145 ERR("(%p, %p): page fault occurred ! Caused by bug ?\n", dst, src);
146 SetLastError( ERROR_INVALID_PARAMETER );
147 return NULL;
149 __ENDTRY
150 return dst;
154 /***********************************************************************
155 * lstrcpyW (KERNEL32.@)
157 LPWSTR WINAPI lstrcpyW( LPWSTR dst, LPCWSTR src )
159 __TRY
161 strcpyW( dst, src );
163 __EXCEPT(page_fault)
165 SetLastError( ERROR_INVALID_PARAMETER );
166 return NULL;
168 __ENDTRY
169 return dst;
173 /***********************************************************************
174 * lstrcpyn (KERNEL.353)
176 SEGPTR WINAPI lstrcpyn16( SEGPTR dst, LPCSTR src, INT16 n )
178 lstrcpynA( MapSL(dst), src, n );
179 return dst;
183 /***********************************************************************
184 * lstrcpyn (KERNEL32.@)
185 * lstrcpynA (KERNEL32.@)
187 * Note: this function differs from the UNIX strncpy, it _always_ writes
188 * a terminating \0.
190 * Note: n is an INT but Windows treats it as unsigned, and will happily
191 * copy a gazillion chars if n is negative.
193 LPSTR WINAPI lstrcpynA( LPSTR dst, LPCSTR src, INT n )
195 LPSTR p = dst;
196 UINT count = n;
198 TRACE("(%p, %s, %i)\n", dst, debugstr_a(src), n);
200 /* In real windows the whole function is protected by an exception handler
201 * that returns ERROR_INVALID_PARAMETER on faulty parameters
202 * We currently just check for NULL.
204 if (!dst || !src) {
205 SetLastError(ERROR_INVALID_PARAMETER);
206 return 0;
208 while ((count > 1) && *src)
210 count--;
211 *p++ = *src++;
213 if (count) *p = 0;
214 return dst;
218 /***********************************************************************
219 * lstrcpynW (KERNEL32.@)
221 * Note: this function differs from the UNIX strncpy, it _always_ writes
222 * a terminating \0
224 * Note: n is an INT but Windows treats it as unsigned, and will happily
225 * copy a gazillion chars if n is negative.
227 LPWSTR WINAPI lstrcpynW( LPWSTR dst, LPCWSTR src, INT n )
229 LPWSTR p = dst;
230 UINT count = n;
232 TRACE("(%p, %s, %i)\n", dst, debugstr_w(src), n);
234 /* In real windows the whole function is protected by an exception handler
235 * that returns ERROR_INVALID_PARAMETER on faulty parameters
236 * We currently just check for NULL.
238 if (!dst || !src) {
239 SetLastError(ERROR_INVALID_PARAMETER);
240 return 0;
242 while ((count > 1) && *src)
244 count--;
245 *p++ = *src++;
247 if (count) *p = 0;
248 return dst;
252 /***********************************************************************
253 * lstrlen (KERNEL.90)
255 INT16 WINAPI lstrlen16( LPCSTR str )
257 return (INT16)lstrlenA( str );
261 /***********************************************************************
262 * lstrlen (KERNEL32.@)
263 * lstrlenA (KERNEL32.@)
265 INT WINAPI lstrlenA( LPCSTR str )
267 INT ret;
268 __TRY
270 ret = strlen(str);
272 __EXCEPT(page_fault)
274 SetLastError( ERROR_INVALID_PARAMETER );
275 return 0;
277 __ENDTRY
278 return ret;
282 /***********************************************************************
283 * lstrlenW (KERNEL32.@)
285 INT WINAPI lstrlenW( LPCWSTR str )
287 INT ret;
288 __TRY
290 ret = strlenW(str);
292 __EXCEPT(page_fault)
294 SetLastError( ERROR_INVALID_PARAMETER );
295 return 0;
297 __ENDTRY
298 return ret;
302 /***********************************************************************
303 * UnicodeToAnsi (KERNEL.434)
305 INT16 WINAPI UnicodeToAnsi16( LPCWSTR src, LPSTR dst, INT16 codepage )
307 if ( codepage == -1 )
308 codepage = CP_ACP;
310 return WideCharToMultiByte( codepage, 0, src, -1, dst, 0x7fffffff, NULL, NULL );