Fixed small ==, != mixup.
[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 #include "wine/winbase16.h"
11 #include "wine/winuser16.h"
12 #include "wine/keyboard16.h"
13 #include "winerror.h"
14 #include "ldt.h"
15 #include "debug.h"
16 #include "winnls.h"
17
18 static const BYTE STRING_Oem2Ansi[256] =
19 "\000\001\002\003\004\005\006\007\010\011\012\013\014\015\016\244"
20 "\020\021\022\023\266\247\026\027\030\031\032\033\034\035\036\037"
21 "\040\041\042\043\044\045\046\047\050\051\052\053\054\055\056\057"
22 "\060\061\062\063\064\065\066\067\070\071\072\073\074\075\076\077"
23 "\100\101\102\103\104\105\106\107\110\111\112\113\114\115\116\117"
24 "\120\121\122\123\124\125\126\127\130\131\132\133\134\135\136\137"
25 "\140\141\142\143\144\145\146\147\150\151\152\153\154\155\156\157"
26 "\160\161\162\163\164\165\166\167\170\171\172\173\174\175\176\177"
27 "\307\374\351\342\344\340\345\347\352\353\350\357\356\354\304\305"
28 "\311\346\306\364\366\362\373\371\377\326\334\242\243\245\120\203"
29 "\341\355\363\372\361\321\252\272\277\137\254\275\274\241\253\273"
30 "\137\137\137\246\246\246\246\053\053\246\246\053\053\053\053\053"
31 "\053\055\055\053\055\053\246\246\053\053\055\055\246\055\053\055"
32 "\055\055\055\053\053\053\053\053\053\053\053\137\137\246\137\137"
33 "\137\337\137\266\137\137\265\137\137\137\137\137\137\137\137\137"
34 "\137\261\137\137\137\137\367\137\260\225\267\137\156\262\137\137";
35
36 static const BYTE STRING_Ansi2Oem[256] =
37 "\000\001\002\003\004\005\006\007\010\011\012\013\014\015\016\017"
38 "\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037"
39 "\040\041\042\043\044\045\046\047\050\051\052\053\054\055\056\057"
40 "\060\061\062\063\064\065\066\067\070\071\072\073\074\075\076\077"
41 "\100\101\102\103\104\105\106\107\110\111\112\113\114\115\116\117"
42 "\120\121\122\123\124\125\126\127\130\131\132\133\134\135\136\137"
43 "\140\141\142\143\144\145\146\147\150\151\152\153\154\155\156\157"
44 "\160\161\162\163\164\165\166\167\170\171\172\173\174\175\176\177"
45 "\200\201\054\237\054\137\375\374\210\045\123\074\117\215\216\217"
46 "\220\140\047\042\042\371\055\137\230\231\163\076\157\235\236\131"
47 "\040\255\233\234\017\235\335\025\042\143\246\256\252\055\162\137"
48 "\370\361\375\063\047\346\024\372\054\061\247\257\254\253\137\250"
49 "\101\101\101\101\216\217\222\200\105\220\105\105\111\111\111\111"
50 "\104\245\117\117\117\117\231\170\117\125\125\125\232\131\137\341"
51 "\205\240\203\141\204\206\221\207\212\202\210\211\215\241\214\213"
52 "\144\244\225\242\223\157\224\366\157\227\243\226\201\171\137\230";
53
54 #define OEM_TO_ANSI(ch) (STRING_Oem2Ansi[(unsigned char)(ch)])
55 #define ANSI_TO_OEM(ch) (STRING_Ansi2Oem[(unsigned char)(ch)])
56
57 /* Internaly used by strchr family functions */
58 static BOOL ChrCmpA( WORD word1, WORD word2);
59 static BOOL ChrCmpW( WORD word1, WORD word2);
60
61 extern LPWSTR __cdecl CRTDLL_wcschr(LPCWSTR str,WCHAR xchar);
62
63
64 /***********************************************************************
65  *           hmemcpy   (KERNEL.348)
66  */
67 void WINAPI hmemcpy16( LPVOID dst, LPCVOID src, LONG count )
68 {
69     memcpy( dst, src, count );
70 }
71
72
73 /***********************************************************************
74  *           lstrcat16   (KERNEL.89)
75  */
76 SEGPTR WINAPI lstrcat16( SEGPTR dst, LPCSTR src )
77 {
78     lstrcatA( (LPSTR)PTR_SEG_TO_LIN(dst), src );
79     return dst;
80 }
81
82
83 /***********************************************************************
84  *           lstrcat32A   (KERNEL32.599)
85  */
86 LPSTR WINAPI lstrcatA( LPSTR dst, LPCSTR src )
87 {
88     TRACE(string,"Append %s to %s\n",
89                    debugstr_a (src), debugstr_a (dst));
90     /* Windows does not check for NULL pointers here, so we don't either */
91     strcat( dst, src );
92     return dst;
93 }
94
95
96 /***********************************************************************
97  *           lstrcat32W   (KERNEL32.600)
98  */
99 LPWSTR WINAPI lstrcatW( LPWSTR dst, LPCWSTR src )
100 {
101     register LPWSTR p = dst;
102     TRACE(string,"Append L%s to L%s\n",
103                    debugstr_w (src), debugstr_w (dst));
104     /* Windows does not check for NULL pointers here, so we don't either */
105     while (*p) p++;
106     while ((*p++ = *src++));
107     return dst;
108 }
109
110
111 /***********************************************************************
112  *           lstrcatn16   (KERNEL.352)
113  */
114 SEGPTR WINAPI lstrcatn16( SEGPTR dst, LPCSTR src, INT16 n )
115 {
116     lstrcatnA( (LPSTR)PTR_SEG_TO_LIN(dst), src, n );
117     return dst;
118 }
119
120
121 /***********************************************************************
122  *           lstrcatn32A   (Not a Windows API)
123  */
124 LPSTR WINAPI lstrcatnA( LPSTR dst, LPCSTR src, INT n )
125 {
126     register LPSTR p = dst;
127     TRACE(string,"strcatn add %d chars from %s to %s\n",
128                    n, debugstr_an (src, n), debugstr_a (dst));
129     while (*p) p++;
130     if ((n -= (INT)(p - dst)) <= 0) return dst;
131     lstrcpynA( p, src, n );
132     return dst;
133 }
134
135
136 /***********************************************************************
137  *           lstrcatn32W   (Not a Windows API)
138  */
139 LPWSTR WINAPI lstrcatnW( LPWSTR dst, LPCWSTR src, INT n )
140 {
141     register LPWSTR p = dst;
142     TRACE(string,"strcatn add %d chars from L%s to L%s\n",
143                    n, debugstr_wn (src, n), debugstr_w (dst));
144     while (*p) p++;
145     if ((n -= (INT)(p - dst)) <= 0) return dst;
146     lstrcpynW( p, src, n );
147     return dst;
148 }
149
150
151 /***********************************************************************
152  *           lstrcmp16   (USER.430)
153  */
154 INT16 WINAPI lstrcmp16( LPCSTR str1, LPCSTR str2 )
155 {
156     return (INT16)strcmp( str1, str2 );
157 }
158
159
160 /***********************************************************************
161  *           lstrcmp32A   (KERNEL.602)
162  */
163 INT WINAPI lstrcmpA( LPCSTR str1, LPCSTR str2 )
164 {
165     return CompareStringA(LOCALE_SYSTEM_DEFAULT,0,str1,-1,str2,-1) - 2 ;
166 }
167
168
169 /***********************************************************************
170  *           lstrcmp32W   (KERNEL.603)
171  * FIXME : should call CompareString32W, when it is implemented.
172  *    This implementation is not "word sort", as it should.
173  */
174 INT WINAPI lstrcmpW( LPCWSTR str1, LPCWSTR str2 )
175 {
176     TRACE(string,"L%s and L%s\n",
177                    debugstr_w (str1), debugstr_w (str2));
178     if (!str1 || !str2) {
179         SetLastError(ERROR_INVALID_PARAMETER);
180         return 0;
181     }
182     while (*str1 && (*str1 == *str2)) { str1++; str2++; }
183     return (INT)(*str1 - *str2);
184 }
185
186
187 /***********************************************************************
188  *           lstrcmpi16   (USER.471)
189  */
190 INT16 WINAPI lstrcmpi16( LPCSTR str1, LPCSTR str2 )
191 {
192     return (INT16)lstrcmpiA( str1, str2 );
193 }
194
195
196 /***********************************************************************
197  *           lstrcmpi32A   (KERNEL32.605)
198  */
199 INT WINAPI lstrcmpiA( LPCSTR str1, LPCSTR str2 )
200 {    TRACE(string,"strcmpi %s and %s\n",
201                    debugstr_a (str1), debugstr_a (str2));
202     return CompareStringA(LOCALE_SYSTEM_DEFAULT,NORM_IGNORECASE,str1,-1,str2,-1)-2;
203 }
204
205
206 /***********************************************************************
207  *           lstrcmpi32W   (KERNEL32.606)
208  */
209 INT WINAPI lstrcmpiW( LPCWSTR str1, LPCWSTR str2 )
210 {
211     INT res;
212
213 #if 0
214     /* Too much!  (From registry loading.)  */
215     TRACE(string,"strcmpi L%s and L%s\n",
216                    debugstr_w (str1), debugstr_w (str2));
217 #endif
218     if (!str1 || !str2) {
219         SetLastError(ERROR_INVALID_PARAMETER);
220         return 0;
221     }
222     while (*str1)
223     {
224         if ((*str1<0x100 ) && (*str2<0x100)) {
225             if ((res = toupper(*str1) - toupper(*str2)) != 0) return res;
226         } else {
227             if ((res = towupper(*str1) - towupper(*str2)) != 0) return res;
228         }
229         str1++;
230         str2++;
231     }
232     return towupper(*str1) - towupper(*str2);
233 }
234
235
236 /***********************************************************************
237  *           lstrcpy16   (KERNEL.88)
238  */
239 SEGPTR WINAPI lstrcpy16( SEGPTR dst, LPCSTR src )
240 {
241     lstrcpyA( (LPSTR)PTR_SEG_TO_LIN(dst), src );
242     return dst;
243 }
244
245
246 /***********************************************************************
247  *           lstrcpyA   (KERNEL32.608)
248  */
249 LPSTR WINAPI lstrcpyA( LPSTR dst, LPCSTR src )
250 {
251     TRACE(string,"strcpy %s\n", debugstr_a (src));
252     /* In real windows the whole function is protected by an exception handler
253      * that returns ERROR_INVALID_PARAMETER on faulty parameters
254      * We currently just check for NULL.
255      */
256     if (!dst || !src) {
257         SetLastError(ERROR_INVALID_PARAMETER);
258         return 0;
259     }
260     /* this is how Windows does it */
261     memmove( dst, src, strlen(src)+1 );
262     return dst;
263 }
264
265
266 /***********************************************************************
267  *           lstrcpy32W   (KERNEL32.609)
268  */
269 LPWSTR WINAPI lstrcpyW( LPWSTR dst, LPCWSTR src )
270 {
271     register LPWSTR p = dst;
272     TRACE(string,"strcpy L%s\n", debugstr_w (src));
273     /* In real windows the whole function is protected by an exception handler
274      * that returns ERROR_INVALID_PARAMETER on faulty parameters
275      * We currently just check for NULL.
276      */
277     if (!dst || !src) {
278         SetLastError(ERROR_INVALID_PARAMETER);
279         return 0;
280     }
281     while ((*p++ = *src++));
282     return dst;
283 }
284
285
286 /***********************************************************************
287  *           lstrcpyn16   (KERNEL.353)
288  */
289 SEGPTR WINAPI lstrcpyn16( SEGPTR dst, LPCSTR src, INT16 n )
290 {
291     lstrcpynA( (LPSTR)PTR_SEG_TO_LIN(dst), src, n );
292     return dst;
293 }
294
295
296 /***********************************************************************
297  *           lstrcpyn32A   (KERNEL32.611)
298  * Note: this function differs from the UNIX strncpy, it _always_ writes
299  * a terminating \0
300  */
301 LPSTR WINAPI lstrcpynA( LPSTR dst, LPCSTR src, INT n )
302 {
303     LPSTR p = dst;
304     TRACE(string,"strcpyn %s for %d chars\n",
305                    debugstr_an (src,n), n);
306     /* In real windows the whole function is protected by an exception handler
307      * that returns ERROR_INVALID_PARAMETER on faulty parameters
308      * We currently just check for NULL.
309      */
310     if (!dst || !src) {
311         SetLastError(ERROR_INVALID_PARAMETER);
312         return 0;
313     }
314     while ((n-- > 1) && *src) *p++ = *src++;
315     if (n >= 0) *p = 0;
316     return dst;
317 }
318
319
320 /***********************************************************************
321  *           lstrcpyn32W   (KERNEL32.612)
322  * Note: this function differs from the UNIX strncpy, it _always_ writes
323  * a terminating \0
324  */
325 LPWSTR WINAPI lstrcpynW( LPWSTR dst, LPCWSTR src, INT n )
326 {
327     LPWSTR p = dst;
328     TRACE(string,"strcpyn L%s for %d chars\n",
329                    debugstr_wn (src,n), n);
330     /* In real windows the whole function is protected by an exception handler
331      * that returns ERROR_INVALID_PARAMETER on faulty parameters
332      * We currently just check for NULL.
333      */
334     if (!dst || !src) {
335         SetLastError(ERROR_INVALID_PARAMETER);
336         return 0;
337     }
338     while ((n-- > 1) && *src) *p++ = *src++;
339     if (n >= 0) *p = 0;
340     return dst;
341 }
342
343
344 /***********************************************************************
345  *           lstrlen16   (KERNEL.90)
346  */
347 INT16 WINAPI lstrlen16( LPCSTR str )
348 {
349     return (INT16)lstrlenA( str );
350 }
351
352
353 /***********************************************************************
354  *           lstrlen32A   (KERNEL32.614)
355  */
356 INT WINAPI lstrlenA( LPCSTR str )
357 {
358     /* looks weird, but win3.1 KERNEL got a GeneralProtection handler
359      * in lstrlen() ... we check only for NULL pointer reference.
360      * - Marcus Meissner
361      */
362     TRACE(string,"strlen %s\n", debugstr_a (str));
363     if (!str) return 0;
364     return (INT)strlen(str);
365 }
366
367
368 /***********************************************************************
369  *           lstrlen32W   (KERNEL32.615)
370  */
371 INT WINAPI lstrlenW( LPCWSTR str )
372 {
373     INT len = 0;
374     TRACE(string,"strlen L%s\n", debugstr_w (str));
375     if (!str) return 0;
376     while (*str++) len++;
377     return len;
378 }
379
380
381 /***********************************************************************
382  *           lstrncmp32A   (Not a Windows API)
383  */
384 INT WINAPI lstrncmpA( LPCSTR str1, LPCSTR str2, INT n )
385 {
386     TRACE(string,"strncmp %s and %s for %d chars\n",
387                    debugstr_an (str1, n), debugstr_an (str2, n), n);
388     return (INT)strncmp( str1, str2, n );
389 }
390
391
392 /***********************************************************************
393  *           lstrncmp32W   (Not a Windows API)
394  */
395 INT WINAPI lstrncmpW( LPCWSTR str1, LPCWSTR str2, INT n )
396 {
397     TRACE(string,"strncmp L%s and L%s for %d chars\n",
398                    debugstr_wn (str1, n), debugstr_wn (str2, n), n);
399     if (!n) return 0;
400     while ((--n > 0) && *str1 && (*str1 == *str2)) { str1++; str2++; }
401     return (INT)(*str1 - *str2);
402 }
403
404
405 /***********************************************************************
406  *           lstrncmpi32A   (Not a Windows API)
407  */
408 INT WINAPI lstrncmpiA( LPCSTR str1, LPCSTR str2, INT n )
409 {
410     INT res;
411
412     TRACE(string,"strncmpi %s and %s for %d chars\n",
413                    debugstr_an (str1, n), debugstr_an (str2, n), n);
414     if (!n) return 0;
415     while ((--n > 0) && *str1)
416       if ( (res = toupper(*str1++) - toupper(*str2++)) ) return res;
417
418     return toupper(*str1) - toupper(*str2);
419 }
420
421
422 /***********************************************************************
423  *           lstrncmpi32W   (Not a Windows API)
424  */
425 INT WINAPI lstrncmpiW( LPCWSTR str1, LPCWSTR str2, INT n )
426 {
427     INT res;
428
429     TRACE(string,"strncmpi L%s and L%s for %d chars\n",
430                    debugstr_wn (str1, n), debugstr_wn (str2, n), n);
431     if (!n) return 0;
432     while ((--n > 0) && *str1)
433     {
434         if ((res = towupper(*str1) - towupper(*str2)) != 0) return res;
435         str1++;
436         str2++;
437     }
438     return towupper(*str1) - towupper(*str2);
439 }
440
441
442 /***********************************************************************
443  *           lstrcpyAtoW   (Not a Windows API)
444  */
445 LPWSTR WINAPI lstrcpyAtoW( LPWSTR dst, LPCSTR src )
446 {
447     register LPWSTR p = dst;
448
449     TRACE(string,"%s\n",src);
450
451     while ((*p++ = (WCHAR)(unsigned char)*src++));
452     return dst;
453 }
454
455
456 /***********************************************************************
457  *           lstrcpyWtoA   (Not a Windows API)
458  */
459 LPSTR WINAPI lstrcpyWtoA( LPSTR dst, LPCWSTR src )
460 {
461     register LPSTR p = dst;
462
463     TRACE(string,"L%s\n",debugstr_w(src));
464
465     while ((*p++ = (CHAR)*src++));
466     return dst;
467 }
468
469
470 /***********************************************************************
471  *           lstrcpynAtoW   (Not a Windows API)
472  * Note: this function differs from the UNIX strncpy, it _always_ writes
473  * a terminating \0
474  */
475 LPWSTR WINAPI lstrcpynAtoW( LPWSTR dst, LPCSTR src, INT n )
476 {
477     LPWSTR p = dst;
478
479     TRACE(string,"%s %i\n",src, n);
480
481     while ((n-- > 1) && *src) *p++ = (WCHAR)(unsigned char)*src++;
482     if (n >= 0) *p = 0;
483     return dst;
484 }
485
486
487 /***********************************************************************
488  *           lstrcpynWtoA   (Not a Windows API)
489  * Note: this function differs from the UNIX strncpy, it _always_ writes
490  * a terminating \0
491  */
492 LPSTR WINAPI lstrcpynWtoA( LPSTR dst, LPCWSTR src, INT n )
493 {
494     LPSTR p = dst;
495
496     TRACE(string,"L%s %i\n",debugstr_w(src), n);
497
498     while ((n-- > 1) && *src) *p++ = (CHAR)*src++;
499     if (n >= 0) *p = 0;
500     return dst;
501 }
502
503 /***********************************************************************
504  *           UnicodeToAnsi   (KERNEL.434)
505  */
506 INT16 WINAPI UnicodeToAnsi16( LPCWSTR src, LPSTR dst, INT16 codepage )
507 {
508     if ( codepage != -1 )
509         FIXME( string, "codepage %d not supported\n", codepage );
510
511     lstrcpyWtoA( dst, src );
512
513     return (INT16)lstrlenA( dst );
514 }
515
516
517 /***********************************************************************
518  *           Copy   (GDI.250)
519  */
520 void WINAPI Copy16( LPVOID src, LPVOID dst, WORD size )
521 {
522     memcpy( dst, src, size );
523 }
524
525
526 /***********************************************************************
527  *           RtlFillMemory   (KERNEL32.441)
528  */
529 VOID WINAPI RtlFillMemory( LPVOID ptr, UINT len, UINT fill )
530 {
531     memset( ptr, fill, len );
532 }
533
534
535 /***********************************************************************
536  *           RtlMoveMemory   (KERNEL32.442)
537  */
538 VOID WINAPI RtlMoveMemory( LPVOID dst, LPCVOID src, UINT len )
539 {
540     memmove( dst, src, len );
541 }
542
543
544 /***********************************************************************
545  *           RtlZeroMemory   (KERNEL32.444)
546  */
547 VOID WINAPI RtlZeroMemory( LPVOID ptr, UINT len )
548 {
549     memset( ptr, 0, len );
550 }
551
552
553 /***********************************************************************
554  *           AnsiToOem16   (KEYBOARD.5)
555  */
556 INT16 WINAPI AnsiToOem16( LPCSTR s, LPSTR d )
557 {
558     CharToOemA( s, d );
559     return -1;
560 }
561
562
563 /***********************************************************************
564  *           OemToAnsi16   (KEYBOARD.6)
565  */
566 INT16 WINAPI OemToAnsi16( LPCSTR s, LPSTR d )
567 {
568     OemToCharA( s, d );
569     return -1;
570 }
571
572
573 /***********************************************************************
574  *           AnsiToOemBuff16   (KEYBOARD.134)
575  */
576 void WINAPI AnsiToOemBuff16( LPCSTR s, LPSTR d, UINT16 len )
577 {
578     if (len != 0) CharToOemBuffA( s, d, len );
579 }
580
581
582 /***********************************************************************
583  *           OemToAnsiBuff16   (KEYBOARD.135)
584  */
585 void WINAPI OemToAnsiBuff16( LPCSTR s, LPSTR d, UINT16 len )
586 {
587     if (len != 0) OemToCharBuffA( s, d, len );
588 }
589
590
591 /***********************************************************************
592  *           CharToOem32A   (USER32.37)
593  */
594 BOOL WINAPI CharToOemA( LPCSTR s, LPSTR d )
595 {
596     LPSTR oldd = d;
597     if (!s || !d) return TRUE;
598     TRACE(string,"CharToOem %s\n", debugstr_a (s));
599     while ((*d++ = ANSI_TO_OEM(*s++)));
600     TRACE(string,"       to %s\n", debugstr_a (oldd));
601     return TRUE;
602 }
603
604
605 /***********************************************************************
606  *           CharToOemBuff32A   (USER32.38)
607  */
608 BOOL WINAPI CharToOemBuffA( LPCSTR s, LPSTR d, DWORD len )
609 {
610     while (len--) *d++ = ANSI_TO_OEM(*s++);
611     return TRUE;
612 }
613
614
615 /***********************************************************************
616  *           CharToOemBuff32W   (USER32.39)
617  */
618 BOOL WINAPI CharToOemBuffW( LPCWSTR s, LPSTR d, DWORD len )
619 {
620     while (len--) *d++ = ANSI_TO_OEM(*s++);
621     return TRUE;
622 }
623
624
625 /***********************************************************************
626  *           CharToOem32W   (USER32.40)
627  */
628 BOOL WINAPI CharToOemW( LPCWSTR s, LPSTR d )
629 {
630     LPSTR oldd = d;
631     if (!s || !d) return TRUE;
632     TRACE(string,"CharToOem L%s\n", debugstr_w (s));
633     while ((*d++ = ANSI_TO_OEM(*s++)));
634     TRACE(string,"       to %s\n", debugstr_a (oldd));
635     return TRUE;
636 }
637
638
639 /***********************************************************************
640  *           OemToChar32A   (USER32.402)
641  */
642 BOOL WINAPI OemToCharA( LPCSTR s, LPSTR d )
643 {
644     LPSTR oldd = d;
645     TRACE(string,"OemToChar %s\n", debugstr_a (s));
646     while ((*d++ = OEM_TO_ANSI(*s++)));
647     TRACE(string,"       to %s\n", debugstr_a (oldd));
648     return TRUE;
649 }
650
651
652 /***********************************************************************
653  *           OemToCharBuff32A   (USER32.403)
654  */
655 BOOL WINAPI OemToCharBuffA( LPCSTR s, LPSTR d, DWORD len )
656 {
657     TRACE(string,"OemToCharBuff %s\n", debugstr_an (s, len));
658     while (len--) *d++ = OEM_TO_ANSI(*s++);
659     return TRUE;
660 }
661
662
663 /***********************************************************************
664  *           OemToCharBuff32W   (USER32.404)
665  */
666 BOOL WINAPI OemToCharBuffW( LPCSTR s, LPWSTR d, DWORD len )
667 {
668     TRACE(string,"OemToCharBuff %s\n", debugstr_an (s, len));
669     while (len--) *d++ = (WCHAR)OEM_TO_ANSI(*s++);
670     return TRUE;
671 }
672
673
674 /***********************************************************************
675  *           OemToChar32W   (USER32.405)
676  */
677 BOOL WINAPI OemToCharW( LPCSTR s, LPWSTR d )
678 {
679     while ((*d++ = (WCHAR)OEM_TO_ANSI(*s++)));
680     return TRUE;
681 }
682
683 /***********************************************************************
684  *  WideCharToLocal (Not a Windows API)
685  *  similar lstrcpyWtoA, should handle codepages properly
686  *
687  *  RETURNS
688  *    strlen of the destination string
689  */
690  
691 INT WINAPI WideCharToLocal(
692     LPSTR pLocal, 
693                 LPWSTR pWide, 
694                 INT dwChars)
695 { *pLocal = 0;
696   TRACE(string,"(%p, %s, %i)\n",        pLocal, debugstr_w(pWide),dwChars);
697   WideCharToMultiByte(CP_ACP,0,pWide,-1,pLocal,dwChars,NULL,NULL);
698   return strlen(pLocal);
699 }
700 /***********************************************************************
701  *  LocalToWideChar (Not a Windows API)
702  *  similar lstrcpyAtoW, should handle codepages properly
703  *
704  *  RETURNS
705  *    strlen of the destination string
706  */
707 INT WINAPI LocalToWideChar(
708     LPWSTR pWide, 
709                 LPSTR pLocal, 
710                 INT dwChars)
711 { *pWide = 0;
712   TRACE(string,"(%p, %s, %i)\n",pWide,  pLocal, dwChars);
713         MultiByteToWideChar(CP_ACP,0,pLocal,-1,pWide,dwChars); 
714   return lstrlenW(pWide);
715 }
716
717
718 /***********************************************************************
719  *           lstrrchr   (Not a Windows API)
720  *
721  * This is the implementation meant to be invoked form within
722  * COMCTL32_StrRChrA and shell32(TODO)...
723  *
724  * Return a pointer to the last occurence of wMatch in lpStart
725  * not looking further than lpEnd...
726  */
727 LPSTR WINAPI lstrrchr( LPCSTR lpStart, LPCSTR lpEnd, WORD wMatch )
728 {
729   LPCSTR lpGotIt = NULL;
730
731   TRACE(string,"(%s, %s)\n", lpStart, lpEnd);
732
733   if (!lpEnd) lpEnd = lpStart + strlen(lpStart);
734
735   for(; lpStart < lpEnd; lpStart = CharNextA(lpStart)) 
736     if (!ChrCmpA( GET_WORD(lpStart), wMatch)) 
737       lpGotIt = lpStart;
738     
739   return ((LPSTR)lpGotIt);
740 }
741
742 /***********************************************************************
743  *           lstrrchrw    (Not a Windows API)
744  *
745  * This is the implementation meant to be invoked form within
746  * COMCTL32_StrRChrW and shell32(TODO)...
747  *
748  * Return a pointer to the last occurence of wMatch in lpStart
749  * not looking further than lpEnd...
750  */  
751 LPWSTR WINAPI lstrrchrw( LPCWSTR lpStart, LPCWSTR lpEnd, WORD wMatch )
752 {
753   LPCWSTR lpGotIt = NULL;
754
755   TRACE(string,"(%p, %p, %c)\n", lpStart,      lpEnd, wMatch);
756   if (!lpEnd) lpEnd = lpStart + lstrlenW(lpStart);
757
758   for(; lpStart < lpEnd; lpStart = CharNextW(lpStart)) 
759     if (!ChrCmpW( GET_WORD(lpStart), wMatch)) 
760       lpGotIt = lpStart;
761     
762   return (LPWSTR)lpGotIt;
763 }
764
765 /***********************************************************************
766  *           strstrw   (Not a Windows API)
767  *
768  * This is the implementation meant to be invoked form within
769  * COMCTL32_StrStrW and shell32(TODO)...
770  *
771  */
772 LPWSTR WINAPI strstrw( LPCWSTR lpFirst, LPCWSTR lpSrch) {
773   UINT uSrchLen  = (UINT)lstrlenW(lpSrch);
774   WORD wMatchBeg   = *(WORD*)lpSrch;
775
776   TRACE(string,"(%p, %p)\n", lpFirst,  lpSrch);
777
778   for(; 
779     ((lpFirst=CRTDLL_wcschr(lpFirst, wMatchBeg))!=0) && 
780       lstrncmpW(lpFirst, lpSrch, uSrchLen); 
781     lpFirst++) {
782       continue;
783   }
784   return (LPWSTR)lpFirst;
785 }
786
787
788 /***********************************************************************
789  *           ChrCmpA   
790  * This fuction returns FALSE if both words match, TRUE otherwise...
791  */
792 static BOOL ChrCmpA( WORD word1, WORD word2) {
793   if (LOBYTE(word1) == LOBYTE(word2)) {
794     if (IsDBCSLeadByte(LOBYTE(word1))) {
795       return (word1 != word2);
796     }
797     return FALSE;
798   }
799   return TRUE;
800 }
801
802 /***********************************************************************
803  *           ChrCmpW   
804  * This fuction returns FALSE if both words match, TRUE otherwise...
805  */
806 static BOOL ChrCmpW( WORD word1, WORD word2) {
807   return (word1 != word2);
808 }
809
810