OSDN Git Service

PATCH: [ 1236598 ] Add binary file check to quick contents compare
authorKimmo Varis <kimmov@gmail.com>
Wed, 13 Jul 2005 08:47:26 +0000 (08:47 +0000)
committerKimmo Varis <kimmov@gmail.com>
Wed, 13 Jul 2005 08:47:26 +0000 (08:47 +0000)
Src/ByteComparator.cpp
Src/ByteComparator.h
Src/DiffWrapper.cpp
Src/readme.txt

index e793cf2..afd83f0 100644 (file)
@@ -249,3 +249,16 @@ need_more:
                return RESULT_SAME;
        }
 }
+
+/**
+ * @brief Reset all ignore settings for compare.
+ * Causes compare to be done byte-per-byte which we want for binary files.
+ */
+void ByteComparator::ResetIgnore()
+{
+       m_ignore_case = false;
+       m_ignore_space_change = false;
+       m_ignore_all_space = false;
+       m_ignore_eol_diff = false;
+       m_ignore_blank_lines = false;
+}
index 7938ab9..180235f 100644 (file)
@@ -24,6 +24,7 @@ public:
        typedef enum { RESULT_DIFF, RESULT_SAME, NEED_MORE_0, NEED_MORE_1, NEED_MORE_BOTH } COMP_RESULT;
 
        COMP_RESULT CompareBuffers(LPCSTR &ptr0, LPCSTR &ptr1, LPCSTR end0, LPCSTR end1, bool eof0, bool eof1);
+       void ResetIgnore();
 
 private:
        // settings
index ce21eaa..ee054e5 100644 (file)
@@ -58,6 +58,7 @@ static const int MEG = 1024 * 1024; // Mega(byte)
 static const int CMP_SIZE_LIMIT = 2 * MEG;
 
 static void GetComparePaths(CDiffContext * pCtxt, const DIFFITEM &di, CString & left, CString & right);
+static inline BOOL isBinaryBuf(char * bufBegin, char * bufEnd);
 
 /**
  * @brief Default constructor
@@ -1671,6 +1672,7 @@ int DiffFileData::byte_compare_files()
        FILE * fp[2]; // for files to compare
        FileHandle fhd[2]; // to ensure file handles fp get closed
        int i;
+       int diffcode = 0;
 
        // Open both files
        for (i=0; i<2; ++i)
@@ -1725,18 +1727,29 @@ int DiffFileData::byte_compare_files()
                LPCSTR end0 = &buff[0][bfend[0]];
                LPCSTR end1 = &buff[1][bfend[1]];
 
+               BOOL bBin0 = isBinaryBuf(&buff[0][bfstart[0]], &buff[0][bfend[0]]);
+               BOOL bBin1 = isBinaryBuf(&buff[1][bfstart[1]], &buff[1][bfend[1]]);
+
+               // If either buffer is binary file, don't bother ignoring differences
+               // for whitespaces or EOLs anymore.
+               if (bBin0 || bBin1)
+               {
+                       diffcode |= DIFFCODE::BIN;
+                       comparator.ResetIgnore();
+               }
+
                // are these two buffers the same?
                if (!comparator.CompareBuffers(ptr0, ptr1, end0, end1, eof[0], eof[1]))
-                       return DIFFCODE::DIFF;
+                       return diffcode | DIFFCODE::DIFF;
 
 
                // did we finish both files?
                if (eof[0] && eof[1])
                {
                        if (ptr0 == end0 && ptr1 == end1)
-                               return DIFFCODE::SAME;
+                               return diffcode | DIFFCODE::SAME;
                        else
-                               return DIFFCODE::DIFF;
+                               return diffcode | DIFFCODE::DIFF;
                }
 
                // move our current pointers over what we just compared
@@ -1746,3 +1759,20 @@ int DiffFileData::byte_compare_files()
                bfstart[1] += ptr1-orig1;
        }
 }
+
+/**
+ * @brief Check if given buffer contains zero-bytes.
+ * If buffer has zero-bytes we determine it contains binary data.
+ * @param [in] bufBegin Start address of the buffer.
+ * @param [in] bufEnd End address of the buffer.
+ * @return TRUE if zero-bytes found.
+ */
+inline BOOL isBinaryBuf(char * bufBegin, char * bufEnd)
+{
+       for (char * pByte = bufBegin; pByte <= bufEnd; ++pByte)
+       {
+               if (*pByte == 0x0)
+                       return TRUE;
+       }
+       return FALSE;
+}
index 2c29fa5..5aa63e1 100644 (file)
@@ -1,6 +1,8 @@
 2005-07-13 Kimmo
  PATCH: [ 1236602 ] Add icon for messagebox about overwriting patch file
   Src: PatchDlg.cpp
+ PATCH: [ 1236598 ] Add binary file check to quick contents compare
+  Src: ByteComparator.cpp ByteComparator.h DiffWrapper.cpp
 
 2005-07-11 Perry
  PATCH: [ 1229024 ] Rename CPropFilter to CPropLineFilter