OSDN Git Service

PATCH: [ 792668 ] Move file load code to CMergeDoc
authorKimmo Varis <kimmov@gmail.com>
Sun, 24 Aug 2003 11:24:42 +0000 (11:24 +0000)
committerKimmo Varis <kimmov@gmail.com>
Sun, 24 Aug 2003 11:24:42 +0000 (11:24 +0000)
Src/MainFrm.cpp
Src/MergeDoc.cpp
Src/MergeDoc.h
Src/readme.txt

index adff100..6fe86a3 100644 (file)
@@ -38,8 +38,6 @@
 #include "MergeEditView.h"
 
 #include "diff.h"
-#include "getopt.h"
-#include "fnmatch.h"
 #include "coretools.h"
 #include "Splash.h"
 #include "VssPrompt.h"
@@ -56,7 +54,6 @@
 #include "multimon.h"
 #include "paths.h"
 #include "WaitStatusCursor.h"
-#include "files.h"
 
 
 #ifdef _DEBUG
@@ -303,82 +300,20 @@ void CMainFrame::OnFileOpen()
        DoFileOpen();
 }
 
+/// Creates new MergeDoc instance and shows documents
 void CMainFrame::ShowMergeDoc(CDirDoc * pDirDoc, LPCTSTR szLeft, LPCTSTR szRight)
 {
-       CString sError;
        BOOL docNull;
+       BOOL bOpenSuccess = FALSE;
        int nRescanResult = RESCAN_OK;
        CMergeDoc * pMergeDoc = GetMergeDocToShow(pDirDoc, &docNull);
 
        ASSERT(pMergeDoc);              // must ASSERT to get an answer to the question below ;-)
        if (!pMergeDoc) return; // when does this happen ?
 
-       CChildFrame *pf = pMergeDoc->GetParentFrame();
-       ASSERT(pf);
-
-       pMergeDoc->m_strLeftFile = szLeft;
-       pMergeDoc->m_strRightFile = szRight;
-       pMergeDoc->m_ltBuf.FreeAll();
-       pMergeDoc->m_rtBuf.FreeAll();
-       pMergeDoc->m_ltBuf.SetEolSensitivity(m_bEolSensitive);
-       pMergeDoc->m_rtBuf.SetEolSensitivity(m_bEolSensitive);
-       pMergeDoc->undoTgt.clear();
-       pMergeDoc->curUndo = pMergeDoc->undoTgt.begin();
-
-       // Load left side
-       int nLeftSuccess = pMergeDoc->m_ltBuf.LoadFromFile(szLeft);
-       if (nLeftSuccess != FRESULT_OK)
-       {
-               if (nLeftSuccess == FRESULT_ERROR)
-                       AfxFormatString1(sError, IDS_ERROR_FILE_NOT_FOUND, szLeft);
-               else if (nLeftSuccess == FRESULT_BINARY)
-               {
-                       sError.LoadString(IDS_FILEBINARY);
-                       sError += "\n(";
-                       sError += szLeft;
-                       sError += ")";
-               }
-       }
-
-       int nRightSuccess = FRESULT_ERROR;
-       
-       // Load right side only if left side was succesfully loaded
-       if (nLeftSuccess == FRESULT_OK)
-               nRightSuccess = pMergeDoc->m_rtBuf.LoadFromFile(szRight);
-
-       // Left side was OK but right side failed
-       if (nLeftSuccess == FRESULT_OK && nRightSuccess != FRESULT_OK)
-       {
-               pMergeDoc->m_rtBuf.InitNew();
-
-               if (nRightSuccess == FRESULT_ERROR)
-                       AfxFormatString1(sError, IDS_ERROR_FILE_NOT_FOUND, szRight);
-               else if (nRightSuccess == FRESULT_BINARY)
-               {
-                       sError.LoadString(IDS_FILEBINARY);
-                       sError += "\n(";
-                       sError += szRight;
-                       sError += ")";
-               }
-       }
-       // Bail out if either side failed
-       if (!sError.IsEmpty())
-       {
-               pMergeDoc->m_ltBuf.FreeAll();
-               pMergeDoc->m_ltBuf.InitNew();
-               pMergeDoc->m_rtBuf.FreeAll();
-               pMergeDoc->m_rtBuf.InitNew();
+       bOpenSuccess = pMergeDoc->OpenDocs(szLeft, szRight);
 
-               AfxMessageBox(sError, MB_OK | MB_ICONSTOP);
-               pMergeDoc->GetParentFrame()->DestroyWindow();
-               return;
-       }
-       
-       nRescanResult = pMergeDoc->Rescan();
-
-       // Open different and identical files
-       if (nRescanResult == RESCAN_OK ||
-               nRescanResult == RESCAN_IDENTICAL)
+       if (bOpenSuccess)
        {
                if (docNull)
                {
@@ -387,61 +322,6 @@ void CMainFrame::ShowMergeDoc(CDirDoc * pDirDoc, LPCTSTR szLeft, LPCTSTR szRight
                }
                else
                        MDINext();
-
-               CMergeEditView * pLeft = pMergeDoc->GetLeftView();
-               CMergeEditView * pRight = pMergeDoc->GetRightView();
-                       
-               // scroll to first diff
-               if(m_bScrollToFirst && pMergeDoc->m_diffs.GetSize()!=0)
-               {
-                       pLeft->SelectDiff(0, TRUE, FALSE);
-               }
-
-               // Enable/disable automatic rescan (rescanning after edit)
-               pLeft->EnableRescan(m_bAutomaticRescan);
-               pRight->EnableRescan(m_bAutomaticRescan);
-
-               // set the document types
-               CString sext;
-               SplitFilename(szLeft, 0, 0, &sext);
-               pLeft->SetTextType(sext);
-               SplitFilename(szRight, 0, 0, &sext);
-               pRight->SetTextType(sext);
-
-               // SetTextType will revert to language dependent defaults for tab
-               pLeft->SetTabSize(mf->m_nTabSize);
-               pRight->SetTabSize(mf->m_nTabSize);
-               pLeft->SetViewTabs(mf->m_bViewWhitespace);
-               pRight->SetViewTabs(mf->m_bViewWhitespace);
-               pLeft->SetViewEols(mf->m_bViewWhitespace);
-               pRight->SetViewEols(mf->m_bViewWhitespace);
-
-               // Enable Backspace at beginning of line 
-               pLeft->SetDisableBSAtSOL( FALSE ); 
-               pRight->SetDisableBSAtSOL( FALSE ); 
-
-               // set the frame window header
-               pf->SetHeaderText(0, szLeft);
-               pf->SetHeaderText(1, szRight);
-
-               // Set tab type (tabs/spaces)
-               BOOL bInsertTabs = (m_nTabType == 0);
-               pLeft->SetInsertTabs(bInsertTabs);
-               pRight->SetInsertTabs(bInsertTabs);
-
-               // Inform user that files are identical
-               if (nRescanResult == RESCAN_IDENTICAL)
-                       pMergeDoc->ShowRescanError(nRescanResult);
-       }
-       else
-       {
-               // CMergeDoc::Rescan fails if files are identical, or 
-               // does not exist on both sides or the really arcane case
-               // that the temp files couldn't be created, which is too
-               // obscure to bother reporting if you can't write to your
-               // temp directory, doing nothing is graceful enough for that).
-               pMergeDoc->ShowRescanError(nRescanResult);
-               pMergeDoc->GetParentFrame()->DestroyWindow();
        }
 }
 
index aa2fdd4..f1a3f85 100644 (file)
@@ -1742,3 +1742,159 @@ void CMergeDoc::Showlinediff(CMergeEditView * pView)
        pView->SelectArea(ptBegin, ptEnd);
        pView->SetCursorPos(ptBegin);
 }
