fix and reduce code

This commit is contained in:
Katayama Hirofumi MZ 2024-05-14 08:44:32 +09:00
parent 7bbb0dfcd5
commit 05f8814f4c
3 changed files with 20 additions and 86 deletions

View file

@ -78,88 +78,21 @@ IContextMenu_Invoke(
return ret;
}
/*************************************************************************
* PathAddDefExtW [Internal]
*
* @param pszPath The path string.
* @param dwFlags The PADE_... flags.
* @param pdwAttrs A pointer to the file attributes. Optional.
* @return TRUE if successful.
*/
static BOOL
PathAddDefExtW(
_Inout_ LPWSTR pszPath,
_In_ DWORD dwFlags,
_Out_opt_ LPDWORD pdwAttrs)
{
INT cchPath = lstrlenW(pszPath);
if (cchPath + 4 + 1 > MAX_PATH) // ".ext" is 4 letters, and then a NUL
return FALSE;
LPWSTR pch = &pszPath[cchPath];
INT_PTR cchFileTitle = pch - PathFindFileNameW(pszPath);
WIN32_FIND_DATAW FindData;
StringCchCatW(pszPath, MAX_PATH, L".*");
HANDLE hFind = FindFirstFileW(pszPath, &FindData);
if (hFind == INVALID_HANDLE_VALUE)
{
*pch = UNICODE_NULL;
return FALSE;
}
DWORD dwAttrs = INVALID_FILE_ATTRIBUTES;
static const LPCWSTR s_DotExts[] =
{
L".pif", L".com", L".exe", L".bat", L".lnk", L".cmd", L"", NULL
};
BOOL ret = FALSE;
do
{
for (SIZE_T iExt = 0, nBits = dwFlags; s_DotExts[iExt]; ++iExt)
{
if ((nBits & 1) || iExt == 6) // 6 --> L""
{
if (lstrcmpiW(&FindData.cFileName[cchFileTitle], s_DotExts[iExt]) == 0)
{
dwAttrs = FindData.dwFileAttributes;
*pch = UNICODE_NULL;
StringCchCatW(pszPath, MAX_PATH, s_DotExts[iExt]);
ret = TRUE;
break;
}
}
nBits >>= 1;
}
} while (!ret && FindNextFileW(hFind, &FindData));
FindClose(hFind);
if (pdwAttrs)
*pdwAttrs = dwAttrs;
if (!ret)
*pch = UNICODE_NULL;
return ret;
}
/*************************************************************************
* PathFileExistsDefExtAndAttributesW [SHLWAPI.511]
*
* @param pszPath The path string.
* @param dwFlags The PADE_... flags.
* @param dwWhich The WHICH_... flags.
* @param pdwFileAttributes A pointer to the file attributes. Optional.
* @return TRUE if successful.
*/
BOOL WINAPI
PathFileExistsDefExtAndAttributesW(
_Inout_ LPWSTR pszPath,
_In_ DWORD dwFlags,
_In_ DWORD dwWhich,
_Out_opt_ LPDWORD pdwFileAttributes)
{
TRACE("(%s, 0x%lX, %p)\n", debugstr_w(pszPath), dwFlags, pdwFileAttributes);
TRACE("(%s, 0x%lX, %p)\n", debugstr_w(pszPath), dwWhich, pdwFileAttributes);
if (pdwFileAttributes)
*pdwFileAttributes = INVALID_FILE_ATTRIBUTES;
@ -167,8 +100,18 @@ PathFileExistsDefExtAndAttributesW(
if (!pszPath)
return FALSE;
if (!dwFlags || (*PathFindExtensionW(pszPath) && (dwFlags & PADE_OPTIONAL)))
if (!dwWhich || (*PathFindExtensionW(pszPath) && (dwWhich & WHICH_OPTIONAL)))
return PathFileExistsAndAttributesW(pszPath, pdwFileAttributes);
return PathAddDefExtW(pszPath, dwFlags, pdwFileAttributes);
if (!PathFileExistsDefExtW(pszPath, dwWhich))
{
if (pdwFileAttributes)
*pdwFileAttributes = INVALID_FILE_ATTRIBUTES;
return FALSE;
}
if (pdwFileAttributes)
*pdwFileAttributes = GetFileAttributesW(pszPath);
return TRUE;
}

View file

@ -47,14 +47,14 @@ START_TEST(PathFileExistsDefExtAndAttributesW)
ok_int(ret, FALSE);
/* Add .exe */
ret = PathFileExistsDefExtAndAttributesW(szPath, PADE_EXE, NULL);
ret = PathFileExistsDefExtAndAttributesW(szPath, WHICH_EXE, NULL);
ok_int(ret, TRUE);
ret = PathFileExistsW(szPath);
ok_int(ret, TRUE);
/* notepad.cmd doesn't exist */
PathRemoveExtensionW(szPath);
ret = PathFileExistsDefExtAndAttributesW(szPath, PADE_CMD, NULL);
ret = PathFileExistsDefExtAndAttributesW(szPath, WHICH_CMD, NULL);
ok_int(ret, FALSE);
ret = PathFileExistsW(szPath);
ok_int(ret, FALSE);

View file

@ -283,7 +283,8 @@ ShellMessageBoxWrapW(
_In_ UINT fuStyle,
...);
/* dwWhich flags for PathFileExistsDefExtW and PathFindOnPathExW */
/* dwWhich flags for PathFileExistsDefExtW, PathFindOnPathExW,
and PathFileExistsDefExtAndAttributesW. */
#define WHICH_PIF (1 << 0)
#define WHICH_COM (1 << 1)
#define WHICH_EXE (1 << 2)
@ -324,20 +325,10 @@ IContextMenu_Invoke(
DWORD WINAPI SHGetObjectCompatFlags(IUnknown *pUnk, const CLSID *clsid);
/* Flags for appending an extension in PathFileExistsDefExtAndAttributesW */
#define PADE_PIF 0x0001
#define PADE_COM 0x0002
#define PADE_EXE 0x0004
#define PADE_BAT 0x0008
#define PADE_LNK 0x0010
#define PADE_CMD 0x0020
#define PADE_OPTIONAL 0x0040
#define PADE_ALL (PADE_PIF | PADE_COM | PADE_EXE | PADE_BAT | PADE_LNK | PADE_CMD | PADE_OPTIONAL)
BOOL WINAPI
PathFileExistsDefExtAndAttributesW(
_Inout_ LPWSTR pszPath,
_In_ DWORD dwFlags,
_In_ DWORD dwWhich,
_Out_opt_ LPDWORD pdwFileAttributes);
#ifdef __cplusplus