Cosmetics.
[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  *           lstrcpy32A   (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     strcpy( dst, src );
261     return dst;
262 }
263
264
265 /***********************************************************************
266  *           lstrcpy32W   (KERNEL32.609)
267  */
268 LPWSTR WINAPI lstrcpyW( LPWSTR dst, LPCWSTR src )
269 {
270     register LPWSTR p = dst;
271     TRACE(string,"strcpy L%s\n", debugstr_w (src));
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.
275      */
276     if (!dst || !src) {
277         SetLastError(ERROR_INVALID_PARAMETER);
278         return 0;
279     }
280     while ((*p++ = *src++));
281     return dst;
282 }
283
284
285 /***********************************************************************
286  *           lstrcpyn16   (KERNEL.353)
287  */
288 SEGPTR WINAPI lstrcpyn16( SEGPTR dst, LPCSTR src, INT16 n )
289 {
290     lstrcpynA( (LPSTR)PTR_SEG_TO_LIN(dst), src, n );
291     return dst;
292 }
293
294
295 /***********************************************************************
296  *           lstrcpyn32A   (KERNEL32.611)
297  * Note: this function differs from the UNIX strncpy, it _always_ writes
298  * a terminating \0
299  */
300 LPSTR WINAPI lstrcpynA( LPSTR dst, LPCSTR src, INT n )
301 {
302     LPSTR p = dst;
303     TRACE(string,"strcpyn %s for %d chars\n",
304                    debugstr_an (src,n), n);
305     /* In real windows the whole function is protected by an exception handler
306      * that returns ERROR_INVALID_PARAMETER on faulty parameters
307      * We currently just check for NULL.
308      */
309     if (!dst || !src) {
310         SetLastError(ERROR_INVALID_PARAMETER);
311         return 0;
312     }
313     while ((n-- > 1) && *src) *p++ = *src++;
314     if (n >= 0) *p = 0;
315     return dst;
316 }
317
318
319 /***********************************************************************
320  *           lstrcpyn32W   (KERNEL32.612)
321  * Note: this function differs from the UNIX strncpy, it _always_ writes
322  * a terminating \0
323  */
324 LPWSTR WINAPI lstrcpynW( LPWSTR dst, LPCWSTR src, INT n )
325 {
326     LPWSTR p = dst;
327     TRACE(string,"strcpyn L%s for %d chars\n",
328                    debugstr_wn (src,n), n);
329     /* In real windows the whole function is protected by an exception handler
330      * that returns ERROR_INVALID_PARAMETER on faulty parameters
331      * We currently just check for NULL.
332      */
333     if (!dst || !src) {
334         SetLastError(ERROR_INVALID_PARAMETER);
335         return 0;
336     }
337     while ((n-- > 1) && *src) *p++ = *src++;
338     if (n >= 0) *p = 0;
339     return dst;
340 }
341
342
343 /***********************************************************************
344  *           lstrlen16   (KERNEL.90)
345  */
346 INT16 WINAPI lstrlen16( LPCSTR str )
347 {
348     return (INT16)lstrlenA( str );
349 }
350
351
352 /***********************************************************************
353  *           lstrlen32A   (KERNEL32.614)
354  */
355 INT WINAPI lstrlenA( LPCSTR str )
356 {
357     /* looks weird, but win3.1 KERNEL got a GeneralProtection handler
358      * in lstrlen() ... we check only for NULL pointer reference.
359      * - Marcus Meissner
360      */
361     TRACE(string,"strlen %s\n", debugstr_a (str));
362     if (!str) return 0;
363     return (INT)strlen(str);
364 }
365
366
367 /***********************************************************************
368  *           lstrlen32W   (KERNEL32.615)
369  */
370 INT WINAPI lstrlenW( LPCWSTR str )
371 {
372     INT len = 0;
373     TRACE(string,"strlen L%s\n", debugstr_w (str));
374     if (!str) return 0;
375     while (*str++) len++;
376     return len;
377 }
378
379
380 /***********************************************************************
381  *           lstrncmp32A   (Not a Windows API)
382  */
383 INT WINAPI lstrncmpA( LPCSTR str1, LPCSTR str2, INT n )
384 {
385     TRACE(string,"strncmp %s and %s for %d chars\n",
386                    debugstr_an (str1, n), debugstr_an (str2, n), n);
387     return (INT)strncmp( str1, str2, n );
388 }
389
390
391 /***********************************************************************
392  *           lstrncmp32W   (Not a Windows API)
393  */
394 INT WINAPI lstrncmpW( LPCWSTR str1, LPCWSTR str2, INT n )
395 {
396     TRACE(string,"strncmp L%s and L%s for %d chars\n",
397                    debugstr_wn (str1, n), debugstr_wn (str2, n), n);
398     if (!n) return 0;
399     while ((--n > 0) && *str1 && (*str1 == *str2)) { str1++; str2++; }
400     return (INT)(*str1 - *str2);
401 }
402
403
404 /***********************************************************************
405  *           lstrncmpi32A   (Not a Windows API)
406  */
407 INT WINAPI lstrncmpiA( LPCSTR str1, LPCSTR str2, INT n )
408 {
409     INT res;
410
411     TRACE(string,"strncmpi %s and %s for %d chars\n",
412                    debugstr_an (str1, n), debugstr_an (str2, n), n);
413     if (!n) return 0;
414     while ((--n > 0) && *str1)
415       if ( (res = toupper(*str1++) - toupper(*str2++)) ) return res;
416
417     return toupper(*str1) - toupper(*str2);
418 }
419
420
421 /***********************************************************************
422  *           lstrncmpi32W   (Not a Windows API)
423  */
424 INT WINAPI lstrncmpiW( LPCWSTR str1, LPCWSTR str2, INT n )
425 {
426     INT res;
427
428     TRACE(string,"strncmpi L%s and L%s for %d chars\n",
429                    debugstr_wn (str1, n), debugstr_wn (str2, n), n);
430     if (!n) return 0;
431     while ((--n > 0) && *str1)
432     {
433         if ((res = towupper(*str1) - towupper(*str2)) != 0) return res;
434         str1++;
435         str2++;
436     }
437     return towupper(*str1) - towupper(*str2);
438 }
439
440
441 /***********************************************************************
442  *           lstrcpyAtoW   (Not a Windows API)
443  */
444 LPWSTR WINAPI lstrcpyAtoW( LPWSTR dst, LPCSTR src )
445 {
446     register LPWSTR p = dst;
447
448     TRACE(string,"%s\n",src);
449
450     while ((*p++ = (WCHAR)(unsigned char)*src++));
451     return dst;
452 }
453
454
455 /***********************************************************************
456  *           lstrcpyWtoA   (Not a Windows API)
457  */
458 LPSTR WINAPI lstrcpyWtoA( LPSTR dst, LPCWSTR src )
459 {
460     register LPSTR p = dst;
461
462     TRACE(string,"L%s\n",debugstr_w(src));
463
464     while ((*p++ = (CHAR)*src++));
465     return dst;
466 }
467
468
469 /***********************************************************************
470  *           lstrcpynAtoW   (Not a Windows API)
471  * Note: this function differs from the UNIX strncpy, it _always_ writes
472  * a terminating \0
473  */
474 LPWSTR WINAPI lstrcpynAtoW( LPWSTR dst, LPCSTR src, INT n )
475 {
476     LPWSTR p = dst;
477
478     TRACE(string,"%s %i\n",src, n);
479
480     while ((n-- > 1) && *src) *p++ = (WCHAR)(unsigned char)*src++;
481     if (n >= 0) *p = 0;
482     return dst;
483 }
484
485
486 /***********************************************************************
487  *           lstrcpynWtoA   (Not a Windows API)
488  * Note: this function differs from the UNIX strncpy, it _always_ writes
489  * a terminating \0
490  */
491 LPSTR WINAPI lstrcpynWtoA( LPSTR dst, LPCWSTR src, INT n )
492 {
493     LPSTR p = dst;
494
495     TRACE(string,"L%s %i\n",debugstr_w(src), n);
496
497     while ((n-- > 1) && *src) *p++ = (CHAR)*src++;
498     if (n >= 0) *p = 0;
499     return dst;
500 }
501
502 /***********************************************************************
503  *           UnicodeToAnsi   (KERNEL.434)
504  */
505 INT16 WINAPI UnicodeToAnsi16( LPCWSTR src, LPSTR dst, INT16 codepage )
506 {
507     if ( codepage != -1 )
508         FIXME( string, "codepage %d not supported\n", codepage );
509
510     lstrcpyWtoA( dst, src );
511
512     return (INT16)lstrlenA( dst );
513 }
514
515
516 /***********************************************************************
517  *           Copy   (GDI.250)
518  */
519 void WINAPI Copy16( LPVOID src, LPVOID dst, WORD size )
520 {
521     memcpy( dst, src, size );
522 }
523
524
525 /***********************************************************************
526  *           RtlFillMemory   (KERNEL32.441)
527  */
528 VOID WINAPI RtlFillMemory( LPVOID ptr, UINT len, UINT fill )
529 {
530     memset( ptr, fill, len );
531 }
532
533
534 /***********************************************************************
535  *           RtlMoveMemory   (KERNEL32.442)
536  */
537 VOID WINAPI RtlMoveMemory( LPVOID dst, LPCVOID src, UINT len )
538 {
539     memmove( dst, src, len );
540 }
541
542
543 /***********************************************************************
544  *           RtlZeroMemory   (KERNEL32.444)
545  */
546 VOID WINAPI RtlZeroMemory( LPVOID ptr, UINT len )
547 {
548     memset( ptr, 0, len );
549 }
550
551
552 /***********************************************************************
553  *           AnsiToOem16   (KEYBOARD.5)
554  */
555 INT16 WINAPI AnsiToOem16( LPCSTR s, LPSTR d )
556 {
557     CharToOemA( s, d );
558     return -1;
559 }
560
561
562 /***********************************************************************
563  *           OemToAnsi16   (KEYBOARD.6)
564  */
565 INT16 WINAPI OemToAnsi16( LPCSTR s, LPSTR d )
566 {
567     OemToCharA( s, d );
568     return -1;
569 }
570
571
572 /***********************************************************************
573  *           AnsiToOemBuff16   (KEYBOARD.134)
574  */
575 void WINAPI AnsiToOemBuff16( LPCSTR s, LPSTR d, UINT16 len )
576 {
577     if (len != 0) CharToOemBuffA( s, d, len );
578 }
579
580
581 /***********************************************************************
582  *           OemToAnsiBuff16   (KEYBOARD.135)
583  */
584 void WINAPI OemToAnsiBuff16( LPCSTR s, LPSTR d, UINT16 len )
585 {
586     if (len != 0) OemToCharBuffA( s, d, len );
587 }
588
589
590 /***********************************************************************
591  *           CharToOem32A   (USER32.37)
592  */
593 BOOL WINAPI CharToOemA( LPCSTR s, LPSTR d )
594 {
595     LPSTR oldd = d;
596     if (!s || !d) return TRUE;
597     TRACE(string,"CharToOem %s\n", debugstr_a (s));
598     while ((*d++ = ANSI_TO_OEM(*s++)));
599     TRACE(string,"       to %s\n", debugstr_a (oldd));
600     return TRUE;
601 }
602
603
604 /***********************************************************************
605  *           CharToOemBuff32A   (USER32.38)
606  */
607 BOOL WINAPI CharToOemBuffA( LPCSTR s, LPSTR d, DWORD len )
608 {
609     while (len--) *d++ = ANSI_TO_OEM(*s++);
610     return TRUE;
611 }
612
613
614 /***********************************************************************
615  *           CharToOemBuff32W   (USER32.39)
616  */
617 BOOL WINAPI CharToOemBuffW( LPCWSTR s, LPSTR d, DWORD len )
618 {
619     while (len--) *d++ = ANSI_TO_OEM(*s++);
620     return TRUE;
621 }
622
623
624 /***********************************************************************
625  *           CharToOem32W   (USER32.40)
626  */
627 BOOL WINAPI CharToOemW( LPCWSTR s, LPSTR d )
628 {
629     LPSTR oldd = d;
630     if (!s || !d) return TRUE;
631     TRACE(string,"CharToOem L%s\n", debugstr_w (s));
632     while ((*d++ = ANSI_TO_OEM(*s++)));
633     TRACE(string,"       to %s\n", debugstr_a (oldd));
634     return TRUE;
635 }
636
637
638 /***********************************************************************
639  *           OemToChar32A   (USER32.402)
640  */
641 BOOL WINAPI OemToCharA( LPCSTR s, LPSTR d )
642 {
643     LPSTR oldd = d;
644     TRACE(string,"OemToChar %s\n", debugstr_a (s));
645     while ((*d++ = OEM_TO_ANSI(*s++)));
646     TRACE(string,"       to %s\n", debugstr_a (oldd));
647     return TRUE;
648 }
649
650
651 /***********************************************************************
652  *           OemToCharBuff32A   (USER32.403)
653  */
654 BOOL WINAPI OemToCharBuffA( LPCSTR s, LPSTR d, DWORD len )
655 {
656     TRACE(string,"OemToCharBuff %s\n", debugstr_an (s, len));
657     while (len--) *d++ = OEM_TO_ANSI(*s++);
658     return TRUE;
659 }
660
661
662 /***********************************************************************
663  *           OemToCharBuff32W   (USER32.404)
664  */
665 BOOL WINAPI OemToCharBuffW( LPCSTR s, LPWSTR d, DWORD len )
666 {
667     TRACE(string,"OemToCharBuff %s\n", debugstr_an (s, len));
668     while (len--) *d++ = (WCHAR)OEM_TO_ANSI(*s++);
669     return TRUE;
670 }
671
672
673 /***********************************************************************
674  *           OemToChar32W   (USER32.405)
675  */
676 BOOL WINAPI OemToCharW( LPCSTR s, LPWSTR d )
677 {
678     while ((*d++ = (WCHAR)OEM_TO_ANSI(*s++)));
679     return TRUE;
680 }
681
682 /***********************************************************************
683  *  WideCharToLocal (Not a Windows API)
684  *  similar lstrcpyWtoA, should handle codepages properly
685  *
686  *  RETURNS
687  *    strlen of the destination string
688  */
689  
690 INT WINAPI WideCharToLocal(
691     LPSTR pLocal, 
692                 LPWSTR pWide, 
693                 INT dwChars)
694 { *pLocal = 0;
695   TRACE(string,"(%p, %s, %i)\n",        pLocal, debugstr_w(pWide),dwChars);
696   WideCharToMultiByte(CP_ACP,0,pWide,-1,pLocal,dwChars,NULL,NULL);
697   return strlen(pLocal);
698 }
699 /***********************************************************************
700  *  LocalToWideChar (Not a Windows API)
701  *  similar lstrcpyAtoW, should handle codepages properly
702  *
703  *  RETURNS
704  *    strlen of the destination string
705  */
706 INT WINAPI LocalToWideChar(
707     LPWSTR pWide, 
708                 LPSTR pLocal, 
709                 INT dwChars)
710 { *pWide = 0;
711   TRACE(string,"(%p, %s, %i)\n",pWide,  pLocal, dwChars);
712         MultiByteToWideChar(CP_ACP,0,pLocal,-1,pWide,dwChars); 
713   return lstrlenW(pWide);
714 }
715
716
717 /***********************************************************************
718  *           lstrrchr   (Not a Windows API)
719  *
720  * This is the implementation meant to be invoked form within
721  * COMCTL32_StrRChrA and shell32(TODO)...
722  *
723  * Return a pointer to the last occurence of wMatch in lpStart
724  * not looking further than lpEnd...
725  */
726 LPSTR WINAPI lstrrchr( LPCSTR lpStart, LPCSTR lpEnd, WORD wMatch )
727 {
728   LPCSTR lpGotIt = NULL;
729
730   TRACE(string,"(%s, %s)\n", lpStart, lpEnd);
731
732   if (!lpEnd) lpEnd = lpStart + strlen(lpStart);
733
734   for(; lpStart < lpEnd; lpStart = CharNextA(lpStart)) 
735     if (!ChrCmpA( GET_WORD(lpStart), wMatch)) 
736       lpGotIt = lpStart;
737     
738   return ((LPSTR)lpGotIt);
739 }
740
741 /***********************************************************************
742  *           lstrrchrw    (Not a Windows API)
743  *
744  * This is the implementation meant to be invoked form within
745  * COMCTL32_StrRChrW and shell32(TODO)...
746  *
747  * Return a pointer to the last occurence of wMatch in lpStart
748  * not looking further than lpEnd...
749  */  
750 LPWSTR WINAPI lstrrchrw( LPCWSTR lpStart, LPCWSTR lpEnd, WORD wMatch )
751 {
752   LPCWSTR lpGotIt = NULL;
753
754   TRACE(string,"(%p, %p, %c)\n", lpStart,      lpEnd, wMatch);
755   if (!lpEnd) lpEnd = lpStart + lstrlenW(lpStart);
756
757   for(; lpStart < lpEnd; lpStart = CharNextW(lpStart)) 
758     if (!ChrCmpW( GET_WORD(lpStart), wMatch)) 
759       lpGotIt = lpStart;
760     
761   return (LPWSTR)lpGotIt;
762 }
763
764 /***********************************************************************
765  *           strstrw   (Not a Windows API)
766  *
767  * This is the implementation meant to be invoked form within
768  * COMCTL32_StrStrW and shell32(TODO)...
769  *
770  */
771 LPWSTR WINAPI strstrw( LPCWSTR lpFirst, LPCWSTR lpSrch) {
772   UINT uSrchLen  = (UINT)lstrlenW(lpSrch);
773   WORD wMatchBeg   = *(WORD*)lpSrch;
774
775   TRACE(string,"(%p, %p)\n", lpFirst,  lpSrch);
776
777   for(; 
778     ((lpFirst=CRTDLL_wcschr(lpFirst, wMatchBeg))!=0) && 
779       lstrncmpW(lpFirst, lpSrch, uSrchLen); 
780     lpFirst++) {
781       continue;
782   }
783   return (LPWSTR)lpFirst;
784 }
785
786
787 /***********************************************************************
788  *           ChrCmpA   
789  * This fuction returns FALSE if both words match, TRUE otherwise...
790  */
791 static BOOL ChrCmpA( WORD word1, WORD word2) {
792   if (LOBYTE(word1) == LOBYTE(word2)) {
793     if (IsDBCSLeadByte(LOBYTE(word1))) {
794       return (word1 != word2);
795     }
796     return FALSE;
797   }
798   return TRUE;
799 }
800
801 /***********************************************************************
802  *           ChrCmpW   
803  * This fuction returns FALSE if both words match, TRUE otherwise...
804  */
805 static BOOL ChrCmpW( WORD word1, WORD word2) {
806   return (word1 != word2);
807 }
808
809