Skip to content

Commit

Permalink
Merge branch 'master' into improve-substitution-filters
Browse files Browse the repository at this point in the history
# Conflicts:
#	Src/CompareEngines/Wrap_DiffUtils.cpp
#	Src/DiffWrapper.h
  • Loading branch information
sdottaka committed Sep 17, 2023
2 parents e8dec6f + ea8dd30 commit 0529f2e
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 241 deletions.
60 changes: 11 additions & 49 deletions Src/CompareEngines/ByteCompare.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "DiffContext.h"
#include "diff.h"
#include "ByteComparator.h"
#include "DiffFileData.h"

namespace CompareEngines
{
Expand All @@ -24,15 +25,12 @@ static const int KILO = 1024; // Kilo(byte)
/** @brief Quick contents compare's file buffer size. */
static const int WMCMPBUFF = 32 * KILO;

static void CopyTextStats(const FileTextStats * stats, FileTextStats * myTextStats);

/**
* @brief Default constructor.
*/
ByteCompare::ByteCompare()
: m_pOptions(nullptr)
, m_piAbortable(nullptr)
, m_inf(nullptr)
{
}

Expand Down Expand Up @@ -68,30 +66,15 @@ void ByteCompare::SetAbortable(const IAbortable * piAbortable)
m_piAbortable = const_cast<IAbortable*>(piAbortable);
}

/**
* @brief Set filedata.
* @param [in] items Count of filedata items to set.
* @param [in] data File data.
* @note Path names are set by SetPaths() function.
*/
void ByteCompare::SetFileData(int items, file_data *data)
{
// We support only two files currently!
assert(items == 2);
m_inf = data;
m_textStats[0].clear();
m_textStats[1].clear();
}


