From 39c6d5fe02445b6ee63c531922998fdace6edb16 Mon Sep 17 00:00:00 2001 From: "METANEOCORTEX\\Kotti" Date: Sun, 3 Oct 2021 23:38:35 +0200 Subject: [PATCH] some more LongPath handling --- src/Config/Config.cpp | 2 +- src/Dialogs.c | 68 +++++++++++++++++++++---------------------- src/Dialogs.h | 2 +- src/Notepad3.c | 49 +++++++++++++++---------------- src/PathLib.c | 4 +-- src/PathLib.h | 2 +- 6 files changed, 63 insertions(+), 64 deletions(-) diff --git a/src/Config/Config.cpp b/src/Config/Config.cpp index 7a8c40346..f68667e24 100644 --- a/src/Config/Config.cpp +++ b/src/Config/Config.cpp @@ -937,7 +937,7 @@ static bool _HandleIniFileRedirect(LPCWSTR lpszSecName, LPCWSTR lpszKeyName, HPA wchar_t* const buf = Path_WriteAccessBuf(hredirect, PATHLONG_MAX_CCH); if (IniFileGetString(Path_Get(hpth_in_out), lpszSecName, lpszKeyName, L"", buf, PATHLONG_MAX_CCH)) { Path_Sanitize(hredirect); - Path_FreeExtra(hredirect, MAX_PATH); + Path_FreeExtra(hredirect, 0); if (_CheckAndSetIniFile(hredirect)) { Path_Swap(hpth_in_out, hredirect); } diff --git a/src/Dialogs.c b/src/Dialogs.c index 4c0f481db..c2fc2ae55 100644 --- a/src/Dialogs.c +++ b/src/Dialogs.c @@ -1373,7 +1373,7 @@ CASE_WM_CTLCOLOR_SET: if (GetOpenFileName(&ofn)) { Path_Sanitize(hfile_pth); - Path_QuoteSpaces(hfile_pth); + Path_QuoteSpaces(hfile_pth, true); StrgReset(hargs_str, Path_Get(hfile_pth)); if (StrgIsNotEmpty(hargs2_str)) { StrgCat(hargs_str, L" "); @@ -4484,59 +4484,59 @@ WINDOWPLACEMENT WindowPlacementFromInfo(HWND hwnd, const WININFO* pWinInfo, SCRE //============================================================================= // -// DialogNewWindow() TODO: §§§ MAX_PATH limit §§§ @@@! +// DialogNewWindow() // // -void DialogNewWindow(HWND hwnd, bool bSaveOnRunTools, LPCWSTR lpcwFilePath, WININFO* wi) { - +void DialogNewWindow(HWND hwnd, bool bSaveOnRunTools, const HPATHL hFilePath, WININFO* wi) +{ if (bSaveOnRunTools && !FileSave(false, true, false, false, Flags.bPreserveFileModTime)) { return; } - WCHAR szModuleName[MAX_PATH] = { L'\0' }; - GetModuleFileName(NULL, szModuleName, COUNTOF(szModuleName)); - PathCanonicalizeEx(szModuleName, COUNTOF(szModuleName)); + WCHAR wch[80] = { L'\0' }; - WCHAR tch[64] = { L'\0' }; - WCHAR szParameters[2 * MAX_PATH + 64] = { L'\0' }; - StringCchPrintf(tch, COUNTOF(tch), L"\"-appid=%s\"", Settings2.AppUserModelID); - StringCchCopy(szParameters, COUNTOF(szParameters), tch); + HPATHL hmod_pth = Path_Allocate(NULL); + Path_GetModuleFilePath(hmod_pth); + Path_CanonicalizeEx(hmod_pth); - StringCchPrintf(tch, COUNTOF(tch), L"\" -sysmru=%i\"", (Flags.ShellUseSystemMRU ? 1 : 0)); - StringCchCat(szParameters, COUNTOF(szParameters), tch); - - StringCchCat(szParameters, COUNTOF(szParameters), L" -f"); + StringCchPrintf(wch, COUNTOF(wch), L"\"-appid=%s\"", Settings2.AppUserModelID); + HSTRINGW hparam_str = StrgCreate(wch); + StringCchPrintf(wch, COUNTOF(wch), L"\" -sysmru=%i\"", (Flags.ShellUseSystemMRU ? 1 : 0)); + StrgCat(hparam_str, wch); if (Path_IsNotEmpty(Paths.IniFile)) { - StringCchCat(szParameters, COUNTOF(szParameters), L" \""); - StringCchCat(szParameters, COUNTOF(szParameters), Path_Get(Paths.IniFile)); //TODO: §§§ MAX_PATH limit §§§ @@@! - StringCchCat(szParameters, COUNTOF(szParameters), L"\""); + HPATHL hini_path = Path_Copy(Paths.IniFile); + Path_QuoteSpaces(hini_path, true); + StrgCat(hparam_str, L" -f "); + StrgCat(hparam_str, Path_Get(hini_path)); + Path_Release(hini_path); } else { - StringCchCat(szParameters, COUNTOF(szParameters), L"0"); + StrgCat(hparam_str, L" -f0"); } - StringCchCat(szParameters, COUNTOF(szParameters), Flags.bSingleFileInstance ? L" -ns" : L" -n"); + StrgCat(hparam_str, Flags.bSingleFileInstance ? L" -ns" : L" -n"); WININFO const _wi = (Flags.bStickyWindowPosition ? g_IniWinInfo : (wi ? *wi : GetMyWindowPlacement(hwnd, NULL, Settings2.LaunchInstanceWndPosOffset))); - StringCchPrintf(tch, COUNTOF(tch), L" -pos %i,%i,%i,%i,%i", _wi.x, _wi.y, _wi.cx, _wi.cy, _wi.max); - StringCchCat(szParameters, COUNTOF(szParameters), tch); + StringCchPrintf(wch, COUNTOF(wch), L" -pos %i,%i,%i,%i,%i ", _wi.x, _wi.y, _wi.cx, _wi.cy, _wi.max); + StrgCat(hparam_str, wch); - if (StrIsNotEmpty(lpcwFilePath)) { - WCHAR szFileName[MAX_PATH] = { L'\0' }; - StringCchCopy(szFileName, COUNTOF(szFileName), lpcwFilePath); - PathQuoteSpaces(szFileName); - StringCchCat(szParameters, COUNTOF(szParameters), L" "); - StringCchCat(szParameters, COUNTOF(szParameters), szFileName); + if (Path_IsNotEmpty(hFilePath)) { + HPATHL hfile_pth = Path_Copy(hFilePath); + Path_QuoteSpaces(hfile_pth, true); + StrgCat(hparam_str, Path_Get(hfile_pth)); + Path_Release(hfile_pth); } SHELLEXECUTEINFO sei = { sizeof(SHELLEXECUTEINFO) }; sei.fMask = SEE_MASK_NOASYNC | SEE_MASK_NOZONECHECKS; sei.hwnd = hwnd; sei.lpVerb = NULL; - sei.lpFile = szModuleName; - sei.lpParameters = szParameters; + sei.lpFile = Path_Get(hmod_pth); + sei.lpParameters = StrgGet(hparam_str); sei.lpDirectory = Path_Get(Paths.WorkingDirectory); sei.nShow = SW_SHOWNORMAL; ShellExecuteEx(&sei); + + StrgDestroy(hparam_str); } @@ -4588,7 +4588,7 @@ void DialogFileBrowse(HWND hwnd) if (Path_IsNotEmpty(Paths.CurrentFile)) { HPATHL pthTmp = Path_Copy(Paths.CurrentFile); - Path_QuoteSpaces(pthTmp); + Path_QuoteSpaces(pthTmp, true); StringCchCat(param_buf, PATHLONG_MAX_CCH, Path_Get(pthTmp)); Path_Release(pthTmp); } @@ -4872,7 +4872,7 @@ void DialogAdminExe(HWND hwnd, bool bExecInstaller) // bool s_bFreezeAppTitle = false; // extern visible -static WCHAR s_wchAdditionalTitleInfo[MAX_PATH] = { L'\0' }; +static WCHAR s_wchAdditionalTitleInfo[MIDSZ_BUFFER] = { L'\0' }; void SetAdditionalTitleInfo(LPCWSTR lpszAddTitleInfo) { StringCchCopy(s_wchAdditionalTitleInfo, COUNTOF(s_wchAdditionalTitleInfo), lpszAddTitleInfo); @@ -5292,7 +5292,7 @@ void ResizeDlg_InitEx(HWND hwnd, int cxFrame, int cyFrame, int nIdGrip, RSZ_DLG_ SetWindowStyle(hwnd, style); SetWindowPos(hwnd, NULL, 0, 0, 0, 0, SWP_NOZORDER | SWP_NOMOVE | SWP_NOSIZE | SWP_FRAMECHANGED); - WCHAR wch[MAX_PATH]; + WCHAR wch[MIDSZ_BUFFER]; GetMenuString(GetSystemMenu(GetParent(hwnd), FALSE), SC_SIZE, wch, COUNTOF(wch), MF_BYCOMMAND); InsertMenu(GetSystemMenu(hwnd, FALSE), SC_CLOSE, MF_BYCOMMAND | MF_STRING | MF_ENABLED, SC_SIZE, wch); InsertMenu(GetSystemMenu(hwnd, FALSE), SC_CLOSE, MF_BYCOMMAND | MF_SEPARATOR, 0, NULL); @@ -5631,7 +5631,7 @@ void StatusSetTextID(HWND hwnd, BYTE nPart, UINT uID) if (!uID) { SendMessage(hwnd, SB_SETTEXT, (WPARAM)wParam, (LPARAM)L""); } else { - WCHAR szText[MAX_PATH] = { L'\0' }; + WCHAR szText[MIDSZ_BUFFER] = { L'\0' }; if (!GetLngString(uID, szText, COUNTOF(szText))) { SendMessage(hwnd, SB_SETTEXT, (WPARAM)wParam, (LPARAM)L""); } else { diff --git a/src/Dialogs.h b/src/Dialogs.h index 2590954cc..360d6c0e0 100644 --- a/src/Dialogs.h +++ b/src/Dialogs.h @@ -69,7 +69,7 @@ bool GetWindowRectEx(HWND hwnd, LPRECT pRect); void FitIntoMonitorGeometry(LPRECT pRect, WININFO* pWinInfo, SCREEN_MODE mode, bool bTopLeft); WINDOWPLACEMENT WindowPlacementFromInfo(HWND hwnd, const WININFO* pWinInfo, SCREEN_MODE mode); -void DialogNewWindow(HWND hwnd, bool bSaveOnRunTools, LPCWSTR lpcwFilePath, WININFO* wi); +void DialogNewWindow(HWND hwnd, bool bSaveOnRunTools, const HPATHL hFilePath, WININFO* wi); void DialogFileBrowse(HWND hwnd); void DialogGrepWin(HWND hwnd, LPCWSTR searchPattern); void DialogAdminExe(HWND hwnd,bool); diff --git a/src/Notepad3.c b/src/Notepad3.c index 74354d2aa..fdd2c9de6 100644 --- a/src/Notepad3.c +++ b/src/Notepad3.c @@ -3200,33 +3200,28 @@ LRESULT MsgDrawItem(HWND hwnd, WPARAM wParam, LPARAM lParam) // // _OnDropOneFile() // -// TODO: §§§ @@@ MAX_PATH -static LRESULT _OnDropOneFile(HWND hwnd, LPCWSTR szFilePath, WININFO* wi) { - +static LRESULT _OnDropOneFile(HWND hwnd, HPATHL hFilePath, WININFO* wi) +{ if (IsIconic(hwnd)) { ShowWindow(hwnd, SW_RESTORE); } - HPATHL hfile_pth = Path_Allocate(szFilePath); - - if (Path_IsExistingDirectory(hfile_pth)) { - if (OpenFileDlg(Globals.hwndMain, hfile_pth, hfile_pth)) { - FileLoad(hfile_pth, false, false, false, Settings.SkipUnicodeDetection, Settings.SkipANSICodePageDetection, false); + if (Path_IsExistingDirectory(hFilePath)) { + if (OpenFileDlg(Globals.hwndMain, hFilePath, hFilePath)) { + FileLoad(hFilePath, false, false, false, Settings.SkipUnicodeDetection, Settings.SkipANSICodePageDetection, false); } } - else if (PathIsExistingFile(szFilePath)) { + else if (Path_IsExistingFile(hFilePath)) { //~ ignore Flags.bReuseWindow if (IsKeyDown(VK_CONTROL) || wi) { - DialogNewWindow(hwnd, Settings.SaveBeforeRunningTools, szFilePath, wi); + DialogNewWindow(hwnd, Settings.SaveBeforeRunningTools, hFilePath, wi); } else { - FileLoad(hfile_pth, false, false, false, Settings.SkipUnicodeDetection, Settings.SkipANSICodePageDetection, false); + FileLoad(hFilePath, false, false, false, Settings.SkipUnicodeDetection, Settings.SkipANSICodePageDetection, false); } } else { // Windows Bug: wParam (HDROP) pointer is corrupted if dropped from 32-bit App InfoBoxLng(MB_ICONWARNING, NULL, IDS_MUI_DROP_NO_FILE); } - - Path_Release(hfile_pth); return FALSE; } @@ -3241,18 +3236,20 @@ LRESULT MsgDropFiles(HWND hwnd, WPARAM wParam, LPARAM lParam) HDROP const hDrop = (HDROP)wParam; bool const vkCtrlDown = IsKeyDown(VK_CONTROL); - WCHAR szDropFilePath[MAX_PATH + 40]; + HPATHL hdrop_pth = Path_Allocate(NULL); + wchar_t* const drop_buf = Path_WriteAccessBuf(hdrop_pth, STRINGW_MAX_URL_LENGTH); UINT const cnt = DragQueryFile(hDrop, UINT_MAX, NULL, 0); int const offset = Settings2.LaunchInstanceWndPosOffset; for (UINT i = 0; i < cnt; ++i) { WININFO wi = GetMyWindowPlacement(hwnd, NULL, (vkCtrlDown ? (offset * (i + 1)) : 0)); - DragQueryFile(hDrop, i, szDropFilePath, COUNTOF(szDropFilePath)); - _OnDropOneFile(hwnd, szDropFilePath, (((0 == i) && !IsKeyDown(VK_CONTROL)) ? NULL : &wi)); + DragQueryFile(hDrop, i, drop_buf, (UINT)Path_GetBufCount(hdrop_pth)); + _OnDropOneFile(hwnd, hdrop_pth, (((0 == i) && !IsKeyDown(VK_CONTROL)) ? NULL : &wi)); } DragFinish(hDrop); + Path_Release(hdrop_pth); return 0; } @@ -4338,8 +4335,8 @@ LRESULT MsgCommand(HWND hwnd, UINT umsg, WPARAM wParam, LPARAM lParam) case IDM_FILE_NEWWINDOW: case IDM_FILE_NEWWINDOW2: { SaveAllSettings(false); - LPCWSTR lpcwFilePath = (iLoWParam == IDM_FILE_NEWWINDOW2) ? Path_Get(Paths.CurrentFile) : NULL; - DialogNewWindow(hwnd, Settings.SaveBeforeRunningTools, lpcwFilePath, NULL); + HPATHL hpth = (iLoWParam == IDM_FILE_NEWWINDOW2) ? Paths.CurrentFile : NULL; + DialogNewWindow(hwnd, Settings.SaveBeforeRunningTools, hpth, NULL); } break; @@ -4405,7 +4402,7 @@ LRESULT MsgCommand(HWND hwnd, UINT umsg, WPARAM wParam, LPARAM lParam) break; } HPATHL hcpy = Path_Copy(Paths.CurrentFile); - Path_QuoteSpaces(hcpy); + Path_QuoteSpaces(hcpy, false); RunDlg(hwnd, Path_Get(hcpy)); Path_Release(hcpy); } @@ -8219,12 +8216,14 @@ static LRESULT _MsgNotifyFromEdit(HWND hwnd, const SCNotification* const scn) case SCN_URIDROPPED: { - WCHAR szFilePath[MAX_PATH + 40] = { L'\0' }; - if (MultiByteToWideChar(CP_UTF8, 0, scn->text, -1, szFilePath, (int)COUNTOF(szFilePath)) > 0) { - return _OnDropOneFile(hwnd, szFilePath, NULL); - } + HPATHL hfile_pth = Path_Allocate(NULL); + wchar_t* const file_buf = Path_WriteAccessBuf(hfile_pth, STRINGW_MAX_URL_LENGTH); + int const cnt = MultiByteToWideChar(CP_UTF8, 0, scn->text, -1, file_buf, (int)Path_GetBufCount(hfile_pth)); + LRESULT const result = (cnt > 0) ? _OnDropOneFile(hwnd, hfile_pth, NULL) : FALSE; + Path_Release(hfile_pth); + return result; } - break; + default: return FALSE; @@ -10983,7 +10982,7 @@ bool FileSave(bool bSaveAlways, bool bAsk, bool bSaveAs, bool bSaveCopy, bool bP answer = INFOBOX_ANSW(InfoBoxLng(MB_YESNO | MB_ICONINFORMATION, L"ReloadExSavedCfg", IDS_MUI_RELOADSETTINGS, L"")); } if ((IDOK == answer) || (IDYES == answer)) { - DialogNewWindow(Globals.hwndMain, false, Path_Get(Paths.CurrentFile), NULL); + DialogNewWindow(Globals.hwndMain, false, Paths.CurrentFile, NULL); CloseApplication(); } } diff --git a/src/PathLib.c b/src/PathLib.c index b13873c5a..900dc1e50 100644 --- a/src/PathLib.c +++ b/src/PathLib.c @@ -1148,13 +1148,13 @@ const wchar_t* PTHAPI Path_FindFileName(const HPATHL hpth) // ---------------------------------------------------------------------------- -bool PTHAPI Path_QuoteSpaces(HPATHL hpth_in_out) +bool PTHAPI Path_QuoteSpaces(HPATHL hpth_in_out, bool bForceQuotes) { HSTRINGW hstr_io = ToHStrgW(hpth_in_out); if (!hstr_io) return false; - bool const found = (StrgFindCh(hstr_io, L' ', 0) != STRINGW_INVALID_IDX); + bool const found = bForceQuotes ? true : (StrgFindCh(hstr_io, L' ', 0) != STRINGW_INVALID_IDX); if (found) { if (StrgGetAt(hstr_io, 0) != L'"') { StrgInsert(hstr_io, 0, L"\""); diff --git a/src/PathLib.h b/src/PathLib.h index c2b2b274b..315bea773 100644 --- a/src/PathLib.h +++ b/src/PathLib.h @@ -77,7 +77,7 @@ bool PTHAPI Path_IsPrefix(const HPATHL hprefix, const HPATHL hpth); size_t PTHAPI Path_CommonPrefix(const HPATHL hpth1, const HPATHL hpth2, HPATHL hpfx_out); const wchar_t* PTHAPI Path_FindFileName(const HPATHL hpth); const wchar_t* PTHAPI Path_FindExtension(const HPATHL hpth); -bool PTHAPI Path_QuoteSpaces(HPATHL hpth_in_out); +bool PTHAPI Path_QuoteSpaces(HPATHL hpth_in_out, bool bForceQuotes); int PTHAPI Path_GetDriveNumber(const HPATHL hpth); wchar_t PTHAPI Path_GetDriveLetterByNumber(const int number); DWORD PTHAPI Path_GetFileAttributes(const HPATHL hpth);