OSDN Git Service

PATCH: [ 1529599 ] Cleanup DiffWrapper
authorKimmo Varis <kimmov@gmail.com>
Fri, 28 Jul 2006 07:48:37 +0000 (07:48 +0000)
committerKimmo Varis <kimmov@gmail.com>
Fri, 28 Jul 2006 07:48:37 +0000 (07:48 +0000)
Src/Changes.txt
Src/DiffWrapper.cpp
Src/DiffWrapper.h
Src/MergeDoc.cpp
Src/PatchTool.cpp

index d61354d..96a0b91 100644 (file)
@@ -2,6 +2,10 @@ Src\Changes.txt
 Add new items to top.
 (This summarizes all changes to all files under Src, including Src\Languages.)
 
+2006-07-28 Kimmo
+ PATCH: [ 1529599 ] Cleanup DiffWrapper
+  Src: DiffWrapper.cpp DiffWrapper.h MergeDoc.cpp PatchTool.cpp
+
 2006-07-27 Takashi
  PATCH: [ 1527843 ] prevent dialog from flickering
   Src: DirCompProgressDlg.cpp DirView.cpp
index b3fbab8..bdeefb8 100644 (file)
@@ -207,22 +207,24 @@ private:
 };
 
 /**
- * @brief Default constructor
+ * @brief Default constructor.
+ * Initializes members and creates new FilterCommentsManager.
  */
-CDiffWrapper::CDiffWrapper():m_FilterCommentsManager(new FilterCommentsManager)
+CDiffWrapper::CDiffWrapper()
+: m_FilterCommentsManager(new FilterCommentsManager)
+, m_bCreatePatchFile(FALSE)
+, m_bUseDiffList(FALSE)
+, m_bDetectMovedBlocks(FALSE)
+, m_bAddCmdLine(TRUE)
+, m_bAppendFiles(FALSE)
+, m_nDiffs(0)
+, m_infoPrediffer(NULL)
+, m_pDiffList(NULL)
+, m_bPathsAreTemp(FALSE)
 {
        ZeroMemory(&m_settings, sizeof(DIFFSETTINGS));
        ZeroMemory(&m_globalSettings, sizeof(DIFFSETTINGS));
        ZeroMemory(&m_status, sizeof(DIFFSTATUS));
-       m_bCreatePatchFile = FALSE;
-       m_bUseDiffList = FALSE;
-       m_bDetectMovedBlocks = FALSE;
-       m_bAddCmdLine = TRUE;
-       m_bAppendFiles = FALSE;
-       m_nDiffs = 0;
-       m_infoPrediffer = NULL;
-       m_pDiffList = NULL;
-
        m_settings.heuristic = 1;
        m_settings.outputStyle = OUTPUT_NORMAL;
        m_settings.context = -1;
@@ -241,25 +243,45 @@ CDiffWrapper::~CDiffWrapper()
 }
 
 /**
- * @brief Sets filename of produced patch-file.
- * @param [in] file Filename of patch file.
+ * @brief Enables/disables patch-file creation and sets filename.
+ * This function enables or disables patch file creation. When
+ * @p filename is empty, patch files are disabled.
+ * @param [in] filename Filename for patch file, or empty string.
  */
-void CDiffWrapper::SetPatchFile(const CString &file)
+void CDiffWrapper::SetCreatePatchFile(const CString &filename)
 {
-       m_sPatchFile = file;
-       m_sPatchFile.Replace('/', '\\');
+       if (filename.IsEmpty())
+       {
+               m_bCreatePatchFile = FALSE;
+               m_sPatchFile.Empty();
+       }
+       else
+       {
+               m_bCreatePatchFile = TRUE;
+               m_sPatchFile = filename;
+               m_sPatchFile.Replace('/', '\\');
+       }
 }
 
 /**
- * @brief Sets pointer to DiffList getting compare results.
- * CDiffWrapper adds compare results to DiffList. This function
- * sets external DiffList which gets compare results.
+ * @brief Enables/disabled DiffList creation ands sets DiffList.
+ * This function enables or disables DiffList creation. When
+ * @p diffList is NULL difflist is not created. When valid DiffList
+ * pointer is given, compare results are stored into it.
  * @param [in] difflist Pointer to DiffList getting compare results.
  */
