forked from WinMerge/winmerge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiffContext.cpp
305 lines (276 loc) · 8.79 KB
/
DiffContext.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/////////////////////////////////////////////////////////////////////////////
// WinMerge: an interactive diff/merge utility
// Copyright (C) 1997-2000 Thingamahoochie Software
// Author: Dean Grimm
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
/////////////////////////////////////////////////////////////////////////////
/**
* @file DiffContext.cpp
*
* @brief Implementation of CDiffContext
*/
// ID line follows -- this is updated by SVN
// $Id$
//////////////////////////////////////////////////////////////////////
#include "StdAfx.h"
#include <shlwapi.h>
#include "UnicodeString.h"
#include "Merge.h"
#include "CompareOptions.h"
#include "CompareStats.h"
#include "version.h"
#include "FilterList.h"
#include "DiffContext.h"
#include "paths.h"
#include "coretools.h"
#include "codepage_detect.h"
#include "DiffItemList.h"
#include "IAbortable.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
/**
* @brief Construct CDiffContext.
*
* @param [in] pszLeft Initial left-side path.
* @param [in] pszRight Initial right-side path.
*/
CDiffContext::CDiffContext(LPCTSTR pszLeft /*=NULL*/, LPCTSTR pszRight /*=NULL*/)
: m_piFilterGlobal(NULL)
, m_piPluginInfos(NULL)
, m_nCompMethod(-1)
, m_bIgnoreSmallTimeDiff(FALSE)
, m_pCompareStats(NULL)
, m_piAbortable(NULL)
, m_bStopAfterFirstDiff(false)
, m_pFilterList(NULL)
, m_pCompareOptions(NULL)
, m_pOptions(NULL)
, m_bPluginsEnabled(false)
{
m_paths.SetLeft(pszLeft);
m_paths.SetRight(pszRight);
}
/**
* @brief Destructor.
*/
CDiffContext::~CDiffContext()
{
delete m_pOptions;
delete m_pCompareOptions;
delete m_pFilterList;
}
/**
* @brief Update info in item in result list from disk.
* This function updates result list item's file information from actual
* file in the disk. This updates info like date, size and attributes.
* @param [in] diffpos DIFFITEM to update.
* @param [in] bLeft Update left-side info.
* @param [in] bRight Update right-side info.
*/
void CDiffContext::UpdateStatusFromDisk(POSITION diffpos, BOOL bLeft, BOOL bRight)
{
DIFFITEM &di = GetDiffRefAt(diffpos);
if (bLeft)
{
di.left.ClearPartial();
if (!di.diffcode.isSideRightOnly())
UpdateInfoFromDiskHalf(di, TRUE);
}
if (bRight)
{
di.right.ClearPartial();
if (!di.diffcode.isSideLeftOnly())
UpdateInfoFromDiskHalf(di, FALSE);
}
}
/**
* @brief Update file information from disk for DIFFITEM.
* This function updates DIFFITEM's file information from actual file in
* the disk. This updates info like date, size and attributes.
* @param [in, out] di DIFFITEM to update (selected side, see bLeft param).
* @param [in] bLeft If TRUE left side information is updated,
* right side otherwise.
* @return TRUE if file exists
*/
BOOL CDiffContext::UpdateInfoFromDiskHalf(DIFFITEM & di, BOOL bLeft)
{
String filepath;
if (bLeft == TRUE)
filepath = paths_ConcatPath(di.getLeftFilepath(GetNormalizedLeft()), di.left.filename);
else
filepath = paths_ConcatPath(di.getRightFilepath(GetNormalizedRight()), di.right.filename);
DiffFileInfo & dfi = bLeft ? di.left : di.right;
if (!dfi.Update(filepath.c_str()))
return FALSE;
UpdateVersion(di, bLeft);
GuessCodepageEncoding(filepath.c_str(), &dfi.encoding, m_bGuessEncoding);
return TRUE;
}
/**
* @brief Determine if file is one to have a version information.
* This function determines if the given file has a version information
* attached into it in resource. This is done by comparing file extension to
* list of known filename extensions usually to have a version information.
* @param [in] ext Extension to check.
* @return true if extension has version info, false otherwise.
*/
static bool CheckFileForVersion(LPCTSTR ext)
{
if (!lstrcmpi(ext, _T(".EXE")) || !lstrcmpi(ext, _T(".DLL")) || !lstrcmpi(ext, _T(".SYS")) ||
!lstrcmpi(ext, _T(".DRV")) || !lstrcmpi(ext, _T(".OCX")) || !lstrcmpi(ext, _T(".CPL")) ||
!lstrcmpi(ext, _T(".SCR")) || !lstrcmpi(ext, _T(".LANG")))
{
return true;
}
else
{
return false;
}
}
/**
* @brief Load file version from disk.
* Update fileversion for given item and side from disk. Note that versions
* are read from only some filetypes. See CheckFileForVersion() function
* for list of files to check versions.
* @param [in,out] di DIFFITEM to update.
* @param [in] bLeft If TRUE left-side file is updated, right-side otherwise.
*/
void CDiffContext::UpdateVersion(DIFFITEM & di, BOOL bLeft) const
{
DiffFileInfo & dfi = bLeft ? di.left : di.right;
// Check only binary files
dfi.version.Clear();
dfi.bVersionChecked = true;
if (di.diffcode.isDirectory())
return;
String spath;
if (bLeft)
{
if (di.diffcode.isSideRightOnly())
return;
LPCTSTR ext = PathFindExtension(di.left.filename.c_str());
if (!CheckFileForVersion(ext))
return;
spath = di.getLeftFilepath(GetNormalizedLeft());
spath = paths_ConcatPath(spath, di.left.filename);
}
else
{
if (di.diffcode.isSideLeftOnly())
return;
LPCTSTR ext = PathFindExtension(di.right.filename.c_str());
if (!CheckFileForVersion(ext))
return;
spath = di.getRightFilepath(GetNormalizedRight());
spath = paths_ConcatPath(spath, di.right.filename);
}
// Get version info if it exists
CVersionInfo ver(spath.c_str());
DWORD verMS = 0;
DWORD verLS = 0;
if (ver.GetFixedFileVersion(verMS, verLS))
dfi.version.SetFileVersion(verMS, verLS);
}
/**
* @brief Create compare-method specific compare options class.
* This function creates a compare options class that is specific for
* selected compare method. Compare options class is initialized from
* given set of options.
* @param [in] compareMethod Selected compare method.
* @param [in] options Initial set of compare options.
* @return TRUE if creation succeeds.
*/
BOOL CDiffContext::CreateCompareOptions(int compareMethod, const DIFFOPTIONS & options)
{
if (m_pOptions != NULL)
delete m_pOptions;
m_pOptions = new DIFFOPTIONS;
if (m_pOptions != NULL)
CopyMemory(m_pOptions, &options, sizeof(DIFFOPTIONS));
else
return FALSE;
m_pCompareOptions = GetCompareOptions(compareMethod);
if (m_pCompareOptions == NULL)
{
// For Date and Date+Size compare NULL is ok since they don't have actual
// compare options.
if (compareMethod == CMP_DATE || compareMethod == CMP_DATE_SIZE ||
compareMethod == CMP_SIZE)
{
return TRUE;
}
else
return FALSE;
}
return TRUE;
}
/**
* @brief Get compare-type specific compare options.
* This function returns per-compare method options. The compare options
* returned are converted from general options to match options for specific
* comapare type. Not all compare options in general set are available for
* some other compare type. And some options can have different values.
* @param [in] compareMethod Compare method used.
* @return Compare options class.
*/
CompareOptions * CDiffContext::GetCompareOptions(int compareMethod)
{
// If compare method is same than in previous time, return cached value
if (compareMethod == m_nCompMethod && m_pCompareOptions != NULL)
return m_pCompareOptions;
// Otherwise we have to create new options
delete m_pCompareOptions;
m_pCompareOptions = NULL;
switch (compareMethod)
{
case CMP_CONTENT:
m_pCompareOptions = new DiffutilsOptions();
break;
case CMP_QUICK_CONTENT:
m_pCompareOptions = new QuickCompareOptions();
break;
default:
// No really options to set..
break;
}
m_nCompMethod = compareMethod;
if (m_pCompareOptions == NULL)
return NULL;
m_pCompareOptions->SetFromDiffOptions(*m_pOptions);
return m_pCompareOptions;
}
/** @brief Forward call to retrieve plugin info (winds up in DirDoc) */
void CDiffContext::FetchPluginInfos(LPCTSTR filteredFilenames,
PackingInfo ** infoUnpacker, PrediffingInfo ** infoPrediffer)
{
ASSERT(m_piPluginInfos);
m_piPluginInfos->FetchPluginInfos(filteredFilenames, infoUnpacker, infoPrediffer);
}
/**
* @brief Check if user has requested aborting the compare.
* @return true if user has requested abort, false otherwise.
*/
bool CDiffContext::ShouldAbort() const
{
return m_piAbortable && m_piAbortable->ShouldAbort();
}