diff --git a/Src/HexMergeDoc.cpp b/Src/HexMergeDoc.cpp index 3d06c85c497..e5c7071a9be 100644 --- a/Src/HexMergeDoc.cpp +++ b/Src/HexMergeDoc.cpp @@ -231,20 +231,20 @@ bool CHexMergeDoc::PromptAndSaveIfNeeded(bool bAllowCancel) if (!bAllowCancel) dlg.m_bDisableCancel = true; if (!pathLeft.empty()) - dlg.m_sLeftFile = pathLeft; + dlg.m_sLeftFile = m_strSaveAsPath.empty() ? pathLeft : m_strSaveAsPath; else - dlg.m_sLeftFile = m_strDesc[0]; + dlg.m_sLeftFile = m_strSaveAsPath.empty() ? m_strDesc[0] : m_strSaveAsPath; if (m_nBuffers == 3) { if (!pathMiddle.empty()) - dlg.m_sMiddleFile = pathMiddle; + dlg.m_sMiddleFile = m_strSaveAsPath.empty() ? pathMiddle : m_strSaveAsPath; else - dlg.m_sMiddleFile = m_strDesc[1]; + dlg.m_sMiddleFile = m_strSaveAsPath.empty() ? m_strDesc[1] : m_strSaveAsPath; } if (!pathRight.empty()) - dlg.m_sRightFile = pathRight; + dlg.m_sRightFile = m_strSaveAsPath.empty() ? pathRight : m_strSaveAsPath; else - dlg.m_sRightFile = m_strDesc[1]; + dlg.m_sRightFile = m_strSaveAsPath.empty() ? m_strDesc[1] : m_strSaveAsPath; if (dlg.DoModal() == IDOK) { @@ -329,15 +329,35 @@ bool CHexMergeDoc::DoFileSave(int nBuffer) result = DoFileSaveAs(nBuffer); else { - const String &path = m_filePaths.GetPath(nBuffer); - HRESULT hr = m_pView[nBuffer]->SaveFile(path.c_str()); + String strSavePath = m_strSaveAsPath.empty() ? m_filePaths.GetPath(nBuffer) : m_strSaveAsPath; + // Warn user in case file has been changed by someone else + if (m_pView[nBuffer]->IsFileChangedOnDisk(m_filePaths.GetPath(nBuffer).c_str()) == IMergeDoc::FileChange::Changed) + { + String msg = strutils::format_string1(_("Another application has updated file\n%1\nsince WinMerge loaded it.\n\nOverwrite changed file?"), m_filePaths.GetPath(nBuffer)); + if (AfxMessageBox(msg.c_str(), MB_ICONWARNING | MB_YESNO) == IDNO) + return false; + } + // Ask user what to do about FILE_ATTRIBUTE_READONLY + String strPath = strSavePath; + bool bApplyToAll = false; + if (CMergeApp::HandleReadonlySave(strPath, false, bApplyToAll) == IDCANCEL) + return false; + strSavePath = strPath.c_str(); + // Take a chance to create a backup + if (!CMergeApp::CreateBackup(false, strSavePath)) + return false; + HRESULT hr = m_pView[nBuffer]->SaveFile(strSavePath.c_str()); if (Try(hr) == IDCANCEL) return false; if (FAILED(hr)) return DoFileSaveAs(nBuffer); result = true; if (result) + { + m_filePaths[nBuffer] = strSavePath; + UpdateHeaderPath(nBuffer); UpdateDiffItem(m_pDirDoc); + } } } return result; diff --git a/Src/HexMergeView.cpp b/Src/HexMergeView.cpp index b55780001db..556cf9bd90e 100644 --- a/Src/HexMergeView.cpp +++ b/Src/HexMergeView.cpp @@ -318,22 +318,6 @@ HRESULT CHexMergeView::LoadFile(const tchar_t* path) */ HRESULT CHexMergeView::SaveFile(const tchar_t* path, bool packing) { - // Warn user in case file has been changed by someone else - if (IsFileChangedOnDisk(path) == IMergeDoc::FileChange::Changed) - { - String msg = strutils::format_string1(_("Another application has updated file\n%1\nsince WinMerge loaded it.\n\nOverwrite changed file?"), path); - if (AfxMessageBox(msg.c_str(), MB_ICONWARNING | MB_YESNO) == IDNO) - return E_FAIL; - } - // Ask user what to do about FILE_ATTRIBUTE_READONLY - String strPath = path; - bool bApplyToAll = false; - if (CMergeApp::HandleReadonlySave(strPath, false, bApplyToAll) == IDCANCEL) - return E_FAIL; - path = strPath.c_str(); - // Take a chance to create a backup - if (!CMergeApp::CreateBackup(false, path)) - return E_FAIL; // Write data to an intermediate file String tempPath = env::GetTemporaryPath(); String sIntermediateFilename = env::GetTemporaryFileName(tempPath, _T("MRG_"), 0); diff --git a/Src/ImgMergeFrm.cpp b/Src/ImgMergeFrm.cpp index 04a6b354797..d2776a56163 100644 --- a/Src/ImgMergeFrm.cpp +++ b/Src/ImgMergeFrm.cpp @@ -722,7 +722,8 @@ bool CImgMergeFrame::DoFileSave(int pane) return false; CMergeApp::CreateBackup(false, m_filePaths[pane]); int savepoint = m_pImgMergeWindow->GetSavePoint(pane); - if (!m_pImgMergeWindow->SaveImage(pane)) + bool result = m_strSaveAsPath.empty() ? m_pImgMergeWindow->SaveImage(pane) : m_pImgMergeWindow->SaveImageAs(pane, m_strSaveAsPath.c_str()); + if (!result) { String str = strutils::format_string2(_("Saving file failed.\n%1\n%2\nDo you want to:\n\t- use a different filename (Press OK)\n\t- abort the current operation (Press Cancel)?"), filename, GetSysError()); int answer = AfxMessageBox(str.c_str(), MB_OKCANCEL | MB_ICONWARNING); @@ -730,6 +731,8 @@ bool CImgMergeFrame::DoFileSave(int pane) return DoFileSaveAs(pane); return false; } + if (!m_strSaveAsPath.empty()) + m_filePaths[pane] = m_strSaveAsPath; if (filename != m_filePaths[pane]) { if (!m_infoUnpacker.Packing(filename, m_filePaths[pane], m_unpackerSubcodes[pane], { m_filePaths[pane] })) diff --git a/Src/Merge.cpp b/Src/Merge.cpp index d787dcca8cf..8a8afe5e969 100644 --- a/Src/Merge.cpp +++ b/Src/Merge.cpp @@ -883,7 +883,14 @@ bool CMergeApp::ParseArgsAndDoOpen(MergeCmdLineInfo& cmdInfo, CMainFrame* pMainF pOpenTableFileParams->m_tableDelimiter = cmdInfo.m_cTableDelimiter; pOpenTableFileParams->m_tableQuote = cmdInfo.m_cTableQuote; pOpenTableFileParams->m_tableAllowNewlinesInQuotes = cmdInfo.m_bTableAllowNewlinesInQuotes; - pOpenTableFileParams->m_strSaveAsPath = cmdInfo.m_sOutputpath; + } + if (auto* pOpenBinaryFileParams = dynamic_cast(pOpenParams.get())) + { + pOpenBinaryFileParams->m_strSaveAsPath = cmdInfo.m_sOutputpath; + } + if (auto* pOpenImageFileParams = dynamic_cast(pOpenParams.get())) + { + pOpenImageFileParams->m_strSaveAsPath = cmdInfo.m_sOutputpath; } if (cmdInfo.m_Files.GetSize() > 2) { diff --git a/Src/MergeDoc.cpp b/Src/MergeDoc.cpp index edfe8476ad4..ff49b0ac30a 100644 --- a/Src/MergeDoc.cpp +++ b/Src/MergeDoc.cpp @@ -1657,7 +1657,7 @@ bool CMergeDoc::TrySaveAs(String &strPath, int &nSaveResult, String & sError, bool CMergeDoc::DoSave(const tchar_t* szPath, bool &bSaveSuccess, int nBuffer) { DiffFileInfo fileInfo; - String strSavePath(szPath); + String strSavePath(m_strSaveAsPath.empty() ? szPath : m_strSaveAsPath); FileChange fileChanged; bool bApplyToAll = false; int nRetVal = -1; @@ -1680,21 +1680,6 @@ bool CMergeDoc::DoSave(const tchar_t* szPath, bool &bSaveSuccess, int nBuffer) bSaveSuccess = false; - // Check third arg possibly given from command-line - if (!m_strSaveAsPath.empty()) - { - if (paths::DoesPathExist(m_strSaveAsPath) == paths::IS_EXISTING_DIR) - { - // third arg was a directory, so get append the filename - String sname; - paths::SplitFilename(szPath, 0, &sname, 0); - strSavePath = m_strSaveAsPath; - strSavePath = paths::ConcatPath(strSavePath, sname); - } - else - strSavePath = m_strSaveAsPath; - } - nRetVal = CMergeApp::HandleReadonlySave(strSavePath, false, bApplyToAll); if (nRetVal == IDCANCEL) return false; @@ -2644,35 +2629,20 @@ bool CMergeDoc::PromptAndSaveIfNeeded(bool bAllowCancel) if (!bAllowCancel) dlg.m_bDisableCancel = true; if (!m_filePaths.GetLeft().empty()) - { - if (m_strSaveAsPath.empty()) - dlg.m_sLeftFile = m_filePaths.GetLeft(); - else - dlg.m_sLeftFile = m_strSaveAsPath; - } + dlg.m_sLeftFile = m_strSaveAsPath.empty() ? m_filePaths.GetLeft() : m_strSaveAsPath; else - dlg.m_sLeftFile = m_strDesc[0]; + dlg.m_sLeftFile = m_strSaveAsPath.empty() ? m_strDesc[0] : m_strSaveAsPath; if (m_nBuffers == 3) { if (!m_filePaths.GetMiddle().empty()) - { - if (m_strSaveAsPath.empty()) - dlg.m_sMiddleFile = m_filePaths.GetMiddle(); - else - dlg.m_sMiddleFile = m_strSaveAsPath; - } + dlg.m_sMiddleFile = m_strSaveAsPath.empty() ? m_filePaths.GetMiddle() : m_strSaveAsPath; else - dlg.m_sMiddleFile = m_strDesc[1]; + dlg.m_sMiddleFile = m_strSaveAsPath.empty() ? m_strDesc[1] : m_strSaveAsPath; } if (!m_filePaths.GetRight().empty()) - { - if (m_strSaveAsPath.empty()) - dlg.m_sRightFile = m_filePaths.GetRight(); - else - dlg.m_sRightFile = m_strSaveAsPath; - } + dlg.m_sRightFile = m_strSaveAsPath.empty() ?m_filePaths.GetRight() : m_strSaveAsPath; else - dlg.m_sRightFile = m_strDesc[m_nBuffers - 1]; + dlg.m_sRightFile = m_strSaveAsPath.empty() ? m_strDesc[m_nBuffers - 1] : m_strSaveAsPath; if (dlg.DoModal() == IDOK) {