/**
* @brief Compare two specified files, byte-by-byte
* @param [in] bStopAfterFirstDiff Stop compare after we find first difference?
* @param [in] piAbortable Interface allowing to abort compare
* @return DIFFCODE
*/
int ByteCompare::CompareFiles(FileLocation *location)
int ByteCompare::CompareFiles(DiffFileData* diffData)
{
diffData->m_textStats[0].clear();
diffData->m_textStats[1].clear();

// TODO
// Right now, we assume files are in 8-bit encoding
// because transform code converted any UCS-2 files to UTF-8
Expand Down Expand Up @@ -137,18 +120,18 @@ int ByteCompare::CompareFiles(FileLocation *location)
{
// Assume our blocks are in range of int
int space = sizeof(buff[i])/sizeof(buff[i][0]) - (int) bfend[i];
int rtn = cio::read_i(m_inf[i].desc, &buff[i][bfend[i]], space);
int rtn = cio::read_i(diffData->m_inf[i].desc, &buff[i][bfend[i]], space);
if (rtn == -1)
return DIFFCODE::CMPERR;
if (rtn < space)
eof[i] = true;
bfend[i] += rtn;
if (m_inf[0].desc == m_inf[1].desc)
if (diffData->m_inf[0].desc == diffData->m_inf[1].desc)
{
bfstart[1] = bfstart[0];
bfend[1] = bfend[0];
eof[1] = eof[0];
location[1] = location[0];
diffData->m_FileLocation[1] = diffData->m_FileLocation[0];
memcpy(&buff[1][bfend[1] - rtn], &buff[0][bfend[0] - rtn], rtn);
break;
}
Expand All @@ -170,7 +153,7 @@ int ByteCompare::CompareFiles(FileLocation *location)
int64_t offset1 = (ptr1 - &buff[1][0]);

// are these two buffers the same?
int result = comparator.CompareBuffers(m_textStats[0], m_textStats[1],
int result = comparator.CompareBuffers(diffData->m_textStats[0], diffData->m_textStats[1],
ptr0, ptr1, end0, end1, eof[0], eof[1], offset0, offset1);
if (result == ByteComparator::RESULT_DIFF)
{
Expand Down Expand Up @@ -253,8 +236,8 @@ int ByteCompare::CompareFiles(FileLocation *location)
// then the result is reliable.
if (eof[0] && eof[1])
{
bool bBin0 = (m_textStats[0].nzeros > 0);
bool bBin1 = (m_textStats[1].nzeros > 0);
bool bBin0 = (diffData->m_textStats[0].nzeros > 0);
bool bBin1 = (diffData->m_textStats[1].nzeros > 0);

if (bBin0 && bBin1)
diffcode |= DIFFCODE::BIN | DIFFCODE::BINSIDE1 | DIFFCODE::BINSIDE2;
Expand All @@ -277,25 +260,4 @@ int ByteCompare::CompareFiles(FileLocation *location)
return diffcode;
}

/**
* @brief Copy text stat results from diffutils back into the FileTextStats structure
*/
static void CopyTextStats(const FileTextStats * stats, FileTextStats * myTextStats)
{
myTextStats->ncrlfs = stats->ncrlfs;
myTextStats->ncrs = stats->ncrs;
myTextStats->nlfs = stats->nlfs;
myTextStats->nzeros = stats->nzeros;
}

/**
* @brief Return text statistics for last compare.
* @param [in] side For which file to return statistics.
* @param [out] stats Stats as asked.
*/
void ByteCompare::GetTextStats(int side, FileTextStats *stats) const
{
CopyTextStats(&m_textStats[side], stats);
}

} // namespace CompareEngines
11 changes: 3 additions & 8 deletions Src/CompareEngines/ByteCompare.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
class CompareOptions;
class QuickCompareOptions;
class IAbortable;
struct FileLocation;
struct file_data;
struct DiffFileData;

namespace CompareEngines
{
Expand All @@ -31,16 +30,12 @@ class ByteCompare
void SetCompareOptions(const CompareOptions & options);
void SetAdditionalOptions(bool stopAfterFirstDiff);
void SetAbortable(const IAbortable * piAbortable);
void SetFileData(int items, file_data *data);
int CompareFiles(FileLocation *location);
void GetTextStats(int side, FileTextStats *stats) const;

int CompareFiles(DiffFileData* diffData);

private:
std::unique_ptr<QuickCompareOptions> m_pOptions; /**< Compare options for diffutils. */
IAbortable * m_piAbortable;
file_data * m_inf; /**< Compared files data (for diffutils). */
FileTextStats m_textStats[2];

};

} // namespace CompareEngines
75 changes: 15 additions & 60 deletions Src/CompareEngines/Wrap_DiffUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "DiffWrapper.h"
#include "xdiff_gnudiff_compat.h"
#include "unicoder.h"
#include "DiffFileData.h"

namespace CompareEngines
{
Expand All @@ -30,9 +31,7 @@ static void CopyTextStats(const file_data * inf, FileTextStats * myTextStats);
* @brief Default constructor.
*/
DiffUtils::DiffUtils()
: m_pOptions(nullptr)
, m_inf(nullptr)
, m_pDiffWrapper(new ::CDiffWrapper)
: m_pDiffWrapper(new ::CDiffWrapper)
, m_ndiffs(0)
, m_ntrivialdiffs(0)
{
Expand All @@ -49,10 +48,11 @@ DiffUtils::~DiffUtils()
* @brief Set compare options from general compare options.
* @param [in ]options General compare options.
*/
void DiffUtils::SetCompareOptions(const CompareOptions & options)
void DiffUtils::SetCompareOptions(const CompareOptions& options)
{
m_pOptions.reset(new DiffutilsOptions(static_cast<const DiffutilsOptions&>(options)));
m_pOptions->SetToDiffUtils();
DIFFOPTIONS doptions;
static_cast<const DiffutilsOptions&>(options).GetAsDiffOptions(doptions);
m_pDiffWrapper->SetOptions(&doptions);
}

/**
Expand Down Expand Up @@ -88,30 +88,18 @@ void DiffUtils::SetCodepage(int codepage)
m_pDiffWrapper->SetCodepage(codepage);
}

/**
* @brief Set filedata.
* @param [in] items Count of filedata items to set.
* @param [in] data File data.
*/
void DiffUtils::SetFileData(int items, file_data *data)
{
// We support only two files currently!
assert(items == 2);
m_inf = data;
}

/**
* @brief Compare two files (as earlier specified).
* @return DIFFCODE as a result of compare.
*/
int DiffUtils::diffutils_compare_files()
int DiffUtils::CompareFiles(DiffFileData* diffData)
{
int bin_flag = 0;
int bin_file = 0; // bitmap for binary files

// Do the actual comparison (generating a change script)
struct change *script = nullptr;
bool success = Diff2Files(&script, 0, &bin_flag, false, &bin_file);
bool success = m_pDiffWrapper->Diff2Files(&script, diffData, & bin_flag, & bin_file);
if (!success)
{
return DIFFCODE::FILE | DIFFCODE::TEXT | DIFFCODE::CMPERR;
Expand All @@ -125,19 +113,16 @@ int DiffUtils::diffutils_compare_files()

if (script != nullptr)
{
const bool usefilters = m_pOptions->m_filterCommentsLines ||
const bool usefilters = m_pDiffWrapper->GetOptions().m_filterCommentsLines ||
(m_pDiffWrapper->GetFilterList() && m_pDiffWrapper->GetFilterList()->HasRegExps()) ||
(m_pDiffWrapper->GetSubstitutionList() && m_pDiffWrapper->GetSubstitutionList()->HasRegExps());

PostFilterContext ctxt{};
String Ext = ucr::toTString(m_inf[0].name);
String Ext = ucr::toTString(diffData->m_inf[0].name);
size_t PosOfDot = Ext.rfind('.');
if (PosOfDot != String::npos)
Ext.erase(0, PosOfDot + 1);

DIFFOPTIONS options = {0};
m_pOptions->GetAsDiffOptions(options);
m_pDiffWrapper->SetOptions(&options);
m_pDiffWrapper->SetFilterCommentsSourceDef(Ext);

struct change *next = script;
Expand All @@ -159,7 +144,7 @@ int DiffUtils::diffutils_compare_files()
{
/* Determine range of line numbers involved in each file. */
int first0 = 0, last0 = 0, first1 = 0, last1 = 0, deletes = 0, inserts = 0;
analyze_hunk (thisob, &first0, &last0, &first1, &last1, &deletes, &inserts, m_inf);
analyze_hunk (thisob, &first0, &last0, &first1, &last1, &deletes, &inserts, diffData->m_inf);

/* Reconnect the script so it will all be freed properly. */
end->link = next;
Expand All @@ -170,7 +155,7 @@ int DiffUtils::diffutils_compare_files()

if (op != OP_TRIVIAL && usefilters)
{
m_pDiffWrapper->PostFilter(ctxt, thisob, m_inf);
m_pDiffWrapper->PostFilter(ctxt, thisob, diffData->m_inf);
}
}
}
Expand Down Expand Up @@ -248,31 +233,10 @@ int DiffUtils::diffutils_compare_files()
second file if first is binary).
* @return `true` when compare succeeds, `false` if error happened during compare.
*/
bool DiffUtils::Diff2Files(struct change ** diffs, int depth,
int * bin_status, bool bMovedBlocks, int * bin_file) const
bool DiffUtils::Diff2Files(struct change ** diffs, DiffFileData *diffData,
int * bin_status, int * bin_file) const
{
bool bRet = true;
SE_Handler seh;
try
{
if (m_pOptions->m_diffAlgorithm != DIFF_ALGORITHM_DEFAULT)
{
const unsigned xdl_flags = make_xdl_flags(*m_pOptions);
*diffs = diff_2_files_xdiff(m_inf, bin_status, bMovedBlocks, bin_file, xdl_flags);
files[0] = m_inf[0];
files[1] = m_inf[1];
}
else
{
*diffs = diff_2_files(m_inf, depth, bin_status, bMovedBlocks, bin_file);
}
}
catch (SE_Exception&)
{
*diffs = nullptr;
bRet = false;
}
return bRet;
return m_pDiffWrapper->Diff2Files(diffs, diffData, bin_status, bin_file);
}

/**
Expand All @@ -297,14 +261,5 @@ void DiffUtils::GetDiffCounts(int & diffs, int & trivialDiffs) const
trivialDiffs = m_ntrivialdiffs;
}

/**
* @brief Return text statistics for last compare.
* @param [in] side For which file to return statistics.
* @param [out] stats Stats as asked.
*/
void DiffUtils::GetTextStats(int side, FileTextStats *stats) const
{
CopyTextStats(&m_inf[side], stats);
}

} // namespace CompareEngines
22 changes: 10 additions & 12 deletions Src/CompareEngines/Wrap_DiffUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@

#include <memory>

class CompareOptions;
class FilterList;
class SubstitutionList;
class DiffutilsOptions;
struct file_data;
class CompareOptions;
struct FileTextStats;
class CDiffWrapper;
struct DiffFileData;

namespace CompareEngines
{
Expand All @@ -31,22 +30,21 @@ class DiffUtils
public:
DiffUtils();
~DiffUtils();
void SetCompareOptions(const CompareOptions & options);

void SetCodepage(int codepage);
void SetCompareOptions(const CompareOptions& options);
void SetFilterList(std::shared_ptr<FilterList> plist);
void ClearFilterList();
void SetSubstitutionList(std::shared_ptr<SubstitutionList> plist);
void ClearSubstitutionList();
void SetFileData(int items, file_data *data);
int diffutils_compare_files();

int CompareFiles(DiffFileData* diffData);
bool Diff2Files(struct change ** diffs, DiffFileData *diffData,
int * bin_status, int * bin_file) const;

void GetDiffCounts(int & diffs, int & trivialDiffs) const;
void GetTextStats(int side, FileTextStats *stats) const;
bool Diff2Files(struct change ** diffs, int depth,
int * bin_status, bool bMovedBlocks, int * bin_file) const;
void SetCodepage(int codepage);

private:
std::unique_ptr<DiffutilsOptions> m_pOptions; /**< Compare options for diffutils. */
file_data * m_inf; /**< Compared files data (for diffutils). */
int m_ndiffs; /**< Real diffs found. */
int m_ntrivialdiffs; /**< Ignored diffs found. */
std::unique_ptr<CDiffWrapper> m_pDiffWrapper;
Expand Down
1 change: 1 addition & 0 deletions Src/CompareOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ void DiffutilsOptions::GetAsDiffOptions(DIFFOPTIONS &options) const
options.bIgnoreCase = m_bIgnoreCase;
options.bIgnoreEol = m_bIgnoreEOLDifference;
options.bIgnoreNumbers = m_bIgnoreNumbers;
options.nDiffAlgorithm = m_diffAlgorithm;

switch (m_ignoreWhitespace)
{
Expand Down
5 changes: 3 additions & 2 deletions Src/DiffWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ class CDiffWrapper
void SetCreatePatchFile(const String &filename);
void SetCreateDiffList(DiffList *diffList);
void GetOptions(DIFFOPTIONS *options) const;
const DiffutilsOptions& GetOptions() const { return m_options; }
void SetOptions(const DIFFOPTIONS *options);
void SetTextForAutomaticPrediff(const String &text);
void SetPrediffer(const PrediffingInfo * prediffer = nullptr);
Expand Down Expand Up @@ -192,11 +193,11 @@ class CDiffWrapper
void SetCodepage(int codepage) { m_codepage = codepage; }
void EnablePlugins(bool enable);
bool PostFilter(PostFilterContext& ctxt, change* thisob, const file_data* file_data_ary) const;
bool Diff2Files(struct change ** diffs, DiffFileData *diffData,
int * bin_status, int * bin_file) const;

protected:
String FormatSwitchString() const;
bool Diff2Files(struct change ** diffs, DiffFileData *diffData,
int * bin_status, int * bin_file) const;
void LoadWinMergeDiffsFromDiffUtilsScript(struct change * script, const file_data * inf);
void WritePatchFile(struct change * script, file_data * inf);
public:
Expand Down
Loading

0 comments on commit 0529f2e

Please sign in to comment.