Updated README.fr so it's in line with README.
[wine] / memory / string.c
1 /*
2  * String functions
3  *
4  * Copyright 1993 Yngvi Sigurjonsson
5  * Copyright 1996 Alexandre Julliard
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include <ctype.h>
23 #include <string.h>
24
25 #include "windef.h"
26 #include "winbase.h"
27 #include "wine/winbase16.h"
28 #include "wine/exception.h"
29 #include "wine/unicode.h"
30 #include "winerror.h"
31 #include "winnls.h"
32 #include "msvcrt/excpt.h"
33 #include "wine/debug.h"
34
35 WINE_DEFAULT_DEBUG_CHANNEL(string);
36
37 /* filter for page-fault exceptions */
38 static WINE_EXCEPTION_FILTER(page_fault)
39 {
40     if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
41         return EXCEPTION_EXECUTE_HANDLER;
42     return EXCEPTION_CONTINUE_SEARCH;
43 }
44
45
46 /***********************************************************************
47  *           hmemcpy   (KERNEL.348)
48  */
49 void WINAPI hmemcpy16( LPVOID dst, LPCVOID src, LONG count )
50 {
51     memcpy( dst, src, count );
52 }
53
54
55 /***********************************************************************
56  *           lstrcat   (KERNEL.89)
57  */
58 SEGPTR WINAPI lstrcat16( SEGPTR dst, LPCSTR src )
59 {
60     /* Windows does not check for NULL pointers here, so we don't either */
61     strcat( MapSL(dst), src );
62     return dst;
63 }
64
65
66 /***********************************************************************
67  *           lstrcat    (KERNEL32.@)
68  *           lstrcatA   (KERNEL32.@)
69  */
70 LPSTR WINAPI lstrcatA( LPSTR dst, LPCSTR src )
71 {
72     __TRY
73     {
74         strcat( dst, src );
75     }
76     __EXCEPT(page_fault)
77     {
78         SetLastError( ERROR_INVALID_PARAMETER );
79         return NULL;
80     }
81     __ENDTRY
82     return dst;
83 }
84
85
86 /***********************************************************************
87  *           lstrcatW   (KERNEL32.@)
88  */
89 LPWSTR WINAPI lstrcatW( LPWSTR dst, LPCWSTR src )
90 {
91     __TRY
92     {
93         strcatW( dst, src );
94     }
95     __EXCEPT(page_fault)
96     {
97         SetLastError( ERROR_INVALID_PARAMETER );
98         return NULL;
99     }
100     __ENDTRY
101     return dst;
102 }
103
104
105 /***********************************************************************
106  *           lstrcatn   (KERNEL.352)
107  */
108 SEGPTR WINAPI lstrcatn16( SEGPTR dst, LPCSTR src, INT16 n )
109 {
110     LPSTR p = MapSL(dst);
111     LPSTR start = p;
112
113     while (*p) p++;
114     if ((n -= (p - start)) <= 0) return dst;
115     lstrcpynA( p, src, n );
116     return dst;
117 }
118
119
120 /***********************************************************************
121  *           lstrcmp    (KERNEL32.@)
122  *           lstrcmpA   (KERNEL32.@)
123  */
124 INT WINAPI lstrcmpA( LPCSTR str1, LPCSTR str2 )
125 {
126     return CompareStringA(LOCALE_SYSTEM_DEFAULT,0,str1,-1,str2,-1) - 2 ;
127 }
128
129
130 /***********************************************************************
131  *           lstrcmpW   (KERNEL32.@)
132  * FIXME : should call CompareStringW, when it is implemented.
133  *    This implementation is not "word sort", as it should.
134  */
135 INT WINAPI lstrcmpW( LPCWSTR str1, LPCWSTR str2 )
136 {
137     TRACE("%s and %s\n",
138                    debugstr_w (str1), debugstr_w (str2));
139     if (!str1 || !str2) {
140         SetLastError(ERROR_INVALID_PARAMETER);
141         return 0;
142     }
143     while (*str1 && (*str1 == *str2)) { str1++; str2++; }
144     return (INT)(*str1 - *str2);
145 }
146
147
148 /***********************************************************************
149  *           lstrcmpi    (KERNEL32.@)
150  *           lstrcmpiA   (KERNEL32.@)
151  */
152 INT WINAPI lstrcmpiA( LPCSTR str1, LPCSTR str2 )
153 {    TRACE("strcmpi %s and %s\n",
154                    debugstr_a (str1), debugstr_a (str2));
155     return CompareStringA(LOCALE_SYSTEM_DEFAULT,NORM_IGNORECASE,str1,-1,str2,-1)-2;
156 }
157
158
159 /***********************************************************************
160  *           lstrcmpiW   (KERNEL32.@)
161  */
162 INT WINAPI lstrcmpiW( LPCWSTR str1, LPCWSTR str2 )
163 {
164     if (!str1 || !str2) {
165         SetLastError(ERROR_INVALID_PARAMETER);
166         return 0;
167     }
168     return strcmpiW( str1, str2 );
169 }
170
171
172 /***********************************************************************
173  *           lstrcpy   (KERNEL.88)
174  */
175 SEGPTR WINAPI lstrcpy16( SEGPTR dst, LPCSTR src )
176 {
177     if (!lstrcpyA( MapSL(dst), src )) dst = 0;
178     return dst;
179 }
180
181
182 /***********************************************************************
183  *           lstrcpy    (KERNEL32.@)
184  *           lstrcpyA   (KERNEL32.@)
185  */
186 LPSTR WINAPI lstrcpyA( LPSTR dst, LPCSTR src )
187 {
188     __TRY
189     {
190         /* this is how Windows does it */
191         memmove( dst, src, strlen(src)+1 );
192     }
193     __EXCEPT(page_fault)
194     {
195         ERR("(%p, %p): page fault occurred ! Caused by bug ?\n", dst, src);
196         SetLastError( ERROR_INVALID_PARAMETER );
197         return NULL;
198     }
199     __ENDTRY
200     return dst;
201 }
202
203
204 /***********************************************************************
205  *           lstrcpyW   (KERNEL32.@)
206  */
207 LPWSTR WINAPI lstrcpyW( LPWSTR dst, LPCWSTR src )
208 {
209     __TRY
210     {
211         strcpyW( dst, src );
212     }
213     __EXCEPT(page_fault)
214     {
215         SetLastError( ERROR_INVALID_PARAMETER );
216         return NULL;
217     }
218     __ENDTRY
219     return dst;
220 }
221
222
223 /***********************************************************************
224  *           lstrcpyn   (KERNEL.353)
225  */
226 SEGPTR WINAPI lstrcpyn16( SEGPTR dst, LPCSTR src, INT16 n )
227 {
228     lstrcpynA( MapSL(dst), src, n );
229     return dst;
230 }
231
232
233 /***********************************************************************
234  *           lstrcpyn    (KERNEL32.@)
235  *           lstrcpynA   (KERNEL32.@)
236  *
237  * Note: this function differs from the UNIX strncpy, it _always_ writes
238  * a terminating \0
239  */
240 LPSTR WINAPI lstrcpynA( LPSTR dst, LPCSTR src, INT n )
241 {
242     LPSTR p = dst;
243     TRACE("(%p, %s, %i)\n", dst, debugstr_an(src,n), n);
244     /* In real windows the whole function is protected by an exception handler
245      * that returns ERROR_INVALID_PARAMETER on faulty parameters
246      * We currently just check for NULL.
247      */
248     if (!dst || !src) {
249         SetLastError(ERROR_INVALID_PARAMETER);
250         return 0;
251     }
252     while ((n-- > 1) && *src) *p++ = *src++;
253     if (n >= 0) *p = 0;
254     return dst;
255 }
256
257
258 /***********************************************************************
259  *           lstrcpynW   (KERNEL32.@)
260  * Note: this function differs from the UNIX strncpy, it _always_ writes
261  * a terminating \0
262  */
263 LPWSTR WINAPI lstrcpynW( LPWSTR dst, LPCWSTR src, INT n )
264 {
265     LPWSTR p = dst;
266     TRACE("(%p, %s, %i)\n", dst,  debugstr_wn(src,n), n);
267     /* In real windows the whole function is protected by an exception handler
268      * that returns ERROR_INVALID_PARAMETER on faulty parameters
269      * We currently just check for NULL.
270      */
271     if (!dst || !src) {
272         SetLastError(ERROR_INVALID_PARAMETER);
273         return 0;
274     }
275     while ((n-- > 1) && *src) *p++ = *src++;
276     if (n >= 0) *p = 0;
277     return dst;
278 }
279
280
281 /***********************************************************************
282  *           lstrlen   (KERNEL.90)
283  */
284 INT16 WINAPI lstrlen16( LPCSTR str )
285 {
286     return (INT16)lstrlenA( str );
287 }
288
289
290 /***********************************************************************
291  *           lstrlen    (KERNEL32.@)
292  *           lstrlenA   (KERNEL32.@)
293  */
294 INT WINAPI lstrlenA( LPCSTR str )
295 {
296     INT ret;
297     __TRY
298     {
299         ret = strlen(str);
300     }
301     __EXCEPT(page_fault)
302     {
303         SetLastError( ERROR_INVALID_PARAMETER );
304         return 0;
305     }
306     __ENDTRY
307     return ret;
308 }
309
310
311 /***********************************************************************
312  *           lstrlenW   (KERNEL32.@)
313  */
314 INT WINAPI lstrlenW( LPCWSTR str )
315 {
316     INT ret;
317     __TRY
318     {
319         ret = strlenW(str);
320     }
321     __EXCEPT(page_fault)
322     {
323         SetLastError( ERROR_INVALID_PARAMETER );
324         return 0;
325     }
326     __ENDTRY
327     return ret;
328 }
329
330
331 /***********************************************************************
332  *           UnicodeToAnsi   (KERNEL.434)
333  */
334 INT16 WINAPI UnicodeToAnsi16( LPCWSTR src, LPSTR dst, INT16 codepage )
335 {
336     if ( codepage == -1 )
337         codepage = CP_ACP;
338
339     return WideCharToMultiByte( codepage, 0, src, -1, dst, 0x7fffffff, NULL, NULL );
340 }