-void CDiffWrapper::SetDiffList(DiffList *diffList)
+void CDiffWrapper::SetCreateDiffList(DiffList *diffList)
 {
-       ASSERT(diffList);
-       m_pDiffList = diffList;
+       if (diffList == NULL)
+       {
+               m_bUseDiffList = FALSE;
+               m_pDiffList = NULL;
+       }
+       else
+       {
+               m_bUseDiffList = TRUE;
+               m_pDiffList = diffList;
+       }
 }
 
 /**
@@ -335,53 +357,6 @@ void CDiffWrapper::SetPatchOptions(const PATCHOPTIONS *options)
 }
 
 /**
- * @brief Determines if external results-list is used.
- * When diff'ing files results are stored to external DiffList,
- * set by SetDiffList(). This function determines if we are currently
- * using that external list.
- * @return TRUE if results are added to external DiffList.
- */
-BOOL CDiffWrapper::GetUseDiffList() const
-{
-       return m_bUseDiffList;
-}
-
-/**
- * @brief Enables/disables external result-list.
- * CDiffWrapper uses external DiffList to return compare results.
- * This function enables/disables usage of that external DiffList.
- * @param [in] bUseDifList TRUE if external DiffList is used.
- * @return Old value of the setting.
- */
-BOOL CDiffWrapper::SetUseDiffList(BOOL bUseDiffList)
-{
-       BOOL temp = m_bUseDiffList;
-       m_bUseDiffList = bUseDiffList;
-       return temp;
-}
-
-/**
- * @brief Determines if patch-file is created.
- * @return TRUE if patch file will be created.
- */
-BOOL CDiffWrapper::GetCreatePatchFile() const 
-{
-       return m_bCreatePatchFile;
-}
-
-/**
- * @brief Enables/disables creation of patch-file.
- * @param [in] bCreatePatchFile If TRUE patch file will be created.
- * @return Previous value for setting.
- */
-BOOL CDiffWrapper::SetCreatePatchFile(BOOL bCreatePatchFile)
-{
-       BOOL temp = m_bCreatePatchFile;
-       m_bCreatePatchFile = bCreatePatchFile;
-       return temp;
-}
-
-/**
  * @brief Test for trivial only characters in string
  * @param [in] Start                           - Start position in string
  * @param [in] End                                     - One character pass the end position of the string
@@ -740,14 +715,19 @@ void PostFilter(int LineNumberLeft, int QtyLinesLeft, int LineNumberRight, int Q
 
 /**
  * @brief Set source paths for diffing two files.
- * Sets full paths to two files we are diffing.
+ * Sets full paths to two files we are diffing. Paths can be actual user files
+ * or temporary copies of user files. Parameter @p tempPaths tells if paths
+ * are temporary paths that can be deleted.
  * @param [in] filepath1 First file to compare "original file".
  * @param [in] filepath2 Second file to compare "changed file".
+ * @param [in] tempPaths Are given paths temporary (can be deleted)?.
  */
-void CDiffWrapper::SetPaths(const CString &filepath1, const CString &filepath2)
+void CDiffWrapper::SetPaths(const CString &filepath1, const CString &filepath2,
+               BOOL tempPaths)
 {
        m_s1File = filepath1;
        m_s2File = filepath2;
+       m_bPathsAreTemp = tempPaths;
 }
 
 /**
@@ -779,7 +759,7 @@ void CDiffWrapper::SetAlternativePaths(const CString &altPath1, const CString &a
 /**
  * @brief Runs diff-engine.
  */
