Fixed some issues found by winapi_check.
[wine] / dlls / shlwapi / string.c
1 /*
2  * Shlwapi string functions
3  *
4  * Copyright 1998 Juergen Schmied
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <ctype.h>
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
25
26 #include "winerror.h"
27 #include "windef.h"
28 #include "winbase.h"
29 #include "wingdi.h"
30 #include "winuser.h"
31 #include "winreg.h"
32 #define NO_SHLWAPI_STREAM
33 #include "shlwapi.h"
34 #include "shlobj.h"
35 #include "wine/unicode.h"
36 #include "wine/debug.h"
37
38 WINE_DEFAULT_DEBUG_CHANNEL(shell);
39
40 HRESULT WINAPI _SHStrDupAA(LPCSTR src, LPSTR * dest);
41 HRESULT WINAPI _SHStrDupAW(LPCWSTR src, LPSTR * dest);
42
43 /*************************************************************************
44  * ChrCmpIA                                     [SHLWAPI.385]
45  *
46  * Note: Returns 0 (FALSE) if characters are equal (insensitive).
47  */
48 BOOL WINAPI ChrCmpIA (WORD w1, WORD w2)
49 {
50         TRACE("%c ? %c\n", w1, w2);
51         return (toupper(w1) != toupper(w2));
52 }
53
54 /*************************************************************************
55  * ChrCmpIW                                     [SHLWAPI.386]
56  *
57  * Note: Returns 0 (FALSE) if characters are equal (insensitive).
58  */
59 BOOL WINAPI ChrCmpIW (WCHAR w1, WCHAR w2)
60 {
61         TRACE("%c ? %c\n", w1, w2);
62         return (toupperW(w1) != toupperW(w2));
63 }
64
65 /*************************************************************************
66  * StrChrA                                      [SHLWAPI.@]
67  */
68 LPSTR WINAPI StrChrA (LPCSTR str, WORD c)
69 {
70         TRACE("%s %i\n", str,c);
71         return strchr(str, c);
72 }
73
74 /*************************************************************************
75  * StrChrW                                      [SHLWAPI.@]
76  *
77  */
78 LPWSTR WINAPI StrChrW (LPCWSTR str, WCHAR x )
79 {
80         TRACE("%s 0x%04x\n",debugstr_w(str),x);
81         return strchrW(str, x);
82 }
83
84 /*************************************************************************
85  * StrCmpIW                                     [SHLWAPI.@]
86  */
87 int WINAPI StrCmpIW ( LPCWSTR wstr1, LPCWSTR wstr2 )
88 {
89     TRACE("%s %s\n", debugstr_w(wstr1),debugstr_w(wstr2));
90     return strcmpiW( wstr1, wstr2 );
91 }
92
93 /*************************************************************************
94  * StrCmpNA                                     [SHLWAPI.@]
95  */
96 INT WINAPI StrCmpNA ( LPCSTR str1, LPCSTR str2, INT len)
97 {
98         TRACE("%s %s %i stub\n", str1,str2,len);
99         return strncmp(str1, str2, len);
100 }
101
102 /*************************************************************************
103  * StrCmpNW                                     [SHLWAPI.@]
104  */
105 INT WINAPI StrCmpNW ( LPCWSTR wstr1, LPCWSTR wstr2, INT len)
106 {
107         TRACE("%s %s %i stub\n", debugstr_w(wstr1),debugstr_w(wstr2),len);
108         return strncmpW(wstr1, wstr2, len);
109 }
110
111 /*************************************************************************
112  * StrCmpNIA                                    [SHLWAPI.@]
113  */
114 int WINAPI StrCmpNIA ( LPCSTR str1, LPCSTR str2, int len)
115 {
116         TRACE("%s %s %i stub\n", str1,str2,len);
117         return strncasecmp(str1, str2, len);
118 }
119
120 /*************************************************************************
121  * StrCmpNIW                                    [SHLWAPI.@]
122  */
123 int WINAPI StrCmpNIW ( LPCWSTR wstr1, LPCWSTR wstr2, int len)
124 {
125         TRACE("%s %s %i stub\n", debugstr_w(wstr1),debugstr_w(wstr2),len);
126         return strncmpiW(wstr1, wstr2, len);
127 }
128
129 /*************************************************************************
130  * StrCmpW                                      [SHLWAPI.@]
131  */
132 int WINAPI StrCmpW ( LPCWSTR wstr1, LPCWSTR wstr2 )
133 {
134     TRACE("%s %s\n", debugstr_w(wstr1),debugstr_w(wstr2));
135     return strcmpW( wstr1, wstr2 );
136 }
137
138 /*************************************************************************
139  * StrCatW                                      [SHLWAPI.@]
140  */
141 LPWSTR WINAPI StrCatW( LPWSTR wstr1, LPCWSTR wstr2 )
142 {
143     return strcatW( wstr1, wstr2 );
144 }
145
146
147 /*************************************************************************
148  * StrCpyW                                      [SHLWAPI.@]
149  */
150 LPWSTR WINAPI StrCpyW( LPWSTR wstr1, LPCWSTR wstr2 )
151 {
152     return strcpyW( wstr1, wstr2 );
153 }
154
155
156 /*************************************************************************
157  * StrCpyNW                                     [SHLWAPI.@]
158  */
159 LPWSTR WINAPI StrCpyNW( LPWSTR wstr1, LPCWSTR wstr2, int n )
160 {
161     return lstrcpynW( wstr1, wstr2, n );
162 }
163
164
165 /*************************************************************************
166  * StrStrA                                      [SHLWAPI.@]
167  */
168 LPSTR WINAPI StrStrA(LPCSTR lpFirst, LPCSTR lpSrch)
169 {
170     while (*lpFirst)
171     {
172         LPCSTR p1 = lpFirst, p2 = lpSrch;
173         while (*p1 && *p2 && *p1 == *p2) { p1++; p2++; }
174         if (!*p2) return (LPSTR)lpFirst;
175         lpFirst++;
176     }
177     return NULL;
178 }
179
180 /*************************************************************************
181  * StrStrW                                      [SHLWAPI.@]
182  */
183 LPWSTR WINAPI StrStrW(LPCWSTR lpFirst, LPCWSTR lpSrch)
184 {
185     while (*lpFirst)
186     {
187         LPCWSTR p1 = lpFirst, p2 = lpSrch;
188         while (*p1 && *p2 && *p1 == *p2) { p1++; p2++; }
189         if (!*p2) return (LPWSTR)lpFirst;
190         lpFirst++;
191     }
192     return NULL;
193 }
194
195 /*************************************************************************
196  * StrStrIA                                     [SHLWAPI.@]
197  */
198 LPSTR WINAPI StrStrIA(LPCSTR lpFirst, LPCSTR lpSrch)
199 {
200     while (*lpFirst)
201     {
202         LPCSTR p1 = lpFirst, p2 = lpSrch;
203         while (*p1 && *p2 && toupper(*p1) == toupper(*p2)) { p1++; p2++; }
204         if (!*p2) return (LPSTR)lpFirst;
205         lpFirst++;
206     }
207     return NULL;
208 }
209
210 /*************************************************************************
211  * StrStrIW                                     [SHLWAPI.@]
212  */
213 LPWSTR WINAPI StrStrIW(LPCWSTR lpFirst, LPCWSTR lpSrch)
214 {
215     while (*lpFirst)
216     {
217         LPCWSTR p1 = lpFirst, p2 = lpSrch;
218         while (*p1 && *p2 && toupperW(*p1) == toupperW(*p2)) { p1++; p2++; }
219         if (!*p2) return (LPWSTR)lpFirst;
220         lpFirst++;
221     }
222     return NULL;
223 }
224
225 /*************************************************************************
226  *      StrToIntA                       [SHLWAPI.@]
227  */
228 int WINAPI StrToIntA(LPCSTR lpSrc)
229 {
230         TRACE("%s\n", lpSrc);
231         return atol(lpSrc);
232 }
233
234 /*************************************************************************
235  *      StrToIntW                       [SHLWAPI.@]
236  */
237 int WINAPI StrToIntW(LPCWSTR lpSrc)
238 {
239     char buffer[32];
240
241     TRACE("%s\n", debugstr_w(lpSrc));
242     WideCharToMultiByte( CP_ACP, 0, lpSrc, -1, buffer, sizeof(buffer), NULL, NULL );
243     buffer[sizeof(buffer)-1] = 0;
244     return atol(buffer);
245 }
246
247 /*************************************************************************
248  *      StrToIntExA                     [SHLWAPI.@]
249  */
250 BOOL WINAPI StrToIntExA( LPCSTR pszString, DWORD dwFlags, LPINT piRet)
251 {
252         TRACE("%s %ld stub !\n", debugstr_a(pszString), dwFlags);
253         piRet = (LPINT) StrToIntA(pszString);
254         return TRUE;
255 }
256
257 /*************************************************************************
258  *      StrToIntExW                     [SHLWAPI.@]
259  */
260 BOOL WINAPI StrToIntExW( LPCWSTR pszString, DWORD dwFlags, LPINT piRet)
261 {
262         TRACE("%s %ld stub !\n", debugstr_w(pszString), dwFlags);
263         piRet = (LPINT) StrToIntW(pszString);
264         return TRUE;
265 }
266
267 /*************************************************************************
268  *      StrDupA                 [SHLWAPI.@]
269  */
270 LPSTR WINAPI StrDupA (LPCSTR lpSrc)
271 {
272         int len = strlen(lpSrc);
273         LPSTR lpDest = (LPSTR) LocalAlloc(LMEM_FIXED, len+1);
274
275         TRACE("%s\n", lpSrc);
276
277         if (lpDest) strcpy(lpDest, lpSrc);
278         return lpDest;
279 }
280
281 /*************************************************************************
282  *      StrDupW                 [SHLWAPI.@]
283  */
284 LPWSTR WINAPI StrDupW (LPCWSTR lpSrc)
285 {
286         int len = strlenW(lpSrc);
287         LPWSTR lpDest = (LPWSTR) LocalAlloc(LMEM_FIXED, sizeof(WCHAR) * (len+1));
288
289         TRACE("%s\n", debugstr_w(lpSrc));
290
291         if (lpDest) strcpyW(lpDest, lpSrc);
292         return lpDest;
293 }
294
295 /*************************************************************************
296  *      StrCSpnA                [SHLWAPI.@]
297  */
298 int WINAPI StrCSpnA (LPCSTR lpStr, LPCSTR lpSet)
299 {
300         int i,j, pos = strlen(lpStr);
301
302         TRACE("(%p %s  %p %s)\n",
303            lpStr, debugstr_a(lpStr), lpSet, debugstr_a(lpSet));
304
305         for (i=0; i < strlen(lpSet) ; i++ )
306         {
307           for (j = 0; j < pos;j++)
308           {
309             if (lpStr[j] == lpSet[i])
310             {
311               pos = j;
312             }
313           }
314         }
315         TRACE("-- %u\n", pos);
316         return pos;
317 }
318
319 /*************************************************************************
320  *      StrCSpnW                [SHLWAPI.@]
321  */
322 int WINAPI StrCSpnW (LPCWSTR lpStr, LPCWSTR lpSet)
323 {
324         int i,j, pos = strlenW(lpStr);
325
326         TRACE("(%p %s %p %s)\n",
327            lpStr, debugstr_w(lpStr), lpSet, debugstr_w(lpSet));
328
329         for (i=0; i < strlenW(lpSet) ; i++ )
330         {
331           for (j = 0; j < pos;j++)
332           {
333             if (lpStr[j] == lpSet[i])
334             {
335               pos = j;
336             }
337           }
338         }
339         TRACE("-- %u\n", pos);
340         return pos;
341 }
342
343 /**************************************************************************
344  * StrRChrA [SHLWAPI.@]
345  *
346  */
347 LPSTR WINAPI StrRChrA( LPCSTR lpStart, LPCSTR lpEnd, WORD wMatch )
348 {
349     LPCSTR lpGotIt = NULL;
350     BOOL dbcs = IsDBCSLeadByte( LOBYTE(wMatch) );
351
352     TRACE("(%p, %p, %x)\n", lpStart, lpEnd, wMatch);
353     if (!lpStart && !lpEnd) return NULL;
354     if (!lpEnd) lpEnd = lpStart + strlen(lpStart);
355
356     for(; lpStart < lpEnd; lpStart = CharNextA(lpStart))
357     {
358         if (*lpStart != LOBYTE(wMatch)) continue;
359         if (dbcs && lpStart[1] != HIBYTE(wMatch)) continue;
360         lpGotIt = lpStart;
361     }
362     return (LPSTR)lpGotIt;
363 }
364
365
366 /**************************************************************************
367  * StrRChrW [SHLWAPI.@]
368  *
369  */
370 LPWSTR WINAPI StrRChrW( LPCWSTR lpStart, LPCWSTR lpEnd, WORD wMatch)
371 {
372     LPCWSTR lpGotIt = NULL;
373
374     TRACE("(%p, %p, %x)\n", lpStart, lpEnd, wMatch);
375     if (!lpStart && !lpEnd) return NULL;
376     if (!lpEnd) lpEnd = lpStart + strlenW(lpStart);
377
378     for(; lpStart < lpEnd; lpStart = CharNextW(lpStart))
379         if (*lpStart == wMatch) lpGotIt = lpStart;
380
381     return (LPWSTR)lpGotIt;
382 }
383
384
385 /**************************************************************************
386  * StrRChrIA [SHLWAPI.@]
387  *
388  */
389 LPSTR WINAPI StrRChrIA( LPCSTR lpStart, LPCSTR lpEnd, WORD wMatch )
390 {
391     LPCSTR lpGotIt = NULL;
392     BOOL dbcs = IsDBCSLeadByte( LOBYTE(wMatch) );
393
394     TRACE("(%p, %p, %x)\n", lpStart, lpEnd, wMatch);
395     if (!lpStart && !lpEnd) return NULL;
396     if (!lpEnd) lpEnd = lpStart + strlen(lpStart);
397
398     for(; lpStart < lpEnd; lpStart = CharNextA(lpStart))
399     {
400         if (dbcs) {
401             /*
402             if (_mbctoupper(*lpStart) == _mbctoupper(wMatch))
403                 lpGotIt = lpStart;
404             */
405             if (toupper(*lpStart) == toupper(wMatch)) lpGotIt = lpStart;
406         } else {
407             if (toupper(*lpStart) == toupper(wMatch)) lpGotIt = lpStart;
408         }
409     }
410     return (LPSTR)lpGotIt;
411 }
412
413
414 /**************************************************************************
415  * StrRChrIW [SHLWAPI.@]
416  *
417  */
418 LPWSTR WINAPI StrRChrIW( LPCWSTR lpStart, LPCWSTR lpEnd, WORD wMatch)
419 {
420     LPCWSTR lpGotIt = NULL;
421
422     TRACE("(%p, %p, %x)\n", lpStart, lpEnd, wMatch);
423     if (!lpStart && !lpEnd) return NULL;
424     if (!lpEnd) lpEnd = lpStart + strlenW(lpStart);
425
426     for(; lpStart < lpEnd; lpStart = CharNextW(lpStart))
427         if (toupperW(*lpStart) == toupperW(wMatch)) lpGotIt = lpStart;
428
429     return (LPWSTR)lpGotIt;
430 }
431
432
433 /*************************************************************************
434  *      StrCatBuffA             [SHLWAPI.@]
435  *
436  * Appends back onto front, stopping when front is size-1 characters long.
437  * Returns front.
438  *
439  */
440 LPSTR WINAPI StrCatBuffA(LPSTR front, LPCSTR back, INT size)
441 {
442     LPSTR dst = front + strlen(front);
443     LPCSTR src = back, end = front + size - 1;
444
445     while(dst < end && *src)
446         *dst++ = *src++;
447     *dst = '\0';
448     return front;
449 }
450
451 /*************************************************************************
452  *      StrCatBuffW             [SHLWAPI.@]
453  *
454  * Appends back onto front, stopping when front is size-1 characters long.
455  * Returns front.
456  *
457  */
458 LPWSTR WINAPI StrCatBuffW(LPWSTR front, LPCWSTR back, INT size)
459 {
460     LPWSTR dst = front + strlenW(front);
461     LPCWSTR src = back, end = front + size - 1;
462
463     while(dst < end && *src)
464         *dst++ = *src++;
465     *dst = '\0';
466     return front;
467 }
468
469 /*************************************************************************
470  * StrRetToBufA                                 [SHLWAPI.@]
471  *
472  * converts a STRRET to a normal string
473  *
474  * NOTES
475  *  the pidl is for STRRET OFFSET
476  *
477  * ***** NOTE *****
478  *  This routine is identical to StrRetToStrNA in dlls/shell32/shellstring.c.
479  *  It was duplicated there because not every version of Shlwapi.dll exports
480  *  StrRetToBufA. If you change one routine, change them both. YOU HAVE BEEN
481  *  WARNED.
482  * ***** NOTE *****
483  */
484 HRESULT WINAPI StrRetToBufA (LPSTRRET src, const ITEMIDLIST *pidl, LPSTR dest, DWORD len)
485 {
486         TRACE("dest=%p len=0x%lx strret=%p pidl=%p stub\n",dest,len,src,pidl);
487
488         switch (src->uType)
489         {
490           case STRRET_WSTR:
491             WideCharToMultiByte(CP_ACP, 0, src->u.pOleStr, -1, (LPSTR)dest, len, NULL, NULL);
492             CoTaskMemFree(src->u.pOleStr);
493             break;
494
495           case STRRET_CSTR:
496             lstrcpynA((LPSTR)dest, src->u.cStr, len);
497             break;
498
499           case STRRET_OFFSET:
500             lstrcpynA((LPSTR)dest, ((LPCSTR)&pidl->mkid)+src->u.uOffset, len);
501             break;
502
503           default:
504             FIXME("unknown type!\n");
505             if (len)
506             {
507               *(LPSTR)dest = '\0';
508             }
509             return(FALSE);
510         }
511         return S_OK;
512 }
513
514 /*************************************************************************
515  * StrRetToBufW                                 [SHLWAPI.@]
516  *
517  * converts a STRRET to a normal string
518  *
519  * NOTES
520  *  the pidl is for STRRET OFFSET
521  *
522  * ***** NOTE *****
523  *  This routine is identical to StrRetToStrNW in dlls/shell32/shellstring.c.
524  *  It was duplicated there because not every version of Shlwapi.dll exports
525  *  StrRetToBufW. If you change one routine, change them both. YOU HAVE BEEN
526  *  WARNED.
527  * ***** NOTE *****
528  */
529 HRESULT WINAPI StrRetToBufW (LPSTRRET src, const ITEMIDLIST *pidl, LPWSTR dest, DWORD len)
530 {
531         TRACE("dest=%p len=0x%lx strret=%p pidl=%p stub\n",dest,len,src,pidl);
532
533         switch (src->uType)
534         {
535           case STRRET_WSTR:
536             lstrcpynW((LPWSTR)dest, src->u.pOleStr, len);
537             CoTaskMemFree(src->u.pOleStr);
538             break;
539
540           case STRRET_CSTR:
541               if (!MultiByteToWideChar( CP_ACP, 0, src->u.cStr, -1, dest, len ) && len)
542                   dest[len-1] = 0;
543             break;
544
545           case STRRET_OFFSET:
546             if (pidl)
547             {
548               if (!MultiByteToWideChar( CP_ACP, 0, ((LPCSTR)&pidl->mkid)+src->u.uOffset, -1,
549                                         dest, len ) && len)
550                   dest[len-1] = 0;
551             }
552             break;
553
554           default:
555             FIXME("unknown type!\n");
556             if (len)
557             { *(LPSTR)dest = '\0';
558             }
559             return(FALSE);
560         }
561         return S_OK;
562 }
563
564 /*************************************************************************
565  * StrRetToStrA                                 [SHLWAPI.@]
566  *
567  * converts a STRRET to a normal string
568  */
569 HRESULT WINAPI StrRetToStrA(LPSTRRET pstr, const ITEMIDLIST * pidl, LPSTR* ppszName)
570 {
571         HRESULT ret = E_FAIL;
572
573         switch (pstr->uType) {
574             case STRRET_WSTR:
575                 ret = _SHStrDupAW(pstr->u.pOleStr, ppszName);
576                 CoTaskMemFree(pstr->u.pOleStr);
577                 break;
578
579             case STRRET_CSTR:
580                 ret = _SHStrDupAA(pstr->u.cStr, ppszName);
581                 break;
582
583             case STRRET_OFFSET:
584                 ret = _SHStrDupAA(((LPCSTR)&pidl->mkid)+pstr->u.uOffset, ppszName);
585                 break;
586
587             default:
588                 *ppszName = NULL;
589         }
590         return ret;
591 }
592
593 /*************************************************************************
594  * StrRetToStrW                                 [SHLWAPI.@]
595  *
596  * converts a STRRET to a normal string
597  */
598 HRESULT WINAPI StrRetToStrW(LPSTRRET pstr, const ITEMIDLIST * pidl, LPWSTR* ppszName)
599 {
600         HRESULT ret = E_FAIL;
601
602         switch (pstr->uType) {
603             case STRRET_WSTR:
604                 ret = SHStrDupW(pstr->u.pOleStr, ppszName);
605                 CoTaskMemFree(pstr->u.pOleStr);
606                 break;
607
608             case STRRET_CSTR:
609                 ret = SHStrDupA(pstr->u.cStr, ppszName);
610                 break;
611
612             case STRRET_OFFSET:
613                 ret = SHStrDupA(((LPCSTR)&pidl->mkid)+pstr->u.uOffset, ppszName);
614                 break;
615
616             default:
617                 *ppszName = NULL;
618         }
619         return ret;
620 }
621
622 /*************************************************************************
623  * StrFormatByteSizeA                           [SHLWAPI.@]
624  */
625 LPSTR WINAPI StrFormatByteSizeA ( DWORD dw, LPSTR pszBuf, UINT cchBuf )
626 {       char buf[64];
627         TRACE("%lx %p %i\n", dw, pszBuf, cchBuf);
628         if ( dw<1024L )
629         { sprintf (buf,"%ld bytes", dw);
630         }
631         else if ( dw<1048576L)
632         { sprintf (buf,"%3.1f KB", (FLOAT)dw/1024);
633         }
634         else if ( dw < 1073741824L)
635         { sprintf (buf,"%3.1f MB", (FLOAT)dw/1048576L);
636         }
637         else
638         { sprintf (buf,"%3.1f GB", (FLOAT)dw/1073741824L);
639         }
640         lstrcpynA (pszBuf, buf, cchBuf);
641         return pszBuf;
642 }
643
644 /*************************************************************************
645  * StrFormatByteSizeW                           [SHLWAPI.@]
646  */
647 LPWSTR WINAPI StrFormatByteSizeW ( DWORD dw, LPWSTR pszBuf, UINT cchBuf )
648 {
649         char buf[64];
650         StrFormatByteSizeA( dw, buf, sizeof(buf) );
651         if (!MultiByteToWideChar( CP_ACP, 0, buf, -1, pszBuf, cchBuf ) && cchBuf)
652             pszBuf[cchBuf-1] = 0;
653         return pszBuf;
654 }
655
656 /*************************************************************************
657  *      StrNCatA        [SHLWAPI.@]
658  */
659 LPSTR WINAPI StrNCatA(LPSTR front, LPCSTR back, INT cchMax)
660 {
661         TRACE("%s %s %i stub\n", debugstr_a(front),debugstr_a(back),cchMax);
662         return (front);
663 }
664
665 /*************************************************************************
666  *      StrNCatW        [SHLWAPI.@]
667  */
668 LPWSTR WINAPI StrNCatW(LPWSTR front, LPCWSTR back, INT cchMax)
669 {
670         TRACE("%s %s %i stub\n", debugstr_w(front),debugstr_w(back),cchMax);
671         return (front);
672 }
673
674 /*************************************************************************
675  *      StrTrimA        [SHLWAPI.@]
676  */
677 BOOL WINAPI StrTrimA(LPSTR pszSource, LPCSTR pszTrimChars)
678 {
679     BOOL trimmed = FALSE;
680     LPSTR pSrc;
681     LPCSTR pTrim;
682
683     TRACE("('%s', '%s');\n", pszSource, pszTrimChars);
684     for (pTrim = pszTrimChars; *pTrim; pTrim++)
685     {
686          for (pSrc = pszSource; *pSrc; pSrc++)
687              if (*pSrc == *pTrim)
688              {
689                  /* match -> remove this char.
690                   * strlen(pSrc) equiv. to the correct strlen(pSrc+1)+1 */
691                  memmove(pSrc, pSrc+1, strlen(pSrc));
692                  trimmed = TRUE;
693              }
694     }
695     TRACE("<- '%s'\n", pszSource);
696     return trimmed;
697 }
698
699 /*************************************************************************
700  *      _SHStrDupA      [INTERNAL]
701  *
702  * Duplicates a ASCII string to ASCII. The destination buffer is allocated.
703  */
704 HRESULT WINAPI _SHStrDupAA(LPCSTR src, LPSTR * dest)
705 {
706         HRESULT hr;
707         int len = 0;
708
709         if (src) {
710             len = lstrlenA(src);
711             *dest = CoTaskMemAlloc(len);
712         } else {
713             *dest = NULL;
714         }
715
716         if (*dest) {
717             lstrcpynA(*dest,src, len);
718             hr = S_OK;
719         } else {
720             hr = E_OUTOFMEMORY;
721         }
722
723         TRACE("%s->(%p)\n", debugstr_a(src), *dest);
724         return hr;
725 }
726
727 /*************************************************************************
728  *      SHStrDupA       [SHLWAPI.@]
729  *
730  * Duplicates a ASCII string to UNICODE. The destination buffer is allocated.
731  */
732 HRESULT WINAPI SHStrDupA(LPCSTR src, LPWSTR * dest)
733 {
734         HRESULT hr;
735         int len = 0;
736
737         if (src) {
738             len = (MultiByteToWideChar(0,0,src,-1,0,0) + 1)* sizeof(WCHAR);
739             *dest = CoTaskMemAlloc(len);
740         } else {
741             *dest = NULL;
742         }
743
744         if (*dest) {
745             MultiByteToWideChar(0,0,src,-1,*dest,len);
746             hr = S_OK;
747         } else {
748             hr = E_OUTOFMEMORY;
749         }
750
751         TRACE("%s->(%p)\n", debugstr_a(src), *dest);
752         return hr;
753 }
754
755 /*************************************************************************
756  *      _SHStrDupAW     [INTERNAL]
757  *
758  * Duplicates a UNICODE to a ASCII string. The destination buffer is allocated.
759  */
760 HRESULT WINAPI _SHStrDupAW(LPCWSTR src, LPSTR * dest)
761 {
762         HRESULT hr;
763         int len = 0;
764
765         if (src) {
766             len = WideCharToMultiByte(CP_ACP, 0, src, -1, NULL, 0, NULL, NULL);
767             *dest = CoTaskMemAlloc(len);
768         } else {
769             *dest = NULL;
770         }
771
772         if (*dest) {
773             WideCharToMultiByte(CP_ACP, 0, src, -1, *dest, len, NULL, NULL);
774             hr = S_OK;
775         } else {
776             hr = E_OUTOFMEMORY;
777         }
778
779         TRACE("%s->(%p)\n", debugstr_w(src), *dest);
780         return hr;
781 }
782
783 /*************************************************************************
784  *      SHStrDupW       [SHLWAPI.@]
785  *
786  * Duplicates a UNICODE string. The destination buffer is allocated.
787  */
788 HRESULT WINAPI SHStrDupW(LPCWSTR src, LPWSTR * dest)
789 {
790         HRESULT hr;
791         int len = 0;
792
793         if (src) {
794             len = (lstrlenW(src) + 1) * sizeof(WCHAR);
795             *dest = CoTaskMemAlloc(len);
796         } else {
797             *dest = NULL;
798         }
799
800         if (*dest) {
801             memcpy(*dest, src, len);
802             hr = S_OK;
803         } else {
804             hr = E_OUTOFMEMORY;
805         }
806
807         TRACE("%s->(%p)\n", debugstr_w(src), *dest);
808         return hr;
809 }