New set of macros for server calls; makes requests without variable
[wine] / memory / string.c
1 /*
2  * String functions
3  *
4  * Copyright 1993 Yngvi Sigurjonsson
5  * Copyright 1996 Alexandre Julliard
6  */
7
8 #include <ctype.h>
9 #include <string.h>
10
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"
19
20 DEFAULT_DEBUG_CHANNEL(string);
21
22 /* filter for page-fault exceptions */
23 static WINE_EXCEPTION_FILTER(page_fault)
24 {
25     if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
26         return EXCEPTION_EXECUTE_HANDLER;
27     return EXCEPTION_CONTINUE_SEARCH;
28 }
29
30
31 /***********************************************************************
32  *           hmemcpy16   (KERNEL.348)
33  */
34 void WINAPI hmemcpy16( LPVOID dst, LPCVOID src, LONG count )
35 {
36     memcpy( dst, src, count );
37 }
38
39
40 /***********************************************************************
41  *           lstrcat16   (KERNEL.89)
42  */
43 SEGPTR WINAPI lstrcat16( SEGPTR dst, LPCSTR src )
44 {
45     /* Windows does not check for NULL pointers here, so we don't either */
46     strcat( MapSL(dst), src );
47     return dst;
48 }
49
50
51 /***********************************************************************
52  *           lstrcatA   (KERNEL32.599)
53  */
54 LPSTR WINAPI lstrcatA( LPSTR dst, LPCSTR src )
55 {
56     __TRY
57     {
58         strcat( dst, src );
59     }
60     __EXCEPT(page_fault)
61     {
62         SetLastError( ERROR_INVALID_PARAMETER );
63         return NULL;
64     }
65     __ENDTRY
66     return dst;
67 }
68
69
70 /***********************************************************************
71  *           lstrcatW   (KERNEL32.600)
72  */
73 LPWSTR WINAPI lstrcatW( LPWSTR dst, LPCWSTR src )
74 {
75     __TRY
76     {
77         strcatW( dst, src );
78     }
79     __EXCEPT(page_fault)
80     {
81         SetLastError( ERROR_INVALID_PARAMETER );
82         return NULL;
83     }
84     __ENDTRY
85     return dst;
86 }
87
88
89 /***********************************************************************
90  *           lstrcatn16   (KERNEL.352)
91  */
92 SEGPTR WINAPI lstrcatn16( SEGPTR dst, LPCSTR src, INT16 n )
93 {
94     LPSTR p = MapSL(dst);
95     LPSTR start = p;
96
97     while (*p) p++;
98     if ((n -= (p - start)) <= 0) return dst;
99     lstrcpynA( p, src, n );
100     return dst;
101 }
102
103
104 /***********************************************************************
105  *           lstrcmpA   (KERNEL.602)
106  */
107 INT WINAPI lstrcmpA( LPCSTR str1, LPCSTR str2 )
108 {
109     return CompareStringA(LOCALE_SYSTEM_DEFAULT,0,str1,-1,str2,-1) - 2 ;
110 }
111
112
113 /***********************************************************************
114  *           lstrcmpW   (KERNEL.603)
115  * FIXME : should call CompareStringW, when it is implemented.
116  *    This implementation is not "word sort", as it should.
117  */
118 INT WINAPI lstrcmpW( LPCWSTR str1, LPCWSTR str2 )
119 {
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;
125     }
126     while (*str1 && (*str1 == *str2)) { str1++; str2++; }
127     return (INT)(*str1 - *str2);
128 }
129
130
131 /***********************************************************************
132  *           lstrcmpiA   (KERNEL32.605)
133  */
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;
138 }
139
140
141 /***********************************************************************
142  *           lstrcmpiW   (KERNEL32.606)
143  */
144 INT WINAPI lstrcmpiW( LPCWSTR str1, LPCWSTR str2 )
145 {
146     if (!str1 || !str2) {
147         SetLastError(ERROR_INVALID_PARAMETER);
148         return 0;
149     }
150     return strcmpiW( str1, str2 );
151 }
152
153
154 /***********************************************************************
155  *           lstrcpy16   (KERNEL.88)
156  */
157 SEGPTR WINAPI lstrcpy16( SEGPTR dst, LPCSTR src )
158 {
159     if (!lstrcpyA( MapSL(dst), src )) dst = 0;
160     return dst;
161 }
162
163
164 /***********************************************************************
165  *           lstrcpyA   (KERNEL32.608)
166  */
167 LPSTR WINAPI lstrcpyA( LPSTR dst, LPCSTR src )
168 {
169     __TRY
170     {
171         /* this is how Windows does it */
172         memmove( dst, src, strlen(src)+1 );
173     }
174     __EXCEPT(page_fault)
175     {
176         ERR("(%p, %p): page fault occurred ! Caused by bug ?\n", dst, src);
177         SetLastError( ERROR_INVALID_PARAMETER );
178         return NULL;
179     }
180     __ENDTRY
181     return dst;
182 }
183
184
185 /***********************************************************************
186  *           lstrcpyW   (KERNEL32.609)
187  */
188 LPWSTR WINAPI lstrcpyW( LPWSTR dst, LPCWSTR src )
189 {
190     __TRY
191     {
192         strcpyW( dst, src );
193     }
194     __EXCEPT(page_fault)
195     {
196         SetLastError( ERROR_INVALID_PARAMETER );
197         return NULL;
198     }
199     __ENDTRY
200     return dst;
201 }
202
203
204 /***********************************************************************
205  *           lstrcpyn16   (KERNEL.353)
206  */
207 SEGPTR WINAPI lstrcpyn16( SEGPTR dst, LPCSTR src, INT16 n )
208 {
209     lstrcpynA( MapSL(dst), src, n );
210     return dst;
211 }
212
213
214 /***********************************************************************
215  *           lstrcpynA   (KERNEL32.611)
216  * Note: this function differs from the UNIX strncpy, it _always_ writes
217  * a terminating \0
218  */
219 LPSTR WINAPI lstrcpynA( LPSTR dst, LPCSTR src, INT n )
220 {
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.
226      */
227     if (!dst || !src) {
228         SetLastError(ERROR_INVALID_PARAMETER);
229         return 0;
230     }
231     while ((n-- > 1) && *src) *p++ = *src++;
232     if (n >= 0) *p = 0;
233     return dst;
234 }
235
236
237 /***********************************************************************
238  *           lstrcpynW   (KERNEL32.612)
239  * Note: this function differs from the UNIX strncpy, it _always_ writes
240  * a terminating \0
241  */
242 LPWSTR WINAPI lstrcpynW( LPWSTR dst, LPCWSTR src, INT n )
243 {
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.
249      */
250     if (!dst || !src) {
251         SetLastError(ERROR_INVALID_PARAMETER);
252         return 0;
253     }
254     while ((n-- > 1) && *src) *p++ = *src++;
255     if (n >= 0) *p = 0;
256     return dst;
257 }
258
259
260 /***********************************************************************
261  *           lstrlen16   (KERNEL.90)
262  */
263 INT16 WINAPI lstrlen16( LPCSTR str )
264 {
265     return (INT16)lstrlenA( str );
266 }
267
268
269 /***********************************************************************
270  *           lstrlenA   (KERNEL32.614)
271  */
272 INT WINAPI lstrlenA( LPCSTR str )
273 {
274     INT ret;
275     __TRY
276     {
277         ret = strlen(str);
278     }
279     __EXCEPT(page_fault)
280     {
281         SetLastError( ERROR_INVALID_PARAMETER );
282         return 0;
283     }
284     __ENDTRY
285     return ret;
286 }
287
288
289 /***********************************************************************
290  *           lstrlenW   (KERNEL32.615)
291  */
292 INT WINAPI lstrlenW( LPCWSTR str )
293 {
294     INT ret;
295     __TRY
296     {
297         ret = strlenW(str);
298     }
299     __EXCEPT(page_fault)
300     {
301         SetLastError( ERROR_INVALID_PARAMETER );
302         return 0;
303     }
304     __ENDTRY
305     return ret;
306 }
307
308
309 /***********************************************************************
310  *           UnicodeToAnsi   (KERNEL.434)
311  */
312 INT16 WINAPI UnicodeToAnsi16( LPCWSTR src, LPSTR dst, INT16 codepage )
313 {
314     if ( codepage == -1 )
315         codepage = CP_ACP;
316
317     return WideCharToMultiByte( codepage, 0, src, -1, dst, 0x7fffffff, NULL, NULL );
318 }