-BOOL CDiffWrapper::RunFileDiff(ARETEMPFILES areTempFiles)
+BOOL CDiffWrapper::RunFileDiff()
 {
        CString filepath1(m_s1File);
        CString filepath2(m_s2File);
@@ -795,48 +775,51 @@ BOOL CDiffWrapper::RunFileDiff(ARETEMPFILES areTempFiles)
        if (m_bUseDiffList)
                m_nDiffs = m_pDiffList->GetSize();
 
-       // Are our working files overwritable (temp)?
-       BOOL bMayOverwrite = (areTempFiles == YESTEMPFILES);
-
        // Do the preprocessing now, overwrite the temp files
        // NOTE: FileTransform_UCS2ToUTF8() may create new temp
        // files and return new names, those created temp files
        // are deleted in end of function.
        if (m_infoPrediffer->bToBeScanned)
        {
-               // this can only fail if the data can not be saved back (no more place on disk ???)
-               // what to do then ??
-               FileTransform_Prediffing(strFile1Temp, m_sToFindPrediffer, m_infoPrediffer, bMayOverwrite);
+               // this can only fail if the data can not be saved back (no more
+               // place on disk ???) What to do then ??
+               FileTransform_Prediffing(strFile1Temp, m_sToFindPrediffer, m_infoPrediffer,
+                       m_bPathsAreTemp);
        }
        else
        {
-               // this can failed if the prediffer has a problem
-               if (FileTransform_Prediffing(strFile1Temp, *m_infoPrediffer, bMayOverwrite) == FALSE)
+               // This can fail if the prediffer has a problem
+               if (FileTransform_Prediffing(strFile1Temp, *m_infoPrediffer,
+                       m_bPathsAreTemp) == FALSE)
                {
                        // display a message box
                        CString sError;
-                       AfxFormatString2(sError, IDS_PREDIFFER_ERROR, strFile1Temp, m_infoPrediffer->pluginName);
+                       AfxFormatString2(sError, IDS_PREDIFFER_ERROR, strFile1Temp,
+                               m_infoPrediffer->pluginName);
                        AfxMessageBox(sError, MB_OK | MB_ICONSTOP);
                        // don't use any more this prediffer
                        m_infoPrediffer->bToBeScanned = FALSE;
-                       m_infoPrediffer->pluginName = _T("");
+                       m_infoPrediffer->pluginName.Empty();
                }
        }
 
-       FileTransform_UCS2ToUTF8(strFile1Temp, bMayOverwrite);
-       // we use the same plugin for both files, so it must be defined before second file
+       FileTransform_UCS2ToUTF8(strFile1Temp, m_bPathsAreTemp);
+       // We use the same plugin for both files, so it must be defined before
+       // second file
        ASSERT(m_infoPrediffer->bToBeScanned == FALSE);
-       if (FileTransform_Prediffing(strFile2Temp, *m_infoPrediffer, bMayOverwrite) == FALSE)
+       if (FileTransform_Prediffing(strFile2Temp, *m_infoPrediffer,
+               m_bPathsAreTemp) == FALSE)
        {
                // display a message box
                CString sError;
-               AfxFormatString2(sError, IDS_PREDIFFER_ERROR, strFile2Temp, m_infoPrediffer->pluginName);
+               AfxFormatString2(sError, IDS_PREDIFFER_ERROR, strFile2Temp,
+                       m_infoPrediffer->pluginName);
                AfxMessageBox(sError, MB_OK | MB_ICONSTOP);
                // don't use any more this prediffer
                m_infoPrediffer->bToBeScanned = FALSE;
                m_infoPrediffer->pluginName = _T("");
        }
-       FileTransform_UCS2ToUTF8(strFile2Temp, bMayOverwrite);
+       FileTransform_UCS2ToUTF8(strFile2Temp, m_bPathsAreTemp);
 
        DiffFileData diffdata;
        diffdata.SetDisplayFilepaths(filepath1, filepath2); // store true names for diff utils patch file
@@ -911,7 +894,6 @@ BOOL CDiffWrapper::RunFileDiff(ARETEMPFILES areTempFiles)
 
        FreeDiffUtilsScript(script);
 
-
        // Done with diffutils filedata
        diffdata.Close();
 
@@ -2162,7 +2144,6 @@ CDiffWrapper::LoadWinMergeDiffsFromDiffUtilsScript(struct change * script, const
  * delimiters from \ to / since we want to keep compatibility with patch-tools.
  * @param [in] script list of changes.
  * @param [in] inf file_data table containing filenames
- * @todo filepath1 and filepath2 aren't really needed, paths are already in inf.
  */
 void CDiffWrapper::WritePatchFile(struct change * script, file_data * inf)
 {
index 7e058a2..b905f76 100644 (file)
@@ -17,7 +17,7 @@
 /** 
  * @file  DiffWrapper.h
  *
- * @brief Declaration file for CDiffWrapper
+ * @brief Declaration file for CDiffWrapper.
  *
  * @date  Created: 2003-08-22
  */
@@ -121,11 +121,6 @@ enum DIFF_OUTPUT_TYPE
        DIFF_OUTPUT_SDIFF
 };
 
-typedef enum {
-       YESTEMPFILES // arguments are temp files
-       , NOTEMPFILES // arguments not temp files
-} ARETEMPFILES;
-
 /**
  * @brief Diffutils options users of this class must use
  */
@@ -186,14 +181,20 @@ struct DIFFSETTINGS
 class FilterCommentsManager;
 
 /**
- * @brief Wrapper class for GNU/diffutils
+ * @brief Wrapper class for diffengine (diffutils and ByteComparator).
+ * Diffwappre class is used to run selected diffengine. For folder compare
+ * there are several methods (COMPARE_TYPE), but for file compare diffutils
+ * is used always. For file compare diffutils can output results to external
+ * DiffList or to patch file. Output type must be selected with member
+ * functions SetCreatePatchFile() and SetCreateDiffList().
  */
 class CDiffWrapper
 {
 public:
        CDiffWrapper();
        ~CDiffWrapper();
-       void SetPatchFile(const CString &file);
+       void SetCreatePatchFile(const CString &filename);
+       void SetCreateDiffList(DiffList *diffList);
        void SetDiffList(DiffList *diffList);
        void GetOptions(DIFFOPTIONS *options) const;
        void SetOptions(const DIFFOPTIONS *options);
@@ -202,17 +203,13 @@ public:
        void GetPrediffer(PrediffingInfo * prediffer);
        void GetPatchOptions(PATCHOPTIONS *options) const;
        void SetPatchOptions(const PATCHOPTIONS *options);
-       BOOL GetUseDiffList() const;
-       BOOL SetUseDiffList(BOOL bUseDiffList);
        void SetDetectMovedBlocks(BOOL bDetectMovedBlocks) { m_bDetectMovedBlocks = bDetectMovedBlocks; }
        BOOL GetDetectMovedBlocks() { return m_bDetectMovedBlocks; }
        BOOL GetAppendFiles() const;
        BOOL SetAppendFiles(BOOL bAppendFiles);
-       BOOL GetCreatePatchFile() const;
-       BOOL SetCreatePatchFile(BOOL bCreatePatchFile);
-       void SetPaths(const CString &filepath1, const CString &filepath2);
+       void SetPaths(const CString &filepath1, const CString &filepath2, BOOL tempPaths);
        void SetAlternativePaths(const CString &altPath1, const CString &altPath2);
-       BOOL RunFileDiff(ARETEMPFILES areTempFiles);
+       BOOL RunFileDiff();
        void GetDiffStatus(DIFFSTATUS *status);
        void AddDiffRange(UINT begin0, UINT end0, UINT begin1, UINT end1, BYTE op);
        void FixLastDiffRange(int leftBufferLines, int rightBufferLines, BOOL left);
@@ -237,9 +234,9 @@ protected:
        void WritePatchFile(struct change * script, file_data * inf);
 
 private:
-       DIFFSETTINGS m_settings; 
-       DIFFSETTINGS m_globalSettings;  // Temp for storing globals
-       DIFFSTATUS m_status;
+       DIFFSETTINGS m_settings; /**< Compare settings for current compare */
+       DIFFSETTINGS m_globalSettings; /**< Global compare settings */
+       DIFFSTATUS m_status; /**< Status of last compare */
        CString m_s1File; /**< Full path to first diff'ed file. */
        CString m_s2File; /**< Full path to second diff'ed file. */
        CString m_s1AlternativePath; /**< First file's alternative path (may be relative). */
@@ -247,6 +244,7 @@ private:
        CString m_sOriginalFile1; /**< First file's original (NON-TEMP) path. */
        CString m_sOriginalFile2; /**< Second file's original (NON-TEMP) path. */
        CString m_sPatchFile; /**< Full path to created patch file. */
+       BOOL m_bPathsAreTemp; /**< Are compared paths temporary? */
        /// prediffer info are stored only for MergeDoc
        PrediffingInfo * m_infoPrediffer;
        /// prediffer info are stored only for MergeDoc
@@ -256,11 +254,11 @@ private:
        BOOL m_bCreatePatchFile; /**< Do we create a patch file? */
        BOOL m_bAddCmdLine; /**< Do we add commandline to patch file? */
        BOOL m_bAppendFiles; /**< Do we append to existing patch file? */
-       int m_nDiffs;
-       DiffList *m_pDiffList;
-       CMap<int, int, int, int> m_moved0;
-       CMap<int, int, int, int> m_moved1;
-       FilterCommentsManager * m_FilterCommentsManager;
+       int m_nDiffs; /**< Difference count */
+       DiffList *m_pDiffList; /**< Pointer to external DiffList */
+       CMap<int, int, int, int> m_moved0; /**< Moved lines map for first side */
+       CMap<int, int, int, int> m_moved1; /**< Moved lines map for second side */
+       FilterCommentsManager * m_FilterCommentsManager; /**< Comments filtering manager */
 };
 
 // forward declarations needed by DiffFileData
index 566b54d..fc438ec 100644 (file)
@@ -420,8 +420,7 @@ int CMergeDoc::Rescan(BOOL &bBinary, BOOL &bIdentical,
        }
 
        // Set up DiffWrapper
-       m_diffWrapper.SetDiffList(&m_diffList);
-       m_diffWrapper.SetUseDiffList(TRUE);             // Add diffs to list
+       m_diffWrapper.SetCreateDiffList(&m_diffList);
        m_diffWrapper.GetOptions(&diffOptions);
        
        // Clear diff list
@@ -431,9 +430,9 @@ int CMergeDoc::Rescan(BOOL &bBinary, BOOL &bIdentical,
        m_diffWrapper.ClearMovedLists();
 
        // Set paths for diffing and run diff
-       m_diffWrapper.SetPaths(m_pTempFiles->GetLeft(), m_pTempFiles->GetRight());
+       m_diffWrapper.SetPaths(m_pTempFiles->GetLeft(), m_pTempFiles->GetRight(), TRUE);
        m_diffWrapper.SetCompareFiles(m_filePaths.GetLeft(), m_filePaths.GetRight());
-       diffSuccess = m_diffWrapper.RunFileDiff(YESTEMPFILES);
+       diffSuccess = m_diffWrapper.RunFileDiff();
 
        // Read diff-status
        DIFFSTATUS status;
index 9de03af..a128435 100644 (file)
@@ -88,9 +88,9 @@ int CPatchTool::CreatePatch()
                }
 
                // Select patch create -mode
-               m_diffWrapper.SetUseDiffList(FALSE);
-               m_diffWrapper.SetCreatePatchFile(TRUE);
+               m_diffWrapper.SetCreatePatchFile(m_dlgPatch.m_fileResult);
                m_diffWrapper.SetAppendFiles(m_dlgPatch.m_appendFile);
+               m_diffWrapper.SetPrediffer(NULL);
 
                int fileCount = m_dlgPatch.GetItemCount();
                POSITION pos = m_dlgPatch.GetFirstItem();
@@ -98,15 +98,12 @@ int CPatchTool::CreatePatch()
                for (int i = 0; i < fileCount; i++)
                {
                        PATCHFILES files = m_dlgPatch.GetNextItem(pos);
-                       CString filename1 = files.lfile;
-                       CString filename2 = files.rfile;
                        
                        // Set up DiffWrapper
-                       m_diffWrapper.SetPrediffer(NULL);
-                       m_diffWrapper.SetPaths(files.lfile, files.rfile);
+                       m_diffWrapper.SetPaths(files.lfile, files.rfile, FALSE);
                        m_diffWrapper.SetAlternativePaths(files.pathLeft, files.pathRight);
                        m_diffWrapper.SetCompareFiles(files.lfile, files.rfile);
-                       bDiffSuccess = m_diffWrapper.RunFileDiff(NOTEMPFILES);
+                       bDiffSuccess = m_diffWrapper.RunFileDiff();
                        m_diffWrapper.GetDiffStatus(&status);
 
                        if (!bDiffSuccess)
@@ -164,8 +161,6 @@ BOOL CPatchTool::ShowDialog()
                if (m_dlgPatch.GetItemCount() < 1)
                        bRetVal = FALSE;
 
-               m_diffWrapper.SetPatchFile(m_dlgPatch.m_fileResult);
-
                // These two are from dropdown list - can't be wrong
                patchOptions.outputStyle = m_dlgPatch.m_outputStyle;
                patchOptions.nContext = m_dlgPatch.m_contextLines;