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
28 extern HINSTANCE shlwapi_hInstance
;
30 static HRESULT
_SHStrDupAA(LPCSTR
,LPSTR
*);
31 static HRESULT
_SHStrDupAW(LPCWSTR
,LPSTR
*);
34 static void FillNumberFmt(NUMBERFMTW
*fmt
, LPWSTR decimal_buffer
, int decimal_bufwlen
,
35 LPWSTR thousand_buffer
, int thousand_bufwlen
)
40 GetLocaleInfoW(LOCALE_USER_DEFAULT
, LOCALE_ILZERO
|LOCALE_RETURN_NUMBER
, (LPWSTR
)&fmt
->LeadingZero
, sizeof(fmt
->LeadingZero
)/sizeof(WCHAR
));
41 GetLocaleInfoW(LOCALE_USER_DEFAULT
, LOCALE_INEGNUMBER
|LOCALE_RETURN_NUMBER
, (LPWSTR
)&fmt
->LeadingZero
, sizeof(fmt
->NegativeOrder
)/sizeof(WCHAR
));
43 GetLocaleInfoW(LOCALE_USER_DEFAULT
, LOCALE_SDECIMAL
, decimal_buffer
, decimal_bufwlen
);
44 GetLocaleInfoW(LOCALE_USER_DEFAULT
, LOCALE_STHOUSAND
, thousand_buffer
, thousand_bufwlen
);
45 fmt
->lpThousandSep
= thousand_buffer
;
46 fmt
->lpDecimalSep
= decimal_buffer
;
49 * Converting grouping string to number as described on
50 * http://blogs.msdn.com/oldnewthing/archive/2006/04/18/578251.aspx
53 GetLocaleInfoW(LOCALE_USER_DEFAULT
, LOCALE_SGROUPING
, grouping
, sizeof(grouping
)/sizeof(WCHAR
));
54 for (c
= grouping
; *c
; c
++)
55 if (*c
>= '0' && *c
< '9')
58 fmt
->Grouping
+= *c
- '0';
61 if (fmt
->Grouping
% 10 == 0)
67 /*************************************************************************
68 * FormatInt [internal]
70 * Format an integer according to the current locale
73 * The number of characters written on success or 0 on failure
75 static int FormatInt(LONGLONG qdwValue
, LPWSTR pszBuf
, int cchBuf
)
78 WCHAR decimal
[8], thousand
[8];
81 BOOL neg
= (qdwValue
< 0);
83 FillNumberFmt(&fmt
, decimal
, sizeof decimal
/ sizeof (WCHAR
),
84 thousand
, sizeof thousand
/ sizeof (WCHAR
));
90 *(--c
) = '0' + (qdwValue
%10);
92 } while (qdwValue
> 0);
96 return GetNumberFormatW(LOCALE_USER_DEFAULT
, 0, c
, &fmt
, pszBuf
, cchBuf
);
99 /*************************************************************************
100 * FormatDouble [internal]
102 * Format an integer according to the current locale. Prints the specified number of digits
103 * after the decimal point
106 * The number of characters written on success or 0 on failure
108 static int FormatDouble(double value
, int decimals
, LPWSTR pszBuf
, int cchBuf
)
110 static const WCHAR flfmt
[] = {'%','f',0};
113 WCHAR decimal
[8], thousand
[8];
115 snprintfW(buf
, 64, flfmt
, value
);
117 FillNumberFmt(&fmt
, decimal
, sizeof decimal
/ sizeof (WCHAR
),
118 thousand
, sizeof thousand
/ sizeof (WCHAR
));
119 fmt
.NumDigits
= decimals
;
120 return GetNumberFormatW(LOCALE_USER_DEFAULT
, 0, buf
, &fmt
, pszBuf
, cchBuf
);
123 /*************************************************************************
124 * SHLWAPI_ChrCmpHelperA
126 * Internal helper for SHLWAPI_ChrCmpA/ChrCMPIA.
129 * Both this function and its Unicode counterpart are very inefficient. To
130 * fix this, CompareString must be completely implemented and optimised
131 * first. Then the core character test can be taken out of that function and
132 * placed here, so that it need never be called at all. Until then, do not
133 * attempt to optimise this code unless you are willing to test that it
134 * still performs correctly.
136 static BOOL
SHLWAPI_ChrCmpHelperA(WORD ch1
, WORD ch2
, DWORD dwFlags
)
138 char str1
[3], str2
[3];
140 str1
[0] = LOBYTE(ch1
);
141 if (IsDBCSLeadByte(str1
[0]))
143 str1
[1] = HIBYTE(ch1
);
149 str2
[0] = LOBYTE(ch2
);
150 if (IsDBCSLeadByte(str2
[0]))
152 str2
[1] = HIBYTE(ch2
);
158 return CompareStringA(GetThreadLocale(), dwFlags
, str1
, -1, str2
, -1) - CSTR_EQUAL
;
161 /*************************************************************************
164 * Internal helper function.
166 static BOOL WINAPI
SHLWAPI_ChrCmpA(WORD ch1
, WORD ch2
)
168 return SHLWAPI_ChrCmpHelperA(ch1
, ch2
, 0);
171 /*************************************************************************
172 * ChrCmpIA (SHLWAPI.385)
174 * Compare two characters, ignoring case.
177 * ch1 [I] First character to compare
178 * ch2 [I] Second character to compare
181 * FALSE, if the characters are equal.
182 * Non-zero otherwise.
184 BOOL WINAPI
ChrCmpIA(WORD ch1
, WORD ch2
)
186 TRACE("(%d,%d)\n", ch1
, ch2
);
188 return SHLWAPI_ChrCmpHelperA(ch1
, ch2
, NORM_IGNORECASE
);
191 /*************************************************************************
192 * ChrCmpIW [SHLWAPI.386]
196 BOOL WINAPI
ChrCmpIW(WCHAR ch1
, WCHAR ch2
)
198 return CompareStringW(GetThreadLocale(), NORM_IGNORECASE
, &ch1
, 1, &ch2
, 1) - CSTR_EQUAL
;
201 /*************************************************************************
202 * StrChrA [SHLWAPI.@]
204 * Find a given character in a string.
207 * lpszStr [I] String to search in.
208 * ch [I] Character to search for.
211 * Success: A pointer to the first occurrence of ch in lpszStr, or NULL if
213 * Failure: NULL, if any arguments are invalid.
215 LPSTR WINAPI
StrChrA(LPCSTR lpszStr
, WORD ch
)
217 TRACE("(%s,%i)\n", debugstr_a(lpszStr
), ch
);
223 if (!SHLWAPI_ChrCmpA(*lpszStr
, ch
))
224 return (LPSTR
)lpszStr
;
225 lpszStr
= CharNextA(lpszStr
);
231 /*************************************************************************
232 * StrChrW [SHLWAPI.@]
236 LPWSTR WINAPI
StrChrW(LPCWSTR lpszStr
, WCHAR ch
)
238 LPWSTR lpszRet
= NULL
;
240 TRACE("(%s,%i)\n", debugstr_w(lpszStr
), ch
);
243 lpszRet
= strchrW(lpszStr
, ch
);
247 /*************************************************************************
248 * StrChrIA [SHLWAPI.@]
250 * Find a given character in a string, ignoring case.
253 * lpszStr [I] String to search in.
254 * ch [I] Character to search for.
257 * Success: A pointer to the first occurrence of ch in lpszStr, or NULL if
259 * Failure: NULL, if any arguments are invalid.
261 LPSTR WINAPI
StrChrIA(LPCSTR lpszStr
, WORD ch
)
263 TRACE("(%s,%i)\n", debugstr_a(lpszStr
), ch
);
269 if (!ChrCmpIA(*lpszStr
, ch
))
270 return (LPSTR
)lpszStr
;
271 lpszStr
= CharNextA(lpszStr
);
277 /*************************************************************************
278 * StrChrIW [SHLWAPI.@]
282 LPWSTR WINAPI
StrChrIW(LPCWSTR lpszStr
, WCHAR ch
)
284 TRACE("(%s,%i)\n", debugstr_w(lpszStr
), ch
);
291 if (toupperW(*lpszStr
) == ch
)
292 return (LPWSTR
)lpszStr
;
297 return (LPWSTR
)lpszStr
;
300 /*************************************************************************
301 * StrChrNW [SHLWAPI.@]
303 LPWSTR WINAPI
StrChrNW(LPCWSTR lpszStr
, WCHAR ch
, UINT cchMax
)
305 TRACE("(%s(%i),%i)\n", debugstr_wn(lpszStr
,cchMax
), cchMax
, ch
);
309 while (*lpszStr
&& cchMax
-- > 0)
312 return (LPWSTR
)lpszStr
;
319 /*************************************************************************
320 * StrCmpIW [SHLWAPI.@]
322 * Compare two strings, ignoring case.
325 * lpszStr [I] First string to compare
326 * lpszComp [I] Second string to compare
329 * An integer less than, equal to or greater than 0, indicating that
330 * lpszStr is less than, the same, or greater than lpszComp.
332 int WINAPI
StrCmpIW(LPCWSTR lpszStr
, LPCWSTR lpszComp
)
334 TRACE("(%s,%s)\n", debugstr_w(lpszStr
),debugstr_w(lpszComp
));
335 return CompareStringW(GetThreadLocale(), NORM_IGNORECASE
, lpszStr
, -1, lpszComp
, -1) - CSTR_EQUAL
;
338 /*************************************************************************
339 * StrCmpNA [SHLWAPI.@]
341 * Compare two strings, up to a maximum length.
344 * lpszStr [I] First string to compare
345 * lpszComp [I] Second string to compare
346 * iLen [I] Maximum number of chars to compare.
349 * An integer less than, equal to or greater than 0, indicating that
350 * lpszStr is less than, the same, or greater than lpszComp.
352 INT WINAPI
StrCmpNA(LPCSTR lpszStr
, LPCSTR lpszComp
, INT iLen
)
354 TRACE("(%s,%s,%i)\n", debugstr_a(lpszStr
), debugstr_a(lpszComp
), iLen
);
355 return CompareStringA(GetThreadLocale(), 0, lpszStr
, iLen
, lpszComp
, iLen
) - CSTR_EQUAL
;
358 /*************************************************************************
359 * StrCmpNW [SHLWAPI.@]
363 INT WINAPI
StrCmpNW(LPCWSTR lpszStr
, LPCWSTR lpszComp
, INT iLen
)
365 TRACE("(%s,%s,%i)\n", debugstr_w(lpszStr
), debugstr_w(lpszComp
), iLen
);
366 return CompareStringW(GetThreadLocale(), 0, lpszStr
, iLen
, lpszComp
, iLen
) - CSTR_EQUAL
;
369 /*************************************************************************
370 * StrCmpNIA [SHLWAPI.@]
372 * Compare two strings, up to a maximum length, ignoring case.
375 * lpszStr [I] First string to compare
376 * lpszComp [I] Second string to compare
377 * iLen [I] Maximum number of chars to compare.
380 * An integer less than, equal to or greater than 0, indicating that
381 * lpszStr is less than, the same, or greater than lpszComp.
383 int WINAPI
StrCmpNIA(LPCSTR lpszStr
, LPCSTR lpszComp
, int iLen
)
385 TRACE("(%s,%s,%i)\n", debugstr_a(lpszStr
), debugstr_a(lpszComp
), iLen
);
386 return CompareStringA(GetThreadLocale(), NORM_IGNORECASE
, lpszStr
, iLen
, lpszComp
, iLen
) - CSTR_EQUAL
;
389 /*************************************************************************
390 * StrCmpNIW [SHLWAPI.@]
394 INT WINAPI
StrCmpNIW(LPCWSTR lpszStr
, LPCWSTR lpszComp
, int iLen
)
396 TRACE("(%s,%s,%i)\n", debugstr_w(lpszStr
), debugstr_w(lpszComp
), iLen
);
397 return CompareStringW(GetThreadLocale(), NORM_IGNORECASE
, lpszStr
, iLen
, lpszComp
, iLen
) - CSTR_EQUAL
;
400 /*************************************************************************
401 * StrCmpW [SHLWAPI.@]
403 * Compare two strings.
406 * lpszStr [I] First string to compare
407 * lpszComp [I] Second string to compare
410 * An integer less than, equal to or greater than 0, indicating that
411 * lpszStr is less than, the same, or greater than lpszComp.
413 int WINAPI
StrCmpW(LPCWSTR lpszStr
, LPCWSTR lpszComp
)
415 TRACE("(%s,%s)\n", debugstr_w(lpszStr
), debugstr_w(lpszComp
));
416 return CompareStringW(GetThreadLocale(), 0, lpszStr
, -1, lpszComp
, -1) - CSTR_EQUAL
;
419 /*************************************************************************
420 * StrCatW [SHLWAPI.@]
422 * Concatenate two strings.
425 * lpszStr [O] Initial string
426 * lpszSrc [I] String to concatenate
431 LPWSTR WINAPI
StrCatW(LPWSTR lpszStr
, LPCWSTR lpszSrc
)
433 TRACE("(%s,%s)\n", debugstr_w(lpszStr
), debugstr_w(lpszSrc
));
435 if (lpszStr
&& lpszSrc
)
436 strcatW(lpszStr
, lpszSrc
);
440 /*************************************************************************
441 * StrCatChainW [SHLWAPI.@]
443 * Concatenates two unicode strings.
446 * lpszStr [O] Initial string
447 * cchMax [I] Length of destination buffer
448 * ichAt [I] Offset from the destination buffer to begin concatenation
449 * lpszCat [I] String to concatenate
452 * The offset from the beginning of pszDst to the terminating NULL.
454 DWORD WINAPI
StrCatChainW(LPWSTR lpszStr
, DWORD cchMax
, DWORD ichAt
, LPCWSTR lpszCat
)
456 TRACE("(%s,%u,%d,%s)\n", debugstr_w(lpszStr
), cchMax
, ichAt
, debugstr_w(lpszCat
));
459 ichAt
= strlenW(lpszStr
);
467 if (lpszCat
&& ichAt
< cchMax
)
470 while (ichAt
< cchMax
- 1 && *lpszCat
)
472 *lpszStr
++ = *lpszCat
++;
481 /*************************************************************************
482 * StrCpyW [SHLWAPI.@]
484 * Copy a string to another string.
487 * lpszStr [O] Destination string
488 * lpszSrc [I] Source string
493 LPWSTR WINAPI
StrCpyW(LPWSTR lpszStr
, LPCWSTR lpszSrc
)
495 TRACE("(%p,%s)\n", lpszStr
, debugstr_w(lpszSrc
));
497 if (lpszStr
&& lpszSrc
)
498 strcpyW(lpszStr
, lpszSrc
);
502 /*************************************************************************
503 * StrCpyNW [SHLWAPI.@]
505 * Copy a string to another string, up to a maximum number of characters.
508 * dst [O] Destination string
509 * src [I] Source string
510 * count [I] Maximum number of chars to copy
515 LPWSTR WINAPI
StrCpyNW(LPWSTR dst
, LPCWSTR src
, int count
)
520 TRACE("(%p,%s,%i)\n", dst
, debugstr_w(src
), count
);
524 while ((count
> 1) && *s
)
535 /*************************************************************************
536 * SHLWAPI_StrStrHelperA
538 * Internal implementation of StrStrA/StrStrIA
540 static LPSTR
SHLWAPI_StrStrHelperA(LPCSTR lpszStr
, LPCSTR lpszSearch
,
541 INT (WINAPI
*pStrCmpFn
)(LPCSTR
,LPCSTR
,INT
))
545 if (!lpszStr
|| !lpszSearch
|| !*lpszSearch
)
548 iLen
= strlen(lpszSearch
);
552 if (!pStrCmpFn(lpszStr
, lpszSearch
, iLen
))
553 return (LPSTR
)lpszStr
;
554 lpszStr
= CharNextA(lpszStr
);
559 /*************************************************************************
560 * StrStrA [SHLWAPI.@]
562 * Find a substring within a string.
565 * lpszStr [I] String to search in
566 * lpszSearch [I] String to look for
569 * The start of lpszSearch within lpszStr, or NULL if not found.
571 LPSTR WINAPI
StrStrA(LPCSTR lpszStr
, LPCSTR lpszSearch
)
573 TRACE("(%s,%s)\n", debugstr_a(lpszStr
), debugstr_a(lpszSearch
));
575 return SHLWAPI_StrStrHelperA(lpszStr
, lpszSearch
, StrCmpNA
);
578 /*************************************************************************
579 * StrStrW [SHLWAPI.@]
583 LPWSTR WINAPI
StrStrW(LPCWSTR lpszStr
, LPCWSTR lpszSearch
)
585 TRACE("(%s, %s)\n", debugstr_w(lpszStr
), debugstr_w(lpszSearch
));
587 if (!lpszStr
|| !lpszSearch
|| !*lpszSearch
) return NULL
;
588 return strstrW( lpszStr
, lpszSearch
);
591 /*************************************************************************
592 * StrRStrIA [SHLWAPI.@]
594 * Find the last occurrence of a substring within a string.
597 * lpszStr [I] String to search in
598 * lpszEnd [I] End of lpszStr
599 * lpszSearch [I] String to look for
602 * The last occurrence lpszSearch within lpszStr, or NULL if not found.
604 LPSTR WINAPI
StrRStrIA(LPCSTR lpszStr
, LPCSTR lpszEnd
, LPCSTR lpszSearch
)
609 TRACE("(%s,%s)\n", debugstr_a(lpszStr
), debugstr_a(lpszSearch
));
611 if (!lpszStr
|| !lpszSearch
|| !*lpszSearch
)
615 lpszEnd
= lpszStr
+ lstrlenA(lpszStr
);
616 if (lpszEnd
== lpszStr
)
619 if (IsDBCSLeadByte(*lpszSearch
))
620 ch1
= *lpszSearch
<< 8 | (UCHAR
)lpszSearch
[1];
623 iLen
= lstrlenA(lpszSearch
);
627 lpszEnd
= CharPrevA(lpszStr
, lpszEnd
);
628 ch2
= IsDBCSLeadByte(*lpszEnd
)? *lpszEnd
<< 8 | (UCHAR
)lpszEnd
[1] : *lpszEnd
;
629 if (!ChrCmpIA(ch1
, ch2
))
631 if (!StrCmpNIA(lpszEnd
, lpszSearch
, iLen
))
632 return (LPSTR
)lpszEnd
;
634 } while (lpszEnd
> lpszStr
);
638 /*************************************************************************
639 * StrRStrIW [SHLWAPI.@]
643 LPWSTR WINAPI
StrRStrIW(LPCWSTR lpszStr
, LPCWSTR lpszEnd
, LPCWSTR lpszSearch
)
647 TRACE("(%s,%s)\n", debugstr_w(lpszStr
), debugstr_w(lpszSearch
));
649 if (!lpszStr
|| !lpszSearch
|| !*lpszSearch
)
653 lpszEnd
= lpszStr
+ strlenW(lpszStr
);
655 iLen
= strlenW(lpszSearch
);
657 while (lpszEnd
> lpszStr
)
660 if (!StrCmpNIW(lpszEnd
, lpszSearch
, iLen
))
661 return (LPWSTR
)lpszEnd
;
666 /*************************************************************************
667 * StrStrIA [SHLWAPI.@]
669 * Find a substring within a string, ignoring case.
672 * lpszStr [I] String to search in
673 * lpszSearch [I] String to look for
676 * The start of lpszSearch within lpszStr, or NULL if not found.
678 LPSTR WINAPI
StrStrIA(LPCSTR lpszStr
, LPCSTR lpszSearch
)
680 TRACE("(%s,%s)\n", debugstr_a(lpszStr
), debugstr_a(lpszSearch
));
682 return SHLWAPI_StrStrHelperA(lpszStr
, lpszSearch
, StrCmpNIA
);
685 /*************************************************************************
686 * StrStrIW [SHLWAPI.@]
690 LPWSTR WINAPI
StrStrIW(LPCWSTR lpszStr
, LPCWSTR lpszSearch
)
694 TRACE("(%s,%s)\n", debugstr_w(lpszStr
), debugstr_w(lpszSearch
));
696 if (!lpszStr
|| !lpszSearch
|| !*lpszSearch
)
699 iLen
= strlenW(lpszSearch
);
703 if (!StrCmpNIW(lpszStr
, lpszSearch
, iLen
))
704 return (LPWSTR
)lpszStr
;
710 /*************************************************************************
711 * StrStrNW [SHLWAPI.@]
713 * Find a substring within a string up to a given number of initial characters.
716 * lpFirst [I] String to search in
717 * lpSrch [I] String to look for
718 * cchMax [I] Maximum number of initial search characters
721 * The start of lpFirst within lpSrch, or NULL if not found.
723 LPWSTR WINAPI
StrStrNW(LPCWSTR lpFirst
, LPCWSTR lpSrch
, UINT cchMax
)
728 TRACE("(%s, %s, %u)\n", debugstr_w(lpFirst
), debugstr_w(lpSrch
), cchMax
);
730 if (!lpFirst
|| !lpSrch
|| !*lpSrch
|| !cchMax
)
733 len
= strlenW(lpSrch
);
735 for (i
= cchMax
; *lpFirst
&& (i
> 0); i
--, lpFirst
++)
737 if (!strncmpW(lpFirst
, lpSrch
, len
))
738 return (LPWSTR
)lpFirst
;
744 /*************************************************************************
745 * StrStrNIW [SHLWAPI.@]
747 * Find a substring within a string up to a given number of initial characters,
751 * lpFirst [I] String to search in
752 * lpSrch [I] String to look for
753 * cchMax [I] Maximum number of initial search characters
756 * The start of lpFirst within lpSrch, or NULL if not found.
758 LPWSTR WINAPI
StrStrNIW(LPCWSTR lpFirst
, LPCWSTR lpSrch
, UINT cchMax
)
763 TRACE("(%s, %s, %u)\n", debugstr_w(lpFirst
), debugstr_w(lpSrch
), cchMax
);
765 if (!lpFirst
|| !lpSrch
|| !*lpSrch
|| !cchMax
)
768 len
= strlenW(lpSrch
);
770 for (i
= cchMax
; *lpFirst
&& (i
> 0); i
--, lpFirst
++)
772 if (!strncmpiW(lpFirst
, lpSrch
, len
))
773 return (LPWSTR
)lpFirst
;
779 /*************************************************************************
780 * StrToIntA [SHLWAPI.@]
782 * Read a signed integer from a string.
785 * lpszStr [I] String to read integer from
788 * The signed integer value represented by the string, or 0 if no integer is
792 * No leading space is allowed before the number, although a leading '-' is.
794 int WINAPI
StrToIntA(LPCSTR lpszStr
)
798 TRACE("(%s)\n", debugstr_a(lpszStr
));
802 WARN("Invalid lpszStr would crash under Win32!\n");
806 if (*lpszStr
== '-' || isdigit(*lpszStr
))
807 StrToIntExA(lpszStr
, 0, &iRet
);
811 /*************************************************************************
812 * StrToIntW [SHLWAPI.@]
816 int WINAPI
StrToIntW(LPCWSTR lpszStr
)
820 TRACE("(%s)\n", debugstr_w(lpszStr
));
824 WARN("Invalid lpszStr would crash under Win32!\n");
828 if (*lpszStr
== '-' || isdigitW(*lpszStr
))
829 StrToIntExW(lpszStr
, 0, &iRet
);
833 /*************************************************************************
834 * StrToIntExA [SHLWAPI.@]
836 * Read an integer from a string.
839 * lpszStr [I] String to read integer from
840 * dwFlags [I] Flags controlling the conversion
841 * lpiRet [O] Destination for read integer.
844 * Success: TRUE. lpiRet contains the integer value represented by the string.
845 * Failure: FALSE, if the string is invalid, or no number is present.
848 * Leading whitespace, '-' and '+' are allowed before the number. If
849 * dwFlags includes STIF_SUPPORT_HEX, hexadecimal numbers are allowed, if
850 * preceded by '0x'. If this flag is not set, or there is no '0x' prefix,
851 * the string is treated as a decimal string. A leading '-' is ignored for
852 * hexadecimal numbers.
854 BOOL WINAPI
StrToIntExA(LPCSTR lpszStr
, DWORD dwFlags
, LPINT lpiRet
)
859 TRACE("(%s,%08X,%p)\n", debugstr_a(lpszStr
), dwFlags
, lpiRet
);
861 bRes
= StrToInt64ExA(lpszStr
, dwFlags
, &li
);
862 if (bRes
) *lpiRet
= li
;
866 /*************************************************************************
867 * StrToInt64ExA [SHLWAPI.@]
871 BOOL WINAPI
StrToInt64ExA(LPCSTR lpszStr
, DWORD dwFlags
, LONGLONG
*lpiRet
)
873 BOOL bNegative
= FALSE
;
876 TRACE("(%s,%08X,%p)\n", debugstr_a(lpszStr
), dwFlags
, lpiRet
);
878 if (!lpszStr
|| !lpiRet
)
880 WARN("Invalid parameter would crash under Win32!\n");
883 if (dwFlags
> STIF_SUPPORT_HEX
) WARN("Unknown flags %08x\n", dwFlags
);
885 /* Skip leading space, '+', '-' */
886 while (isspace(*lpszStr
))
887 lpszStr
= CharNextA(lpszStr
);
894 else if (*lpszStr
== '+')
897 if (dwFlags
& STIF_SUPPORT_HEX
&&
898 *lpszStr
== '0' && tolower(lpszStr
[1]) == 'x')
900 /* Read hex number */
903 if (!isxdigit(*lpszStr
))
906 while (isxdigit(*lpszStr
))
909 if (isdigit(*lpszStr
))
910 iRet
+= (*lpszStr
- '0');
912 iRet
+= 10 + (tolower(*lpszStr
) - 'a');
919 /* Read decimal number */
920 if (!isdigit(*lpszStr
))
923 while (isdigit(*lpszStr
))
926 iRet
+= (*lpszStr
- '0');
929 *lpiRet
= bNegative
? -iRet
: iRet
;
933 /*************************************************************************
934 * StrToIntExW [SHLWAPI.@]
938 BOOL WINAPI
StrToIntExW(LPCWSTR lpszStr
, DWORD dwFlags
, LPINT lpiRet
)
943 TRACE("(%s,%08X,%p)\n", debugstr_w(lpszStr
), dwFlags
, lpiRet
);
945 bRes
= StrToInt64ExW(lpszStr
, dwFlags
, &li
);
946 if (bRes
) *lpiRet
= li
;
950 /*************************************************************************
951 * StrToInt64ExW [SHLWAPI.@]
955 BOOL WINAPI
StrToInt64ExW(LPCWSTR lpszStr
, DWORD dwFlags
, LONGLONG
*lpiRet
)
957 BOOL bNegative
= FALSE
;
960 TRACE("(%s,%08X,%p)\n", debugstr_w(lpszStr
), dwFlags
, lpiRet
);
962 if (!lpszStr
|| !lpiRet
)
964 WARN("Invalid parameter would crash under Win32!\n");
967 if (dwFlags
> STIF_SUPPORT_HEX
) WARN("Unknown flags %08x\n", dwFlags
);
969 /* Skip leading space, '+', '-' */
970 while (isspaceW(*lpszStr
)) lpszStr
++;
977 else if (*lpszStr
== '+')
980 if (dwFlags
& STIF_SUPPORT_HEX
&&
981 *lpszStr
== '0' && tolowerW(lpszStr
[1]) == 'x')
983 /* Read hex number */
986 if (!isxdigitW(*lpszStr
))
989 while (isxdigitW(*lpszStr
))
992 if (isdigitW(*lpszStr
))
993 iRet
+= (*lpszStr
- '0');
995 iRet
+= 10 + (tolowerW(*lpszStr
) - 'a');
1002 /* Read decimal number */
1003 if (!isdigitW(*lpszStr
))
1006 while (isdigitW(*lpszStr
))
1009 iRet
+= (*lpszStr
- '0');
1012 *lpiRet
= bNegative
? -iRet
: iRet
;
1016 /*************************************************************************
1017 * StrDupA [SHLWAPI.@]
1019 * Duplicate a string.
1022 * lpszStr [I] String to duplicate.
1025 * Success: A pointer to a new string containing the contents of lpszStr
1026 * Failure: NULL, if memory cannot be allocated
1029 * The string memory is allocated with LocalAlloc(), and so should be released
1030 * by calling LocalFree().
1032 LPSTR WINAPI
StrDupA(LPCSTR lpszStr
)
1037 TRACE("(%s)\n",debugstr_a(lpszStr
));
1039 iLen
= lpszStr
? strlen(lpszStr
) + 1 : 1;
1040 lpszRet
= LocalAlloc(LMEM_FIXED
, iLen
);
1045 memcpy(lpszRet
, lpszStr
, iLen
);
1052 /*************************************************************************
1053 * StrDupW [SHLWAPI.@]
1057 LPWSTR WINAPI
StrDupW(LPCWSTR lpszStr
)
1062 TRACE("(%s)\n",debugstr_w(lpszStr
));
1064 iLen
= (lpszStr
? strlenW(lpszStr
) + 1 : 1) * sizeof(WCHAR
);
1065 lpszRet
= LocalAlloc(LMEM_FIXED
, iLen
);
1070 memcpy(lpszRet
, lpszStr
, iLen
);
1077 /*************************************************************************
1078 * SHLWAPI_StrSpnHelperA
1080 * Internal implementation of StrSpnA/StrCSpnA/StrCSpnIA
1082 static int SHLWAPI_StrSpnHelperA(LPCSTR lpszStr
, LPCSTR lpszMatch
,
1083 LPSTR (WINAPI
*pStrChrFn
)(LPCSTR
,WORD
),
1086 LPCSTR lpszRead
= lpszStr
;
1087 if (lpszStr
&& *lpszStr
&& lpszMatch
)
1091 LPCSTR lpszTest
= pStrChrFn(lpszMatch
, *lpszRead
);
1093 if (!bInvert
&& !lpszTest
)
1095 if (bInvert
&& lpszTest
)
1097 lpszRead
= CharNextA(lpszRead
);
1100 return lpszRead
- lpszStr
;
1103 /*************************************************************************
1104 * StrSpnA [SHLWAPI.@]
1106 * Find the length of the start of a string that contains only certain
1110 * lpszStr [I] String to search
1111 * lpszMatch [I] Characters that can be in the substring
1114 * The length of the part of lpszStr containing only chars from lpszMatch,
1115 * or 0 if any parameter is invalid.
1117 int WINAPI
StrSpnA(LPCSTR lpszStr
, LPCSTR lpszMatch
)
1119 TRACE("(%s,%s)\n",debugstr_a(lpszStr
), debugstr_a(lpszMatch
));
1121 return SHLWAPI_StrSpnHelperA(lpszStr
, lpszMatch
, StrChrA
, FALSE
);
1124 /*************************************************************************
1125 * StrSpnW [SHLWAPI.@]
1129 int WINAPI
StrSpnW(LPCWSTR lpszStr
, LPCWSTR lpszMatch
)
1131 if (!lpszStr
|| !lpszMatch
) return 0;
1132 return strspnW( lpszStr
, lpszMatch
);
1135 /*************************************************************************
1136 * StrCSpnA [SHLWAPI.@]
1138 * Find the length of the start of a string that does not contain certain
1142 * lpszStr [I] String to search
1143 * lpszMatch [I] Characters that cannot be in the substring
1146 * The length of the part of lpszStr containing only chars not in lpszMatch,
1147 * or 0 if any parameter is invalid.
1149 int WINAPI
StrCSpnA(LPCSTR lpszStr
, LPCSTR lpszMatch
)
1151 TRACE("(%s,%s)\n",debugstr_a(lpszStr
), debugstr_a(lpszMatch
));
1153 return SHLWAPI_StrSpnHelperA(lpszStr
, lpszMatch
, StrChrA
, TRUE
);
1156 /*************************************************************************
1157 * StrCSpnW [SHLWAPI.@]
1161 int WINAPI
StrCSpnW(LPCWSTR lpszStr
, LPCWSTR lpszMatch
)
1163 if (!lpszStr
|| !lpszMatch
) return 0;
1164 return strcspnW( lpszStr
, lpszMatch
);
1167 /*************************************************************************
1168 * StrCSpnIA [SHLWAPI.@]
1170 * Find the length of the start of a string that does not contain certain
1171 * characters, ignoring case.
1174 * lpszStr [I] String to search
1175 * lpszMatch [I] Characters that cannot be in the substring
1178 * The length of the part of lpszStr containing only chars not in lpszMatch,
1179 * or 0 if any parameter is invalid.
1181 int WINAPI
StrCSpnIA(LPCSTR lpszStr
, LPCSTR lpszMatch
)
1183 TRACE("(%s,%s)\n",debugstr_a(lpszStr
), debugstr_a(lpszMatch
));
1185 return SHLWAPI_StrSpnHelperA(lpszStr
, lpszMatch
, StrChrIA
, TRUE
);
1188 /*************************************************************************
1189 * StrCSpnIW [SHLWAPI.@]
1193 int WINAPI
StrCSpnIW(LPCWSTR lpszStr
, LPCWSTR lpszMatch
)
1195 LPCWSTR lpszRead
= lpszStr
;
1197 TRACE("(%s,%s)\n",debugstr_w(lpszStr
), debugstr_w(lpszMatch
));
1199 if (lpszStr
&& *lpszStr
&& lpszMatch
)
1203 if (StrChrIW(lpszMatch
, *lpszRead
)) break;
1207 return lpszRead
- lpszStr
;
1210 /*************************************************************************
1211 * StrPBrkA [SHLWAPI.@]
1213 * Search a string for any of a group of characters.
1216 * lpszStr [I] String to search
1217 * lpszMatch [I] Characters to match
1220 * A pointer to the first matching character in lpszStr, or NULL if no
1223 LPSTR WINAPI
StrPBrkA(LPCSTR lpszStr
, LPCSTR lpszMatch
)
1225 TRACE("(%s,%s)\n",debugstr_a(lpszStr
), debugstr_a(lpszMatch
));
1227 if (lpszStr
&& lpszMatch
&& *lpszMatch
)
1231 if (StrChrA(lpszMatch
, *lpszStr
))
1232 return (LPSTR
)lpszStr
;
1233 lpszStr
= CharNextA(lpszStr
);
1239 /*************************************************************************
1240 * StrPBrkW [SHLWAPI.@]
1244 LPWSTR WINAPI
StrPBrkW(LPCWSTR lpszStr
, LPCWSTR lpszMatch
)
1246 if (!lpszStr
|| !lpszMatch
) return NULL
;
1247 return strpbrkW( lpszStr
, lpszMatch
);
1250 /*************************************************************************
1251 * SHLWAPI_StrRChrHelperA
1253 * Internal implementation of StrRChrA/StrRChrIA.
1255 static LPSTR
SHLWAPI_StrRChrHelperA(LPCSTR lpszStr
,
1256 LPCSTR lpszEnd
, WORD ch
,
1257 BOOL (WINAPI
*pChrCmpFn
)(WORD
,WORD
))
1259 LPCSTR lpszRet
= NULL
;
1266 lpszEnd
= lpszStr
+ lstrlenA(lpszStr
);
1268 while (*lpszStr
&& lpszStr
<= lpszEnd
)
1270 ch2
= IsDBCSLeadByte(*lpszStr
)? *lpszStr
<< 8 | lpszStr
[1] : *lpszStr
;
1272 if (!pChrCmpFn(ch
, ch2
))
1274 lpszStr
= CharNextA(lpszStr
);
1277 return (LPSTR
)lpszRet
;
1280 /**************************************************************************
1281 * StrRChrA [SHLWAPI.@]
1283 * Find the last occurrence of a character in string.
1286 * lpszStr [I] String to search in
1287 * lpszEnd [I] Place to end search, or NULL to search until the end of lpszStr
1288 * ch [I] Character to search for.
1291 * Success: A pointer to the last occurrence of ch in lpszStr before lpszEnd,
1292 * or NULL if not found.
1293 * Failure: NULL, if any arguments are invalid.
1295 LPSTR WINAPI
StrRChrA(LPCSTR lpszStr
, LPCSTR lpszEnd
, WORD ch
)
1297 TRACE("(%s,%s,%x)\n", debugstr_a(lpszStr
), debugstr_a(lpszEnd
), ch
);
1299 return SHLWAPI_StrRChrHelperA(lpszStr
, lpszEnd
, ch
, SHLWAPI_ChrCmpA
);
1302 /**************************************************************************
1303 * StrRChrW [SHLWAPI.@]
1307 LPWSTR WINAPI
StrRChrW(LPCWSTR str
, LPCWSTR end
, WORD ch
)
1311 if (!str
) return NULL
;
1312 if (!end
) end
= str
+ strlenW(str
);
1315 if (*str
== ch
) ret
= (WCHAR
*)str
;
1321 /**************************************************************************
1322 * StrRChrIA [SHLWAPI.@]
1324 * Find the last occurrence of a character in string, ignoring case.
1327 * lpszStr [I] String to search in
1328 * lpszEnd [I] Place to end search, or NULL to search until the end of lpszStr
1329 * ch [I] Character to search for.
1332 * Success: A pointer to the last occurrence of ch in lpszStr before lpszEnd,
1333 * or NULL if not found.
1334 * Failure: NULL, if any arguments are invalid.
1336 LPSTR WINAPI
StrRChrIA(LPCSTR lpszStr
, LPCSTR lpszEnd
, WORD ch
)
1338 TRACE("(%s,%s,%x)\n", debugstr_a(lpszStr
), debugstr_a(lpszEnd
), ch
);
1340 return SHLWAPI_StrRChrHelperA(lpszStr
, lpszEnd
, ch
, ChrCmpIA
);
1343 /**************************************************************************
1344 * StrRChrIW [SHLWAPI.@]
1348 LPWSTR WINAPI
StrRChrIW(LPCWSTR str
, LPCWSTR end
, WORD ch
)
1352 if (!str
) return NULL
;
1353 if (!end
) end
= str
+ strlenW(str
);
1356 if (!ChrCmpIW(*str
, ch
)) ret
= (WCHAR
*)str
;
1362 /*************************************************************************
1363 * StrCatBuffA [SHLWAPI.@]
1365 * Concatenate two strings together.
1368 * lpszStr [O] String to concatenate to
1369 * lpszCat [I] String to add to lpszCat
1370 * cchMax [I] Maximum number of characters for the whole string
1376 * cchMax determines the number of characters in the final length of the
1377 * string, not the number appended to lpszStr from lpszCat.
1379 LPSTR WINAPI
StrCatBuffA(LPSTR lpszStr
, LPCSTR lpszCat
, INT cchMax
)
1383 TRACE("(%p,%s,%d)\n", lpszStr
, debugstr_a(lpszCat
), cchMax
);
1387 WARN("Invalid lpszStr would crash under Win32!\n");
1391 iLen
= strlen(lpszStr
);
1395 StrCpyNA(lpszStr
+ iLen
, lpszCat
, cchMax
);
1399 /*************************************************************************
1400 * StrCatBuffW [SHLWAPI.@]
1404 LPWSTR WINAPI
StrCatBuffW(LPWSTR lpszStr
, LPCWSTR lpszCat
, INT cchMax
)
1408 TRACE("(%p,%s,%d)\n", lpszStr
, debugstr_w(lpszCat
), cchMax
);
1412 WARN("Invalid lpszStr would crash under Win32!\n");
1416 iLen
= strlenW(lpszStr
);
1420 StrCpyNW(lpszStr
+ iLen
, lpszCat
, cchMax
);
1424 /*************************************************************************
1425 * StrRetToBufA [SHLWAPI.@]
1427 * Convert a STRRET to a normal string.
1430 * lpStrRet [O] STRRET to convert
1431 * pIdl [I] ITEMIDLIST for lpStrRet->uType == STRRET_OFFSET
1432 * lpszDest [O] Destination for normal string
1433 * dwLen [I] Length of lpszDest
1436 * Success: S_OK. lpszDest contains up to dwLen characters of the string.
1437 * If lpStrRet is of type STRRET_WSTR, its memory is freed with
1438 * CoTaskMemFree() and its type set to STRRET_CSTRA.
1439 * Failure: E_FAIL, if any parameters are invalid.
1441 HRESULT WINAPI
StrRetToBufA (LPSTRRET src
, const ITEMIDLIST
*pidl
, LPSTR dest
, UINT len
)
1444 * This routine is identical to that in dlls/shell32/shellstring.c.
1445 * It was duplicated because not every version of Shlwapi.dll exports
1446 * StrRetToBufA. If you change one routine, change them both.
1448 TRACE("dest=%p len=0x%x strret=%p pidl=%p\n", dest
, len
, src
, pidl
);
1452 WARN("Invalid lpStrRet would crash under Win32!\n");
1466 WideCharToMultiByte(CP_ACP
, 0, src
->u
.pOleStr
, -1, dest
, len
, NULL
, NULL
);
1467 CoTaskMemFree(src
->u
.pOleStr
);
1471 lstrcpynA(dest
, src
->u
.cStr
, len
);
1475 lstrcpynA((LPSTR
)dest
, ((LPCSTR
)&pidl
->mkid
)+src
->u
.uOffset
, len
);
1479 FIXME("unknown type!\n");
1485 /*************************************************************************
1486 * StrRetToBufW [SHLWAPI.@]
1490 HRESULT WINAPI
StrRetToBufW (LPSTRRET src
, const ITEMIDLIST
*pidl
, LPWSTR dest
, UINT len
)
1492 TRACE("dest=%p len=0x%x strret=%p pidl=%p\n", dest
, len
, src
, pidl
);
1496 WARN("Invalid lpStrRet would crash under Win32!\n");
1510 lstrcpynW(dest
, src
->u
.pOleStr
, len
);
1511 CoTaskMemFree(src
->u
.pOleStr
);
1515 if (!MultiByteToWideChar( CP_ACP
, 0, src
->u
.cStr
, -1, dest
, len
))
1522 if (!MultiByteToWideChar( CP_ACP
, 0, ((LPCSTR
)&pidl
->mkid
)+src
->u
.uOffset
, -1,
1529 FIXME("unknown type!\n");
1535 /*************************************************************************
1536 * StrRetToStrA [SHLWAPI.@]
1538 * Converts a STRRET to a normal string.
1541 * lpStrRet [O] STRRET to convert
1542 * pidl [I] ITEMIDLIST for lpStrRet->uType == STRRET_OFFSET
1543 * ppszName [O] Destination for converted string
1546 * Success: S_OK. ppszName contains the new string, allocated with CoTaskMemAlloc().
1547 * Failure: E_FAIL, if any parameters are invalid.
1549 HRESULT WINAPI
StrRetToStrA(LPSTRRET lpStrRet
, const ITEMIDLIST
*pidl
, LPSTR
*ppszName
)
1551 HRESULT hRet
= E_FAIL
;
1553 switch (lpStrRet
->uType
)
1556 hRet
= _SHStrDupAW(lpStrRet
->u
.pOleStr
, ppszName
);
1557 CoTaskMemFree(lpStrRet
->u
.pOleStr
);
1561 hRet
= _SHStrDupAA(lpStrRet
->u
.cStr
, ppszName
);
1565 hRet
= _SHStrDupAA(((LPCSTR
)&pidl
->mkid
) + lpStrRet
->u
.uOffset
, ppszName
);
1575 /*************************************************************************
1576 * StrRetToStrW [SHLWAPI.@]
1580 HRESULT WINAPI
StrRetToStrW(LPSTRRET lpStrRet
, const ITEMIDLIST
*pidl
, LPWSTR
*ppszName
)
1582 HRESULT hRet
= E_FAIL
;
1584 switch (lpStrRet
->uType
)
1587 hRet
= SHStrDupW(lpStrRet
->u
.pOleStr
, ppszName
);
1588 CoTaskMemFree(lpStrRet
->u
.pOleStr
);
1592 hRet
= SHStrDupA(lpStrRet
->u
.cStr
, ppszName
);
1596 hRet
= SHStrDupA(((LPCSTR
)&pidl
->mkid
) + lpStrRet
->u
.uOffset
, ppszName
);
1606 /* Create an ASCII string copy using SysAllocString() */
1607 static HRESULT
_SHStrDupAToBSTR(LPCSTR src
, BSTR
*pBstrOut
)
1613 INT len
= MultiByteToWideChar(CP_ACP
, 0, src
, -1, NULL
, 0);
1614 WCHAR
* szTemp
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1618 MultiByteToWideChar(CP_ACP
, 0, src
, -1, szTemp
, len
);
1619 *pBstrOut
= SysAllocString(szTemp
);
1620 HeapFree(GetProcessHeap(), 0, szTemp
);
1626 return E_OUTOFMEMORY
;
1629 /*************************************************************************
1630 * StrRetToBSTR [SHLWAPI.@]
1632 * Converts a STRRET to a BSTR.
1635 * lpStrRet [O] STRRET to convert
1636 * pidl [I] ITEMIDLIST for lpStrRet->uType = STRRET_OFFSET
1637 * pBstrOut [O] Destination for converted BSTR
1640 * Success: S_OK. pBstrOut contains the new string.
1641 * Failure: E_FAIL, if any parameters are invalid.
1643 HRESULT WINAPI
StrRetToBSTR(STRRET
*lpStrRet
, LPCITEMIDLIST pidl
, BSTR
* pBstrOut
)
1645 HRESULT hRet
= E_FAIL
;
1647 switch (lpStrRet
->uType
)
1650 *pBstrOut
= SysAllocString(lpStrRet
->u
.pOleStr
);
1653 CoTaskMemFree(lpStrRet
->u
.pOleStr
);
1657 hRet
= _SHStrDupAToBSTR(lpStrRet
->u
.cStr
, pBstrOut
);
1661 hRet
= _SHStrDupAToBSTR(((LPCSTR
)&pidl
->mkid
) + lpStrRet
->u
.uOffset
, pBstrOut
);
1671 /*************************************************************************
1672 * StrFormatKBSizeA [SHLWAPI.@]
1674 * Create a formatted string containing a byte count in Kilobytes.
1677 * llBytes [I] Byte size to format
1678 * lpszDest [I] Destination for formatted string
1679 * cchMax [I] Size of lpszDest
1684 LPSTR WINAPI
StrFormatKBSizeA(LONGLONG llBytes
, LPSTR lpszDest
, UINT cchMax
)
1688 if (!StrFormatKBSizeW(llBytes
, wszBuf
, 256))
1690 if (!WideCharToMultiByte(CP_ACP
, 0, wszBuf
, -1, lpszDest
, cchMax
, NULL
, NULL
))
1695 /*************************************************************************
1696 * StrFormatKBSizeW [SHLWAPI.@]
1698 * See StrFormatKBSizeA.
1700 LPWSTR WINAPI
StrFormatKBSizeW(LONGLONG llBytes
, LPWSTR lpszDest
, UINT cchMax
)
1702 static const WCHAR kb
[] = {' ','K','B',0};
1703 LONGLONG llKB
= (llBytes
+ 1023) >> 10;
1706 TRACE("(0x%s,%p,%d)\n", wine_dbgstr_longlong(llBytes
), lpszDest
, cchMax
);
1708 if (!FormatInt(llKB
, lpszDest
, cchMax
))
1711 len
= lstrlenW(lpszDest
);
1712 if (cchMax
- len
< 4)
1714 lstrcatW(lpszDest
, kb
);
1718 /*************************************************************************
1719 * StrNCatA [SHLWAPI.@]
1721 * Concatenate two strings together.
1724 * lpszStr [O] String to concatenate to
1725 * lpszCat [I] String to add to lpszCat
1726 * cchMax [I] Maximum number of characters to concatenate
1732 * cchMax determines the number of characters that are appended to lpszStr,
1733 * not the total length of the string.
1735 LPSTR WINAPI
StrNCatA(LPSTR lpszStr
, LPCSTR lpszCat
, INT cchMax
)
1737 LPSTR lpszRet
= lpszStr
;
1739 TRACE("(%s,%s,%i)\n", debugstr_a(lpszStr
), debugstr_a(lpszCat
), cchMax
);
1743 WARN("Invalid lpszStr would crash under Win32!\n");
1747 StrCpyNA(lpszStr
+ strlen(lpszStr
), lpszCat
, cchMax
);
1751 /*************************************************************************
1752 * StrNCatW [SHLWAPI.@]
1756 LPWSTR WINAPI
StrNCatW(LPWSTR lpszStr
, LPCWSTR lpszCat
, INT cchMax
)
1758 LPWSTR lpszRet
= lpszStr
;
1760 TRACE("(%s,%s,%i)\n", debugstr_w(lpszStr
), debugstr_w(lpszCat
), cchMax
);
1764 WARN("Invalid lpszStr would crash under Win32\n");
1768 StrCpyNW(lpszStr
+ strlenW(lpszStr
), lpszCat
, cchMax
);
1772 /*************************************************************************
1773 * StrTrimA [SHLWAPI.@]
1775 * Remove characters from the start and end of a string.
1778 * lpszStr [O] String to remove characters from
1779 * lpszTrim [I] Characters to remove from lpszStr
1782 * TRUE If lpszStr was valid and modified
1785 BOOL WINAPI
StrTrimA(LPSTR lpszStr
, LPCSTR lpszTrim
)
1788 LPSTR lpszRead
= lpszStr
;
1791 TRACE("(%s,%s)\n", debugstr_a(lpszStr
), debugstr_a(lpszTrim
));
1793 if (lpszRead
&& *lpszRead
)
1795 while (*lpszRead
&& StrChrA(lpszTrim
, *lpszRead
))
1796 lpszRead
= CharNextA(lpszRead
); /* Skip leading matches */
1798 dwLen
= strlen(lpszRead
);
1800 if (lpszRead
!= lpszStr
)
1802 memmove(lpszStr
, lpszRead
, dwLen
+ 1);
1807 lpszRead
= lpszStr
+ dwLen
;
1808 while (StrChrA(lpszTrim
, lpszRead
[-1]))
1809 lpszRead
= CharPrevA(lpszStr
, lpszRead
); /* Skip trailing matches */
1811 if (lpszRead
!= lpszStr
+ dwLen
)
1821 /*************************************************************************
1822 * StrTrimW [SHLWAPI.@]
1826 BOOL WINAPI
StrTrimW(LPWSTR lpszStr
, LPCWSTR lpszTrim
)
1829 LPWSTR lpszRead
= lpszStr
;
1832 TRACE("(%s,%s)\n", debugstr_w(lpszStr
), debugstr_w(lpszTrim
));
1834 if (lpszRead
&& *lpszRead
)
1836 while (*lpszRead
&& StrChrW(lpszTrim
, *lpszRead
)) lpszRead
++;
1838 dwLen
= strlenW(lpszRead
);
1840 if (lpszRead
!= lpszStr
)
1842 memmove(lpszStr
, lpszRead
, (dwLen
+ 1) * sizeof(WCHAR
));
1847 lpszRead
= lpszStr
+ dwLen
;
1848 while (StrChrW(lpszTrim
, lpszRead
[-1]))
1849 lpszRead
--; /* Skip trailing matches */
1851 if (lpszRead
!= lpszStr
+ dwLen
)
1861 /*************************************************************************
1862 * _SHStrDupAA [INTERNAL]
1864 * Duplicates a ASCII string to ASCII. The destination buffer is allocated.
1866 static HRESULT
_SHStrDupAA(LPCSTR src
, LPSTR
* dest
)
1872 len
= lstrlenA(src
) + 1;
1873 *dest
= CoTaskMemAlloc(len
);
1879 lstrcpynA(*dest
,src
, len
);
1885 TRACE("%s->(%p)\n", debugstr_a(src
), *dest
);
1889 /*************************************************************************
1890 * SHStrDupA [SHLWAPI.@]
1892 * Return a Unicode copy of a string, in memory allocated by CoTaskMemAlloc().
1895 * lpszStr [I] String to copy
1896 * lppszDest [O] Destination for the new string copy
1899 * Success: S_OK. lppszDest contains the new string in Unicode format.
1900 * Failure: E_OUTOFMEMORY, If any arguments are invalid or memory allocation
1903 HRESULT WINAPI
SHStrDupA(LPCSTR lpszStr
, LPWSTR
* lppszDest
)
1910 len
= MultiByteToWideChar(CP_ACP
, 0, lpszStr
, -1, NULL
, 0) * sizeof(WCHAR
);
1911 *lppszDest
= CoTaskMemAlloc(len
);
1918 MultiByteToWideChar(CP_ACP
, 0, lpszStr
, -1, *lppszDest
, len
/sizeof(WCHAR
));
1922 hRet
= E_OUTOFMEMORY
;
1924 TRACE("%s->(%p)\n", debugstr_a(lpszStr
), *lppszDest
);
1928 /*************************************************************************
1929 * _SHStrDupAW [INTERNAL]
1931 * Duplicates a UNICODE to a ASCII string. The destination buffer is allocated.
1933 static HRESULT
_SHStrDupAW(LPCWSTR src
, LPSTR
* dest
)
1939 len
= WideCharToMultiByte(CP_ACP
, 0, src
, -1, NULL
, 0, NULL
, NULL
);
1940 *dest
= CoTaskMemAlloc(len
);
1946 WideCharToMultiByte(CP_ACP
, 0, src
, -1, *dest
, len
, NULL
, NULL
);
1952 TRACE("%s->(%p)\n", debugstr_w(src
), *dest
);
1956 /*************************************************************************
1957 * SHStrDupW [SHLWAPI.@]
1961 HRESULT WINAPI
SHStrDupW(LPCWSTR src
, LPWSTR
* dest
)
1967 len
= (lstrlenW(src
) + 1) * sizeof(WCHAR
);
1968 *dest
= CoTaskMemAlloc(len
);
1974 memcpy(*dest
, src
, len
);
1980 TRACE("%s->(%p)\n", debugstr_w(src
), *dest
);
1984 /*************************************************************************
1985 * SHLWAPI_WriteReverseNum
1987 * Internal helper for SHLWAPI_WriteTimeClass.
1989 static inline LPWSTR
SHLWAPI_WriteReverseNum(LPWSTR lpszOut
, DWORD dwNum
)
1993 /* Write a decimal number to a string, backwards */
1996 DWORD dwNextDigit
= dwNum
% 10;
1997 *lpszOut
-- = '0' + dwNextDigit
;
1998 dwNum
= (dwNum
- dwNextDigit
) / 10;
1999 } while (dwNum
> 0);
2004 /*************************************************************************
2005 * SHLWAPI_FormatSignificant
2007 * Internal helper for SHLWAPI_WriteTimeClass.
2009 static inline int SHLWAPI_FormatSignificant(LPWSTR lpszNum
, int dwDigits
)
2011 /* Zero non significant digits, return remaining significant digits */
2015 if (--dwDigits
== 0)
2025 /*************************************************************************
2026 * SHLWAPI_WriteTimeClass
2028 * Internal helper for StrFromTimeIntervalW.
2030 static int SHLWAPI_WriteTimeClass(LPWSTR lpszOut
, DWORD dwValue
,
2031 UINT uClassStringId
, int iDigits
)
2033 WCHAR szBuff
[64], *szOut
= szBuff
+ 32;
2035 szOut
= SHLWAPI_WriteReverseNum(szOut
, dwValue
);
2036 iDigits
= SHLWAPI_FormatSignificant(szOut
+ 1, iDigits
);
2038 LoadStringW(shlwapi_hInstance
, uClassStringId
, szBuff
+ 32, 32);
2039 strcatW(lpszOut
, szOut
);
2043 /*************************************************************************
2044 * StrFromTimeIntervalA [SHLWAPI.@]
2046 * Format a millisecond time interval into a string
2049 * lpszStr [O] Output buffer for formatted time interval
2050 * cchMax [I] Size of lpszStr
2051 * dwMS [I] Number of milliseconds
2052 * iDigits [I] Number of digits to print
2055 * The length of the formatted string, or 0 if any parameter is invalid.
2058 * This implementation mimics the Win32 behaviour of always writing a leading
2059 * space before the time interval begins.
2061 * iDigits is used to provide approximate times if accuracy is not important.
2062 * This number of digits will be written of the first non-zero time class
2063 * (hours/minutes/seconds). If this does not complete the time classification,
2064 * the remaining digits are changed to zeros (i.e. The time is _not_ rounded).
2065 * If there are digits remaining following the writing of a time class, the
2066 * next time class will be written.
2068 * For example, given dwMS represents 138 hours,43 minutes and 15 seconds, the
2069 * following will result from the given values of iDigits:
2071 *| iDigits 1 2 3 4 5 ...
2072 *| lpszStr "100 hr" "130 hr" "138 hr" "138 hr 40 min" "138 hr 43 min" ...
2074 INT WINAPI
StrFromTimeIntervalA(LPSTR lpszStr
, UINT cchMax
, DWORD dwMS
,
2079 TRACE("(%p,%d,%d,%d)\n", lpszStr
, cchMax
, dwMS
, iDigits
);
2081 if (lpszStr
&& cchMax
)
2084 StrFromTimeIntervalW(szBuff
, sizeof(szBuff
)/sizeof(WCHAR
), dwMS
, iDigits
);
2085 WideCharToMultiByte(CP_ACP
,0,szBuff
,-1,lpszStr
,cchMax
,0,0);
2091 /*************************************************************************
2092 * StrFromTimeIntervalW [SHLWAPI.@]
2094 * See StrFromTimeIntervalA.
2096 INT WINAPI
StrFromTimeIntervalW(LPWSTR lpszStr
, UINT cchMax
, DWORD dwMS
,
2101 TRACE("(%p,%d,%d,%d)\n", lpszStr
, cchMax
, dwMS
, iDigits
);
2103 if (lpszStr
&& cchMax
)
2106 DWORD dwHours
, dwMinutes
;
2108 if (!iDigits
|| cchMax
== 1)
2114 /* Calculate the time classes */
2115 dwMS
= (dwMS
+ 500) / 1000;
2116 dwHours
= dwMS
/ 3600;
2117 dwMS
-= dwHours
* 3600;
2118 dwMinutes
= dwMS
/ 60;
2119 dwMS
-= dwMinutes
* 60;
2124 iDigits
= SHLWAPI_WriteTimeClass(szCopy
, dwHours
, IDS_TIME_INTERVAL_HOURS
, iDigits
);
2126 if (dwMinutes
&& iDigits
)
2127 iDigits
= SHLWAPI_WriteTimeClass(szCopy
, dwMinutes
, IDS_TIME_INTERVAL_MINUTES
, iDigits
);
2129 if (iDigits
) /* Always write seconds if we have significant digits */
2130 SHLWAPI_WriteTimeClass(szCopy
, dwMS
, IDS_TIME_INTERVAL_SECONDS
, iDigits
);
2132 lstrcpynW(lpszStr
, szCopy
, cchMax
);
2133 iRet
= strlenW(lpszStr
);
2138 /*************************************************************************
2139 * StrIsIntlEqualA [SHLWAPI.@]
2141 * Compare two strings.
2144 * bCase [I] Whether to compare case sensitively
2145 * lpszStr [I] First string to compare
2146 * lpszComp [I] Second string to compare
2147 * iLen [I] Length to compare
2150 * TRUE If the strings are equal.
2153 BOOL WINAPI
StrIsIntlEqualA(BOOL bCase
, LPCSTR lpszStr
, LPCSTR lpszComp
,
2158 TRACE("(%d,%s,%s,%d)\n", bCase
,
2159 debugstr_a(lpszStr
), debugstr_a(lpszComp
), iLen
);
2161 /* FIXME: This flag is undocumented and unknown by our CompareString.
2162 * We need a define for it.
2164 dwFlags
= 0x10000000;
2165 if (!bCase
) dwFlags
|= NORM_IGNORECASE
;
2167 return (CompareStringA(GetThreadLocale(), dwFlags
, lpszStr
, iLen
, lpszComp
, iLen
) == CSTR_EQUAL
);
2170 /*************************************************************************
2171 * StrIsIntlEqualW [SHLWAPI.@]
2173 * See StrIsIntlEqualA.
2175 BOOL WINAPI
StrIsIntlEqualW(BOOL bCase
, LPCWSTR lpszStr
, LPCWSTR lpszComp
,
2180 TRACE("(%d,%s,%s,%d)\n", bCase
,
2181 debugstr_w(lpszStr
),debugstr_w(lpszComp
), iLen
);
2183 /* FIXME: This flag is undocumented and unknown by our CompareString.
2184 * We need a define for it.
2186 dwFlags
= 0x10000000;
2187 if (!bCase
) dwFlags
|= NORM_IGNORECASE
;
2189 return (CompareStringW(GetThreadLocale(), dwFlags
, lpszStr
, iLen
, lpszComp
, iLen
) == CSTR_EQUAL
);
2192 /*************************************************************************
2195 * Copy a string to another string, up to a maximum number of characters.
2198 * lpszDest [O] Destination string
2199 * lpszSrc [I] Source string
2200 * iLen [I] Maximum number of chars to copy
2203 * Success: A pointer to the last character written to lpszDest.
2204 * Failure: lpszDest, if any arguments are invalid.
2206 LPSTR WINAPI
StrCpyNXA(LPSTR lpszDest
, LPCSTR lpszSrc
, int iLen
)
2208 TRACE("(%p,%s,%i)\n", lpszDest
, debugstr_a(lpszSrc
), iLen
);
2210 if (lpszDest
&& lpszSrc
&& iLen
> 0)
2212 while ((iLen
-- > 1) && *lpszSrc
)
2213 *lpszDest
++ = *lpszSrc
++;
2220 /*************************************************************************
2223 * Unicode version of StrCpyNXA.
2225 LPWSTR WINAPI
StrCpyNXW(LPWSTR lpszDest
, LPCWSTR lpszSrc
, int iLen
)
2227 TRACE("(%p,%s,%i)\n", lpszDest
, debugstr_w(lpszSrc
), iLen
);
2229 if (lpszDest
&& lpszSrc
&& iLen
> 0)
2231 while ((iLen
-- > 1) && *lpszSrc
)
2232 *lpszDest
++ = *lpszSrc
++;
2239 /*************************************************************************
2240 * StrCmpLogicalW [SHLWAPI.@]
2242 * Compare two strings, ignoring case and comparing digits as numbers.
2245 * lpszStr [I] First string to compare
2246 * lpszComp [I] Second string to compare
2247 * iLen [I] Length to compare
2250 * TRUE If the strings are equal.
2253 INT WINAPI
StrCmpLogicalW(LPCWSTR lpszStr
, LPCWSTR lpszComp
)
2257 TRACE("(%s,%s)\n", debugstr_w(lpszStr
), debugstr_w(lpszComp
));
2259 if (lpszStr
&& lpszComp
)
2265 else if (isdigitW(*lpszStr
))
2269 if (!isdigitW(*lpszComp
))
2272 /* Compare the numbers */
2273 StrToIntExW(lpszStr
, 0, &iStr
);
2274 StrToIntExW(lpszComp
, 0, &iComp
);
2278 else if (iStr
> iComp
)
2282 while (isdigitW(*lpszStr
))
2284 while (isdigitW(*lpszComp
))
2287 else if (isdigitW(*lpszComp
))
2291 iDiff
= ChrCmpIW(*lpszStr
,*lpszComp
);
2307 /* Structure for formatting byte strings */
2308 typedef struct tagSHLWAPI_BYTEFORMATS
2315 } SHLWAPI_BYTEFORMATS
;
2317 /*************************************************************************
2318 * StrFormatByteSizeW [SHLWAPI.@]
2320 * Create a string containing an abbreviated byte count of up to 2^63-1.
2323 * llBytes [I] Byte size to format
2324 * lpszDest [I] Destination for formatted string
2325 * cchMax [I] Size of lpszDest
2331 * There is no StrFormatByteSize64W function, it is called StrFormatByteSizeW().
2333 LPWSTR WINAPI
StrFormatByteSizeW(LONGLONG llBytes
, LPWSTR lpszDest
, UINT cchMax
)
2335 #define KB ((ULONGLONG)1024)
2337 #define GB (KB*KB*KB)
2338 #define TB (KB*KB*KB*KB)
2339 #define PB (KB*KB*KB*KB*KB)
2341 static const SHLWAPI_BYTEFORMATS bfFormats
[] =
2343 { 10*KB
, 10.24, 100.0, 2, 'K' }, /* 10 KB */
2344 { 100*KB
, 102.4, 10.0, 1, 'K' }, /* 100 KB */
2345 { 1000*KB
, 1024.0, 1.0, 0, 'K' }, /* 1000 KB */
2346 { 10*MB
, 10485.76, 100.0, 2, 'M' }, /* 10 MB */
2347 { 100*MB
, 104857.6, 10.0, 1, 'M' }, /* 100 MB */
2348 { 1000*MB
, 1048576.0, 1.0, 0, 'M' }, /* 1000 MB */
2349 { 10*GB
, 10737418.24, 100.0, 2, 'G' }, /* 10 GB */
2350 { 100*GB
, 107374182.4, 10.0, 1, 'G' }, /* 100 GB */
2351 { 1000*GB
, 1073741824.0, 1.0, 0, 'G' }, /* 1000 GB */
2352 { 10*TB
, 10485.76, 100.0, 2, 'T' }, /* 10 TB */
2353 { 100*TB
, 104857.6, 10.0, 1, 'T' }, /* 100 TB */
2354 { 1000*TB
, 1048576.0, 1.0, 0, 'T' }, /* 1000 TB */
2355 { 10*PB
, 10737418.24, 100.00, 2, 'P' }, /* 10 PB */
2356 { 100*PB
, 107374182.4, 10.00, 1, 'P' }, /* 100 PB */
2357 { 1000*PB
, 1073741824.0, 1.00, 0, 'P' }, /* 1000 PB */
2358 { 0, 10995116277.76, 100.00, 2, 'E' } /* EB's, catch all */
2360 WCHAR wszAdd
[] = {' ','?','B',0};
2364 TRACE("(0x%s,%p,%d)\n", wine_dbgstr_longlong(llBytes
), lpszDest
, cchMax
);
2366 if (!lpszDest
|| !cchMax
)
2369 if (llBytes
< 1024) /* 1K */
2371 WCHAR wszBytesFormat
[64];
2372 LoadStringW(shlwapi_hInstance
, IDS_BYTES_FORMAT
, wszBytesFormat
, 64);
2373 snprintfW(lpszDest
, cchMax
, wszBytesFormat
, (int)llBytes
);
2377 /* Note that if this loop completes without finding a match, i will be
2378 * pointing at the last entry, which is a catch all for > 1000 PB
2380 while (i
< sizeof(bfFormats
) / sizeof(SHLWAPI_BYTEFORMATS
) - 1)
2382 if (llBytes
< bfFormats
[i
].dLimit
)
2386 /* Above 1 TB we encounter problems with FP accuracy. So for amounts above
2387 * this number we integer shift down by 1 MB first. The table above has
2388 * the divisors scaled down from the '< 10 TB' entry onwards, to account
2389 * for this. We also add a small fudge factor to get the correct result for
2390 * counts that lie exactly on a 1024 byte boundary.
2393 dBytes
= (double)(llBytes
>> 20) + 0.001; /* Scale down by 1 MB */
2395 dBytes
= (double)llBytes
+ 0.00001;
2397 dBytes
= floor(dBytes
/ bfFormats
[i
].dDivisor
) / bfFormats
[i
].dNormaliser
;
2399 if (!FormatDouble(dBytes
, bfFormats
[i
].nDecimals
, lpszDest
, cchMax
))
2401 wszAdd
[1] = bfFormats
[i
].wPrefix
;
2402 StrCatBuffW(lpszDest
, wszAdd
, cchMax
);
2406 /*************************************************************************
2407 * StrFormatByteSize64A [SHLWAPI.@]
2409 * See StrFormatByteSizeW.
2411 LPSTR WINAPI
StrFormatByteSize64A(LONGLONG llBytes
, LPSTR lpszDest
, UINT cchMax
)
2415 StrFormatByteSizeW(llBytes
, wszBuff
, sizeof(wszBuff
)/sizeof(WCHAR
));
2418 WideCharToMultiByte(CP_ACP
, 0, wszBuff
, -1, lpszDest
, cchMax
, 0, 0);
2422 /*************************************************************************
2423 * StrFormatByteSizeA [SHLWAPI.@]
2425 * Create a string containing an abbreviated byte count of up to 2^31-1.
2428 * dwBytes [I] Byte size to format
2429 * lpszDest [I] Destination for formatted string
2430 * cchMax [I] Size of lpszDest
2436 * The Ascii and Unicode versions of this function accept a different
2437 * integer type for dwBytes. See StrFormatByteSize64A().
2439 LPSTR WINAPI
StrFormatByteSizeA(DWORD dwBytes
, LPSTR lpszDest
, UINT cchMax
)
2441 TRACE("(%d,%p,%d)\n", dwBytes
, lpszDest
, cchMax
);
2443 return StrFormatByteSize64A(dwBytes
, lpszDest
, cchMax
);
2446 /*************************************************************************
2449 * Remove a hanging lead byte from the end of a string, if present.
2452 * lpStr [I] String to check for a hanging lead byte
2453 * size [I] Length of lpStr
2456 * Success: The new length of the string. Any hanging lead bytes are removed.
2457 * Failure: 0, if any parameters are invalid.
2459 DWORD WINAPI
SHTruncateString(LPSTR lpStr
, DWORD size
)
2463 LPSTR lastByte
= lpStr
+ size
- 1;
2465 while(lpStr
< lastByte
)
2466 lpStr
+= IsDBCSLeadByte(*lpStr
) ? 2 : 1;
2468 if(lpStr
== lastByte
&& IsDBCSLeadByte(*lpStr
))
2478 /*************************************************************************
2481 * Remove a single non-trailing ampersand ('&') from a string.
2484 * lpszStr [I/O] String to remove ampersand from.
2487 * The character after the first ampersand in lpszStr, or the first character
2488 * in lpszStr if there is no ampersand in the string.
2490 char WINAPI
SHStripMneumonicA(LPCSTR lpszStr
)
2492 LPSTR lpszIter
, lpszTmp
;
2495 TRACE("(%s)\n", debugstr_a(lpszStr
));
2499 if ((lpszIter
= StrChrA(lpszStr
, '&')))
2501 lpszTmp
= CharNextA(lpszIter
);
2504 if (*lpszTmp
!= '&')
2507 memmove( lpszIter
, lpszTmp
, strlen(lpszTmp
) + 1 );
2514 /*************************************************************************
2517 * Unicode version of SHStripMneumonicA.
2519 WCHAR WINAPI
SHStripMneumonicW(LPCWSTR lpszStr
)
2521 LPWSTR lpszIter
, lpszTmp
;
2524 TRACE("(%s)\n", debugstr_w(lpszStr
));
2528 if ((lpszIter
= StrChrW(lpszStr
, '&')))
2530 lpszTmp
= lpszIter
+ 1;
2533 if (*lpszTmp
!= '&')
2536 memmove( lpszIter
, lpszTmp
, (strlenW(lpszTmp
) + 1) * sizeof(WCHAR
) );
2543 /*************************************************************************
2546 * Convert an Ascii string to Unicode.
2549 * dwCp [I] Code page for the conversion
2550 * lpSrcStr [I] Source Ascii string to convert
2551 * lpDstStr [O] Destination for converted Unicode string
2552 * iLen [I] Length of lpDstStr
2555 * The return value of the MultiByteToWideChar() function called on lpSrcStr.
2557 DWORD WINAPI
SHAnsiToUnicodeCP(DWORD dwCp
, LPCSTR lpSrcStr
, LPWSTR lpDstStr
, int iLen
)
2561 dwRet
= MultiByteToWideChar(dwCp
, 0, lpSrcStr
, -1, lpDstStr
, iLen
);
2562 TRACE("%s->%s,ret=%d\n", debugstr_a(lpSrcStr
), debugstr_w(lpDstStr
), dwRet
);
2566 /*************************************************************************
2569 * Convert an Ascii string to Unicode.
2572 * lpSrcStr [I] Source Ascii string to convert
2573 * lpDstStr [O] Destination for converted Unicode string
2574 * iLen [I] Length of lpDstStr
2577 * The return value of the MultiByteToWideChar() function called on lpSrcStr.
2580 * This function simply calls SHAnsiToUnicodeCP with code page CP_ACP.
2582 DWORD WINAPI
SHAnsiToUnicode(LPCSTR lpSrcStr
, LPWSTR lpDstStr
, int iLen
)
2584 return SHAnsiToUnicodeCP(CP_ACP
, lpSrcStr
, lpDstStr
, iLen
);
2587 /*************************************************************************
2590 * Convert a Unicode string to Ascii.
2593 * CodePage [I] Code page to use for the conversion
2594 * lpSrcStr [I] Source Unicode string to convert
2595 * lpDstStr [O] Destination for converted Ascii string
2596 * dstlen [I] Length of buffer at lpDstStr
2599 * Success: The length in bytes of the result at lpDstStr (including the terminator)
2600 * Failure: When using CP_UTF8, CP_UTF7 or 0xc350 as codePage, 0 is returned and
2601 * the result is not nul-terminated.
2602 * When using a different codepage, the length in bytes of the truncated
2603 * result at lpDstStr (including the terminator) is returned and
2604 * lpDstStr is always nul-terminated.
2607 DWORD WINAPI
SHUnicodeToAnsiCP(UINT CodePage
, LPCWSTR lpSrcStr
, LPSTR lpDstStr
, int dstlen
)
2609 static const WCHAR emptyW
[] = { '\0' };
2613 if (!lpDstStr
|| !dstlen
)
2621 len
= strlenW(lpSrcStr
) + 1;
2626 CodePage
= CP_UTF8
; /* Fall through... */
2627 case 0x0000C350: /* FIXME: CP_ #define */
2633 INT needed
= dstlen
- 1;
2636 /* try the user supplied buffer first */
2637 hr
= ConvertINetUnicodeToMultiByte(&dwMode
, CodePage
, lpSrcStr
, &lenW
, lpDstStr
, &needed
);
2640 lpDstStr
[needed
] = '\0';
2644 /* user buffer too small. exclude termination and copy as much as possible */
2646 hr
= ConvertINetUnicodeToMultiByte(&dwMode
, CodePage
, lpSrcStr
, &lenW
, NULL
, &needed
);
2648 mem
= HeapAlloc(GetProcessHeap(), 0, needed
);
2652 hr
= ConvertINetUnicodeToMultiByte(&dwMode
, CodePage
, lpSrcStr
, &len
, mem
, &needed
);
2655 reqLen
= SHTruncateString(mem
, dstlen
);
2656 if (reqLen
> 0) memcpy(lpDstStr
, mem
, reqLen
-1);
2658 HeapFree(GetProcessHeap(), 0, mem
);
2665 /* try the user supplied buffer first */
2666 reqLen
= WideCharToMultiByte(CodePage
, 0, lpSrcStr
, len
, lpDstStr
, dstlen
, NULL
, NULL
);
2668 if (!reqLen
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
2670 reqLen
= WideCharToMultiByte(CodePage
, 0, lpSrcStr
, len
, NULL
, 0, NULL
, NULL
);
2673 mem
= HeapAlloc(GetProcessHeap(), 0, reqLen
);
2676 reqLen
= WideCharToMultiByte(CodePage
, 0, lpSrcStr
, len
, mem
,
2677 reqLen
, NULL
, NULL
);
2679 reqLen
= SHTruncateString(mem
, dstlen
-1);
2682 lstrcpynA(lpDstStr
, mem
, reqLen
);
2683 HeapFree(GetProcessHeap(), 0, mem
);
2684 lpDstStr
[reqLen
-1] = '\0';
2691 /*************************************************************************
2694 * Convert a Unicode string to Ascii.
2697 * lpSrcStr [I] Source Unicode string to convert
2698 * lpDstStr [O] Destination for converted Ascii string
2699 * iLen [O] Length of lpDstStr in characters
2702 * See SHUnicodeToAnsiCP
2705 * This function simply calls SHUnicodeToAnsiCP() with CodePage = CP_ACP.
2707 INT WINAPI
SHUnicodeToAnsi(LPCWSTR lpSrcStr
, LPSTR lpDstStr
, INT iLen
)
2709 return SHUnicodeToAnsiCP(CP_ACP
, lpSrcStr
, lpDstStr
, iLen
);
2712 /*************************************************************************
2715 * Copy one string to another.
2718 * lpszSrc [I] Source string to copy
2719 * lpszDst [O] Destination for copy
2720 * iLen [I] Length of lpszDst in characters
2723 * The length of the copied string, including the terminating NUL. lpszDst
2724 * contains iLen characters of lpszSrc.
2726 DWORD WINAPI
SHAnsiToAnsi(LPCSTR lpszSrc
, LPSTR lpszDst
, int iLen
)
2730 TRACE("(%s,%p,0x%08x)\n", debugstr_a(lpszSrc
), lpszDst
, iLen
);
2732 lpszRet
= StrCpyNXA(lpszDst
, lpszSrc
, iLen
);
2733 return lpszRet
- lpszDst
+ 1;
2736 /*************************************************************************
2739 * Unicode version of SSHAnsiToAnsi.
2741 DWORD WINAPI
SHUnicodeToUnicode(LPCWSTR lpszSrc
, LPWSTR lpszDst
, int iLen
)
2745 TRACE("(%s,%p,0x%08x)\n", debugstr_w(lpszSrc
), lpszDst
, iLen
);
2747 lpszRet
= StrCpyNXW(lpszDst
, lpszSrc
, iLen
);
2748 return lpszRet
- lpszDst
+ 1;
2751 /*************************************************************************
2754 * Determine if an Ascii string converts to Unicode and back identically.
2757 * lpSrcStr [I] Source Unicode string to convert
2758 * lpDst [O] Destination for resulting Ascii string
2759 * iLen [I] Length of lpDst in characters
2762 * TRUE, since Ascii strings always convert identically.
2764 BOOL WINAPI
DoesStringRoundTripA(LPCSTR lpSrcStr
, LPSTR lpDst
, INT iLen
)
2766 lstrcpynA(lpDst
, lpSrcStr
, iLen
);
2770 /*************************************************************************
2773 * Determine if a Unicode string converts to Ascii and back identically.
2776 * lpSrcStr [I] Source Unicode string to convert
2777 * lpDst [O] Destination for resulting Ascii string
2778 * iLen [I] Length of lpDst in characters
2781 * TRUE, if lpSrcStr converts to Ascii and back identically,
2784 BOOL WINAPI
DoesStringRoundTripW(LPCWSTR lpSrcStr
, LPSTR lpDst
, INT iLen
)
2786 WCHAR szBuff
[MAX_PATH
];
2788 SHUnicodeToAnsi(lpSrcStr
, lpDst
, iLen
);
2789 SHAnsiToUnicode(lpDst
, szBuff
, MAX_PATH
);
2790 return !strcmpW(lpSrcStr
, szBuff
);
2793 /*************************************************************************
2794 * SHLoadIndirectString [SHLWAPI.@]
2796 * If passed a string that begins with '@', extract the string from the
2797 * appropriate resource, otherwise do a straight copy.
2800 HRESULT WINAPI
SHLoadIndirectString(LPCWSTR src
, LPWSTR dst
, UINT dst_len
, void **reserved
)
2802 WCHAR
*dllname
= NULL
;
2803 HMODULE hmod
= NULL
;
2804 HRESULT hr
= E_FAIL
;
2806 TRACE("(%s %p %08x %p)\n", debugstr_w(src
), dst
, dst_len
, reserved
);
2814 dllname
= StrDupW(src
+ 1);
2815 index_str
= strchrW(dllname
, ',');
2817 if(!index_str
) goto end
;
2821 index
= atoiW(index_str
);
2823 hmod
= LoadLibraryW(dllname
);
2828 if(LoadStringW(hmod
, -index
, dst
, dst_len
))
2832 FIXME("can't handle non-negative indices (%d)\n", index
);
2837 lstrcpynW(dst
, src
, dst_len
);
2841 TRACE("returning %s\n", debugstr_w(dst
));
2843 if(hmod
) FreeLibrary(hmod
);
2848 BOOL WINAPI
IsCharSpaceA(CHAR c
)
2851 return GetStringTypeA(GetSystemDefaultLCID(), CT_CTYPE1
, &c
, 1, &CharType
) && (CharType
& C1_SPACE
);
2854 /*************************************************************************
2857 * Determine if a Unicode character is a space.
2860 * wc [I] Character to check.
2863 * TRUE, if wc is a space,
2866 BOOL WINAPI
IsCharSpaceW(WCHAR wc
)
2870 return GetStringTypeW(CT_CTYPE1
, &wc
, 1, &CharType
) && (CharType
& C1_SPACE
);