Split bidi16.c and network.c out of misc/ into their respective dlls.
[wine.git] / memory / string.c
blob7306998c3ec740de16db080f641e553c483e12ee
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 "wingdi.h"
14 #include "winuser.h"
15 #include "wine/winbase16.h"
16 #include "wine/winuser16.h"
17 #include "wine/keyboard16.h"
18 #include "wine/exception.h"
19 #include "wine/unicode.h"
20 #include "winerror.h"
21 #include "ldt.h"
22 #include "debugtools.h"
23 #include "winnls.h"
25 DEFAULT_DEBUG_CHANNEL(string);
27 /* Internaly used by strchr family functions */
28 static BOOL ChrCmpA( WORD word1, WORD word2);
31 /* filter for page-fault exceptions */
32 static WINE_EXCEPTION_FILTER(page_fault)
34 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
35 return EXCEPTION_EXECUTE_HANDLER;
36 return EXCEPTION_CONTINUE_SEARCH;
40 /***********************************************************************
41 * hmemcpy (KERNEL.348)
43 void WINAPI hmemcpy16( LPVOID dst, LPCVOID src, LONG count )
45 memcpy( dst, src, count );
49 /***********************************************************************
50 * lstrcat16 (KERNEL.89)
52 SEGPTR WINAPI lstrcat16( SEGPTR dst, LPCSTR src )
54 /* Windows does not check for NULL pointers here, so we don't either */
55 strcat( (LPSTR)PTR_SEG_TO_LIN(dst), src );
56 return dst;
60 /***********************************************************************
61 * lstrcatA (KERNEL32.599)
63 LPSTR WINAPI lstrcatA( LPSTR dst, LPCSTR src )
65 __TRY
67 strcat( dst, src );
69 __EXCEPT(page_fault)
71 SetLastError( ERROR_INVALID_PARAMETER );
72 return NULL;
74 __ENDTRY
75 return dst;
79 /***********************************************************************
80 * lstrcatW (KERNEL32.600)
82 LPWSTR WINAPI lstrcatW( LPWSTR dst, LPCWSTR src )
84 __TRY
86 strcatW( dst, src );
88 __EXCEPT(page_fault)
90 SetLastError( ERROR_INVALID_PARAMETER );
91 return NULL;
93 __ENDTRY
94 return dst;
98 /***********************************************************************
99 * lstrcatn16 (KERNEL.352)
101 SEGPTR WINAPI lstrcatn16( SEGPTR dst, LPCSTR src, INT16 n )
103 LPSTR p = (LPSTR)PTR_SEG_TO_LIN(dst);
105 while (*p) p++;
106 if ((n -= (p - (LPSTR)PTR_SEG_TO_LIN(dst))) <= 0) return dst;
107 lstrcpynA( p, src, n );
108 return dst;
112 /***********************************************************************
113 * lstrcmp16 (USER.430)
115 INT16 WINAPI lstrcmp16( LPCSTR str1, LPCSTR str2 )
117 return (INT16)strcmp( str1, str2 );
121 /***********************************************************************
122 * lstrcmpA (KERNEL.602)
124 INT WINAPI lstrcmpA( LPCSTR str1, LPCSTR str2 )
126 return CompareStringA(LOCALE_SYSTEM_DEFAULT,0,str1,-1,str2,-1) - 2 ;
130 /***********************************************************************
131 * lstrcmpW (KERNEL.603)
132 * FIXME : should call CompareStringW, when it is implemented.
133 * This implementation is not "word sort", as it should.
135 INT WINAPI lstrcmpW( LPCWSTR str1, LPCWSTR str2 )
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;
143 while (*str1 && (*str1 == *str2)) { str1++; str2++; }
144 return (INT)(*str1 - *str2);
148 /***********************************************************************
149 * lstrcmpi16 (USER.471)
151 INT16 WINAPI lstrcmpi16( LPCSTR str1, LPCSTR str2 )
153 return (INT16)lstrcmpiA( str1, str2 );
157 /***********************************************************************
158 * lstrcmpiA (KERNEL32.605)
160 INT WINAPI lstrcmpiA( LPCSTR str1, LPCSTR str2 )
161 { TRACE("strcmpi %s and %s\n",
162 debugstr_a (str1), debugstr_a (str2));
163 return CompareStringA(LOCALE_SYSTEM_DEFAULT,NORM_IGNORECASE,str1,-1,str2,-1)-2;
167 /***********************************************************************
168 * lstrcmpiW (KERNEL32.606)
170 INT WINAPI lstrcmpiW( LPCWSTR str1, LPCWSTR str2 )
172 if (!str1 || !str2) {
173 SetLastError(ERROR_INVALID_PARAMETER);
174 return 0;
176 return strcmpiW( str1, str2 );
180 /***********************************************************************
181 * lstrcpy16 (KERNEL.88)
183 SEGPTR WINAPI lstrcpy16( SEGPTR dst, LPCSTR src )
185 if (!lstrcpyA( PTR_SEG_TO_LIN(dst), src )) dst = 0;
186 return dst;
190 /***********************************************************************
191 * lstrcpyA (KERNEL32.608)
193 LPSTR WINAPI lstrcpyA( LPSTR dst, LPCSTR src )
195 __TRY
197 /* this is how Windows does it */
198 memmove( dst, src, strlen(src)+1 );
200 __EXCEPT(page_fault)
202 ERR("(%p, %p): page fault occurred ! Caused by bug ?\n", dst, src);
203 SetLastError( ERROR_INVALID_PARAMETER );
204 return NULL;
206 __ENDTRY
207 return dst;
211 /***********************************************************************
212 * lstrcpyW (KERNEL32.609)
214 LPWSTR WINAPI lstrcpyW( LPWSTR dst, LPCWSTR src )
216 __TRY
218 strcpyW( dst, src );
220 __EXCEPT(page_fault)
222 SetLastError( ERROR_INVALID_PARAMETER );
223 return NULL;
225 __ENDTRY
226 return dst;
230 /***********************************************************************
231 * lstrcpyn16 (KERNEL.353)
233 SEGPTR WINAPI lstrcpyn16( SEGPTR dst, LPCSTR src, INT16 n )
235 lstrcpynA( (LPSTR)PTR_SEG_TO_LIN(dst), src, n );
236 return dst;
240 /***********************************************************************
241 * lstrcpynA (KERNEL32.611)
242 * Note: this function differs from the UNIX strncpy, it _always_ writes
243 * a terminating \0
245 LPSTR WINAPI lstrcpynA( LPSTR dst, LPCSTR src, INT n )
247 LPSTR p = dst;
248 TRACE("(%p, %s, %i)\n", dst, debugstr_an(src,n), n);
249 /* In real windows the whole function is protected by an exception handler
250 * that returns ERROR_INVALID_PARAMETER on faulty parameters
251 * We currently just check for NULL.
253 if (!dst || !src) {
254 SetLastError(ERROR_INVALID_PARAMETER);
255 return 0;
257 while ((n-- > 1) && *src) *p++ = *src++;
258 if (n >= 0) *p = 0;
259 return dst;
263 /***********************************************************************
264 * lstrcpynW (KERNEL32.612)
265 * Note: this function differs from the UNIX strncpy, it _always_ writes
266 * a terminating \0
268 LPWSTR WINAPI lstrcpynW( LPWSTR dst, LPCWSTR src, INT n )
270 LPWSTR p = dst;
271 TRACE("(%p, %s, %i)\n", dst, debugstr_wn(src,n), n);
272 /* In real windows the whole function is protected by an exception handler
273 * that returns ERROR_INVALID_PARAMETER on faulty parameters
274 * We currently just check for NULL.
276 if (!dst || !src) {
277 SetLastError(ERROR_INVALID_PARAMETER);
278 return 0;
280 while ((n-- > 1) && *src) *p++ = *src++;
281 if (n >= 0) *p = 0;
282 return dst;
286 /***********************************************************************
287 * lstrlen16 (KERNEL.90)
289 INT16 WINAPI lstrlen16( LPCSTR str )
291 return (INT16)lstrlenA( str );
295 /***********************************************************************
296 * lstrlenA (KERNEL32.614)
298 INT WINAPI lstrlenA( LPCSTR str )
300 INT ret;
301 __TRY
303 ret = strlen(str);
305 __EXCEPT(page_fault)
307 SetLastError( ERROR_INVALID_PARAMETER );
308 return 0;
310 __ENDTRY
311 return ret;
315 /***********************************************************************
316 * lstrlenW (KERNEL32.615)
318 INT WINAPI lstrlenW( LPCWSTR str )
320 INT ret;
321 __TRY
323 ret = strlenW(str);
325 __EXCEPT(page_fault)
327 SetLastError( ERROR_INVALID_PARAMETER );
328 return 0;
330 __ENDTRY
331 return ret;
335 /***********************************************************************
336 * lstrcpyAtoW (Not a Windows API)
338 LPWSTR WINAPI lstrcpyAtoW( LPWSTR dst, LPCSTR src )
340 MultiByteToWideChar( CP_ACP, 0, src, -1, dst, 0x7fffffff );
341 return dst;
345 /***********************************************************************
346 * lstrcpyWtoA (Not a Windows API)
348 LPSTR WINAPI lstrcpyWtoA( LPSTR dst, LPCWSTR src )
350 WideCharToMultiByte( CP_ACP, 0, src, -1, dst, 0x7fffffff, NULL, NULL );
351 return dst;
355 /***********************************************************************
356 * lstrcpynAtoW (Not a Windows API)
357 * Note: this function differs from the UNIX strncpy, it _always_ writes
358 * a terminating \0
360 LPWSTR WINAPI lstrcpynAtoW( LPWSTR dst, LPCSTR src, INT n )
362 if (n > 0 && !MultiByteToWideChar( CP_ACP, 0, src, -1, dst, n )) dst[n-1] = 0;
363 return dst;
367 /***********************************************************************
368 * lstrcpynWtoA (Not a Windows API)
369 * Note: this function differs from the UNIX strncpy, it _always_ writes
370 * a terminating \0
372 * The terminating zero should be written at the end of the string, not
373 * the end of the buffer, as some programs specify the wrong size for
374 * the buffer (eg. winnt's sol.exe)
376 LPSTR WINAPI lstrcpynWtoA( LPSTR dst, LPCWSTR src, INT n )
378 if (n > 0 && !WideCharToMultiByte( CP_ACP, 0, src, -1, dst, n, NULL, NULL )) dst[n-1] = 0;
379 return dst;
382 /***********************************************************************
383 * UnicodeToAnsi (KERNEL.434)
385 INT16 WINAPI UnicodeToAnsi16( LPCWSTR src, LPSTR dst, INT16 codepage )
387 if ( codepage == -1 )
388 codepage = CP_ACP;
390 return WideCharToMultiByte( codepage, 0, src, -1, dst, 0x7fffffff, NULL, NULL );
394 /***********************************************************************
395 * Copy (GDI.250)
397 void WINAPI Copy16( LPVOID src, LPVOID dst, WORD size )
399 memcpy( dst, src, size );
402 /***********************************************************************
403 * AnsiToOem16 (KEYBOARD.5)
405 INT16 WINAPI AnsiToOem16( LPCSTR s, LPSTR d )
407 CharToOemA( s, d );
408 return -1;
412 /***********************************************************************
413 * OemToAnsi16 (KEYBOARD.6)
415 INT16 WINAPI OemToAnsi16( LPCSTR s, LPSTR d )
417 OemToCharA( s, d );
418 return -1;
422 /***********************************************************************
423 * AnsiToOemBuff16 (KEYBOARD.134)
425 void WINAPI AnsiToOemBuff16( LPCSTR s, LPSTR d, UINT16 len )
427 if (len != 0) CharToOemBuffA( s, d, len );
431 /***********************************************************************
432 * OemToAnsiBuff16 (KEYBOARD.135)
434 void WINAPI OemToAnsiBuff16( LPCSTR s, LPSTR d, UINT16 len )
436 if (len != 0) OemToCharBuffA( s, d, len );
440 /***********************************************************************
441 * CharToOemA (USER32.37)
443 BOOL WINAPI CharToOemA( LPCSTR s, LPSTR d )
445 return CharToOemBuffA( s, d, strlen( s ) + 1 );
449 /***********************************************************************
450 * CharToOemBuffA (USER32.38)
452 BOOL WINAPI CharToOemBuffA( LPCSTR s, LPSTR d, DWORD len )
454 WCHAR *bufW;
456 bufW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
457 if( bufW )
459 MultiByteToWideChar( CP_ACP, 0, s, len, bufW, len );
460 WideCharToMultiByte( CP_OEMCP, 0, bufW, len, d, len, NULL, NULL );
461 HeapFree( GetProcessHeap(), 0, bufW );
463 return TRUE;
467 /***********************************************************************
468 * CharToOemBuffW (USER32.39)
470 BOOL WINAPI CharToOemBuffW( LPCWSTR s, LPSTR d, DWORD len )
472 WideCharToMultiByte( CP_OEMCP, 0, s, len, d, len, NULL, NULL );
473 return TRUE;
477 /***********************************************************************
478 * CharToOemW (USER32.40)
480 BOOL WINAPI CharToOemW( LPCWSTR s, LPSTR d )
482 return CharToOemBuffW( s, d, strlenW( s ) + 1 );
486 /***********************************************************************
487 * OemToCharA (USER32.402)
489 BOOL WINAPI OemToCharA( LPCSTR s, LPSTR d )
491 return OemToCharBuffA( s, d, strlen( s ) + 1 );
495 /***********************************************************************
496 * OemToCharBuffA (USER32.403)
498 BOOL WINAPI OemToCharBuffA( LPCSTR s, LPSTR d, DWORD len )
500 WCHAR *bufW;
502 bufW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
503 if( bufW )
505 MultiByteToWideChar( CP_OEMCP, 0, s, len, bufW, len );
506 WideCharToMultiByte( CP_ACP, 0, bufW, len, d, len, NULL, NULL );
507 HeapFree( GetProcessHeap(), 0, bufW );
509 return TRUE;
513 /***********************************************************************
514 * OemToCharBuffW (USER32.404)
516 BOOL WINAPI OemToCharBuffW( LPCSTR s, LPWSTR d, DWORD len )
518 MultiByteToWideChar( CP_OEMCP, 0, s, len, d, len );
519 return TRUE;
523 /***********************************************************************
524 * OemToCharW (USER32.405)
526 BOOL WINAPI OemToCharW( LPCSTR s, LPWSTR d )
528 return OemToCharBuffW( s, d, strlen( s ) + 1 );
531 /***********************************************************************
532 * lstrrchr (Not a Windows API)
534 * This is the implementation meant to be invoked from within
535 * COMCTL32_StrRChrA and shell32(TODO)...
537 * Return a pointer to the last occurence of wMatch in lpStart
538 * not looking further than lpEnd...
540 LPSTR WINAPI lstrrchr( LPCSTR lpStart, LPCSTR lpEnd, WORD wMatch )
542 LPCSTR lpGotIt = NULL;
544 TRACE("(%p, %p, %x)\n", lpStart, lpEnd, wMatch);
546 if (!lpEnd) lpEnd = lpStart + strlen(lpStart);
548 for(; lpStart < lpEnd; lpStart = CharNextA(lpStart))
549 if (!ChrCmpA( GET_WORD(lpStart), wMatch))
550 lpGotIt = lpStart;
552 return ((LPSTR)lpGotIt);
555 /***********************************************************************
556 * ChrCmpW
557 * This fuction returns FALSE if both words match, TRUE otherwise...
559 static BOOL ChrCmpW( WORD word1, WORD word2) {
560 return (word1 != word2);
563 /***********************************************************************
564 * lstrrchrw (Not a Windows API)
566 * This is the implementation meant to be invoked form within
567 * COMCTL32_StrRChrW and shell32(TODO)...
569 * Return a pointer to the last occurence of wMatch in lpStart
570 * not looking further than lpEnd...
572 LPWSTR WINAPI lstrrchrw( LPCWSTR lpStart, LPCWSTR lpEnd, WORD wMatch )
574 LPCWSTR lpGotIt = NULL;
576 TRACE("(%p, %p, %x)\n", lpStart, lpEnd, wMatch);
577 if (!lpEnd) lpEnd = lpStart + lstrlenW(lpStart);
579 for(; lpStart < lpEnd; lpStart = CharNextW(lpStart))
580 if (!ChrCmpW( GET_WORD(lpStart), wMatch))
581 lpGotIt = lpStart;
583 return (LPWSTR)lpGotIt;
586 /***********************************************************************
587 * ChrCmpA
588 * This fuction returns FALSE if both words match, TRUE otherwise...
590 static BOOL ChrCmpA( WORD word1, WORD word2) {
591 if (LOBYTE(word1) == LOBYTE(word2)) {
592 if (IsDBCSLeadByte(LOBYTE(word1))) {
593 return (word1 != word2);
595 return FALSE;
597 return TRUE;