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 bytes 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 bytes 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) - 2;
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) - 2;
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)
357 TRACE("(%s,%s)\n", debugstr_w(lpszStr),debugstr_w(lpszComp));
359 iRet = CompareStringW(GetThreadLocale(), NORM_IGNORECASE, lpszStr, -1, lpszComp, -1);
360 return iRet == CSTR_LESS_THAN ? -1 : iRet == CSTR_GREATER_THAN ? 1 : 0;
363 /*************************************************************************
364 * StrCmpNA [SHLWAPI.@]
366 * Compare two strings, up to a maximum length.
369 * lpszStr [I] First string to compare
370 * lpszComp [I] Second string to compare
371 * iLen [I] Maximum number of chars to compare.
374 * An integer less than, equal to or greater than 0, indicating that
375 * lpszStr is less than, the same, or greater than lpszComp.
377 INT WINAPI StrCmpNA(LPCSTR lpszStr, LPCSTR lpszComp, INT iLen)
381 TRACE("(%s,%s,%i)\n", debugstr_a(lpszStr), debugstr_a(lpszComp), iLen);
383 iRet = CompareStringA(GetThreadLocale(), 0, lpszStr, iLen, lpszComp, iLen);
384 return iRet == CSTR_LESS_THAN ? -1 : iRet == CSTR_GREATER_THAN ? 1 : 0;
387 /*************************************************************************
388 * StrCmpNW [SHLWAPI.@]
392 INT WINAPI StrCmpNW(LPCWSTR lpszStr, LPCWSTR lpszComp, INT iLen)
396 TRACE("(%s,%s,%i)\n", debugstr_w(lpszStr), debugstr_w(lpszComp), iLen);
398 iRet = CompareStringW(GetThreadLocale(), 0, lpszStr, iLen, lpszComp, iLen);
399 return iRet == CSTR_LESS_THAN ? -1 : iRet == CSTR_GREATER_THAN ? 1 : 0;
402 /*************************************************************************
403 * StrCmpNIA [SHLWAPI.@]
405 * Compare two strings, up to a maximum length, ignoring case.
408 * lpszStr [I] First string to compare
409 * lpszComp [I] Second string to compare
410 * iLen [I] Maximum number of chars to compare.
413 * An integer less than, equal to or greater than 0, indicating that
414 * lpszStr is less than, the same, or greater than lpszComp.
416 int WINAPI StrCmpNIA(LPCSTR lpszStr, LPCSTR lpszComp, int iLen)
420 TRACE("(%s,%s,%i)\n", debugstr_a(lpszStr), debugstr_a(lpszComp), iLen);
422 iRet = CompareStringA(GetThreadLocale(), NORM_IGNORECASE, lpszStr, iLen, lpszComp, iLen);
423 return iRet == CSTR_LESS_THAN ? -1 : iRet == CSTR_GREATER_THAN ? 1 : 0;
426 /*************************************************************************
427 * StrCmpNIW [SHLWAPI.@]
431 INT WINAPI StrCmpNIW(LPCWSTR lpszStr, LPCWSTR lpszComp, int iLen)
435 TRACE("(%s,%s,%i)\n", debugstr_w(lpszStr), debugstr_w(lpszComp), iLen);
437 iRet = CompareStringW(GetThreadLocale(), NORM_IGNORECASE, lpszStr, iLen, lpszComp, iLen);
438 return iRet == CSTR_LESS_THAN ? -1 : iRet == CSTR_GREATER_THAN ? 1 : 0;
441 /*************************************************************************
442 * StrCmpW [SHLWAPI.@]
444 * Compare two strings.
447 * lpszStr [I] First string to compare
448 * lpszComp [I] Second string to compare
451 * An integer less than, equal to or greater than 0, indicating that
452 * lpszStr is less than, the same, or greater than lpszComp.
454 int WINAPI StrCmpW(LPCWSTR lpszStr, LPCWSTR lpszComp)
458 TRACE("(%s,%s)\n", debugstr_w(lpszStr), debugstr_w(lpszComp));
460 iRet = CompareStringW(GetThreadLocale(), 0, lpszStr, -1, lpszComp, -1);
461 return iRet == CSTR_LESS_THAN ? -1 : iRet == CSTR_GREATER_THAN ? 1 : 0;
464 /*************************************************************************
465 * StrCatW [SHLWAPI.@]
467 * Concatenate two strings.
470 * lpszStr [O] Initial string
471 * lpszSrc [I] String to concatenate
476 LPWSTR WINAPI StrCatW(LPWSTR lpszStr, LPCWSTR lpszSrc)
478 TRACE("(%s,%s)\n", debugstr_w(lpszStr), debugstr_w(lpszSrc));
480 strcatW(lpszStr, lpszSrc);
484 /*************************************************************************
485 * StrCpyW [SHLWAPI.@]
487 * Copy a string to another string.
490 * lpszStr [O] Destination string
491 * lpszSrc [I] Source string
496 LPWSTR WINAPI StrCpyW(LPWSTR lpszStr, LPCWSTR lpszSrc)
498 TRACE("(%p,%s)\n", lpszStr, debugstr_w(lpszSrc));
500 strcpyW(lpszStr, lpszSrc);
504 /*************************************************************************
505 * StrCpyNW [SHLWAPI.@]
507 * Copy a string to another string, up to a maximum number of characters.
510 * lpszStr [O] Destination string
511 * lpszSrc [I] Source string
512 * iLen [I] Maximum number of chars to copy
517 LPWSTR WINAPI StrCpyNW(LPWSTR lpszStr, LPCWSTR lpszSrc, int iLen)
519 TRACE("(%p,%s,%i)\n", lpszStr, debugstr_w(lpszSrc), iLen);
521 lstrcpynW(lpszStr, lpszSrc, iLen);
527 /*************************************************************************
528 * SHLWAPI_StrStrHelperA
530 * Internal implementation of StrStrA/StrStrIA
532 static LPSTR SHLWAPI_StrStrHelperA(LPCSTR lpszStr, LPCSTR lpszSearch,
533 INT (WINAPI *pStrCmpFn)(LPCSTR,LPCSTR,INT))
537 if (!lpszStr || !lpszSearch || !*lpszSearch)
540 iLen = strlen(lpszSearch);
544 if (!pStrCmpFn(lpszStr, lpszSearch, iLen))
545 return (LPSTR)lpszStr;
546 lpszStr = CharNextA(lpszStr);
551 /*************************************************************************
552 * StrStrA [SHLWAPI.@]
554 * Find a substring within a string.
557 * lpszStr [I] String to search in
558 * lpszSearch [I] String to look for
561 * The start of lpszSearch within lpszStr, or NULL if not found.
563 LPSTR WINAPI StrStrA(LPCSTR lpszStr, LPCSTR lpszSearch)
565 TRACE("(%s,%s)\n", debugstr_a(lpszStr), debugstr_a(lpszSearch));
567 return SHLWAPI_StrStrHelperA(lpszStr, lpszSearch, StrCmpNA);
570 /*************************************************************************
571 * StrStrW [SHLWAPI.@]
575 LPWSTR WINAPI StrStrW(LPCWSTR lpszStr, LPCWSTR lpszSearch)
577 if (!lpszStr || !lpszSearch) return NULL;
578 return strstrW( lpszStr, lpszSearch );
581 /*************************************************************************
582 * StrRStrIA [SHLWAPI.@]
584 * Find the last occurrence of a substring within a string.
587 * lpszStr [I] String to search in
588 * lpszEnd [I] End of lpszStr
589 * lpszSearch [I] String to look for
592 * The last occurrence lpszSearch within lpszStr, or NULL if not found.
594 LPSTR WINAPI StrRStrIA(LPCSTR lpszStr, LPCSTR lpszEnd, LPCSTR lpszSearch)
599 TRACE("(%s,%s)\n", debugstr_a(lpszStr), debugstr_a(lpszSearch));
601 if (!lpszStr || !lpszSearch || !*lpszSearch)
605 lpszEnd = lpszStr + lstrlenA(lpszStr);
606 if (lpszEnd == lpszStr)
609 if (IsDBCSLeadByte(*lpszSearch))
610 ch1 = *lpszSearch << 8 | (UCHAR)lpszSearch[1];
613 iLen = lstrlenA(lpszSearch);
617 lpszEnd = CharPrevA(lpszStr, lpszEnd);
618 ch2 = IsDBCSLeadByte(*lpszEnd)? *lpszEnd << 8 | (UCHAR)lpszEnd[1] : *lpszEnd;
619 if (!ChrCmpIA(ch1, ch2))
621 if (!StrCmpNIA(lpszEnd, lpszSearch, iLen))
622 return (LPSTR)lpszEnd;
624 } while (lpszEnd > lpszStr);
628 /*************************************************************************
629 * StrRStrIW [SHLWAPI.@]
633 LPWSTR WINAPI StrRStrIW(LPCWSTR lpszStr, LPCWSTR lpszEnd, LPCWSTR lpszSearch)
637 TRACE("(%s,%s)\n", debugstr_w(lpszStr), debugstr_w(lpszSearch));
639 if (!lpszStr || !lpszSearch || !*lpszSearch)
643 lpszEnd = lpszStr + strlenW(lpszStr);
645 iLen = strlenW(lpszSearch);
647 while (lpszEnd > lpszStr)
650 if (!StrCmpNIW(lpszEnd, lpszSearch, iLen))
651 return (LPWSTR)lpszEnd;
656 /*************************************************************************
657 * StrStrIA [SHLWAPI.@]
659 * Find a substring within a string, ignoring case.
662 * lpszStr [I] String to search in
663 * lpszSearch [I] String to look for
666 * The start of lpszSearch within lpszStr, or NULL if not found.
668 LPSTR WINAPI StrStrIA(LPCSTR lpszStr, LPCSTR lpszSearch)
670 TRACE("(%s,%s)\n", debugstr_a(lpszStr), debugstr_a(lpszSearch));
672 return SHLWAPI_StrStrHelperA(lpszStr, lpszSearch, StrCmpNIA);
675 /*************************************************************************
676 * StrStrIW [SHLWAPI.@]
680 LPWSTR WINAPI StrStrIW(LPCWSTR lpszStr, LPCWSTR lpszSearch)
684 TRACE("(%s,%s)\n", debugstr_w(lpszStr), debugstr_w(lpszSearch));
686 if (!lpszStr || !lpszSearch || !*lpszSearch)
689 iLen = strlenW(lpszSearch);
693 if (!StrCmpNIW(lpszStr, lpszSearch, iLen))
694 return (LPWSTR)lpszStr;
700 /*************************************************************************
701 * StrToIntA [SHLWAPI.@]
703 * Read a signed integer from a string.
706 * lpszStr [I] String to read integer from
709 * The signed integer value represented by the string, or 0 if no integer is
713 * No leading space is allowed before the number, although a leading '-' is.
715 int WINAPI StrToIntA(LPCSTR lpszStr)
719 TRACE("(%s)\n", debugstr_a(lpszStr));
723 WARN("Invalid lpszStr would crash under Win32!\n");
727 if (*lpszStr == '-' || isdigit(*lpszStr))
728 StrToIntExA(lpszStr, 0, &iRet);
732 /*************************************************************************
733 * StrToIntW [SHLWAPI.@]
737 int WINAPI StrToIntW(LPCWSTR lpszStr)
741 TRACE("(%s)\n", debugstr_w(lpszStr));
745 WARN("Invalid lpszStr would crash under Win32!\n");
749 if (*lpszStr == '-' || isdigitW(*lpszStr))
750 StrToIntExW(lpszStr, 0, &iRet);
754 /*************************************************************************
755 * StrToIntExA [SHLWAPI.@]
757 * Read an integer from a string.
760 * lpszStr [I] String to read integer from
761 * dwFlags [I] Flags controlling the conversion
762 * lpiRet [O] Destination for read integer.
765 * Success: TRUE. lpiRet contains the integer value represented by the string.
766 * Failure: FALSE, if the string is invalid, or no number is present.
769 * Leading whitespace, '-' and '+' are allowed before the number. If
770 * dwFlags includes STIF_SUPPORT_HEX, hexadecimal numbers are allowed, if
771 * preceded by '0x'. If this flag is not set, or there is no '0x' prefix,
772 * the string is treated as a decimal string. A leading '-' is ignored for
773 * hexadecimal numbers.
775 BOOL WINAPI StrToIntExA(LPCSTR lpszStr, DWORD dwFlags, LPINT lpiRet)
777 BOOL bNegative = FALSE;
780 TRACE("(%s,%08X,%p)\n", debugstr_a(lpszStr), dwFlags, lpiRet);
782 if (!lpszStr || !lpiRet)
784 WARN("Invalid parameter would crash under Win32!\n");
787 if (dwFlags > STIF_SUPPORT_HEX)
789 WARN("Unknown flags (%08lX)!\n", dwFlags & ~STIF_SUPPORT_HEX);
792 /* Skip leading space, '+', '-' */
793 while (isspace(*lpszStr))
794 lpszStr = CharNextA(lpszStr);
801 else if (*lpszStr == '+')
804 if (dwFlags & STIF_SUPPORT_HEX &&
805 *lpszStr == '0' && tolower(lpszStr[1]) == 'x')
807 /* Read hex number */
810 if (!isxdigit(*lpszStr))
813 while (isxdigit(*lpszStr))
816 if (isdigit(*lpszStr))
817 iRet += (*lpszStr - '0');
819 iRet += 10 + (tolower(*lpszStr) - 'a');
826 /* Read decimal number */
827 if (!isdigit(*lpszStr))
830 while (isdigit(*lpszStr))
833 iRet += (*lpszStr - '0');
836 *lpiRet = bNegative ? -iRet : iRet;
840 /*************************************************************************
841 * StrToIntExW [SHLWAPI.@]
845 BOOL WINAPI StrToIntExW(LPCWSTR lpszStr, DWORD dwFlags, LPINT lpiRet)
847 BOOL bNegative = FALSE;
850 TRACE("(%s,%08X,%p)\n", debugstr_w(lpszStr), dwFlags, lpiRet);
852 if (!lpszStr || !lpiRet)
854 WARN("Invalid parameter would crash under Win32!\n");
857 if (dwFlags > STIF_SUPPORT_HEX)
859 WARN("Unknown flags (%08lX)!\n", dwFlags & ~STIF_SUPPORT_HEX);
862 /* Skip leading space, '+', '-' */
863 while (isspaceW(*lpszStr)) lpszStr++;
870 else if (*lpszStr == '+')
873 if (dwFlags & STIF_SUPPORT_HEX &&
874 *lpszStr == '0' && tolowerW(lpszStr[1]) == 'x')
876 /* Read hex number */
879 if (!isxdigitW(*lpszStr))
882 while (isxdigitW(*lpszStr))
885 if (isdigitW(*lpszStr))
886 iRet += (*lpszStr - '0');
888 iRet += 10 + (tolowerW(*lpszStr) - 'a');
895 /* Read decimal number */
896 if (!isdigitW(*lpszStr))
899 while (isdigitW(*lpszStr))
902 iRet += (*lpszStr - '0');
905 *lpiRet = bNegative ? -iRet : iRet;
909 /*************************************************************************
910 * StrDupA [SHLWAPI.@]
912 * Duplicate a string.
915 * lpszStr [I] String to duplicate.
918 * Success: A pointer to a new string containing the contents of lpszStr
919 * Failure: NULL, if memory cannot be allocated
922 * The string memory is allocated with LocalAlloc(), and so should be released
923 * by calling LocalFree().
925 LPSTR WINAPI StrDupA(LPCSTR lpszStr)
930 TRACE("(%s)\n",debugstr_a(lpszStr));
932 iLen = lpszStr ? strlen(lpszStr) + 1 : 1;
933 lpszRet = LocalAlloc(LMEM_FIXED, iLen);
938 memcpy(lpszRet, lpszStr, iLen);
945 /*************************************************************************
946 * StrDupW [SHLWAPI.@]
950 LPWSTR WINAPI StrDupW(LPCWSTR lpszStr)
955 TRACE("(%s)\n",debugstr_w(lpszStr));
957 iLen = (lpszStr ? strlenW(lpszStr) + 1 : 1) * sizeof(WCHAR);
958 lpszRet = LocalAlloc(LMEM_FIXED, iLen);
963 memcpy(lpszRet, lpszStr, iLen);
970 /*************************************************************************
971 * SHLWAPI_StrSpnHelperA
973 * Internal implementation of StrSpnA/StrCSpnA/StrCSpnIA
975 static int SHLWAPI_StrSpnHelperA(LPCSTR lpszStr, LPCSTR lpszMatch,
976 LPSTR (WINAPI *pStrChrFn)(LPCSTR,WORD),
979 LPCSTR lpszRead = lpszStr;
980 if (lpszStr && *lpszStr && lpszMatch)
984 LPCSTR lpszTest = pStrChrFn(lpszMatch, *lpszRead);
986 if (!bInvert && !lpszTest)
988 if (bInvert && lpszTest)
990 lpszRead = CharNextA(lpszRead);
993 return lpszRead - lpszStr;
996 /*************************************************************************
997 * StrSpnA [SHLWAPI.@]
999 * Find the length of the start of a string that contains only certain
1003 * lpszStr [I] String to search
1004 * lpszMatch [I] Characters that can be in the substring
1007 * The length of the part of lpszStr containing only chars from lpszMatch,
1008 * or 0 if any parameter is invalid.
1010 int WINAPI StrSpnA(LPCSTR lpszStr, LPCSTR lpszMatch)
1012 TRACE("(%s,%s)\n",debugstr_a(lpszStr), debugstr_a(lpszMatch));
1014 return SHLWAPI_StrSpnHelperA(lpszStr, lpszMatch, StrChrA, FALSE);
1017 /*************************************************************************
1018 * StrSpnW [SHLWAPI.@]
1022 int WINAPI StrSpnW(LPCWSTR lpszStr, LPCWSTR lpszMatch)
1024 if (!lpszStr || !lpszMatch) return 0;
1025 return strspnW( lpszStr, lpszMatch );
1028 /*************************************************************************
1029 * StrCSpnA [SHLWAPI.@]
1031 * Find the length of the start of a string that does not contain certain
1035 * lpszStr [I] String to search
1036 * lpszMatch [I] Characters that cannot be in the substring
1039 * The length of the part of lpszStr containing only chars not in lpszMatch,
1040 * or 0 if any parameter is invalid.
1042 int WINAPI StrCSpnA(LPCSTR lpszStr, LPCSTR lpszMatch)
1044 TRACE("(%s,%s)\n",debugstr_a(lpszStr), debugstr_a(lpszMatch));
1046 return SHLWAPI_StrSpnHelperA(lpszStr, lpszMatch, StrChrA, TRUE);
1049 /*************************************************************************
1050 * StrCSpnW [SHLWAPI.@]
1054 int WINAPI StrCSpnW(LPCWSTR lpszStr, LPCWSTR lpszMatch)
1056 if (!lpszStr || !lpszMatch) return 0;
1057 return strcspnW( lpszStr, lpszMatch );
1060 /*************************************************************************
1061 * StrCSpnIA [SHLWAPI.@]
1063 * Find the length of the start of a string that does not contain certain
1064 * characters, ignoring case.
1067 * lpszStr [I] String to search
1068 * lpszMatch [I] Characters that cannot be in the substring
1071 * The length of the part of lpszStr containing only chars not in lpszMatch,
1072 * or 0 if any parameter is invalid.
1074 int WINAPI StrCSpnIA(LPCSTR lpszStr, LPCSTR lpszMatch)
1076 TRACE("(%s,%s)\n",debugstr_a(lpszStr), debugstr_a(lpszMatch));
1078 return SHLWAPI_StrSpnHelperA(lpszStr, lpszMatch, StrChrIA, TRUE);
1081 /*************************************************************************
1082 * StrCSpnIW [SHLWAPI.@]
1086 int WINAPI StrCSpnIW(LPCWSTR lpszStr, LPCWSTR lpszMatch)
1088 LPCWSTR lpszRead = lpszStr;
1090 TRACE("(%s,%s)\n",debugstr_w(lpszStr), debugstr_w(lpszMatch));
1092 if (lpszStr && *lpszStr && lpszMatch)
1096 if (StrChrIW(lpszMatch, *lpszRead)) break;
1100 return lpszRead - lpszStr;
1103 /*************************************************************************
1104 * StrPBrkA [SHLWAPI.@]
1106 * Search a string for any of a group of characters.
1109 * lpszStr [I] String to search
1110 * lpszMatch [I] Characters to match
1113 * A pointer to the first matching character in lpszStr, or NULL if no
1116 LPSTR WINAPI StrPBrkA(LPCSTR lpszStr, LPCSTR lpszMatch)
1118 TRACE("(%s,%s)\n",debugstr_a(lpszStr), debugstr_a(lpszMatch));
1120 if (lpszStr && lpszMatch && *lpszMatch)
1124 if (StrChrA(lpszMatch, *lpszStr))
1125 return (LPSTR)lpszStr;
1126 lpszStr = CharNextA(lpszStr);
1132 /*************************************************************************
1133 * StrPBrkW [SHLWAPI.@]
1137 LPWSTR WINAPI StrPBrkW(LPCWSTR lpszStr, LPCWSTR lpszMatch)
1139 if (!lpszStr || !lpszMatch) return NULL;
1140 return strpbrkW( lpszStr, lpszMatch );
1143 /*************************************************************************
1144 * SHLWAPI_StrRChrHelperA
1146 * Internal implementation of StrRChrA/StrRChrIA.
1148 static LPSTR SHLWAPI_StrRChrHelperA(LPCSTR lpszStr,
1149 LPCSTR lpszEnd, WORD ch,
1150 BOOL (WINAPI *pChrCmpFn)(WORD,WORD))
1152 LPCSTR lpszRet = NULL;
1159 lpszEnd = lpszStr + lstrlenA(lpszStr);
1161 while (*lpszStr && lpszStr <= lpszEnd)
1163 ch2 = IsDBCSLeadByte(*lpszStr)? *lpszStr << 8 | lpszStr[1] : *lpszStr;
1165 if (!pChrCmpFn(ch, ch2))
1167 lpszStr = CharNextA(lpszStr);
1170 return (LPSTR)lpszRet;
1173 /**************************************************************************
1174 * StrRChrA [SHLWAPI.@]
1176 * Find the last occurrence of a character in string.
1179 * lpszStr [I] String to search in
1180 * lpszEnd [I] Place to end search, or NULL to search until the end of lpszStr
1181 * ch [I] Character to search for.
1184 * Success: A pointer to the last occurrence of ch in lpszStr before lpszEnd,
1185 * or NULL if not found.
1186 * Failure: NULL, if any arguments are invalid.
1188 LPSTR WINAPI StrRChrA(LPCSTR lpszStr, LPCSTR lpszEnd, WORD ch)
1190 TRACE("(%s,%s,%x)\n", debugstr_a(lpszStr), debugstr_a(lpszEnd), ch);
1192 return SHLWAPI_StrRChrHelperA(lpszStr, lpszEnd, ch, SHLWAPI_ChrCmpA);
1195 /**************************************************************************
1196 * StrRChrW [SHLWAPI.@]
1200 LPWSTR WINAPI StrRChrW(LPCWSTR str, LPCWSTR end, WORD ch)
1204 if (!str) return NULL;
1205 if (!end) end = str + strlenW(str);
1208 if (*str == ch) ret = (WCHAR *)str;
1214 /**************************************************************************
1215 * StrRChrIA [SHLWAPI.@]
1217 * Find the last occurrence of a character in string, ignoring case.
1220 * lpszStr [I] String to search in
1221 * lpszEnd [I] Place to end search, or NULL to search until the end of lpszStr
1222 * ch [I] Character to search for.
1225 * Success: A pointer to the last occurrence of ch in lpszStr before lpszEnd,
1226 * or NULL if not found.
1227 * Failure: NULL, if any arguments are invalid.
1229 LPSTR WINAPI StrRChrIA(LPCSTR lpszStr, LPCSTR lpszEnd, WORD ch)
1231 TRACE("(%s,%s,%x)\n", debugstr_a(lpszStr), debugstr_a(lpszEnd), ch);
1233 return SHLWAPI_StrRChrHelperA(lpszStr, lpszEnd, ch, ChrCmpIA);
1236 /**************************************************************************
1237 * StrRChrIW [SHLWAPI.@]
1241 LPWSTR WINAPI StrRChrIW(LPCWSTR str, LPCWSTR end, WORD ch)
1245 if (!str) return NULL;
1246 if (!end) end = str + strlenW(str);
1249 if (!ChrCmpIW(*str, ch)) ret = (WCHAR *)str;
1255 /*************************************************************************
1256 * StrCatBuffA [SHLWAPI.@]
1258 * Concatenate two strings together.
1261 * lpszStr [O] String to concatenate to
1262 * lpszCat [I] String to add to lpszCat
1263 * cchMax [I] Maximum number of characters for the whole string
1269 * cchMax determines the number of characters in the final length of the
1270 * string, not the number appended to lpszStr from lpszCat.
1272 LPSTR WINAPI StrCatBuffA(LPSTR lpszStr, LPCSTR lpszCat, INT cchMax)
1276 TRACE("(%p,%s,%d)\n", lpszStr, debugstr_a(lpszCat), cchMax);
1280 WARN("Invalid lpszStr would crash under Win32!\n");
1284 iLen = strlen(lpszStr);
1288 StrCpyNA(lpszStr + iLen, lpszCat, cchMax);
1292 /*************************************************************************
1293 * StrCatBuffW [SHLWAPI.@]
1297 LPWSTR WINAPI StrCatBuffW(LPWSTR lpszStr, LPCWSTR lpszCat, INT cchMax)
1301 TRACE("(%p,%s,%d)\n", lpszStr, debugstr_w(lpszCat), cchMax);
1305 WARN("Invalid lpszStr would crash under Win32!\n");
1309 iLen = strlenW(lpszStr);
1313 StrCpyNW(lpszStr + iLen, lpszCat, cchMax);
1317 /*************************************************************************
1318 * StrRetToBufA [SHLWAPI.@]
1320 * Convert a STRRET to a normal string.
1323 * lpStrRet [O] STRRET to convert
1324 * pIdl [I] ITEMIDLIST for lpStrRet->uType == STRRET_OFFSET
1325 * lpszDest [O] Destination for normal string
1326 * dwLen [I] Length of lpszDest
1329 * Success: S_OK. lpszDest contains up to dwLen characters of the string.
1330 * If lpStrRet is of type STRRET_WSTR, its memory is freed with
1331 * CoTaskMemFree() and its type set to STRRET_CSTRA.
1332 * Failure: E_FAIL, if any parameters are invalid.
1334 HRESULT WINAPI StrRetToBufA (LPSTRRET src, const ITEMIDLIST *pidl, LPSTR dest, UINT len)
1337 * This routine is identical to that in dlls/shell32/shellstring.c.
1338 * It was duplicated because not every version of Shlwapi.dll exports
1339 * StrRetToBufA. If you change one routine, change them both.
1341 TRACE("dest=%p len=0x%x strret=%p pidl=%p stub\n",dest,len,src,pidl);
1345 WARN("Invalid lpStrRet would crash under Win32!\n");
1359 WideCharToMultiByte(CP_ACP, 0, src->u.pOleStr, -1, dest, len, NULL, NULL);
1360 CoTaskMemFree(src->u.pOleStr);
1364 lstrcpynA(dest, src->u.cStr, len);
1368 lstrcpynA((LPSTR)dest, ((LPCSTR)&pidl->mkid)+src->u.uOffset, len);
1372 FIXME("unknown type!\n");
1378 /*************************************************************************
1379 * StrRetToBufW [SHLWAPI.@]
1383 HRESULT WINAPI StrRetToBufW (LPSTRRET src, const ITEMIDLIST *pidl, LPWSTR dest, UINT len)
1385 TRACE("dest=%p len=0x%x strret=%p pidl=%p stub\n",dest,len,src,pidl);
1389 WARN("Invalid lpStrRet would crash under Win32!\n");
1403 lstrcpynW(dest, src->u.pOleStr, len);
1404 CoTaskMemFree(src->u.pOleStr);
1408 if (!MultiByteToWideChar( CP_ACP, 0, src->u.cStr, -1, dest, len ) && len)
1415 if (!MultiByteToWideChar( CP_ACP, 0, ((LPCSTR)&pidl->mkid)+src->u.uOffset, -1,
1422 FIXME("unknown type!\n");
1428 /*************************************************************************
1429 * StrRetToStrA [SHLWAPI.@]
1431 * Converts a STRRET to a normal string.
1434 * lpStrRet [O] STRRET to convert
1435 * pidl [I] ITEMIDLIST for lpStrRet->uType == STRRET_OFFSET
1436 * ppszName [O] Destination for converted string
1439 * Success: S_OK. ppszName contains the new string, allocated with CoTaskMemAlloc().
1440 * Failure: E_FAIL, if any parameters are invalid.
1442 HRESULT WINAPI StrRetToStrA(LPSTRRET lpStrRet, const ITEMIDLIST *pidl, LPSTR *ppszName)
1444 HRESULT hRet = E_FAIL;
1446 switch (lpStrRet->uType)
1449 hRet = _SHStrDupAW(lpStrRet->u.pOleStr, ppszName);
1450 CoTaskMemFree(lpStrRet->u.pOleStr);
1454 hRet = _SHStrDupAA(lpStrRet->u.cStr, ppszName);
1458 hRet = _SHStrDupAA(((LPCSTR)&pidl->mkid) + lpStrRet->u.uOffset, ppszName);
1468 /*************************************************************************
1469 * StrRetToStrW [SHLWAPI.@]
1473 HRESULT WINAPI StrRetToStrW(LPSTRRET lpStrRet, const ITEMIDLIST *pidl, LPWSTR *ppszName)
1475 HRESULT hRet = E_FAIL;
1477 switch (lpStrRet->uType)
1480 hRet = SHStrDupW(lpStrRet->u.pOleStr, ppszName);
1481 CoTaskMemFree(lpStrRet->u.pOleStr);
1485 hRet = SHStrDupA(lpStrRet->u.cStr, ppszName);
1489 hRet = SHStrDupA(((LPCSTR)&pidl->mkid) + lpStrRet->u.uOffset, ppszName);
1499 /* Create an ASCII string copy using SysAllocString() */
1500 static HRESULT _SHStrDupAToBSTR(LPCSTR src, BSTR *pBstrOut)
1506 INT len = MultiByteToWideChar(CP_ACP, 0, src, -1, NULL, 0);
1507 WCHAR* szTemp = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1511 MultiByteToWideChar(CP_ACP, 0, src, -1, szTemp, len);
1512 *pBstrOut = SysAllocString(szTemp);
1513 HeapFree(GetProcessHeap(), 0, szTemp);
1519 return E_OUTOFMEMORY;
1522 /*************************************************************************
1523 * StrRetToBSTR [SHLWAPI.@]
1525 * Converts a STRRET to a BSTR.
1528 * lpStrRet [O] STRRET to convert
1529 * pidl [I] ITEMIDLIST for lpStrRet->uType = STRRET_OFFSET
1530 * pBstrOut [O] Destination for converted BSTR
1533 * Success: S_OK. pBstrOut contains the new string.
1534 * Failure: E_FAIL, if any parameters are invalid.
1536 HRESULT WINAPI StrRetToBSTR(STRRET *lpStrRet, LPCITEMIDLIST pidl, BSTR* pBstrOut)
1538 HRESULT hRet = E_FAIL;
1540 switch (lpStrRet->uType)
1543 *pBstrOut = SysAllocString(lpStrRet->u.pOleStr);
1546 CoTaskMemFree(lpStrRet->u.pOleStr);
1550 hRet = _SHStrDupAToBSTR(lpStrRet->u.cStr, pBstrOut);
1554 hRet = _SHStrDupAToBSTR(((LPCSTR)&pidl->mkid) + lpStrRet->u.uOffset, pBstrOut);
1564 /*************************************************************************
1565 * StrFormatKBSizeA [SHLWAPI.@]
1567 * Create a formatted string containing a byte count in Kilobytes.
1570 * llBytes [I] Byte size to format
1571 * lpszDest [I] Destination for formatted string
1572 * cchMax [I] Size of lpszDest
1577 LPSTR WINAPI StrFormatKBSizeA(LONGLONG llBytes, LPSTR lpszDest, UINT cchMax)
1581 if (!StrFormatKBSizeW(llBytes, wszBuf, 256))
1583 if (!WideCharToMultiByte(CP_ACP, 0, wszBuf, -1, lpszDest, cchMax, NULL, NULL))
1588 /*************************************************************************
1589 * StrFormatKBSizeW [SHLWAPI.@]
1591 * See StrFormatKBSizeA.
1593 LPWSTR WINAPI StrFormatKBSizeW(LONGLONG llBytes, LPWSTR lpszDest, UINT cchMax)
1595 static const WCHAR kb[] = {' ','K','B',0};
1596 LONGLONG llKB = (llBytes + 1023) >> 10;
1599 TRACE("(0x%s,%p,%d)\n", wine_dbgstr_longlong(llBytes), lpszDest, cchMax);
1601 if (!FormatInt(llKB, lpszDest, cchMax))
1604 len = lstrlenW(lpszDest);
1605 if (cchMax - len < 4)
1607 lstrcatW(lpszDest, kb);
1611 /*************************************************************************
1612 * StrNCatA [SHLWAPI.@]
1614 * Concatenate two strings together.
1617 * lpszStr [O] String to concatenate to
1618 * lpszCat [I] String to add to lpszCat
1619 * cchMax [I] Maximum number of characters to concatenate
1625 * cchMax determines the number of characters that are appended to lpszStr,
1626 * not the total length of the string.
1628 LPSTR WINAPI StrNCatA(LPSTR lpszStr, LPCSTR lpszCat, INT cchMax)
1630 LPSTR lpszRet = lpszStr;
1632 TRACE("(%s,%s,%i)\n", debugstr_a(lpszStr), debugstr_a(lpszCat), cchMax);
1636 WARN("Invalid lpszStr would crash under Win32!\n");
1640 StrCpyNA(lpszStr + strlen(lpszStr), lpszCat, cchMax);
1644 /*************************************************************************
1645 * StrNCatW [SHLWAPI.@]
1649 LPWSTR WINAPI StrNCatW(LPWSTR lpszStr, LPCWSTR lpszCat, INT cchMax)
1651 LPWSTR lpszRet = lpszStr;
1653 TRACE("(%s,%s,%i)\n", debugstr_w(lpszStr), debugstr_w(lpszCat), cchMax);
1657 WARN("Invalid lpszStr would crash under Win32\n");
1661 StrCpyNW(lpszStr + strlenW(lpszStr), lpszCat, cchMax);
1665 /*************************************************************************
1666 * StrTrimA [SHLWAPI.@]
1668 * Remove characters from the start and end of a string.
1671 * lpszStr [O] String to remove characters from
1672 * lpszTrim [I] Characters to remove from lpszStr
1675 * TRUE If lpszStr was valid and modified
1678 BOOL WINAPI StrTrimA(LPSTR lpszStr, LPCSTR lpszTrim)
1681 LPSTR lpszRead = lpszStr;
1684 TRACE("(%s,%s)\n", debugstr_a(lpszStr), debugstr_a(lpszTrim));
1686 if (lpszRead && *lpszRead)
1688 while (*lpszRead && StrChrA(lpszTrim, *lpszRead))
1689 lpszRead = CharNextA(lpszRead); /* Skip leading matches */
1691 dwLen = strlen(lpszRead);
1693 if (lpszRead != lpszStr)
1695 memmove(lpszStr, lpszRead, dwLen + 1);
1700 lpszRead = lpszStr + dwLen;
1701 while (StrChrA(lpszTrim, lpszRead[-1]))
1702 lpszRead = CharPrevA(lpszStr, lpszRead); /* Skip trailing matches */
1704 if (lpszRead != lpszStr + dwLen)
1714 /*************************************************************************
1715 * StrTrimW [SHLWAPI.@]
1719 BOOL WINAPI StrTrimW(LPWSTR lpszStr, LPCWSTR lpszTrim)
1722 LPWSTR lpszRead = lpszStr;
1725 TRACE("(%s,%s)\n", debugstr_w(lpszStr), debugstr_w(lpszTrim));
1727 if (lpszRead && *lpszRead)
1729 while (*lpszRead && StrChrW(lpszTrim, *lpszRead)) lpszRead++;
1731 dwLen = strlenW(lpszRead);
1733 if (lpszRead != lpszStr)
1735 memmove(lpszStr, lpszRead, (dwLen + 1) * sizeof(WCHAR));
1740 lpszRead = lpszStr + dwLen;
1741 while (StrChrW(lpszTrim, lpszRead[-1]))
1742 lpszRead--; /* Skip trailing matches */
1744 if (lpszRead != lpszStr + dwLen)
1754 /*************************************************************************
1755 * _SHStrDupAA [INTERNAL]
1757 * Duplicates a ASCII string to ASCII. The destination buffer is allocated.
1759 static HRESULT _SHStrDupAA(LPCSTR src, LPSTR * dest)
1765 len = lstrlenA(src) + 1;
1766 *dest = CoTaskMemAlloc(len);
1772 lstrcpynA(*dest,src, len);
1778 TRACE("%s->(%p)\n", debugstr_a(src), *dest);
1782 /*************************************************************************
1783 * SHStrDupA [SHLWAPI.@]
1785 * Return a Unicode copy of a string, in memory allocated by CoTaskMemAlloc().
1788 * lpszStr [I] String to copy
1789 * lppszDest [O] Destination for the new string copy
1792 * Success: S_OK. lppszDest contains the new string in Unicode format.
1793 * Failure: E_OUTOFMEMORY, If any arguments are invalid or memory allocation
1796 HRESULT WINAPI SHStrDupA(LPCSTR lpszStr, LPWSTR * lppszDest)
1803 len = MultiByteToWideChar(0, 0, lpszStr, -1, 0, 0) * sizeof(WCHAR);
1804 *lppszDest = CoTaskMemAlloc(len);
1811 MultiByteToWideChar(0, 0, lpszStr, -1, *lppszDest, len/sizeof(WCHAR));
1815 hRet = E_OUTOFMEMORY;
1817 TRACE("%s->(%p)\n", debugstr_a(lpszStr), *lppszDest);
1821 /*************************************************************************
1822 * _SHStrDupAW [INTERNAL]
1824 * Duplicates a UNICODE to a ASCII string. The destination buffer is allocated.
1826 static HRESULT _SHStrDupAW(LPCWSTR src, LPSTR * dest)
1832 len = WideCharToMultiByte(CP_ACP, 0, src, -1, NULL, 0, NULL, NULL);
1833 *dest = CoTaskMemAlloc(len);
1839 WideCharToMultiByte(CP_ACP, 0, src, -1, *dest, len, NULL, NULL);
1845 TRACE("%s->(%p)\n", debugstr_w(src), *dest);
1849 /*************************************************************************
1850 * SHStrDupW [SHLWAPI.@]
1854 HRESULT WINAPI SHStrDupW(LPCWSTR src, LPWSTR * dest)
1860 len = (lstrlenW(src) + 1) * sizeof(WCHAR);
1861 *dest = CoTaskMemAlloc(len);
1867 memcpy(*dest, src, len);
1873 TRACE("%s->(%p)\n", debugstr_w(src), *dest);
1877 /*************************************************************************
1878 * SHLWAPI_WriteReverseNum
1880 * Internal helper for SHLWAPI_WriteTimeClass.
1882 static inline LPWSTR SHLWAPI_WriteReverseNum(LPWSTR lpszOut, DWORD dwNum)
1886 /* Write a decimal number to a string, backwards */
1889 DWORD dwNextDigit = dwNum % 10;
1890 *lpszOut-- = '0' + dwNextDigit;
1891 dwNum = (dwNum - dwNextDigit) / 10;
1892 } while (dwNum > 0);
1897 /*************************************************************************
1898 * SHLWAPI_FormatSignificant
1900 * Internal helper for SHLWAPI_WriteTimeClass.
1902 static inline int SHLWAPI_FormatSignificant(LPWSTR lpszNum, int dwDigits)
1904 /* Zero non significant digits, return remaining significant digits */
1908 if (--dwDigits == 0)
1918 /*************************************************************************
1919 * SHLWAPI_WriteTimeClass
1921 * Internal helper for StrFromTimeIntervalW.
1923 static int SHLWAPI_WriteTimeClass(LPWSTR lpszOut, DWORD dwValue,
1924 UINT uClassStringId, int iDigits)
1926 WCHAR szBuff[64], *szOut = szBuff + 32;
1928 szOut = SHLWAPI_WriteReverseNum(szOut, dwValue);
1929 iDigits = SHLWAPI_FormatSignificant(szOut + 1, iDigits);
1931 LoadStringW(shlwapi_hInstance, uClassStringId, szBuff + 32, 32);
1932 strcatW(lpszOut, szOut);
1936 /*************************************************************************
1937 * StrFromTimeIntervalA [SHLWAPI.@]
1939 * Format a millisecond time interval into a string
1942 * lpszStr [O] Output buffer for formatted time interval
1943 * cchMax [I] Size of lpszStr
1944 * dwMS [I] Number of milliseconds
1945 * iDigits [I] Number of digits to print
1948 * The length of the formatted string, or 0 if any parameter is invalid.
1951 * This implementation mimics the Win32 behaviour of always writing a leading
1952 * space before the time interval begins.
1954 * iDigits is used to provide approximate times if accuracy is not important.
1955 * This number of digits will be written of the first non-zero time class
1956 * (hours/minutes/seconds). If this does not complete the time classification,
1957 * the remaining digits are changed to zeros (i.e. The time is _not_ rounded).
1958 * If there are digits remaining following the writing of a time class, the
1959 * next time class will be written.
1961 * For example, given dwMS represents 138 hours,43 minutes and 15 seconds, the
1962 * following will result from the given values of iDigits:
1964 *| iDigits 1 2 3 4 5 ...
1965 *| lpszStr "100 hr" "130 hr" "138 hr" "138 hr 40 min" "138 hr 43 min" ...
1967 INT WINAPI StrFromTimeIntervalA(LPSTR lpszStr, UINT cchMax, DWORD dwMS,
1972 TRACE("(%p,%d,%d,%d)\n", lpszStr, cchMax, dwMS, iDigits);
1974 if (lpszStr && cchMax)
1977 StrFromTimeIntervalW(szBuff, sizeof(szBuff)/sizeof(WCHAR), dwMS, iDigits);
1978 WideCharToMultiByte(CP_ACP,0,szBuff,-1,lpszStr,cchMax,0,0);
1984 /*************************************************************************
1985 * StrFromTimeIntervalW [SHLWAPI.@]
1987 * See StrFromTimeIntervalA.
1989 INT WINAPI StrFromTimeIntervalW(LPWSTR lpszStr, UINT cchMax, DWORD dwMS,
1994 TRACE("(%p,%d,%d,%d)\n", lpszStr, cchMax, dwMS, iDigits);
1996 if (lpszStr && cchMax)
1999 DWORD dwHours, dwMinutes;
2001 if (!iDigits || cchMax == 1)
2007 /* Calculate the time classes */
2008 dwMS = (dwMS + 500) / 1000;
2009 dwHours = dwMS / 3600;
2010 dwMS -= dwHours * 3600;
2011 dwMinutes = dwMS / 60;
2012 dwMS -= dwMinutes * 60;
2017 iDigits = SHLWAPI_WriteTimeClass(szCopy, dwHours, IDS_TIME_INTERVAL_HOURS, iDigits);
2019 if (dwMinutes && iDigits)
2020 iDigits = SHLWAPI_WriteTimeClass(szCopy, dwMinutes, IDS_TIME_INTERVAL_MINUTES, iDigits);
2022 if (iDigits) /* Always write seconds if we have significant digits */
2023 SHLWAPI_WriteTimeClass(szCopy, dwMS, IDS_TIME_INTERVAL_SECONDS, iDigits);
2025 lstrcpynW(lpszStr, szCopy, cchMax);
2026 iRet = strlenW(lpszStr);
2031 /*************************************************************************
2032 * StrIsIntlEqualA [SHLWAPI.@]
2034 * Compare two strings.
2037 * bCase [I] Whether to compare case sensitively
2038 * lpszStr [I] First string to compare
2039 * lpszComp [I] Second string to compare
2040 * iLen [I] Length to compare
2043 * TRUE If the strings are equal.
2046 BOOL WINAPI StrIsIntlEqualA(BOOL bCase, LPCSTR lpszStr, LPCSTR lpszComp,
2051 TRACE("(%d,%s,%s,%d)\n", bCase,
2052 debugstr_a(lpszStr), debugstr_a(lpszComp), iLen);
2054 /* FIXME: This flag is undocumented and unknown by our CompareString.
2055 * We need a define for it.
2057 dwFlags = 0x10000000;
2058 if (!bCase) dwFlags |= NORM_IGNORECASE;
2060 return (CompareStringA(GetThreadLocale(), dwFlags, lpszStr, iLen, lpszComp, iLen) == CSTR_EQUAL);
2063 /*************************************************************************
2064 * StrIsIntlEqualW [SHLWAPI.@]
2066 * See StrIsIntlEqualA.
2068 BOOL WINAPI StrIsIntlEqualW(BOOL bCase, LPCWSTR lpszStr, LPCWSTR lpszComp,
2073 TRACE("(%d,%s,%s,%d)\n", bCase,
2074 debugstr_w(lpszStr),debugstr_w(lpszComp), iLen);
2076 /* FIXME: This flag is undocumented and unknown by our CompareString.
2077 * We need a define for it.
2079 dwFlags = 0x10000000;
2080 if (!bCase) dwFlags |= NORM_IGNORECASE;
2082 return (CompareStringW(GetThreadLocale(), dwFlags, lpszStr, iLen, lpszComp, iLen) == CSTR_EQUAL);
2085 /*************************************************************************
2088 * Copy a string to another string, up to a maximum number of characters.
2091 * lpszDest [O] Destination string
2092 * lpszSrc [I] Source string
2093 * iLen [I] Maximum number of chars to copy
2096 * Success: A pointer to the last character written to lpszDest.
2097 * Failure: lpszDest, if any arguments are invalid.
2099 LPSTR WINAPI StrCpyNXA(LPSTR lpszDest, LPCSTR lpszSrc, int iLen)
2101 TRACE("(%p,%s,%i)\n", lpszDest, debugstr_a(lpszSrc), iLen);
2103 if (lpszDest && lpszSrc && iLen > 0)
2105 while ((iLen-- > 1) && *lpszSrc)
2106 *lpszDest++ = *lpszSrc++;
2113 /*************************************************************************
2116 * Unicode version of StrCpyNXA.
2118 LPWSTR WINAPI StrCpyNXW(LPWSTR lpszDest, LPCWSTR lpszSrc, int iLen)
2120 TRACE("(%p,%s,%i)\n", lpszDest, debugstr_w(lpszSrc), iLen);
2122 if (lpszDest && lpszSrc && iLen > 0)
2124 while ((iLen-- > 1) && *lpszSrc)
2125 *lpszDest++ = *lpszSrc++;
2132 /*************************************************************************
2133 * StrCmpLogicalW [SHLWAPI.@]
2135 * Compare two strings, ignoring case and comparing digits as numbers.
2138 * lpszStr [I] First string to compare
2139 * lpszComp [I] Second string to compare
2140 * iLen [I] Length to compare
2143 * TRUE If the strings are equal.
2146 INT WINAPI StrCmpLogicalW(LPCWSTR lpszStr, LPCWSTR lpszComp)
2150 TRACE("(%s,%s)\n", debugstr_w(lpszStr), debugstr_w(lpszComp));
2152 if (lpszStr && lpszComp)
2158 else if (isdigitW(*lpszStr))
2162 if (!isdigitW(*lpszComp))
2165 /* Compare the numbers */
2166 StrToIntExW(lpszStr, 0, &iStr);
2167 StrToIntExW(lpszComp, 0, &iComp);
2171 else if (iStr > iComp)
2175 while (isdigitW(*lpszStr))
2177 while (isdigitW(*lpszComp))
2180 else if (isdigitW(*lpszComp))
2184 iDiff = ChrCmpIW(*lpszStr,*lpszComp);
2200 /* Structure for formatting byte strings */
2201 typedef struct tagSHLWAPI_BYTEFORMATS
2208 } SHLWAPI_BYTEFORMATS;
2210 /*************************************************************************
2211 * StrFormatByteSizeW [SHLWAPI.@]
2213 * Create a string containing an abbreviated byte count of up to 2^63-1.
2216 * llBytes [I] Byte size to format
2217 * lpszDest [I] Destination for formatted string
2218 * cchMax [I] Size of lpszDest
2224 * There is no StrFormatByteSize64W function, it is called StrFormatByteSizeW().
2226 LPWSTR WINAPI StrFormatByteSizeW(LONGLONG llBytes, LPWSTR lpszDest, UINT cchMax)
2228 #define KB ((ULONGLONG)1024)
2230 #define GB (KB*KB*KB)
2231 #define TB (KB*KB*KB*KB)
2232 #define PB (KB*KB*KB*KB*KB)
2234 static const SHLWAPI_BYTEFORMATS bfFormats[] =
2236 { 10*KB, 10.24, 100.0, 2, 'K' }, /* 10 KB */
2237 { 100*KB, 102.4, 10.0, 1, 'K' }, /* 100 KB */
2238 { 1000*KB, 1024.0, 1.0, 0, 'K' }, /* 1000 KB */
2239 { 10*MB, 10485.76, 100.0, 2, 'M' }, /* 10 MB */
2240 { 100*MB, 104857.6, 10.0, 1, 'M' }, /* 100 MB */
2241 { 1000*MB, 1048576.0, 1.0, 0, 'M' }, /* 1000 MB */
2242 { 10*GB, 10737418.24, 100.0, 2, 'G' }, /* 10 GB */
2243 { 100*GB, 107374182.4, 10.0, 1, 'G' }, /* 100 GB */
2244 { 1000*GB, 1073741824.0, 1.0, 0, 'G' }, /* 1000 GB */
2245 { 10*TB, 10485.76, 100.0, 2, 'T' }, /* 10 TB */
2246 { 100*TB, 104857.6, 10.0, 1, 'T' }, /* 100 TB */
2247 { 1000*TB, 1048576.0, 1.0, 0, 'T' }, /* 1000 TB */
2248 { 10*PB, 10737418.24, 100.00, 2, 'P' }, /* 10 PB */
2249 { 100*PB, 107374182.4, 10.00, 1, 'P' }, /* 100 PB */
2250 { 1000*PB, 1073741824.0, 1.00, 0, 'P' }, /* 1000 PB */
2251 { 0, 10995116277.76, 100.00, 2, 'E' } /* EB's, catch all */
2253 WCHAR wszAdd[] = {' ','?','B',0};
2257 TRACE("(0x%s,%p,%d)\n", wine_dbgstr_longlong(llBytes), lpszDest, cchMax);
2259 if (!lpszDest || !cchMax)
2262 if (llBytes < 1024) /* 1K */
2264 WCHAR wszBytesFormat[64];
2265 LoadStringW(shlwapi_hInstance, IDS_BYTES_FORMAT, wszBytesFormat, 64);
2266 snprintfW(lpszDest, cchMax, wszBytesFormat, (long)llBytes);
2270 /* Note that if this loop completes without finding a match, i will be
2271 * pointing at the last entry, which is a catch all for > 1000 PB
2273 while (i < sizeof(bfFormats) / sizeof(SHLWAPI_BYTEFORMATS) - 1)
2275 if (llBytes < bfFormats[i].dLimit)
2279 /* Above 1 TB we encounter problems with FP accuracy. So for amounts above
2280 * this number we integer shift down by 1 MB first. The table above has
2281 * the divisors scaled down from the '< 10 TB' entry onwards, to account
2282 * for this. We also add a small fudge factor to get the correct result for
2283 * counts that lie exactly on a 1024 byte boundary.
2286 dBytes = (double)(llBytes >> 20) + 0.001; /* Scale down by I MB */
2288 dBytes = (double)llBytes + 0.00001;
2290 dBytes = floor(dBytes / bfFormats[i].dDivisor) / bfFormats[i].dNormaliser;
2292 if (!FormatDouble(dBytes, bfFormats[i].nDecimals, lpszDest, cchMax))
2294 wszAdd[1] = bfFormats[i].wPrefix;
2295 StrCatBuffW(lpszDest, wszAdd, cchMax);
2299 /*************************************************************************
2300 * StrFormatByteSize64A [SHLWAPI.@]
2302 * See StrFormatByteSizeW.
2304 LPSTR WINAPI StrFormatByteSize64A(LONGLONG llBytes, LPSTR lpszDest, UINT cchMax)
2308 StrFormatByteSizeW(llBytes, wszBuff, sizeof(wszBuff)/sizeof(WCHAR));
2311 WideCharToMultiByte(CP_ACP, 0, wszBuff, -1, lpszDest, cchMax, 0, 0);
2315 /*************************************************************************
2316 * StrFormatByteSizeA [SHLWAPI.@]
2318 * Create a string containing an abbreviated byte count of up to 2^31-1.
2321 * dwBytes [I] Byte size to format
2322 * lpszDest [I] Destination for formatted string
2323 * cchMax [I] Size of lpszDest
2329 * The Ascii and Unicode versions of this function accept a different
2330 * integer type for dwBytes. See StrFormatByteSize64A().
2332 LPSTR WINAPI StrFormatByteSizeA(DWORD dwBytes, LPSTR lpszDest, UINT cchMax)
2334 TRACE("(%d,%p,%d)\n", dwBytes, lpszDest, cchMax);
2336 return StrFormatByteSize64A(dwBytes, lpszDest, cchMax);
2339 /*************************************************************************
2342 * Remove a hanging lead byte from the end of a string, if present.
2345 * lpStr [I] String to check for a hanging lead byte
2346 * size [I] Length of lpStr
2349 * Success: The new length of the string. Any hanging lead bytes are removed.
2350 * Failure: 0, if any parameters are invalid.
2352 DWORD WINAPI SHTruncateString(LPSTR lpStr, DWORD size)
2356 LPSTR lastByte = lpStr + size - 1;
2358 while(lpStr < lastByte)
2359 lpStr += IsDBCSLeadByte(*lpStr) ? 2 : 1;
2361 if(lpStr == lastByte && IsDBCSLeadByte(*lpStr))
2371 /*************************************************************************
2374 * Remove a single non-trailing ampersand ('&') from a string.
2377 * lpszStr [I/O] String to remove ampersand from.
2380 * The character after the first ampersand in lpszStr, or the first character
2381 * in lpszStr if there is no ampersand in the string.
2383 char WINAPI SHStripMneumonicA(LPCSTR lpszStr)
2385 LPSTR lpszIter, lpszTmp;
2388 TRACE("(%s)\n", debugstr_a(lpszStr));
2392 if ((lpszIter = StrChrA(lpszStr, '&')))
2394 lpszTmp = CharNextA(lpszIter);
2395 if (lpszTmp && *lpszTmp)
2397 if (*lpszTmp != '&')
2400 while (lpszIter && *lpszIter)
2402 lpszTmp = CharNextA(lpszIter);
2403 *lpszIter = *lpszTmp;
2412 /*************************************************************************
2415 * Unicode version of SHStripMneumonicA.
2417 WCHAR WINAPI SHStripMneumonicW(LPCWSTR lpszStr)
2419 LPWSTR lpszIter, lpszTmp;
2422 TRACE("(%s)\n", debugstr_w(lpszStr));
2426 if ((lpszIter = StrChrW(lpszStr, '&')))
2428 lpszTmp = lpszIter + 1;
2429 if (lpszTmp && *lpszTmp)
2431 if (*lpszTmp != '&')
2434 while (lpszIter && *lpszIter)
2436 lpszTmp = lpszIter + 1;
2437 *lpszIter = *lpszTmp;
2446 /*************************************************************************
2449 * Convert an Ascii string to Unicode.
2452 * dwCp [I] Code page for the conversion
2453 * lpSrcStr [I] Source Ascii string to convert
2454 * lpDstStr [O] Destination for converted Unicode string
2455 * iLen [I] Length of lpDstStr
2458 * The return value of the MultiByteToWideChar() function called on lpSrcStr.
2460 DWORD WINAPI SHAnsiToUnicodeCP(DWORD dwCp, LPCSTR lpSrcStr, LPWSTR lpDstStr, int iLen)
2464 dwRet = MultiByteToWideChar(dwCp, 0, lpSrcStr, -1, lpDstStr, iLen);
2465 TRACE("%s->%s,ret=%d\n", debugstr_a(lpSrcStr), debugstr_w(lpDstStr), dwRet);
2469 /*************************************************************************
2472 * Convert an Ascii string to Unicode.
2475 * lpSrcStr [I] Source Ascii string to convert
2476 * lpDstStr [O] Destination for converted Unicode string
2477 * iLen [I] Length of lpDstStr
2480 * The return value of the MultiByteToWideChar() function called on lpSrcStr.
2483 * This function simply calls SHAnsiToUnicodeCP with code page CP_ACP.
2485 DWORD WINAPI SHAnsiToUnicode(LPCSTR lpSrcStr, LPWSTR lpDstStr, int iLen)
2487 return SHAnsiToUnicodeCP(CP_ACP, lpSrcStr, lpDstStr, iLen);
2490 /*************************************************************************
2493 * Convert a Unicode string to Ascii.
2496 * CodePage [I] Code page to use for the conversion
2497 * lpSrcStr [I] Source Unicode string to convert
2498 * lpDstStr [O] Destination for converted Ascii string
2499 * dstlen [I] Length of buffer at lpDstStr
2502 * Success: The length in bytes of the result at lpDstStr (including the terminator)
2503 * Failure: When using CP_UTF8, CP_UTF7 or 0xc350 as codePage, 0 is returned and
2504 * the result is not nul-terminated.
2505 * When using a different codepage, the length in bytes of the truncated
2506 * result at lpDstStr (including the terminator) is returned and
2507 * lpDstStr is always nul-terminated.
2510 DWORD WINAPI SHUnicodeToAnsiCP(UINT CodePage, LPCWSTR lpSrcStr, LPSTR lpDstStr, int dstlen)
2512 static const WCHAR emptyW[] = { '\0' };
2516 if (!lpDstStr || !dstlen)
2524 len = strlenW(lpSrcStr) + 1;
2529 CodePage = CP_UTF8; /* Fall through... */
2530 case 0x0000C350: /* FIXME: CP_ #define */
2536 INT needed = dstlen - 1;
2539 /* try the user supplied buffer first */
2540 hr = ConvertINetUnicodeToMultiByte(&dwMode, CodePage, lpSrcStr, &lenW, lpDstStr, &needed);
2543 lpDstStr[needed] = '\0';
2547 /* user buffer too small. exclude termination and copy as much as possible */
2549 hr = ConvertINetUnicodeToMultiByte(&dwMode, CodePage, lpSrcStr, &lenW, NULL, &needed);
2551 mem = HeapAlloc(GetProcessHeap(), 0, needed);
2555 hr = ConvertINetUnicodeToMultiByte(&dwMode, CodePage, lpSrcStr, &len, mem, &needed);
2558 reqLen = SHTruncateString(mem, dstlen);
2559 if (reqLen > 0) memcpy(lpDstStr, mem, reqLen-1);
2561 HeapFree(GetProcessHeap(), 0, mem);
2568 /* try the user supplied buffer first */
2569 reqLen = WideCharToMultiByte(CodePage, 0, lpSrcStr, len, lpDstStr, dstlen, NULL, NULL);
2571 if (!reqLen && GetLastError() == ERROR_INSUFFICIENT_BUFFER)
2573 reqLen = WideCharToMultiByte(CodePage, 0, lpSrcStr, len, NULL, 0, NULL, NULL);
2576 mem = HeapAlloc(GetProcessHeap(), 0, reqLen);
2579 reqLen = WideCharToMultiByte(CodePage, 0, lpSrcStr, len, mem,
2580 reqLen, NULL, NULL);
2582 reqLen = SHTruncateString(mem, dstlen -1);
2585 lstrcpynA(lpDstStr, mem, reqLen);
2586 HeapFree(GetProcessHeap(), 0, mem);
2587 lpDstStr[reqLen-1] = '\0';
2594 /*************************************************************************
2597 * Convert a Unicode string to Ascii.
2600 * lpSrcStr [I] Source Unicode string to convert
2601 * lpDstStr [O] Destination for converted Ascii string
2602 * iLen [O] Length of lpDstStr in characters
2605 * See SHUnicodeToAnsiCP
2608 * This function simply calls SHUnicodeToAnsiCP() with CodePage = CP_ACP.
2610 INT WINAPI SHUnicodeToAnsi(LPCWSTR lpSrcStr, LPSTR lpDstStr, INT iLen)
2612 return SHUnicodeToAnsiCP(CP_ACP, lpSrcStr, lpDstStr, iLen);
2615 /*************************************************************************
2618 * Copy one string to another.
2621 * lpszSrc [I] Source string to copy
2622 * lpszDst [O] Destination for copy
2623 * iLen [I] Length of lpszDst in characters
2626 * The length of the copied string, including the terminating NUL. lpszDst
2627 * contains iLen characters of lpszSrc.
2629 DWORD WINAPI SHAnsiToAnsi(LPCSTR lpszSrc, LPSTR lpszDst, int iLen)
2633 TRACE("(%s,%p,0x%08x)\n", debugstr_a(lpszSrc), lpszDst, iLen);
2635 lpszRet = StrCpyNXA(lpszDst, lpszSrc, iLen);
2636 return lpszRet - lpszDst + 1;
2639 /*************************************************************************
2642 * Unicode version of SSHAnsiToAnsi.
2644 DWORD WINAPI SHUnicodeToUnicode(LPCWSTR lpszSrc, LPWSTR lpszDst, int iLen)
2648 TRACE("(%s,%p,0x%08x)\n", debugstr_w(lpszSrc), lpszDst, iLen);
2650 lpszRet = StrCpyNXW(lpszDst, lpszSrc, iLen);
2651 return lpszRet - lpszDst + 1;
2654 /*************************************************************************
2657 * Determine if an Ascii string converts to Unicode and back identically.
2660 * lpSrcStr [I] Source Unicode string to convert
2661 * lpDst [O] Destination for resulting Ascii string
2662 * iLen [I] Length of lpDst in characters
2665 * TRUE, since Ascii strings always convert identically.
2667 BOOL WINAPI DoesStringRoundTripA(LPCSTR lpSrcStr, LPSTR lpDst, INT iLen)
2669 lstrcpynA(lpDst, lpSrcStr, iLen);
2673 /*************************************************************************
2676 * Determine if a Unicode string converts to Ascii and back identically.
2679 * lpSrcStr [I] Source Unicode string to convert
2680 * lpDst [O] Destination for resulting Ascii string
2681 * iLen [I] Length of lpDst in characters
2684 * TRUE, if lpSrcStr converts to Ascii and back identically,
2687 BOOL WINAPI DoesStringRoundTripW(LPCWSTR lpSrcStr, LPSTR lpDst, INT iLen)
2689 WCHAR szBuff[MAX_PATH];
2691 SHUnicodeToAnsi(lpSrcStr, lpDst, iLen);
2692 SHAnsiToUnicode(lpDst, szBuff, MAX_PATH);
2693 return !strcmpW(lpSrcStr, szBuff);
2696 /*************************************************************************
2697 * SHLoadIndirectString [SHLWAPI.@]
2699 * If passed a string that begins with '@', extract the string from the
2700 * appropriate resource, otherwise do a straight copy.
2703 HRESULT WINAPI SHLoadIndirectString(LPCWSTR src, LPWSTR dst, UINT dst_len, void **reserved)
2705 WCHAR *dllname = NULL;
2706 HMODULE hmod = NULL;
2707 HRESULT hr = E_FAIL;
2709 TRACE("(%s %p %08x %p)\n", debugstr_w(src), dst, dst_len, reserved);
2717 dllname = StrDupW(src + 1);
2718 index_str = strchrW(dllname, ',');
2720 if(!index_str) goto end;
2724 index = atoiW(index_str);
2726 hmod = LoadLibraryW(dllname);
2731 if(LoadStringW(hmod, -index, dst, dst_len))
2735 FIXME("can't handle non-negative indices (%d)\n", index);
2740 lstrcpynW(dst, src, dst_len);
2744 TRACE("returning %s\n", debugstr_w(dst));
2746 if(hmod) FreeLibrary(hmod);
2747 HeapFree(GetProcessHeap(), 0, dllname);