OSDN Git Service

Binary Compare: Fix the problem that the file cannot be saved after creating a new one
authorTakashi Sawanaka <sdottaka@users.sourceforge.net>
Sun, 23 May 2021 13:28:01 +0000 (22:28 +0900)
committerTakashi Sawanaka <sdottaka@users.sourceforge.net>
Sun, 23 May 2021 13:28:01 +0000 (22:28 +0900)
Src/HexMergeDoc.cpp
Src/HexMergeDoc.h
Src/HexMergeView.cpp

index 02e3406..824a81e 100644 (file)
@@ -230,15 +230,9 @@ bool CHexMergeDoc::PromptAndSaveIfNeeded(bool bAllowCancel)
                {
                        if (dlg.m_leftSave == SaveClosingDlg::SAVECLOSING_SAVE)
                        {
-                               switch (Try(m_pView[0]->SaveFile(pathLeft.c_str())))
-                               {
-                               case 0:
-                                       bLSaveSuccess = true;
-                                       break;
-                               case IDCANCEL:
+                               bLSaveSuccess = DoFileSave(0);
+                               if (!bLSaveSuccess)
                                        result = false;
-                                       break;
-                               }
                        }
                        else
                        {
@@ -249,15 +243,9 @@ bool CHexMergeDoc::PromptAndSaveIfNeeded(bool bAllowCancel)
                {
                        if (dlg.m_middleSave == SaveClosingDlg::SAVECLOSING_SAVE)
                        {
-                               switch (Try(m_pView[1]->SaveFile(pathMiddle.c_str())))
-                               {
-                               case 0:
-                                       bMSaveSuccess = true;
-                                       break;
-                               case IDCANCEL:
+                               bMSaveSuccess = DoFileSave(1);
+                               if (!bMSaveSuccess)
                                        result = false;
-                                       break;
-                               }
                        }
                        else
                        {
@@ -268,15 +256,9 @@ bool CHexMergeDoc::PromptAndSaveIfNeeded(bool bAllowCancel)
                {
                        if (dlg.m_rightSave == SaveClosingDlg::SAVECLOSING_SAVE)
                        {
-                               switch (Try(m_pView[m_nBuffers - 1]->SaveFile(pathRight.c_str())))
-                               {
-                               case 0:
-                                       bRSaveSuccess = true;
-                                       break;
-                               case IDCANCEL:
+                               bRSaveSuccess = DoFileSave(m_nBuffers - 1);
+                               if (!bRSaveSuccess)
                                        result = false;
-                                       break;
-                               }
                        }
                        else
                        {
@@ -316,23 +298,30 @@ void CHexMergeDoc::OnFileSave()
                DoFileSave(nBuffer);
 }
 
-void CHexMergeDoc::DoFileSave(int nBuffer)
+bool CHexMergeDoc::DoFileSave(int nBuffer)
 {
+       bool result = false;
        if (m_pView[nBuffer]->GetModified())
        {
                if (m_nBufferType[nBuffer] == BUFFERTYPE::UNNAMED)
-                       DoFileSaveAs(nBuffer);
+                       result = DoFileSaveAs(nBuffer);
                else
                {
                        const String &path = m_filePaths.GetPath(nBuffer);
-                       if (Try(m_pView[nBuffer]->SaveFile(path.c_str())) == IDCANCEL)
-                               return;
+                       HRESULT hr = m_pView[nBuffer]->SaveFile(path.c_str());
+                       if (Try(hr) == IDCANCEL)
+                               return false;
+                       if (FAILED(hr))
+                               return DoFileSaveAs(nBuffer);
+                       result = true;
+                       if (result)
+                               UpdateDiffItem(m_pDirDoc);
                }
-               UpdateDiffItem(m_pDirDoc);
        }
+       return result;
 }
 
-void CHexMergeDoc::DoFileSaveAs(int nBuffer, bool packing)
+bool CHexMergeDoc::DoFileSaveAs(int nBuffer, bool packing)
 {
        const String &path = m_filePaths.GetPath(nBuffer);
        String strPath;
@@ -345,8 +334,11 @@ void CHexMergeDoc::DoFileSaveAs(int nBuffer, bool packing)
                title = _("Save Middle File As");
        if (SelectFile(AfxGetMainWnd()->GetSafeHwnd(), strPath, false, path.c_str(), title))
        {
-               if (Try(m_pView[nBuffer]->SaveFile(strPath.c_str(), packing)) == IDCANCEL)
-                       return;
+               HRESULT hr = m_pView[nBuffer]->SaveFile(strPath.c_str());
+               if (Try(hr) == IDCANCEL)
+                       return false;
+               if (FAILED(hr))
+                       return false;
                if (path.empty())
                {
                        // We are saving scratchpad (unnamed file)
@@ -357,7 +349,9 @@ void CHexMergeDoc::DoFileSaveAs(int nBuffer, bool packing)
                m_filePaths.SetPath(nBuffer, strPath);
                UpdateDiffItem(m_pDirDoc);
                UpdateHeaderPath(nBuffer);
+               return true;
        }
+       return false;
 }
 
 /**
index 16f5833..503c015 100644 (file)
@@ -81,8 +81,8 @@ public:
        String GetDescription(int pane) const { return m_strDesc[pane]; };
        void SaveAs(int nBuffer, bool packing = true) { DoFileSaveAs(nBuffer, packing); }
 private:
-       void DoFileSave(int nBuffer);
-       void DoFileSaveAs(int nBuffer, bool packing = true);
+       bool DoFileSave(int nBuffer);
+       bool DoFileSaveAs(int nBuffer, bool packing = true);
        HRESULT LoadOneFile(int index, LPCTSTR filename, bool readOnly, const String& strDesc);
        void RecompareAs(UINT id);
 // Implementation data
index f5c15ce..2f501dc 100644 (file)
@@ -35,7 +35,7 @@ static HRESULT NTAPI SE(BOOL f)
 {
        if (f)
                return S_OK;
-       HRESULT hr = (HRESULT)::GetLastError();
+       HRESULT hr = HRESULT_FROM_WIN32(::GetLastError());
        ASSERT(hr != NULL);
        if (hr == NULL)
                hr = E_UNEXPECTED;
@@ -310,17 +310,17 @@ HRESULT CHexMergeView::SaveFile(LPCTSTR path, bool packing)
        {
                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 S_OK;
+                       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 S_OK;
+               return E_FAIL;
        path = strPath.c_str();
        // Take a chance to create a backup
        if (!CMergeApp::CreateBackup(false, path))
-               return S_OK;
+               return E_FAIL;
        // Write data to an intermediate file
        String tempPath = env::GetTemporaryPath();
        String sIntermediateFilename = env::GetTemporaryFileName(tempPath, _T("MRG_"), 0);
@@ -344,9 +344,9 @@ HRESULT CHexMergeView::SaveFile(LPCTSTR path, bool packing)
        if (hr != S_OK)
                return hr;
 
-       if (packing)
+       CHexMergeDoc* pDoc = static_cast<CHexMergeDoc*>(GetDocument());
+       if (packing && !pDoc->GetUnpacker()->m_PluginName.empty())
        {
-               CHexMergeDoc* pDoc = static_cast<CHexMergeDoc*>(GetDocument());
                if (!FileTransform::Packing(sIntermediateFilename, path, *pDoc->GetUnpacker(), m_unpackerSubcode))
                {
                        String str = CMergeApp::GetPackingErrorMessage(m_nThisPane, pDoc->m_nBuffers, path, pDoc->GetUnpacker()->m_PluginName);