2 * Shlwapi string functions
4 * Copyright 1998 Juergen Schmied
5 * Copyright 2002 Jon Griffiths
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "wine/port.h"
30 #define NONAMELESSUNION
31 #define NONAMELESSSTRUCT
34 #define NO_SHLWAPI_REG
35 #define NO_SHLWAPI_STREAM
42 #include "wine/unicode.h"
43 #include "wine/debug.h"
47 WINE_DEFAULT_DEBUG_CHANNEL(shell
);
49 extern HINSTANCE shlwapi_hInstance
;
51 static HRESULT
_SHStrDupAA(LPCSTR
,LPSTR
*);
52 static HRESULT
_SHStrDupAW(LPCWSTR
,LPSTR
*);
55 static void FillNumberFmt(NUMBERFMTW
*fmt
, LPWSTR decimal_buffer
, int decimal_bufwlen
,
56 LPWSTR thousand_buffer
, int thousand_bufwlen
)
61 GetLocaleInfoW(LOCALE_USER_DEFAULT
, LOCALE_ILZERO
|LOCALE_RETURN_NUMBER
, (LPWSTR
)&fmt
->LeadingZero
, sizeof(fmt
->LeadingZero
)/sizeof(WCHAR
));
62 GetLocaleInfoW(LOCALE_USER_DEFAULT
, LOCALE_INEGNUMBER
|LOCALE_RETURN_NUMBER
, (LPWSTR
)&fmt
->LeadingZero
, sizeof(fmt
->NegativeOrder
)/sizeof(WCHAR
));
64 GetLocaleInfoW(LOCALE_USER_DEFAULT
, LOCALE_SDECIMAL
, decimal_buffer
, decimal_bufwlen
);
65 GetLocaleInfoW(LOCALE_USER_DEFAULT
, LOCALE_STHOUSAND
, thousand_buffer
, thousand_bufwlen
);
66 fmt
->lpThousandSep
= thousand_buffer
;
67 fmt
->lpDecimalSep
= decimal_buffer
;
70 * Converting grouping string to number as described on
71 * http://blogs.msdn.com/oldnewthing/archive/2006/04/18/578251.aspx
74 GetLocaleInfoW(LOCALE_USER_DEFAULT
, LOCALE_SGROUPING
, grouping
, sizeof(grouping
)/sizeof(WCHAR
));
75 for (c
= grouping
; *c
; c
++)
76 if (*c
>= '0' && *c
< '9')
79 fmt
->Grouping
+= *c
- '0';
82 if (fmt
->Grouping
% 10 == 0)
88 /*************************************************************************
89 * FormatInt [internal]
91 * Format an integer according to the current locale
94 * The number of characters written on success or 0 on failure
96 static int FormatInt(LONGLONG qdwValue
, LPWSTR pszBuf
, int cchBuf
)
99 WCHAR decimal
[8], thousand
[8];
102 BOOL neg
= (qdwValue
< 0);
104 FillNumberFmt(&fmt
, decimal
, sizeof decimal
/ sizeof (WCHAR
),
105 thousand
, sizeof thousand
/ sizeof (WCHAR
));
111 *(--c
) = '0' + (qdwValue
%10);
113 } while (qdwValue
> 0);
117 return GetNumberFormatW(LOCALE_USER_DEFAULT
, 0, c
, &fmt
, pszBuf
, cchBuf
);
120 /*************************************************************************
121 * FormatDouble [internal]
123 * Format an integer according to the current locale. Prints the specified number of digits
124 * after the decimal point
127 * The number of characters written on success or 0 on failure
129 static int FormatDouble(double value
, int decimals
, LPWSTR pszBuf
, int cchBuf
)
131 static const WCHAR flfmt
[] = {'%','f',0};
134 WCHAR decimal
[8], thousand
[8];
136 snprintfW(buf
, 64, flfmt
, value
);
138 FillNumberFmt(&fmt
, decimal
, sizeof decimal
/ sizeof (WCHAR
),
139 thousand
, sizeof thousand
/ sizeof (WCHAR
));
140 fmt
.NumDigits
= decimals
;
141 return GetNumberFormatW(LOCALE_USER_DEFAULT
, 0, buf
, &fmt
, pszBuf
, cchBuf
);
144 /*************************************************************************
145 * SHLWAPI_ChrCmpHelperA
147 * Internal helper for SHLWAPI_ChrCmpA/ChrCMPIA.
150 * Both this function and its Unicode counterpart are very inefficient. To
151 * fix this, CompareString must be completely implemented and optimised
152 * first. Then the core character test can be taken out of that function and
153 * placed here, so that it need never be called at all. Until then, do not
154 * attempt to optimise this code unless you are willing to test that it
155 * still performs correctly.
157 static BOOL
SHLWAPI_ChrCmpHelperA(WORD ch1
, WORD ch2
, DWORD dwFlags
)
159 char str1
[3], str2
[3];
161 str1
[0] = LOBYTE(ch1
);
162 if (IsDBCSLeadByte(str1
[0]))
164 str1
[1] = HIBYTE(ch1
);
170 str2
[0] = LOBYTE(ch2
);
171 if (IsDBCSLeadByte(str2
[0]))
173 str2
[1] = HIBYTE(ch2
);
179 return CompareStringA(GetThreadLocale(), dwFlags
, str1
, -1, str2
, -1) - CSTR_EQUAL
;
182 /*************************************************************************
185 * Internal helper function.
187 static BOOL WINAPI
SHLWAPI_ChrCmpA(WORD ch1
, WORD ch2
)
189 return SHLWAPI_ChrCmpHelperA(ch1
, ch2
, 0);
192 /*************************************************************************
193 * ChrCmpIA (SHLWAPI.385)
195 * Compare two characters, ignoring case.
198 * ch1 [I] First character to compare
199 * ch2 [I] Second character to compare
202 * FALSE, if the characters are equal.
203 * Non-zero otherwise.
205 BOOL WINAPI
ChrCmpIA(WORD ch1
, WORD ch2
)
207 TRACE("(%d,%d)\n", ch1
, ch2
);
209 return SHLWAPI_ChrCmpHelperA(ch1
, ch2
, NORM_IGNORECASE
);
212 /*************************************************************************
213 * ChrCmpIW [SHLWAPI.386]
217 BOOL WINAPI
ChrCmpIW(WCHAR ch1
, WCHAR ch2
)
219 return CompareStringW(GetThreadLocale(), NORM_IGNORECASE
, &ch1
, 1, &ch2
, 1) - CSTR_EQUAL
;
222 /*************************************************************************
223 * StrChrA [SHLWAPI.@]
225 * Find a given character in a string.
228 * lpszStr [I] String to search in.
229 * ch [I] Character to search for.
232 * Success: A pointer to the first occurrence of ch in lpszStr, or NULL if
234 * Failure: NULL, if any arguments are invalid.
236 LPSTR WINAPI
StrChrA(LPCSTR lpszStr
, WORD ch
)
238 TRACE("(%s,%i)\n", debugstr_a(lpszStr
), ch
);
244 if (!SHLWAPI_ChrCmpA(*lpszStr
, ch
))
245 return (LPSTR
)lpszStr
;
246 lpszStr
= CharNextA(lpszStr
);
252 /*************************************************************************
253 * StrChrW [SHLWAPI.@]
257 LPWSTR WINAPI
StrChrW(LPCWSTR lpszStr
, WCHAR ch
)
259 LPWSTR lpszRet
= NULL
;
261 TRACE("(%s,%i)\n", debugstr_w(lpszStr
), ch
);
264 lpszRet
= strchrW(lpszStr
, ch
);
268 /*************************************************************************
269 * StrChrIA [SHLWAPI.@]
271 * Find a given character in a string, ignoring case.
274 * lpszStr [I] String to search in.
275 * ch [I] Character to search for.
278 * Success: A pointer to the first occurrence of ch in lpszStr, or NULL if
280 * Failure: NULL, if any arguments are invalid.
282 LPSTR WINAPI
StrChrIA(LPCSTR lpszStr
, WORD ch
)
284 TRACE("(%s,%i)\n", debugstr_a(lpszStr
), ch
);
290 if (!ChrCmpIA(*lpszStr
, ch
))
291 return (LPSTR
)lpszStr
;
292 lpszStr
= CharNextA(lpszStr
);
298 /*************************************************************************
299 * StrChrIW [SHLWAPI.@]
303 LPWSTR WINAPI
StrChrIW(LPCWSTR lpszStr
, WCHAR ch
)
305 TRACE("(%s,%i)\n", debugstr_w(lpszStr
), ch
);
312 if (toupperW(*lpszStr
) == ch
)
313 return (LPWSTR
)lpszStr
;
318 return (LPWSTR
)lpszStr
;
321 /*************************************************************************
322 * StrChrNW [SHLWAPI.@]
324 LPWSTR WINAPI
StrChrNW(LPCWSTR lpszStr
, WCHAR ch
, UINT cchMax
)
326 TRACE("(%s(%i),%i)\n", debugstr_wn(lpszStr
,cchMax
), cchMax
, ch
);
330 while (*lpszStr
&& cchMax
-- > 0)
333 return (LPWSTR
)lpszStr
;
340 /*************************************************************************
341 * StrCmpIW [SHLWAPI.@]
343 * Compare two strings, ignoring case.
346 * lpszStr [I] First string to compare
347 * lpszComp [I] Second string to compare
350 * An integer less than, equal to or greater than 0, indicating that
351 * lpszStr is less than, the same, or greater than lpszComp.
353 int WINAPI
StrCmpIW(LPCWSTR lpszStr
, LPCWSTR lpszComp
)
355 TRACE("(%s,%s)\n", debugstr_w(lpszStr
),debugstr_w(lpszComp
));
356 return CompareStringW(GetThreadLocale(), NORM_IGNORECASE
, lpszStr
, -1, lpszComp
, -1) - CSTR_EQUAL
;
359 /*************************************************************************
360 * StrCmpNA [SHLWAPI.@]
362 * Compare two strings, up to a maximum length.
365 * lpszStr [I] First string to compare
366 * lpszComp [I] Second string to compare
367 * iLen [I] Maximum number of chars to compare.
370 * An integer less than, equal to or greater than 0, indicating that
371 * lpszStr is less than, the same, or greater than lpszComp.
373 INT WINAPI
StrCmpNA(LPCSTR lpszStr
, LPCSTR lpszComp
, INT iLen
)
375 TRACE("(%s,%s,%i)\n", debugstr_a(lpszStr
), debugstr_a(lpszComp
), iLen
);
376 return CompareStringA(GetThreadLocale(), 0, lpszStr
, iLen
, lpszComp
, iLen
) - CSTR_EQUAL
;
379 /*************************************************************************
380 * StrCmpNW [SHLWAPI.@]
384 INT WINAPI
StrCmpNW(LPCWSTR lpszStr
, LPCWSTR lpszComp
, INT iLen
)
386 TRACE("(%s,%s,%i)\n", debugstr_w(lpszStr
), debugstr_w(lpszComp
), iLen
);
387 return CompareStringW(GetThreadLocale(), 0, lpszStr
, iLen
, lpszComp
, iLen
) - CSTR_EQUAL
;
390 /*************************************************************************
391 * StrCmpNIA [SHLWAPI.@]
393 * Compare two strings, up to a maximum length, ignoring case.
396 * lpszStr [I] First string to compare
397 * lpszComp [I] Second string to compare
398 * iLen [I] Maximum number of chars to compare.
401 * An integer less than, equal to or greater than 0, indicating that
402 * lpszStr is less than, the same, or greater than lpszComp.
404 int WINAPI
StrCmpNIA(LPCSTR lpszStr
, LPCSTR lpszComp
, int iLen
)
406 TRACE("(%s,%s,%i)\n", debugstr_a(lpszStr
), debugstr_a(lpszComp
), iLen
);
407 return CompareStringA(GetThreadLocale(), NORM_IGNORECASE
, lpszStr
, iLen
, lpszComp
, iLen
) - CSTR_EQUAL
;
410 /*************************************************************************
411 * StrCmpNIW [SHLWAPI.@]
415 INT WINAPI
StrCmpNIW(LPCWSTR lpszStr
, LPCWSTR lpszComp
, int iLen
)
417 TRACE("(%s,%s,%i)\n", debugstr_w(lpszStr
), debugstr_w(lpszComp
), iLen
);
418 return CompareStringW(GetThreadLocale(), NORM_IGNORECASE
, lpszStr
, iLen
, lpszComp
, iLen
) - CSTR_EQUAL
;
421 /*************************************************************************
422 * StrCmpW [SHLWAPI.@]
424 * Compare two strings.
427 * lpszStr [I] First string to compare
428 * lpszComp [I] Second string to compare
431 * An integer less than, equal to or greater than 0, indicating that
432 * lpszStr is less than, the same, or greater than lpszComp.
434 int WINAPI
StrCmpW(LPCWSTR lpszStr
, LPCWSTR lpszComp
)
436 TRACE("(%s,%s)\n", debugstr_w(lpszStr
), debugstr_w(lpszComp
));
437 return CompareStringW(GetThreadLocale(), 0, lpszStr
, -1, lpszComp
, -1) - CSTR_EQUAL
;
440 /*************************************************************************
441 * StrCatW [SHLWAPI.@]
443 * Concatenate two strings.
446 * lpszStr [O] Initial string
447 * lpszSrc [I] String to concatenate
452 LPWSTR WINAPI
StrCatW(LPWSTR lpszStr
, LPCWSTR lpszSrc
)
454 TRACE("(%s,%s)\n", debugstr_w(lpszStr
), debugstr_w(lpszSrc
));
456 if (lpszStr
&& lpszSrc
)
457 strcatW(lpszStr
, lpszSrc
);
461 /*************************************************************************
462 * StrCpyW [SHLWAPI.@]
464 * Copy a string to another string.
467 * lpszStr [O] Destination string
468 * lpszSrc [I] Source string
473 LPWSTR WINAPI
StrCpyW(LPWSTR lpszStr
, LPCWSTR lpszSrc
)
475 TRACE("(%p,%s)\n", lpszStr
, debugstr_w(lpszSrc
));
477 if (lpszStr
&& lpszSrc
)
478 strcpyW(lpszStr
, lpszSrc
);
482 /*************************************************************************
483 * StrCpyNW [SHLWAPI.@]
485 * Copy a string to another string, up to a maximum number of characters.
488 * dst [O] Destination string
489 * src [I] Source string
490 * count [I] Maximum number of chars to copy
495 LPWSTR WINAPI
StrCpyNW(LPWSTR dst
, LPCWSTR src
, int count
)
500 TRACE("(%p,%s,%i)\n", dst
, debugstr_w(src
), count
);
504 while ((count
> 1) && *s
)
515 /*************************************************************************
516 * SHLWAPI_StrStrHelperA
518 * Internal implementation of StrStrA/StrStrIA
520 static LPSTR
SHLWAPI_StrStrHelperA(LPCSTR lpszStr
, LPCSTR lpszSearch
,
521 INT (WINAPI
*pStrCmpFn
)(LPCSTR
,LPCSTR
,INT
))
525 if (!lpszStr
|| !lpszSearch
|| !*lpszSearch
)
528 iLen
= strlen(lpszSearch
);
532 if (!pStrCmpFn(lpszStr
, lpszSearch
, iLen
))
533 return (LPSTR
)lpszStr
;
534 lpszStr
= CharNextA(lpszStr
);
539 /*************************************************************************
540 * StrStrA [SHLWAPI.@]
542 * Find a substring within a string.
545 * lpszStr [I] String to search in
546 * lpszSearch [I] String to look for
549 * The start of lpszSearch within lpszStr, or NULL if not found.
551 LPSTR WINAPI
StrStrA(LPCSTR lpszStr
, LPCSTR lpszSearch
)
553 TRACE("(%s,%s)\n", debugstr_a(lpszStr
), debugstr_a(lpszSearch
));
555 return SHLWAPI_StrStrHelperA(lpszStr
, lpszSearch
, StrCmpNA
);
558 /*************************************************************************
559 * StrStrW [SHLWAPI.@]
563 LPWSTR WINAPI
StrStrW(LPCWSTR lpszStr
, LPCWSTR lpszSearch
)
565 TRACE("(%s, %s)\n", debugstr_w(lpszStr
), debugstr_w(lpszSearch
));
567 if (!lpszStr
|| !lpszSearch
|| !*lpszSearch
) return NULL
;
568 return strstrW( lpszStr
, lpszSearch
);
571 /*************************************************************************
572 * StrRStrIA [SHLWAPI.@]
574 * Find the last occurrence of a substring within a string.
577 * lpszStr [I] String to search in
578 * lpszEnd [I] End of lpszStr
579 * lpszSearch [I] String to look for
582 * The last occurrence lpszSearch within lpszStr, or NULL if not found.
584 LPSTR WINAPI
StrRStrIA(LPCSTR lpszStr
, LPCSTR lpszEnd
, LPCSTR lpszSearch
)
589 TRACE("(%s,%s)\n", debugstr_a(lpszStr
), debugstr_a(lpszSearch
));
591 if (!lpszStr
|| !lpszSearch
|| !*lpszSearch
)
595 lpszEnd
= lpszStr
+ lstrlenA(lpszStr
);
596 if (lpszEnd
== lpszStr
)
599 if (IsDBCSLeadByte(*lpszSearch
))
600 ch1
= *lpszSearch
<< 8 | (UCHAR
)lpszSearch
[1];
603 iLen
= lstrlenA(lpszSearch
);
607 lpszEnd
= CharPrevA(lpszStr
, lpszEnd
);
608 ch2
= IsDBCSLeadByte(*lpszEnd
)? *lpszEnd
<< 8 | (UCHAR
)lpszEnd
[1] : *lpszEnd
;
609 if (!ChrCmpIA(ch1
, ch2
))
611 if (!StrCmpNIA(lpszEnd
, lpszSearch
, iLen
))
612 return (LPSTR
)lpszEnd
;
614 } while (lpszEnd
> lpszStr
);
618 /*************************************************************************
619 * StrRStrIW [SHLWAPI.@]
623 LPWSTR WINAPI
StrRStrIW(LPCWSTR lpszStr
, LPCWSTR lpszEnd
, LPCWSTR lpszSearch
)
627 TRACE("(%s,%s)\n", debugstr_w(lpszStr
), debugstr_w(lpszSearch
));
629 if (!lpszStr
|| !lpszSearch
|| !*lpszSearch
)
633 lpszEnd
= lpszStr
+ strlenW(lpszStr
);
635 iLen
= strlenW(lpszSearch
);
637 while (lpszEnd
> lpszStr
)
640 if (!StrCmpNIW(lpszEnd
, lpszSearch
, iLen
))
641 return (LPWSTR
)lpszEnd
;
646 /*************************************************************************
647 * StrStrIA [SHLWAPI.@]
649 * Find a substring within a string, ignoring case.
652 * lpszStr [I] String to search in
653 * lpszSearch [I] String to look for
656 * The start of lpszSearch within lpszStr, or NULL if not found.
658 LPSTR WINAPI
StrStrIA(LPCSTR lpszStr
, LPCSTR lpszSearch
)
660 TRACE("(%s,%s)\n", debugstr_a(lpszStr
), debugstr_a(lpszSearch
));
662 return SHLWAPI_StrStrHelperA(lpszStr
, lpszSearch
, StrCmpNIA
);
665 /*************************************************************************
666 * StrStrIW [SHLWAPI.@]
670 LPWSTR WINAPI
StrStrIW(LPCWSTR lpszStr
, LPCWSTR lpszSearch
)
674 TRACE("(%s,%s)\n", debugstr_w(lpszStr
), debugstr_w(lpszSearch
));
676 if (!lpszStr
|| !lpszSearch
|| !*lpszSearch
)
679 iLen
= strlenW(lpszSearch
);
683 if (!StrCmpNIW(lpszStr
, lpszSearch
, iLen
))
684 return (LPWSTR
)lpszStr
;
690 /*************************************************************************
691 * StrStrNW [SHLWAPI.@]
693 * Find a substring within a string up to a given number of initial characters.
696 * lpFirst [I] String to search in
697 * lpSrch [I] String to look for
698 * cchMax [I] Maximum number of initial search characters
701 * The start of lpFirst within lpSrch, or NULL if not found.
703 LPWSTR WINAPI
StrStrNW(LPCWSTR lpFirst
, LPCWSTR lpSrch
, UINT cchMax
)
708 TRACE("(%s, %s, %u)\n", debugstr_w(lpFirst
), debugstr_w(lpSrch
), cchMax
);
710 if (!lpFirst
|| !lpSrch
|| !*lpSrch
|| !cchMax
)
713 len
= strlenW(lpSrch
);
715 for (i
= cchMax
; *lpFirst
&& (i
> 0); i
--, lpFirst
++)
717 if (!strncmpW(lpFirst
, lpSrch
, len
))
718 return (LPWSTR
)lpFirst
;
724 /*************************************************************************
725 * StrStrNIW [SHLWAPI.@]
727 * Find a substring within a string up to a given number of initial characters,
731 * lpFirst [I] String to search in
732 * lpSrch [I] String to look for
733 * cchMax [I] Maximum number of initial search characters
736 * The start of lpFirst within lpSrch, or NULL if not found.
738 LPWSTR WINAPI
StrStrNIW(LPCWSTR lpFirst
, LPCWSTR lpSrch
, UINT cchMax
)
743 TRACE("(%s, %s, %u)\n", debugstr_w(lpFirst
), debugstr_w(lpSrch
), cchMax
);
745 if (!lpFirst
|| !lpSrch
|| !*lpSrch
|| !cchMax
)
748 len
= strlenW(lpSrch
);
750 for (i
= cchMax
; *lpFirst
&& (i
> 0); i
--, lpFirst
++)
752 if (!strncmpiW(lpFirst
, lpSrch
, len
))
753 return (LPWSTR
)lpFirst
;
759 /*************************************************************************
760 * StrToIntA [SHLWAPI.@]
762 * Read a signed integer from a string.
765 * lpszStr [I] String to read integer from
768 * The signed integer value represented by the string, or 0 if no integer is
772 * No leading space is allowed before the number, although a leading '-' is.
774 int WINAPI
StrToIntA(LPCSTR lpszStr
)
778 TRACE("(%s)\n", debugstr_a(lpszStr
));
782 WARN("Invalid lpszStr would crash under Win32!\n");
786 if (*lpszStr
== '-' || isdigit(*lpszStr
))
787 StrToIntExA(lpszStr
, 0, &iRet
);
791 /*************************************************************************
792 * StrToIntW [SHLWAPI.@]
796 int WINAPI
StrToIntW(LPCWSTR lpszStr
)
800 TRACE("(%s)\n", debugstr_w(lpszStr
));
804 WARN("Invalid lpszStr would crash under Win32!\n");
808 if (*lpszStr
== '-' || isdigitW(*lpszStr
))
809 StrToIntExW(lpszStr
, 0, &iRet
);
813 /*************************************************************************
814 * StrToIntExA [SHLWAPI.@]
816 * Read an integer from a string.
819 * lpszStr [I] String to read integer from
820 * dwFlags [I] Flags controlling the conversion
821 * lpiRet [O] Destination for read integer.
824 * Success: TRUE. lpiRet contains the integer value represented by the string.
825 * Failure: FALSE, if the string is invalid, or no number is present.
828 * Leading whitespace, '-' and '+' are allowed before the number. If
829 * dwFlags includes STIF_SUPPORT_HEX, hexadecimal numbers are allowed, if
830 * preceded by '0x'. If this flag is not set, or there is no '0x' prefix,
831 * the string is treated as a decimal string. A leading '-' is ignored for
832 * hexadecimal numbers.
834 BOOL WINAPI
StrToIntExA(LPCSTR lpszStr
, DWORD dwFlags
, LPINT lpiRet
)
839 TRACE("(%s,%08X,%p)\n", debugstr_a(lpszStr
), dwFlags
, lpiRet
);
841 bRes
= StrToInt64ExA(lpszStr
, dwFlags
, &li
);
842 if (bRes
) *lpiRet
= li
;
846 /*************************************************************************
847 * StrToInt64ExA [SHLWAPI.@]
851 BOOL WINAPI
StrToInt64ExA(LPCSTR lpszStr
, DWORD dwFlags
, LONGLONG
*lpiRet
)
853 BOOL bNegative
= FALSE
;
856 TRACE("(%s,%08X,%p)\n", debugstr_a(lpszStr
), dwFlags
, lpiRet
);
858 if (!lpszStr
|| !lpiRet
)
860 WARN("Invalid parameter would crash under Win32!\n");
863 if (dwFlags
> STIF_SUPPORT_HEX
) WARN("Unknown flags %08x\n", dwFlags
);
865 /* Skip leading space, '+', '-' */
866 while (isspace(*lpszStr
))
867 lpszStr
= CharNextA(lpszStr
);
874 else if (*lpszStr
== '+')
877 if (dwFlags
& STIF_SUPPORT_HEX
&&
878 *lpszStr
== '0' && tolower(lpszStr
[1]) == 'x')
880 /* Read hex number */
883 if (!isxdigit(*lpszStr
))
886 while (isxdigit(*lpszStr
))
889 if (isdigit(*lpszStr
))
890 iRet
+= (*lpszStr
- '0');
892 iRet
+= 10 + (tolower(*lpszStr
) - 'a');
899 /* Read decimal number */
900 if (!isdigit(*lpszStr
))
903 while (isdigit(*lpszStr
))
906 iRet
+= (*lpszStr
- '0');
909 *lpiRet
= bNegative
? -iRet
: iRet
;
913 /*************************************************************************
914 * StrToIntExW [SHLWAPI.@]
918 BOOL WINAPI
StrToIntExW(LPCWSTR lpszStr
, DWORD dwFlags
, LPINT lpiRet
)
923 TRACE("(%s,%08X,%p)\n", debugstr_w(lpszStr
), dwFlags
, lpiRet
);
925 bRes
= StrToInt64ExW(lpszStr
, dwFlags
, &li
);
926 if (bRes
) *lpiRet
= li
;
930 /*************************************************************************
931 * StrToInt64ExW [SHLWAPI.@]
935 BOOL WINAPI
StrToInt64ExW(LPCWSTR lpszStr
, DWORD dwFlags
, LONGLONG
*lpiRet
)
937 BOOL bNegative
= FALSE
;
940 TRACE("(%s,%08X,%p)\n", debugstr_w(lpszStr
), dwFlags
, lpiRet
);
942 if (!lpszStr
|| !lpiRet
)
944 WARN("Invalid parameter would crash under Win32!\n");
947 if (dwFlags
> STIF_SUPPORT_HEX
) WARN("Unknown flags %08x\n", dwFlags
);
949 /* Skip leading space, '+', '-' */
950 while (isspaceW(*lpszStr
)) lpszStr
++;
957 else if (*lpszStr
== '+')
960 if (dwFlags
& STIF_SUPPORT_HEX
&&
961 *lpszStr
== '0' && tolowerW(lpszStr
[1]) == 'x')
963 /* Read hex number */
966 if (!isxdigitW(*lpszStr
))
969 while (isxdigitW(*lpszStr
))
972 if (isdigitW(*lpszStr
))
973 iRet
+= (*lpszStr
- '0');
975 iRet
+= 10 + (tolowerW(*lpszStr
) - 'a');
982 /* Read decimal number */
983 if (!isdigitW(*lpszStr
))
986 while (isdigitW(*lpszStr
))
989 iRet
+= (*lpszStr
- '0');
992 *lpiRet
= bNegative
? -iRet
: iRet
;
996 /*************************************************************************
997 * StrDupA [SHLWAPI.@]
999 * Duplicate a string.
1002 * lpszStr [I] String to duplicate.
1005 * Success: A pointer to a new string containing the contents of lpszStr
1006 * Failure: NULL, if memory cannot be allocated
1009 * The string memory is allocated with LocalAlloc(), and so should be released
1010 * by calling LocalFree().
1012 LPSTR WINAPI
StrDupA(LPCSTR lpszStr
)
1017 TRACE("(%s)\n",debugstr_a(lpszStr
));
1019 iLen
= lpszStr
? strlen(lpszStr
) + 1 : 1;
1020 lpszRet
= LocalAlloc(LMEM_FIXED
, iLen
);
1025 memcpy(lpszRet
, lpszStr
, iLen
);
1032 /*************************************************************************
1033 * StrDupW [SHLWAPI.@]
1037 LPWSTR WINAPI
StrDupW(LPCWSTR lpszStr
)
1042 TRACE("(%s)\n",debugstr_w(lpszStr
));
1044 iLen
= (lpszStr
? strlenW(lpszStr
) + 1 : 1) * sizeof(WCHAR
);
1045 lpszRet
= LocalAlloc(LMEM_FIXED
, iLen
);
1050 memcpy(lpszRet
, lpszStr
, iLen
);
1057 /*************************************************************************
1058 * SHLWAPI_StrSpnHelperA
1060 * Internal implementation of StrSpnA/StrCSpnA/StrCSpnIA
1062 static int SHLWAPI_StrSpnHelperA(LPCSTR lpszStr
, LPCSTR lpszMatch
,
1063 LPSTR (WINAPI
*pStrChrFn
)(LPCSTR
,WORD
),
1066 LPCSTR lpszRead
= lpszStr
;
1067 if (lpszStr
&& *lpszStr
&& lpszMatch
)
1071 LPCSTR lpszTest
= pStrChrFn(lpszMatch
, *lpszRead
);
1073 if (!bInvert
&& !lpszTest
)
1075 if (bInvert
&& lpszTest
)
1077 lpszRead
= CharNextA(lpszRead
);
1080 return lpszRead
- lpszStr
;
1083 /*************************************************************************
1084 * StrSpnA [SHLWAPI.@]
1086 * Find the length of the start of a string that contains only certain
1090 * lpszStr [I] String to search
1091 * lpszMatch [I] Characters that can be in the substring
1094 * The length of the part of lpszStr containing only chars from lpszMatch,
1095 * or 0 if any parameter is invalid.
1097 int WINAPI
StrSpnA(LPCSTR lpszStr
, LPCSTR lpszMatch
)
1099 TRACE("(%s,%s)\n",debugstr_a(lpszStr
), debugstr_a(lpszMatch
));
1101 return SHLWAPI_StrSpnHelperA(lpszStr
, lpszMatch
, StrChrA
, FALSE
);
1104 /*************************************************************************
1105 * StrSpnW [SHLWAPI.@]
1109 int WINAPI
StrSpnW(LPCWSTR lpszStr
, LPCWSTR lpszMatch
)
1111 if (!lpszStr
|| !lpszMatch
) return 0;
1112 return strspnW( lpszStr
, lpszMatch
);
1115 /*************************************************************************
1116 * StrCSpnA [SHLWAPI.@]
1118 * Find the length of the start of a string that does not contain certain
1122 * lpszStr [I] String to search
1123 * lpszMatch [I] Characters that cannot be in the substring
1126 * The length of the part of lpszStr containing only chars not in lpszMatch,
1127 * or 0 if any parameter is invalid.
1129 int WINAPI
StrCSpnA(LPCSTR lpszStr
, LPCSTR lpszMatch
)
1131 TRACE("(%s,%s)\n",debugstr_a(lpszStr
), debugstr_a(lpszMatch
));
1133 return SHLWAPI_StrSpnHelperA(lpszStr
, lpszMatch
, StrChrA
, TRUE
);
1136 /*************************************************************************
1137 * StrCSpnW [SHLWAPI.@]
1141 int WINAPI
StrCSpnW(LPCWSTR lpszStr
, LPCWSTR lpszMatch
)
1143 if (!lpszStr
|| !lpszMatch
) return 0;
1144 return strcspnW( lpszStr
, lpszMatch
);
1147 /*************************************************************************
1148 * StrCSpnIA [SHLWAPI.@]
1150 * Find the length of the start of a string that does not contain certain
1151 * characters, ignoring case.
1154 * lpszStr [I] String to search
1155 * lpszMatch [I] Characters that cannot be in the substring
1158 * The length of the part of lpszStr containing only chars not in lpszMatch,
1159 * or 0 if any parameter is invalid.
1161 int WINAPI
StrCSpnIA(LPCSTR lpszStr
, LPCSTR lpszMatch
)
1163 TRACE("(%s,%s)\n",debugstr_a(lpszStr
), debugstr_a(lpszMatch
));
1165 return SHLWAPI_StrSpnHelperA(lpszStr
, lpszMatch
, StrChrIA
, TRUE
);
1168 /*************************************************************************
1169 * StrCSpnIW [SHLWAPI.@]
1173 int WINAPI
StrCSpnIW(LPCWSTR lpszStr
, LPCWSTR lpszMatch
)
1175 LPCWSTR lpszRead
= lpszStr
;
1177 TRACE("(%s,%s)\n",debugstr_w(lpszStr
), debugstr_w(lpszMatch
));
1179 if (lpszStr
&& *lpszStr
&& lpszMatch
)
1183 if (StrChrIW(lpszMatch
, *lpszRead
)) break;
1187 return lpszRead
- lpszStr
;
1190 /*************************************************************************
1191 * StrPBrkA [SHLWAPI.@]
1193 * Search a string for any of a group of characters.
1196 * lpszStr [I] String to search
1197 * lpszMatch [I] Characters to match
1200 * A pointer to the first matching character in lpszStr, or NULL if no
1203 LPSTR WINAPI
StrPBrkA(LPCSTR lpszStr
, LPCSTR lpszMatch
)
1205 TRACE("(%s,%s)\n",debugstr_a(lpszStr
), debugstr_a(lpszMatch
));
1207 if (lpszStr
&& lpszMatch
&& *lpszMatch
)
1211 if (StrChrA(lpszMatch
, *lpszStr
))
1212 return (LPSTR
)lpszStr
;
1213 lpszStr
= CharNextA(lpszStr
);
1219 /*************************************************************************
1220 * StrPBrkW [SHLWAPI.@]
1224 LPWSTR WINAPI
StrPBrkW(LPCWSTR lpszStr
, LPCWSTR lpszMatch
)
1226 if (!lpszStr
|| !lpszMatch
) return NULL
;
1227 return strpbrkW( lpszStr
, lpszMatch
);
1230 /*************************************************************************
1231 * SHLWAPI_StrRChrHelperA
1233 * Internal implementation of StrRChrA/StrRChrIA.
1235 static LPSTR
SHLWAPI_StrRChrHelperA(LPCSTR lpszStr
,
1236 LPCSTR lpszEnd
, WORD ch
,
1237 BOOL (WINAPI
*pChrCmpFn
)(WORD
,WORD
))
1239 LPCSTR lpszRet
= NULL
;
1246 lpszEnd
= lpszStr
+ lstrlenA(lpszStr
);
1248 while (*lpszStr
&& lpszStr
<= lpszEnd
)
1250 ch2
= IsDBCSLeadByte(*lpszStr
)? *lpszStr
<< 8 | lpszStr
[1] : *lpszStr
;
1252 if (!pChrCmpFn(ch
, ch2
))
1254 lpszStr
= CharNextA(lpszStr
);
1257 return (LPSTR
)lpszRet
;
1260 /**************************************************************************
1261 * StrRChrA [SHLWAPI.@]
1263 * Find the last occurrence of a character in string.
1266 * lpszStr [I] String to search in
1267 * lpszEnd [I] Place to end search, or NULL to search until the end of lpszStr
1268 * ch [I] Character to search for.
1271 * Success: A pointer to the last occurrence of ch in lpszStr before lpszEnd,
1272 * or NULL if not found.
1273 * Failure: NULL, if any arguments are invalid.
1275 LPSTR WINAPI
StrRChrA(LPCSTR lpszStr
, LPCSTR lpszEnd
, WORD ch
)
1277 TRACE("(%s,%s,%x)\n", debugstr_a(lpszStr
), debugstr_a(lpszEnd
), ch
);
1279 return SHLWAPI_StrRChrHelperA(lpszStr
, lpszEnd
, ch
, SHLWAPI_ChrCmpA
);
1282 /**************************************************************************
1283 * StrRChrW [SHLWAPI.@]
1287 LPWSTR WINAPI
StrRChrW(LPCWSTR str
, LPCWSTR end
, WORD ch
)
1291 if (!str
) return NULL
;
1292 if (!end
) end
= str
+ strlenW(str
);
1295 if (*str
== ch
) ret
= (WCHAR
*)str
;
1301 /**************************************************************************
1302 * StrRChrIA [SHLWAPI.@]
1304 * Find the last occurrence of a character in string, ignoring case.
1307 * lpszStr [I] String to search in
1308 * lpszEnd [I] Place to end search, or NULL to search until the end of lpszStr
1309 * ch [I] Character to search for.
1312 * Success: A pointer to the last occurrence of ch in lpszStr before lpszEnd,
1313 * or NULL if not found.
1314 * Failure: NULL, if any arguments are invalid.
1316 LPSTR WINAPI
StrRChrIA(LPCSTR lpszStr
, LPCSTR lpszEnd
, WORD ch
)
1318 TRACE("(%s,%s,%x)\n", debugstr_a(lpszStr
), debugstr_a(lpszEnd
), ch
);
1320 return SHLWAPI_StrRChrHelperA(lpszStr
, lpszEnd
, ch
, ChrCmpIA
);
1323 /**************************************************************************
1324 * StrRChrIW [SHLWAPI.@]
1328 LPWSTR WINAPI
StrRChrIW(LPCWSTR str
, LPCWSTR end
, WORD ch
)
1332 if (!str
) return NULL
;
1333 if (!end
) end
= str
+ strlenW(str
);
1336 if (!ChrCmpIW(*str
, ch
)) ret
= (WCHAR
*)str
;
1342 /*************************************************************************
1343 * StrCatBuffA [SHLWAPI.@]
1345 * Concatenate two strings together.
1348 * lpszStr [O] String to concatenate to
1349 * lpszCat [I] String to add to lpszCat
1350 * cchMax [I] Maximum number of characters for the whole string
1356 * cchMax determines the number of characters in the final length of the
1357 * string, not the number appended to lpszStr from lpszCat.
1359 LPSTR WINAPI
StrCatBuffA(LPSTR lpszStr
, LPCSTR lpszCat
, INT cchMax
)
1363 TRACE("(%p,%s,%d)\n", lpszStr
, debugstr_a(lpszCat
), cchMax
);
1367 WARN("Invalid lpszStr would crash under Win32!\n");
1371 iLen
= strlen(lpszStr
);
1375 StrCpyNA(lpszStr
+ iLen
, lpszCat
, cchMax
);
1379 /*************************************************************************
1380 * StrCatBuffW [SHLWAPI.@]
1384 LPWSTR WINAPI
StrCatBuffW(LPWSTR lpszStr
, LPCWSTR lpszCat
, INT cchMax
)
1388 TRACE("(%p,%s,%d)\n", lpszStr
, debugstr_w(lpszCat
), cchMax
);
1392 WARN("Invalid lpszStr would crash under Win32!\n");
1396 iLen
= strlenW(lpszStr
);
1400 StrCpyNW(lpszStr
+ iLen
, lpszCat
, cchMax
);
1404 /*************************************************************************
1405 * StrRetToBufA [SHLWAPI.@]
1407 * Convert a STRRET to a normal string.
1410 * lpStrRet [O] STRRET to convert
1411 * pIdl [I] ITEMIDLIST for lpStrRet->uType == STRRET_OFFSET
1412 * lpszDest [O] Destination for normal string
1413 * dwLen [I] Length of lpszDest
1416 * Success: S_OK. lpszDest contains up to dwLen characters of the string.
1417 * If lpStrRet is of type STRRET_WSTR, its memory is freed with
1418 * CoTaskMemFree() and its type set to STRRET_CSTRA.
1419 * Failure: E_FAIL, if any parameters are invalid.
1421 HRESULT WINAPI
StrRetToBufA (LPSTRRET src
, const ITEMIDLIST
*pidl
, LPSTR dest
, UINT len
)
1424 * This routine is identical to that in dlls/shell32/shellstring.c.
1425 * It was duplicated because not every version of Shlwapi.dll exports
1426 * StrRetToBufA. If you change one routine, change them both.
1428 TRACE("dest=%p len=0x%x strret=%p pidl=%p\n", dest
, len
, src
, pidl
);
1432 WARN("Invalid lpStrRet would crash under Win32!\n");
1446 WideCharToMultiByte(CP_ACP
, 0, src
->u
.pOleStr
, -1, dest
, len
, NULL
, NULL
);
1447 CoTaskMemFree(src
->u
.pOleStr
);
1451 lstrcpynA(dest
, src
->u
.cStr
, len
);
1455 lstrcpynA((LPSTR
)dest
, ((LPCSTR
)&pidl
->mkid
)+src
->u
.uOffset
, len
);
1459 FIXME("unknown type!\n");
1465 /*************************************************************************
1466 * StrRetToBufW [SHLWAPI.@]
1470 HRESULT WINAPI
StrRetToBufW (LPSTRRET src
, const ITEMIDLIST
*pidl
, LPWSTR dest
, UINT len
)
1472 TRACE("dest=%p len=0x%x strret=%p pidl=%p\n", dest
, len
, src
, pidl
);
1476 WARN("Invalid lpStrRet would crash under Win32!\n");
1490 lstrcpynW(dest
, src
->u
.pOleStr
, len
);
1491 CoTaskMemFree(src
->u
.pOleStr
);
1495 if (!MultiByteToWideChar( CP_ACP
, 0, src
->u
.cStr
, -1, dest
, len
))
1502 if (!MultiByteToWideChar( CP_ACP
, 0, ((LPCSTR
)&pidl
->mkid
)+src
->u
.uOffset
, -1,
1509 FIXME("unknown type!\n");
1515 /*************************************************************************
1516 * StrRetToStrA [SHLWAPI.@]
1518 * Converts a STRRET to a normal string.
1521 * lpStrRet [O] STRRET to convert
1522 * pidl [I] ITEMIDLIST for lpStrRet->uType == STRRET_OFFSET
1523 * ppszName [O] Destination for converted string
1526 * Success: S_OK. ppszName contains the new string, allocated with CoTaskMemAlloc().
1527 * Failure: E_FAIL, if any parameters are invalid.
1529 HRESULT WINAPI
StrRetToStrA(LPSTRRET lpStrRet
, const ITEMIDLIST
*pidl
, LPSTR
*ppszName
)
1531 HRESULT hRet
= E_FAIL
;
1533 switch (lpStrRet
->uType
)
1536 hRet
= _SHStrDupAW(lpStrRet
->u
.pOleStr
, ppszName
);
1537 CoTaskMemFree(lpStrRet
->u
.pOleStr
);
1541 hRet
= _SHStrDupAA(lpStrRet
->u
.cStr
, ppszName
);
1545 hRet
= _SHStrDupAA(((LPCSTR
)&pidl
->mkid
) + lpStrRet
->u
.uOffset
, ppszName
);
1555 /*************************************************************************
1556 * StrRetToStrW [SHLWAPI.@]
1560 HRESULT WINAPI
StrRetToStrW(LPSTRRET lpStrRet
, const ITEMIDLIST
*pidl
, LPWSTR
*ppszName
)
1562 HRESULT hRet
= E_FAIL
;
1564 switch (lpStrRet
->uType
)
1567 hRet
= SHStrDupW(lpStrRet
->u
.pOleStr
, ppszName
);
1568 CoTaskMemFree(lpStrRet
->u
.pOleStr
);
1572 hRet
= SHStrDupA(lpStrRet
->u
.cStr
, ppszName
);
1576 hRet
= SHStrDupA(((LPCSTR
)&pidl
->mkid
) + lpStrRet
->u
.uOffset
, ppszName
);
1586 /* Create an ASCII string copy using SysAllocString() */
1587 static HRESULT
_SHStrDupAToBSTR(LPCSTR src
, BSTR
*pBstrOut
)
1593 INT len
= MultiByteToWideChar(CP_ACP
, 0, src
, -1, NULL
, 0);
1594 WCHAR
* szTemp
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1598 MultiByteToWideChar(CP_ACP
, 0, src
, -1, szTemp
, len
);
1599 *pBstrOut
= SysAllocString(szTemp
);
1600 HeapFree(GetProcessHeap(), 0, szTemp
);
1606 return E_OUTOFMEMORY
;
1609 /*************************************************************************
1610 * StrRetToBSTR [SHLWAPI.@]
1612 * Converts a STRRET to a BSTR.
1615 * lpStrRet [O] STRRET to convert
1616 * pidl [I] ITEMIDLIST for lpStrRet->uType = STRRET_OFFSET
1617 * pBstrOut [O] Destination for converted BSTR
1620 * Success: S_OK. pBstrOut contains the new string.
1621 * Failure: E_FAIL, if any parameters are invalid.
1623 HRESULT WINAPI
StrRetToBSTR(STRRET
*lpStrRet
, LPCITEMIDLIST pidl
, BSTR
* pBstrOut
)
1625 HRESULT hRet
= E_FAIL
;
1627 switch (lpStrRet
->uType
)
1630 *pBstrOut
= SysAllocString(lpStrRet
->u
.pOleStr
);
1633 CoTaskMemFree(lpStrRet
->u
.pOleStr
);
1637 hRet
= _SHStrDupAToBSTR(lpStrRet
->u
.cStr
, pBstrOut
);
1641 hRet
= _SHStrDupAToBSTR(((LPCSTR
)&pidl
->mkid
) + lpStrRet
->u
.uOffset
, pBstrOut
);
1651 /*************************************************************************
1652 * StrFormatKBSizeA [SHLWAPI.@]
1654 * Create a formatted string containing a byte count in Kilobytes.
1657 * llBytes [I] Byte size to format
1658 * lpszDest [I] Destination for formatted string
1659 * cchMax [I] Size of lpszDest
1664 LPSTR WINAPI
StrFormatKBSizeA(LONGLONG llBytes
, LPSTR lpszDest
, UINT cchMax
)
1668 if (!StrFormatKBSizeW(llBytes
, wszBuf
, 256))
1670 if (!WideCharToMultiByte(CP_ACP
, 0, wszBuf
, -1, lpszDest
, cchMax
, NULL
, NULL
))
1675 /*************************************************************************
1676 * StrFormatKBSizeW [SHLWAPI.@]
1678 * See StrFormatKBSizeA.
1680 LPWSTR WINAPI
StrFormatKBSizeW(LONGLONG llBytes
, LPWSTR lpszDest
, UINT cchMax
)
1682 static const WCHAR kb
[] = {' ','K','B',0};
1683 LONGLONG llKB
= (llBytes
+ 1023) >> 10;
1686 TRACE("(0x%s,%p,%d)\n", wine_dbgstr_longlong(llBytes
), lpszDest
, cchMax
);
1688 if (!FormatInt(llKB
, lpszDest
, cchMax
))
1691 len
= lstrlenW(lpszDest
);
1692 if (cchMax
- len
< 4)
1694 lstrcatW(lpszDest
, kb
);
1698 /*************************************************************************
1699 * StrNCatA [SHLWAPI.@]
1701 * Concatenate two strings together.
1704 * lpszStr [O] String to concatenate to
1705 * lpszCat [I] String to add to lpszCat
1706 * cchMax [I] Maximum number of characters to concatenate
1712 * cchMax determines the number of characters that are appended to lpszStr,
1713 * not the total length of the string.
1715 LPSTR WINAPI
StrNCatA(LPSTR lpszStr
, LPCSTR lpszCat
, INT cchMax
)
1717 LPSTR lpszRet
= lpszStr
;
1719 TRACE("(%s,%s,%i)\n", debugstr_a(lpszStr
), debugstr_a(lpszCat
), cchMax
);
1723 WARN("Invalid lpszStr would crash under Win32!\n");
1727 StrCpyNA(lpszStr
+ strlen(lpszStr
), lpszCat
, cchMax
);
1731 /*************************************************************************
1732 * StrNCatW [SHLWAPI.@]
1736 LPWSTR WINAPI
StrNCatW(LPWSTR lpszStr
, LPCWSTR lpszCat
, INT cchMax
)
1738 LPWSTR lpszRet
= lpszStr
;
1740 TRACE("(%s,%s,%i)\n", debugstr_w(lpszStr
), debugstr_w(lpszCat
), cchMax
);
1744 WARN("Invalid lpszStr would crash under Win32\n");
1748 StrCpyNW(lpszStr
+ strlenW(lpszStr
), lpszCat
, cchMax
);
1752 /*************************************************************************
1753 * StrTrimA [SHLWAPI.@]
1755 * Remove characters from the start and end of a string.
1758 * lpszStr [O] String to remove characters from
1759 * lpszTrim [I] Characters to remove from lpszStr
1762 * TRUE If lpszStr was valid and modified
1765 BOOL WINAPI
StrTrimA(LPSTR lpszStr
, LPCSTR lpszTrim
)
1768 LPSTR lpszRead
= lpszStr
;
1771 TRACE("(%s,%s)\n", debugstr_a(lpszStr
), debugstr_a(lpszTrim
));
1773 if (lpszRead
&& *lpszRead
)
1775 while (*lpszRead
&& StrChrA(lpszTrim
, *lpszRead
))
1776 lpszRead
= CharNextA(lpszRead
); /* Skip leading matches */
1778 dwLen
= strlen(lpszRead
);
1780 if (lpszRead
!= lpszStr
)
1782 memmove(lpszStr
, lpszRead
, dwLen
+ 1);
1787 lpszRead
= lpszStr
+ dwLen
;
1788 while (StrChrA(lpszTrim
, lpszRead
[-1]))
1789 lpszRead
= CharPrevA(lpszStr
, lpszRead
); /* Skip trailing matches */
1791 if (lpszRead
!= lpszStr
+ dwLen
)
1801 /*************************************************************************
1802 * StrTrimW [SHLWAPI.@]
1806 BOOL WINAPI
StrTrimW(LPWSTR lpszStr
, LPCWSTR lpszTrim
)
1809 LPWSTR lpszRead
= lpszStr
;
1812 TRACE("(%s,%s)\n", debugstr_w(lpszStr
), debugstr_w(lpszTrim
));
1814 if (lpszRead
&& *lpszRead
)
1816 while (*lpszRead
&& StrChrW(lpszTrim
, *lpszRead
)) lpszRead
++;
1818 dwLen
= strlenW(lpszRead
);
1820 if (lpszRead
!= lpszStr
)
1822 memmove(lpszStr
, lpszRead
, (dwLen
+ 1) * sizeof(WCHAR
));
1827 lpszRead
= lpszStr
+ dwLen
;
1828 while (StrChrW(lpszTrim
, lpszRead
[-1]))
1829 lpszRead
--; /* Skip trailing matches */
1831 if (lpszRead
!= lpszStr
+ dwLen
)
1841 /*************************************************************************
1842 * _SHStrDupAA [INTERNAL]
1844 * Duplicates a ASCII string to ASCII. The destination buffer is allocated.
1846 static HRESULT
_SHStrDupAA(LPCSTR src
, LPSTR
* dest
)
1852 len
= lstrlenA(src
) + 1;
1853 *dest
= CoTaskMemAlloc(len
);
1859 lstrcpynA(*dest
,src
, len
);
1865 TRACE("%s->(%p)\n", debugstr_a(src
), *dest
);
1869 /*************************************************************************
1870 * SHStrDupA [SHLWAPI.@]
1872 * Return a Unicode copy of a string, in memory allocated by CoTaskMemAlloc().
1875 * lpszStr [I] String to copy
1876 * lppszDest [O] Destination for the new string copy
1879 * Success: S_OK. lppszDest contains the new string in Unicode format.
1880 * Failure: E_OUTOFMEMORY, If any arguments are invalid or memory allocation
1883 HRESULT WINAPI
SHStrDupA(LPCSTR lpszStr
, LPWSTR
* lppszDest
)
1890 len
= MultiByteToWideChar(CP_ACP
, 0, lpszStr
, -1, NULL
, 0) * sizeof(WCHAR
);
1891 *lppszDest
= CoTaskMemAlloc(len
);
1898 MultiByteToWideChar(CP_ACP
, 0, lpszStr
, -1, *lppszDest
, len
/sizeof(WCHAR
));
1902 hRet
= E_OUTOFMEMORY
;
1904 TRACE("%s->(%p)\n", debugstr_a(lpszStr
), *lppszDest
);
1908 /*************************************************************************
1909 * _SHStrDupAW [INTERNAL]
1911 * Duplicates a UNICODE to a ASCII string. The destination buffer is allocated.
1913 static HRESULT
_SHStrDupAW(LPCWSTR src
, LPSTR
* dest
)
1919 len
= WideCharToMultiByte(CP_ACP
, 0, src
, -1, NULL
, 0, NULL
, NULL
);
1920 *dest
= CoTaskMemAlloc(len
);
1926 WideCharToMultiByte(CP_ACP
, 0, src
, -1, *dest
, len
, NULL
, NULL
);
1932 TRACE("%s->(%p)\n", debugstr_w(src
), *dest
);
1936 /*************************************************************************
1937 * SHStrDupW [SHLWAPI.@]
1941 HRESULT WINAPI
SHStrDupW(LPCWSTR src
, LPWSTR
* dest
)
1947 len
= (lstrlenW(src
) + 1) * sizeof(WCHAR
);
1948 *dest
= CoTaskMemAlloc(len
);
1954 memcpy(*dest
, src
, len
);
1960 TRACE("%s->(%p)\n", debugstr_w(src
), *dest
);
1964 /*************************************************************************
1965 * SHLWAPI_WriteReverseNum
1967 * Internal helper for SHLWAPI_WriteTimeClass.
1969 static inline LPWSTR
SHLWAPI_WriteReverseNum(LPWSTR lpszOut
, DWORD dwNum
)
1973 /* Write a decimal number to a string, backwards */
1976 DWORD dwNextDigit
= dwNum
% 10;
1977 *lpszOut
-- = '0' + dwNextDigit
;
1978 dwNum
= (dwNum
- dwNextDigit
) / 10;
1979 } while (dwNum
> 0);
1984 /*************************************************************************
1985 * SHLWAPI_FormatSignificant
1987 * Internal helper for SHLWAPI_WriteTimeClass.
1989 static inline int SHLWAPI_FormatSignificant(LPWSTR lpszNum
, int dwDigits
)
1991 /* Zero non significant digits, return remaining significant digits */
1995 if (--dwDigits
== 0)
2005 /*************************************************************************
2006 * SHLWAPI_WriteTimeClass
2008 * Internal helper for StrFromTimeIntervalW.
2010 static int SHLWAPI_WriteTimeClass(LPWSTR lpszOut
, DWORD dwValue
,
2011 UINT uClassStringId
, int iDigits
)
2013 WCHAR szBuff
[64], *szOut
= szBuff
+ 32;
2015 szOut
= SHLWAPI_WriteReverseNum(szOut
, dwValue
);
2016 iDigits
= SHLWAPI_FormatSignificant(szOut
+ 1, iDigits
);
2018 LoadStringW(shlwapi_hInstance
, uClassStringId
, szBuff
+ 32, 32);
2019 strcatW(lpszOut
, szOut
);
2023 /*************************************************************************
2024 * StrFromTimeIntervalA [SHLWAPI.@]
2026 * Format a millisecond time interval into a string
2029 * lpszStr [O] Output buffer for formatted time interval
2030 * cchMax [I] Size of lpszStr
2031 * dwMS [I] Number of milliseconds
2032 * iDigits [I] Number of digits to print
2035 * The length of the formatted string, or 0 if any parameter is invalid.
2038 * This implementation mimics the Win32 behaviour of always writing a leading
2039 * space before the time interval begins.
2041 * iDigits is used to provide approximate times if accuracy is not important.
2042 * This number of digits will be written of the first non-zero time class
2043 * (hours/minutes/seconds). If this does not complete the time classification,
2044 * the remaining digits are changed to zeros (i.e. The time is _not_ rounded).
2045 * If there are digits remaining following the writing of a time class, the
2046 * next time class will be written.
2048 * For example, given dwMS represents 138 hours,43 minutes and 15 seconds, the
2049 * following will result from the given values of iDigits:
2051 *| iDigits 1 2 3 4 5 ...
2052 *| lpszStr "100 hr" "130 hr" "138 hr" "138 hr 40 min" "138 hr 43 min" ...
2054 INT WINAPI
StrFromTimeIntervalA(LPSTR lpszStr
, UINT cchMax
, DWORD dwMS
,
2059 TRACE("(%p,%d,%d,%d)\n", lpszStr
, cchMax
, dwMS
, iDigits
);
2061 if (lpszStr
&& cchMax
)
2064 StrFromTimeIntervalW(szBuff
, sizeof(szBuff
)/sizeof(WCHAR
), dwMS
, iDigits
);
2065 WideCharToMultiByte(CP_ACP
,0,szBuff
,-1,lpszStr
,cchMax
,0,0);
2071 /*************************************************************************
2072 * StrFromTimeIntervalW [SHLWAPI.@]
2074 * See StrFromTimeIntervalA.
2076 INT WINAPI
StrFromTimeIntervalW(LPWSTR lpszStr
, UINT cchMax
, DWORD dwMS
,
2081 TRACE("(%p,%d,%d,%d)\n", lpszStr
, cchMax
, dwMS
, iDigits
);
2083 if (lpszStr
&& cchMax
)
2086 DWORD dwHours
, dwMinutes
;
2088 if (!iDigits
|| cchMax
== 1)
2094 /* Calculate the time classes */
2095 dwMS
= (dwMS
+ 500) / 1000;
2096 dwHours
= dwMS
/ 3600;
2097 dwMS
-= dwHours
* 3600;
2098 dwMinutes
= dwMS
/ 60;
2099 dwMS
-= dwMinutes
* 60;
2104 iDigits
= SHLWAPI_WriteTimeClass(szCopy
, dwHours
, IDS_TIME_INTERVAL_HOURS
, iDigits
);
2106 if (dwMinutes
&& iDigits
)
2107 iDigits
= SHLWAPI_WriteTimeClass(szCopy
, dwMinutes
, IDS_TIME_INTERVAL_MINUTES
, iDigits
);
2109 if (iDigits
) /* Always write seconds if we have significant digits */
2110 SHLWAPI_WriteTimeClass(szCopy
, dwMS
, IDS_TIME_INTERVAL_SECONDS
, iDigits
);
2112 lstrcpynW(lpszStr
, szCopy
, cchMax
);
2113 iRet
= strlenW(lpszStr
);
2118 /*************************************************************************
2119 * StrIsIntlEqualA [SHLWAPI.@]
2121 * Compare two strings.
2124 * bCase [I] Whether to compare case sensitively
2125 * lpszStr [I] First string to compare
2126 * lpszComp [I] Second string to compare
2127 * iLen [I] Length to compare
2130 * TRUE If the strings are equal.
2133 BOOL WINAPI
StrIsIntlEqualA(BOOL bCase
, LPCSTR lpszStr
, LPCSTR lpszComp
,
2138 TRACE("(%d,%s,%s,%d)\n", bCase
,
2139 debugstr_a(lpszStr
), debugstr_a(lpszComp
), iLen
);
2141 /* FIXME: This flag is undocumented and unknown by our CompareString.
2142 * We need a define for it.
2144 dwFlags
= 0x10000000;
2145 if (!bCase
) dwFlags
|= NORM_IGNORECASE
;
2147 return (CompareStringA(GetThreadLocale(), dwFlags
, lpszStr
, iLen
, lpszComp
, iLen
) == CSTR_EQUAL
);
2150 /*************************************************************************
2151 * StrIsIntlEqualW [SHLWAPI.@]
2153 * See StrIsIntlEqualA.
2155 BOOL WINAPI
StrIsIntlEqualW(BOOL bCase
, LPCWSTR lpszStr
, LPCWSTR lpszComp
,
2160 TRACE("(%d,%s,%s,%d)\n", bCase
,
2161 debugstr_w(lpszStr
),debugstr_w(lpszComp
), iLen
);
2163 /* FIXME: This flag is undocumented and unknown by our CompareString.
2164 * We need a define for it.
2166 dwFlags
= 0x10000000;
2167 if (!bCase
) dwFlags
|= NORM_IGNORECASE
;
2169 return (CompareStringW(GetThreadLocale(), dwFlags
, lpszStr
, iLen
, lpszComp
, iLen
) == CSTR_EQUAL
);
2172 /*************************************************************************
2175 * Copy a string to another string, up to a maximum number of characters.
2178 * lpszDest [O] Destination string
2179 * lpszSrc [I] Source string
2180 * iLen [I] Maximum number of chars to copy
2183 * Success: A pointer to the last character written to lpszDest.
2184 * Failure: lpszDest, if any arguments are invalid.
2186 LPSTR WINAPI
StrCpyNXA(LPSTR lpszDest
, LPCSTR lpszSrc
, int iLen
)
2188 TRACE("(%p,%s,%i)\n", lpszDest
, debugstr_a(lpszSrc
), iLen
);
2190 if (lpszDest
&& lpszSrc
&& iLen
> 0)
2192 while ((iLen
-- > 1) && *lpszSrc
)
2193 *lpszDest
++ = *lpszSrc
++;
2200 /*************************************************************************
2203 * Unicode version of StrCpyNXA.
2205 LPWSTR WINAPI
StrCpyNXW(LPWSTR lpszDest
, LPCWSTR lpszSrc
, int iLen
)
2207 TRACE("(%p,%s,%i)\n", lpszDest
, debugstr_w(lpszSrc
), iLen
);
2209 if (lpszDest
&& lpszSrc
&& iLen
> 0)
2211 while ((iLen
-- > 1) && *lpszSrc
)
2212 *lpszDest
++ = *lpszSrc
++;
2219 /*************************************************************************
2220 * StrCmpLogicalW [SHLWAPI.@]
2222 * Compare two strings, ignoring case and comparing digits as numbers.
2225 * lpszStr [I] First string to compare
2226 * lpszComp [I] Second string to compare
2227 * iLen [I] Length to compare
2230 * TRUE If the strings are equal.
2233 INT WINAPI
StrCmpLogicalW(LPCWSTR lpszStr
, LPCWSTR lpszComp
)
2237 TRACE("(%s,%s)\n", debugstr_w(lpszStr
), debugstr_w(lpszComp
));
2239 if (lpszStr
&& lpszComp
)
2245 else if (isdigitW(*lpszStr
))
2249 if (!isdigitW(*lpszComp
))
2252 /* Compare the numbers */
2253 StrToIntExW(lpszStr
, 0, &iStr
);
2254 StrToIntExW(lpszComp
, 0, &iComp
);
2258 else if (iStr
> iComp
)
2262 while (isdigitW(*lpszStr
))
2264 while (isdigitW(*lpszComp
))
2267 else if (isdigitW(*lpszComp
))
2271 iDiff
= ChrCmpIW(*lpszStr
,*lpszComp
);
2287 /* Structure for formatting byte strings */
2288 typedef struct tagSHLWAPI_BYTEFORMATS
2295 } SHLWAPI_BYTEFORMATS
;
2297 /*************************************************************************
2298 * StrFormatByteSizeW [SHLWAPI.@]
2300 * Create a string containing an abbreviated byte count of up to 2^63-1.
2303 * llBytes [I] Byte size to format
2304 * lpszDest [I] Destination for formatted string
2305 * cchMax [I] Size of lpszDest
2311 * There is no StrFormatByteSize64W function, it is called StrFormatByteSizeW().
2313 LPWSTR WINAPI
StrFormatByteSizeW(LONGLONG llBytes
, LPWSTR lpszDest
, UINT cchMax
)
2315 #define KB ((ULONGLONG)1024)
2317 #define GB (KB*KB*KB)
2318 #define TB (KB*KB*KB*KB)
2319 #define PB (KB*KB*KB*KB*KB)
2321 static const SHLWAPI_BYTEFORMATS bfFormats
[] =
2323 { 10*KB
, 10.24, 100.0, 2, 'K' }, /* 10 KB */
2324 { 100*KB
, 102.4, 10.0, 1, 'K' }, /* 100 KB */
2325 { 1000*KB
, 1024.0, 1.0, 0, 'K' }, /* 1000 KB */
2326 { 10*MB
, 10485.76, 100.0, 2, 'M' }, /* 10 MB */
2327 { 100*MB
, 104857.6, 10.0, 1, 'M' }, /* 100 MB */
2328 { 1000*MB
, 1048576.0, 1.0, 0, 'M' }, /* 1000 MB */
2329 { 10*GB
, 10737418.24, 100.0, 2, 'G' }, /* 10 GB */
2330 { 100*GB
, 107374182.4, 10.0, 1, 'G' }, /* 100 GB */
2331 { 1000*GB
, 1073741824.0, 1.0, 0, 'G' }, /* 1000 GB */
2332 { 10*TB
, 10485.76, 100.0, 2, 'T' }, /* 10 TB */
2333 { 100*TB
, 104857.6, 10.0, 1, 'T' }, /* 100 TB */
2334 { 1000*TB
, 1048576.0, 1.0, 0, 'T' }, /* 1000 TB */
2335 { 10*PB
, 10737418.24, 100.00, 2, 'P' }, /* 10 PB */
2336 { 100*PB
, 107374182.4, 10.00, 1, 'P' }, /* 100 PB */
2337 { 1000*PB
, 1073741824.0, 1.00, 0, 'P' }, /* 1000 PB */
2338 { 0, 10995116277.76, 100.00, 2, 'E' } /* EB's, catch all */
2340 WCHAR wszAdd
[] = {' ','?','B',0};
2344 TRACE("(0x%s,%p,%d)\n", wine_dbgstr_longlong(llBytes
), lpszDest
, cchMax
);
2346 if (!lpszDest
|| !cchMax
)
2349 if (llBytes
< 1024) /* 1K */
2351 WCHAR wszBytesFormat
[64];
2352 LoadStringW(shlwapi_hInstance
, IDS_BYTES_FORMAT
, wszBytesFormat
, 64);
2353 snprintfW(lpszDest
, cchMax
, wszBytesFormat
, (int)llBytes
);
2357 /* Note that if this loop completes without finding a match, i will be
2358 * pointing at the last entry, which is a catch all for > 1000 PB
2360 while (i
< sizeof(bfFormats
) / sizeof(SHLWAPI_BYTEFORMATS
) - 1)
2362 if (llBytes
< bfFormats
[i
].dLimit
)
2366 /* Above 1 TB we encounter problems with FP accuracy. So for amounts above
2367 * this number we integer shift down by 1 MB first. The table above has
2368 * the divisors scaled down from the '< 10 TB' entry onwards, to account
2369 * for this. We also add a small fudge factor to get the correct result for
2370 * counts that lie exactly on a 1024 byte boundary.
2373 dBytes
= (double)(llBytes
>> 20) + 0.001; /* Scale down by 1 MB */
2375 dBytes
= (double)llBytes
+ 0.00001;
2377 dBytes
= floor(dBytes
/ bfFormats
[i
].dDivisor
) / bfFormats
[i
].dNormaliser
;
2379 if (!FormatDouble(dBytes
, bfFormats
[i
].nDecimals
, lpszDest
, cchMax
))
2381 wszAdd
[1] = bfFormats
[i
].wPrefix
;
2382 StrCatBuffW(lpszDest
, wszAdd
, cchMax
);
2386 /*************************************************************************
2387 * StrFormatByteSize64A [SHLWAPI.@]
2389 * See StrFormatByteSizeW.
2391 LPSTR WINAPI
StrFormatByteSize64A(LONGLONG llBytes
, LPSTR lpszDest
, UINT cchMax
)
2395 StrFormatByteSizeW(llBytes
, wszBuff
, sizeof(wszBuff
)/sizeof(WCHAR
));
2398 WideCharToMultiByte(CP_ACP
, 0, wszBuff
, -1, lpszDest
, cchMax
, 0, 0);
2402 /*************************************************************************
2403 * StrFormatByteSizeA [SHLWAPI.@]
2405 * Create a string containing an abbreviated byte count of up to 2^31-1.
2408 * dwBytes [I] Byte size to format
2409 * lpszDest [I] Destination for formatted string
2410 * cchMax [I] Size of lpszDest
2416 * The Ascii and Unicode versions of this function accept a different
2417 * integer type for dwBytes. See StrFormatByteSize64A().
2419 LPSTR WINAPI
StrFormatByteSizeA(DWORD dwBytes
, LPSTR lpszDest
, UINT cchMax
)
2421 TRACE("(%d,%p,%d)\n", dwBytes
, lpszDest
, cchMax
);
2423 return StrFormatByteSize64A(dwBytes
, lpszDest
, cchMax
);
2426 /*************************************************************************
2429 * Remove a hanging lead byte from the end of a string, if present.
2432 * lpStr [I] String to check for a hanging lead byte
2433 * size [I] Length of lpStr
2436 * Success: The new length of the string. Any hanging lead bytes are removed.
2437 * Failure: 0, if any parameters are invalid.
2439 DWORD WINAPI
SHTruncateString(LPSTR lpStr
, DWORD size
)
2443 LPSTR lastByte
= lpStr
+ size
- 1;
2445 while(lpStr
< lastByte
)
2446 lpStr
+= IsDBCSLeadByte(*lpStr
) ? 2 : 1;
2448 if(lpStr
== lastByte
&& IsDBCSLeadByte(*lpStr
))
2458 /*************************************************************************
2461 * Remove a single non-trailing ampersand ('&') from a string.
2464 * lpszStr [I/O] String to remove ampersand from.
2467 * The character after the first ampersand in lpszStr, or the first character
2468 * in lpszStr if there is no ampersand in the string.
2470 char WINAPI
SHStripMneumonicA(LPCSTR lpszStr
)
2472 LPSTR lpszIter
, lpszTmp
;
2475 TRACE("(%s)\n", debugstr_a(lpszStr
));
2479 if ((lpszIter
= StrChrA(lpszStr
, '&')))
2481 lpszTmp
= CharNextA(lpszIter
);
2484 if (*lpszTmp
!= '&')
2487 memmove( lpszIter
, lpszTmp
, strlen(lpszTmp
) + 1 );
2494 /*************************************************************************
2497 * Unicode version of SHStripMneumonicA.
2499 WCHAR WINAPI
SHStripMneumonicW(LPCWSTR lpszStr
)
2501 LPWSTR lpszIter
, lpszTmp
;
2504 TRACE("(%s)\n", debugstr_w(lpszStr
));
2508 if ((lpszIter
= StrChrW(lpszStr
, '&')))
2510 lpszTmp
= lpszIter
+ 1;
2513 if (*lpszTmp
!= '&')
2516 memmove( lpszIter
, lpszTmp
, (strlenW(lpszTmp
) + 1) * sizeof(WCHAR
) );
2523 /*************************************************************************
2526 * Convert an Ascii string to Unicode.
2529 * dwCp [I] Code page for the conversion
2530 * lpSrcStr [I] Source Ascii string to convert
2531 * lpDstStr [O] Destination for converted Unicode string
2532 * iLen [I] Length of lpDstStr
2535 * The return value of the MultiByteToWideChar() function called on lpSrcStr.
2537 DWORD WINAPI
SHAnsiToUnicodeCP(DWORD dwCp
, LPCSTR lpSrcStr
, LPWSTR lpDstStr
, int iLen
)
2541 dwRet
= MultiByteToWideChar(dwCp
, 0, lpSrcStr
, -1, lpDstStr
, iLen
);
2542 TRACE("%s->%s,ret=%d\n", debugstr_a(lpSrcStr
), debugstr_w(lpDstStr
), dwRet
);
2546 /*************************************************************************
2549 * Convert an Ascii string to Unicode.
2552 * lpSrcStr [I] Source Ascii string to convert
2553 * lpDstStr [O] Destination for converted Unicode string
2554 * iLen [I] Length of lpDstStr
2557 * The return value of the MultiByteToWideChar() function called on lpSrcStr.
2560 * This function simply calls SHAnsiToUnicodeCP with code page CP_ACP.
2562 DWORD WINAPI
SHAnsiToUnicode(LPCSTR lpSrcStr
, LPWSTR lpDstStr
, int iLen
)
2564 return SHAnsiToUnicodeCP(CP_ACP
, lpSrcStr
, lpDstStr
, iLen
);
2567 /*************************************************************************
2570 * Convert a Unicode string to Ascii.
2573 * CodePage [I] Code page to use for the conversion
2574 * lpSrcStr [I] Source Unicode string to convert
2575 * lpDstStr [O] Destination for converted Ascii string
2576 * dstlen [I] Length of buffer at lpDstStr
2579 * Success: The length in bytes of the result at lpDstStr (including the terminator)
2580 * Failure: When using CP_UTF8, CP_UTF7 or 0xc350 as codePage, 0 is returned and
2581 * the result is not nul-terminated.
2582 * When using a different codepage, the length in bytes of the truncated
2583 * result at lpDstStr (including the terminator) is returned and
2584 * lpDstStr is always nul-terminated.
2587 DWORD WINAPI
SHUnicodeToAnsiCP(UINT CodePage
, LPCWSTR lpSrcStr
, LPSTR lpDstStr
, int dstlen
)
2589 static const WCHAR emptyW
[] = { '\0' };
2593 if (!lpDstStr
|| !dstlen
)
2601 len
= strlenW(lpSrcStr
) + 1;
2606 CodePage
= CP_UTF8
; /* Fall through... */
2607 case 0x0000C350: /* FIXME: CP_ #define */
2613 INT needed
= dstlen
- 1;
2616 /* try the user supplied buffer first */
2617 hr
= ConvertINetUnicodeToMultiByte(&dwMode
, CodePage
, lpSrcStr
, &lenW
, lpDstStr
, &needed
);
2620 lpDstStr
[needed
] = '\0';
2624 /* user buffer too small. exclude termination and copy as much as possible */
2626 hr
= ConvertINetUnicodeToMultiByte(&dwMode
, CodePage
, lpSrcStr
, &lenW
, NULL
, &needed
);
2628 mem
= HeapAlloc(GetProcessHeap(), 0, needed
);
2632 hr
= ConvertINetUnicodeToMultiByte(&dwMode
, CodePage
, lpSrcStr
, &len
, mem
, &needed
);
2635 reqLen
= SHTruncateString(mem
, dstlen
);
2636 if (reqLen
> 0) memcpy(lpDstStr
, mem
, reqLen
-1);
2638 HeapFree(GetProcessHeap(), 0, mem
);
2645 /* try the user supplied buffer first */
2646 reqLen
= WideCharToMultiByte(CodePage
, 0, lpSrcStr
, len
, lpDstStr
, dstlen
, NULL
, NULL
);
2648 if (!reqLen
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
2650 reqLen
= WideCharToMultiByte(CodePage
, 0, lpSrcStr
, len
, NULL
, 0, NULL
, NULL
);
2653 mem
= HeapAlloc(GetProcessHeap(), 0, reqLen
);
2656 reqLen
= WideCharToMultiByte(CodePage
, 0, lpSrcStr
, len
, mem
,
2657 reqLen
, NULL
, NULL
);
2659 reqLen
= SHTruncateString(mem
, dstlen
-1);
2662 lstrcpynA(lpDstStr
, mem
, reqLen
);
2663 HeapFree(GetProcessHeap(), 0, mem
);
2664 lpDstStr
[reqLen
-1] = '\0';
2671 /*************************************************************************
2674 * Convert a Unicode string to Ascii.
2677 * lpSrcStr [I] Source Unicode string to convert
2678 * lpDstStr [O] Destination for converted Ascii string
2679 * iLen [O] Length of lpDstStr in characters
2682 * See SHUnicodeToAnsiCP
2685 * This function simply calls SHUnicodeToAnsiCP() with CodePage = CP_ACP.
2687 INT WINAPI
SHUnicodeToAnsi(LPCWSTR lpSrcStr
, LPSTR lpDstStr
, INT iLen
)
2689 return SHUnicodeToAnsiCP(CP_ACP
, lpSrcStr
, lpDstStr
, iLen
);
2692 /*************************************************************************
2695 * Copy one string to another.
2698 * lpszSrc [I] Source string to copy
2699 * lpszDst [O] Destination for copy
2700 * iLen [I] Length of lpszDst in characters
2703 * The length of the copied string, including the terminating NUL. lpszDst
2704 * contains iLen characters of lpszSrc.
2706 DWORD WINAPI
SHAnsiToAnsi(LPCSTR lpszSrc
, LPSTR lpszDst
, int iLen
)
2710 TRACE("(%s,%p,0x%08x)\n", debugstr_a(lpszSrc
), lpszDst
, iLen
);
2712 lpszRet
= StrCpyNXA(lpszDst
, lpszSrc
, iLen
);
2713 return lpszRet
- lpszDst
+ 1;
2716 /*************************************************************************
2719 * Unicode version of SSHAnsiToAnsi.
2721 DWORD WINAPI
SHUnicodeToUnicode(LPCWSTR lpszSrc
, LPWSTR lpszDst
, int iLen
)
2725 TRACE("(%s,%p,0x%08x)\n", debugstr_w(lpszSrc
), lpszDst
, iLen
);
2727 lpszRet
= StrCpyNXW(lpszDst
, lpszSrc
, iLen
);
2728 return lpszRet
- lpszDst
+ 1;
2731 /*************************************************************************
2734 * Determine if an Ascii string converts to Unicode and back identically.
2737 * lpSrcStr [I] Source Unicode string to convert
2738 * lpDst [O] Destination for resulting Ascii string
2739 * iLen [I] Length of lpDst in characters
2742 * TRUE, since Ascii strings always convert identically.
2744 BOOL WINAPI
DoesStringRoundTripA(LPCSTR lpSrcStr
, LPSTR lpDst
, INT iLen
)
2746 lstrcpynA(lpDst
, lpSrcStr
, iLen
);
2750 /*************************************************************************
2753 * Determine if a Unicode string converts to Ascii and back identically.
2756 * lpSrcStr [I] Source Unicode string to convert
2757 * lpDst [O] Destination for resulting Ascii string
2758 * iLen [I] Length of lpDst in characters
2761 * TRUE, if lpSrcStr converts to Ascii and back identically,
2764 BOOL WINAPI
DoesStringRoundTripW(LPCWSTR lpSrcStr
, LPSTR lpDst
, INT iLen
)
2766 WCHAR szBuff
[MAX_PATH
];
2768 SHUnicodeToAnsi(lpSrcStr
, lpDst
, iLen
);
2769 SHAnsiToUnicode(lpDst
, szBuff
, MAX_PATH
);
2770 return !strcmpW(lpSrcStr
, szBuff
);
2773 /*************************************************************************
2774 * SHLoadIndirectString [SHLWAPI.@]
2776 * If passed a string that begins with '@', extract the string from the
2777 * appropriate resource, otherwise do a straight copy.
2780 HRESULT WINAPI
SHLoadIndirectString(LPCWSTR src
, LPWSTR dst
, UINT dst_len
, void **reserved
)
2782 WCHAR
*dllname
= NULL
;
2783 HMODULE hmod
= NULL
;
2784 HRESULT hr
= E_FAIL
;
2786 TRACE("(%s %p %08x %p)\n", debugstr_w(src
), dst
, dst_len
, reserved
);
2794 dllname
= StrDupW(src
+ 1);
2795 index_str
= strchrW(dllname
, ',');
2797 if(!index_str
) goto end
;
2801 index
= atoiW(index_str
);
2803 hmod
= LoadLibraryW(dllname
);
2808 if(LoadStringW(hmod
, -index
, dst
, dst_len
))
2812 FIXME("can't handle non-negative indices (%d)\n", index
);
2817 lstrcpynW(dst
, src
, dst_len
);
2821 TRACE("returning %s\n", debugstr_w(dst
));
2823 if(hmod
) FreeLibrary(hmod
);
2824 HeapFree(GetProcessHeap(), 0, dllname
);
2828 BOOL WINAPI
IsCharSpaceA(CHAR c
)
2831 return GetStringTypeA(GetSystemDefaultLCID(), CT_CTYPE1
, &c
, 1, &CharType
) && (CharType
& C1_SPACE
);
2834 /*************************************************************************
2837 * Determine if a Unicode character is a space.
2840 * wc [I] Character to check.
2843 * TRUE, if wc is a space,
2846 BOOL WINAPI
IsCharSpaceW(WCHAR wc
)
2850 return GetStringTypeW(CT_CTYPE1
, &wc
, 1, &CharType
) && (CharType
& C1_SPACE
);