From 455a09bb3893789eb06f32f4b0487d6c2acde679 Mon Sep 17 00:00:00 2001 From: Kimmo Varis Date: Wed, 13 Jul 2005 08:47:26 +0000 Subject: [PATCH] PATCH: [ 1236598 ] Add binary file check to quick contents compare --- Src/ByteComparator.cpp | 13 +++++++++++++ Src/ByteComparator.h | 1 + Src/DiffWrapper.cpp | 36 +++++++++++++++++++++++++++++++++--- Src/readme.txt | 2 ++ 4 files changed, 49 insertions(+), 3 deletions(-) diff --git a/Src/ByteComparator.cpp b/Src/ByteComparator.cpp index e793cf2ab..afd83f0e8 100644 --- a/Src/ByteComparator.cpp +++ b/Src/ByteComparator.cpp @@ -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; +} diff --git a/Src/ByteComparator.h b/Src/ByteComparator.h index 7938ab923..180235f30 100644 --- a/Src/ByteComparator.h +++ b/Src/ByteComparator.h @@ -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 diff --git a/Src/DiffWrapper.cpp b/Src/DiffWrapper.cpp index ce21eaa1f..ee054e53d 100644 --- a/Src/DiffWrapper.cpp +++ b/Src/DiffWrapper.cpp @@ -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; +} diff --git a/Src/readme.txt b/Src/readme.txt index 2c29fa56e..5aa63e12a 100644 --- a/Src/readme.txt +++ b/Src/readme.txt @@ -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 -- 2.11.0