+
+/**
+* @brief Loads file to buffer and shows load-errors
+*
+* @param sFileName File to open
+* @param bLeft Left/right-side file
+*
+* @return Tells if files were loaded succesfully
+*
+* @note Binary loading message could be improved...
+*
+* @sa CMergeDoc::OpenDocs()
+*
+*/
+int CMergeDoc::LoadFile(CString sFileName, BOOL bLeft)
+{
+       CDiffTextBuffer *pBuf;
+       CString sError;
+       int retVal = FRESULT_ERROR;
+
+       if (bLeft)
+       {
+               pBuf = &m_ltBuf;
+               m_strLeftFile = sFileName;
+       }
+       else
+       {
+               pBuf = &m_rtBuf;
+               m_strRightFile = sFileName;
+       }
+
+       // FreeAll() is needed before loading (this is complicated)
+       pBuf->FreeAll();
+       retVal = pBuf->LoadFromFile(sFileName);
+
+       if (retVal != FRESULT_OK)
+       {
+               if (retVal == FRESULT_ERROR)
+                       AfxFormatString1(sError, IDS_ERROR_FILE_NOT_FOUND, sFileName);
+               else if (retVal == FRESULT_BINARY)
+               {
+                       sError.LoadString(IDS_FILEBINARY);
+                       sError += "\n(";
+                       sError += sFileName;
+                       sError += ")";
+               }
+
+               // Clear buffer, but don't leave as uninitialised
+               pBuf->FreeAll();
+               pBuf->InitNew();
+               AfxMessageBox(sError, MB_OK | MB_ICONSTOP);
+       }
+       return retVal;
+}
+
+/**
+* @brief Loads files and does initial rescan
+*
+* @param sLeftFile File to open to left side
+* @param sRightFile File to open to right side
+*
+* @return Tells if files were loaded and scanned succesfully
+*
+* @note Options are still read from CMainFrame, this will change
+*
+* @sa CMainFrame::ShowMergeDoc()
+*
+*/
+BOOL CMergeDoc::OpenDocs(CString sLeftFile, CString sRightFile)
+{
+       int nRescanResult = RESCAN_OK;
+       CChildFrame *pf = GetParentFrame();
+       ASSERT(pf);
+
+       m_ltBuf.SetEolSensitivity(mf->m_bEolSensitive);
+       m_rtBuf.SetEolSensitivity(mf->m_bEolSensitive);
+       undoTgt.clear();
+       curUndo = undoTgt.begin();
+
+       // Load left side file
+       int nLeftSuccess = LoadFile(sLeftFile, TRUE);
+       
+       // Load right side only if left side was succesfully loaded
+       int nRightSuccess = FRESULT_ERROR;
+       if (nLeftSuccess == FRESULT_OK)
+               nRightSuccess = LoadFile(sRightFile, FALSE);
+
+       // Bail out if either side failed
+       if (nLeftSuccess != FRESULT_OK || nRightSuccess != FRESULT_OK)
+       {
+               GetParentFrame()->DestroyWindow();
+               return FALSE;
+       }
+       
+       nRescanResult = Rescan();
+
+       // Open different and identical files
+       if (nRescanResult == RESCAN_OK ||
+               nRescanResult == RESCAN_IDENTICAL)
+       {
+               CMergeEditView * pLeft = GetLeftView();
+               CMergeEditView * pRight = GetRightView();
+                       
+               // scroll to first diff
+               if(mf->m_bScrollToFirst && m_diffs.GetSize() != 0)
+                       pLeft->SelectDiff(0, TRUE, FALSE);
+
+               // Enable/disable automatic rescan (rescanning after edit)
+               pLeft->EnableRescan(mf->m_bAutomaticRescan);
+               pRight->EnableRescan(mf->m_bAutomaticRescan);
+
+               // set the document types
+               CString sext;
+               SplitFilename(sLeftFile, 0, 0, &sext);
+               pLeft->SetTextType(sext);
+               SplitFilename(sRightFile, 0, 0, &sext);
+               pRight->SetTextType(sext);
+
+               // SetTextType will revert to language dependent defaults for tab
+               pLeft->SetTabSize(mf->m_nTabSize);
+               pRight->SetTabSize(mf->m_nTabSize);
+               pLeft->SetViewTabs(mf->m_bViewWhitespace);
+               pRight->SetViewTabs(mf->m_bViewWhitespace);
+               pLeft->SetViewEols(mf->m_bViewWhitespace);
+               pRight->SetViewEols(mf->m_bViewWhitespace);
+       
+               // Enable Backspace at beginning of line
+               pLeft->SetDisableBSAtSOL(FALSE);
+               pRight->SetDisableBSAtSOL(FALSE);
+               
+               // set the frame window header
+               pf->SetHeaderText(0, sLeftFile);
+               pf->SetHeaderText(1, sRightFile);
+
+               // Set tab type (tabs/spaces)
+               BOOL bInsertTabs = (mf->m_nTabType == 0);
+               pLeft->SetInsertTabs(bInsertTabs);
+               pRight->SetInsertTabs(bInsertTabs);
+
+               // Inform user that files are identical
+               if (nRescanResult == RESCAN_IDENTICAL)
+                       ShowRescanError(nRescanResult);
+       }
+       else
+       {
+               // CMergeDoc::Rescan fails if files are identical, or 
+               // does not exist on both sides or the really arcane case
+               // that the temp files couldn't be created, which is too
+               // obscure to bother reporting if you can't write to your
+               // temp directory, doing nothing is graceful enough for that).
+               ShowRescanError(nRescanResult);
+               GetParentFrame()->DestroyWindow();
+               return FALSE;
+       }
+       return TRUE;
+}
index 7be93cb..b208c70 100644 (file)
@@ -170,6 +170,8 @@ public:
        UINT m_nDiffs;
        CString m_strLeftFile, m_strRightFile;
 
+       BOOL OpenDocs(CString sLeftFile, CString sRightFile);
+       int LoadFile(CString sFileName, BOOL bLeft);
        void RescanIfNeeded(float timeOutInSecond);
        int Rescan(BOOL bForced = FALSE);
        void ShowRescanError(int nRescanResult);
index ede3a69..0979255 100644 (file)
@@ -1,3 +1,7 @@
+2003-08-24 Kimmo
+ PATCH: [ 792668 ] Move file load code to CMergeDoc
+  WinMerge: MainFrm.cpp MergeDoc.h MergeDoc.cpp
+
 2003-08-23 Perry
  PATCH: [ 791487 ] Norwegian (bokmÃ¥l) translation
   Corrections#2 Avbrytt->Avbryt and #3 Opdater->Oppdater