forked from WinMerge/winmerge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiffWrapper.cpp
1678 lines (1524 loc) · 47.6 KB
/
DiffWrapper.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/////////////////////////////////////////////////////////////////////////////
// License (GPLv2+):
// 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 DiffWrapper.cpp
*
* @brief Code for DiffWrapper class
*
* @date Created: 2003-08-22
*/
#define NOMINMAX
#include "DiffWrapper.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <algorithm>
#include <string>
#include <map>
#include <cassert>
#include <exception>
#include <vector>
#include <list>
#include <Poco/Format.h>
#include <Poco/Debugger.h>
#include <Poco/StringTokenizer.h>
#include <Poco/Exception.h>
#include "DiffContext.h"
#include "coretools.h"
#include "DiffList.h"
#include "MovedLines.h"
#include "FilterList.h"
#include "diff.h"
#include "Diff3.h"
#include "FileTransform.h"
#include "paths.h"
#include "CompareOptions.h"
#include "FileTextStats.h"
#include "FolderCmp.h"
#include "FilterCommentsManager.h"
#include "Environment.h"
#include "PatchHTML.h"
#include "UnicodeString.h"
#include "unicoder.h"
#include "TFile.h"
#include "Exceptions.h"
#include "MergeApp.h"
using Poco::Debugger;
using Poco::format;
using Poco::StringTokenizer;
using Poco::Exception;
extern int recursive;
static void FreeDiffUtilsScript(struct change * & script);
static void FreeDiffUtilsScript3(struct change * & script10, struct change * & script12);
static void CopyTextStats(const file_data * inf, FileTextStats * myTextStats);
static void CopyDiffutilTextStats(file_data *inf, DiffFileData * diffData);
/**
* @brief Default constructor.
* Initializes members and creates new FilterCommentsManager.
*/
CDiffWrapper::CDiffWrapper()
: m_pFilterCommentsManager(nullptr)
, m_bCreatePatchFile(false)
, m_bUseDiffList(false)
, m_bAddCmdLine(true)
, m_bAppendFiles(false)
, m_nDiffs(0)
, m_codepage(GetACP())
, m_infoPrediffer(nullptr)
, m_pDiffList(nullptr)
, m_bPathsAreTemp(false)
, m_pFilterList(nullptr)
, m_bPluginsEnabled(false)
{
memset(&m_status, 0, sizeof(DIFFSTATUS));
// character that ends a line. Currently this is always `\n'
line_end_char = '\n';
}
/**
* @brief Destructor.
*/
CDiffWrapper::~CDiffWrapper()
{
}
/**
* @brief Set plugins enabled/disabled.
* @param [in] enable if true plugins are enabled.
*/
void CDiffWrapper::EnablePlugins(bool enable)
{
m_bPluginsEnabled = enable;
}
/**
* @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::SetCreatePatchFile(const String &filename)
{
if (filename.empty())
{
m_bCreatePatchFile = false;
m_sPatchFile.clear();
}
else
{
m_bCreatePatchFile = true;
m_sPatchFile = filename;
strutils::replace(m_sPatchFile, _T("/"), _T("\\"));
}
}
/**
* @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::SetCreateDiffList(DiffList *diffList)
{
if (diffList == NULL)
{
m_bUseDiffList = false;
m_pDiffList = NULL;
}
else
{
m_bUseDiffList = true;
m_pDiffList = diffList;
}
}
/**
* @brief Returns current set of options used by diff-engine.
* This function converts internally used diff-options to
* format used outside CDiffWrapper and returns them.
* @param [in,out] options Pointer to structure getting used options.
*/
void CDiffWrapper::GetOptions(DIFFOPTIONS *options) const
{
assert(options);
DIFFOPTIONS tmpOptions = {0};
m_options.GetAsDiffOptions(tmpOptions);
*options = tmpOptions;
}
/**
* @brief Set options for Diff-engine.
* This function converts given options to format CDiffWrapper uses
* internally and stores them.
* @param [in] options Pointer to structure having new options.
*/
void CDiffWrapper::SetOptions(const DIFFOPTIONS *options)
{
assert(options);
m_options.SetFromDiffOptions(*options);
}
/**
* @brief Set text tested to find the prediffer automatically.
* Most probably a concatenated string of both filenames.
*/
void CDiffWrapper::SetTextForAutomaticPrediff(const String &text)
{
m_sToFindPrediffer = text;
}
void CDiffWrapper::SetPrediffer(const PrediffingInfo * prediffer /*=NULL*/)
{
// all flags are set correctly during the construction
m_infoPrediffer.reset(new PrediffingInfo);
if (prediffer)
*m_infoPrediffer = *prediffer;
}
void CDiffWrapper::GetPrediffer(PrediffingInfo * prediffer) const
{
*prediffer = *m_infoPrediffer;
}
/**
* @brief Set options used for patch-file creation.
* @param [in] options Pointer to structure having new options.
*/
void CDiffWrapper::SetPatchOptions(const PATCHOPTIONS *options)
{
assert(options);
m_options.m_contextLines = options->nContext;
switch (options->outputStyle)
{
case OUTPUT_NORMAL:
m_options.m_outputStyle = DIFF_OUTPUT_NORMAL;
break;
case OUTPUT_CONTEXT:
m_options.m_outputStyle = DIFF_OUTPUT_CONTEXT;
break;
case OUTPUT_UNIFIED:
m_options.m_outputStyle = DIFF_OUTPUT_UNIFIED;
break;
case OUTPUT_HTML:
m_options.m_outputStyle = DIFF_OUTPUT_HTML;
break;
default:
throw "Unknown output style!";
break;
}
m_bAddCmdLine = options->bAddCommandline;
}
/**
* @brief Enables/disables moved block detection.
* @param [in] bDetectMovedBlocks If true moved blocks are detected.
*/
void CDiffWrapper::SetDetectMovedBlocks(bool bDetectMovedBlocks)
{
if (bDetectMovedBlocks)
{
if (m_pMovedLines[0] == NULL)
{
m_pMovedLines[0].reset(new MovedLines);
m_pMovedLines[1].reset(new MovedLines);
m_pMovedLines[2].reset(new MovedLines);
}
}
else
{
m_pMovedLines[0].reset();
m_pMovedLines[1].reset();
m_pMovedLines[2].reset();
}
}
/**
* @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
* @param [in] filtercommentsset - For future use to determine trivial bytes
* @return Returns true if all characters are trivial
*/
bool CDiffWrapper::IsTrivialBytes(const char* Start, const char* End,
const FilterCommentsSet& filtercommentsset) const
{
std::string testdata(Start, End);
//@TODO: Need to replace the following trivial string with a user specified string
size_t pos = testdata.find_first_not_of(" \t\r\n");
return (pos == std::string::npos);
}
/**
* @brief Test for a line of trivial data
* @param [in] Line - String to test for
* @param [in] StartOfComment -
* @param [in] EndOfComment -
* @param [in] InLineComment -
* @param [in] filtercommentsset - Comment marker set used to indicate comment blocks.
* @return Returns true if entire line is trivial
*/
bool CDiffWrapper::IsTrivialLine(const std::string &Line,
const char * StartOfComment,
const char * EndOfComment,
const char * InLineComment,
const FilterCommentsSet& filtercommentsset) const
{
//Do easy test first
if ((!StartOfComment || !EndOfComment) && !InLineComment)
return false;//In no Start and End pair, and no single in-line set, then it's not trivial
if (StartOfComment == Line.c_str() &&
((EndOfComment + filtercommentsset.EndMarker.size()) - StartOfComment) == Line.size())
{//If entire line is blocked by End and Start markers, then entire line is trivial
return true;
}
if (InLineComment && InLineComment < StartOfComment)
{
if (InLineComment == Line.c_str())
return true;//If line starts with InLineComment marker, then entire line is trivial
//Other wise, check if data before InLineComment marker is trivial
return IsTrivialBytes(Line.c_str(), InLineComment, filtercommentsset);
}
//Done with easy test, so now do more complex test
if (StartOfComment &&
EndOfComment &&
StartOfComment < EndOfComment &&
IsTrivialBytes(Line.c_str(), StartOfComment, filtercommentsset) &&
IsTrivialBytes(EndOfComment + filtercommentsset.EndMarker.size(),
Line.c_str()+Line.size(), filtercommentsset))
{
return true;
}
return false;
}
/**
* @brief Find comment marker in string, excluding portions enclosed in quotation marks or apostrophes
* @param [in] target - string to search
* @param [in] marker - marker to search for
* @return Returns position of marker, or NULL if none is present
*/
static const char *FindCommentMarker(const char *target, const char *marker)
{
char prev = '\0';
char quote = '\0';
size_t marker_len = strlen(marker);
while (char c = *target)
{
if (quote == '\0' && strncmp(target, marker, marker_len) == 0)
return target;
if ((prev != '\\') &&
(c == '"' || c == '\'') &&
(quote == '\0' || quote == c))
{
quote ^= c;
}
prev = c;
++target;
}
return NULL;
}
/**
* @brief Replace spaces in a string
* @param [in] str - String to search
* @param [in] rep - String to replace
*/
static void ReplaceSpaces(std::string & str, const char *rep)
{
std::string::size_type pos = 0;
size_t replen = strlen(rep);
while ((pos = str.find_first_of(" \t", pos)) != std::string::npos)
{
std::string::size_type posend = str.find_first_not_of(" \t", pos);
if (posend != std::string::npos)
str.replace(pos, posend - pos, rep);
else
str.replace(pos, 1, rep);
pos += replen;
}
}
/**
@brief Performs post-filtering, by setting comment blocks to trivial
@param [in] StartPos - First line number to read
@param [in] EndPos - The line number PASS the last line number to read
@param [in] QtyLinesInBlock - Number of lines in diff block. Not needed in backward direction.
@param [in] Direction - This should be 1 or -1, to indicate which direction to read (backward or forward)
@param [in,out] Op - This variable is set to trivial if block should be ignored.
@param [in] FileNo - Should be 0 or 1, to indicate left or right file.
@param [in] filtercommentsset - Comment marker set used to indicate comment blocks.
@return Always returns true in reverse direction.
In forward direction, returns false if none trivial data is found within QtyLinesInBlock
*/
bool CDiffWrapper::PostFilter(int StartPos, int EndPos, int Direction,
int QtyLinesInBlock, OP_TYPE &Op, int FileNo,
FilterCommentsSet& filtercommentsset) const
{
if (Op == OP_TRIVIAL) //If already set to trivial, then exit.
return true;
bool OpShouldBeTrivial = false;
int QtyTrivialLines = 0;
for(int i = StartPos + ((Direction == -1)?-1:0); i != EndPos;i += Direction)
{
if ((i - StartPos) == QtyLinesInBlock &&
QtyLinesInBlock == QtyTrivialLines)
{
OpShouldBeTrivial = true;
break;
}
size_t len = files[FileNo].linbuf[i + 1] - files[FileNo].linbuf[i];
const char *LineStr = files[FileNo].linbuf[i];
std::string LineData(LineStr, linelen(LineStr, len));
const char * StartOfComment = FindCommentMarker(LineData.c_str(), filtercommentsset.StartMarker.c_str());
const char * EndOfComment = FindCommentMarker(LineData.c_str(), filtercommentsset.EndMarker.c_str());
const char * InLineComment = FindCommentMarker(LineData.c_str(), filtercommentsset.InlineMarker.c_str());
//The following logic determines if the entire block is a comment block, and only marks it as trivial
//if all the changes are within a comment block.
if (Direction == -1)
{
if (!StartOfComment && EndOfComment)
break;
if (StartOfComment && (!EndOfComment || EndOfComment < StartOfComment) && (!InLineComment || InLineComment > StartOfComment))
{
OpShouldBeTrivial = true;
break;
}
}
else if (Direction == 1)
{
if (IsTrivialBytes(LineData.c_str(), LineData.c_str()+LineData.size(), filtercommentsset) ||
IsTrivialLine(LineData, StartOfComment, EndOfComment, InLineComment, filtercommentsset))
{
++QtyTrivialLines;
}
if (!EndOfComment && StartOfComment)
{
if (i == (StartPos + QtyTrivialLines) )
{
if (StartOfComment == LineData.c_str())
{//If this is at the beginning of the first line, then lets continue
continue;
}
if (IsTrivialBytes(LineData.c_str(), StartOfComment, filtercommentsset))
{//If only trivial bytes before comment marker, then continue
continue;
}
break;
}
//If this is not the first line, then assume
//previous lines are non-trivial, and return true.
return false;
}
if (EndOfComment &&
(!StartOfComment || StartOfComment > EndOfComment) &&
(!InLineComment || InLineComment > EndOfComment) )
{
if (!IsTrivialBytes(EndOfComment+filtercommentsset.EndMarker.size(), LineData.c_str()+LineData.size(), filtercommentsset))
{
return false;
}
if ((i - StartPos) >= (QtyLinesInBlock-1))
{
OpShouldBeTrivial = true;
break;
}
//Lets check if the remaining lines only contain trivial data
bool AllRemainingLinesContainTrivialData = true;
int TrivLinePos = i+1;
for(; TrivLinePos != (StartPos + QtyLinesInBlock);++TrivLinePos)
{
size_t len = files[FileNo].linbuf[TrivLinePos + 1] - files[FileNo].linbuf[TrivLinePos];
const char *LineStrTrvCk = files[FileNo].linbuf[TrivLinePos];
std::string LineDataTrvCk(LineStrTrvCk, linelen(LineStrTrvCk, len));
if (LineDataTrvCk.size() &&
!IsTrivialBytes(LineDataTrvCk.c_str(), LineDataTrvCk.c_str() + LineDataTrvCk.size(), filtercommentsset))
{
AllRemainingLinesContainTrivialData = false;
break;
}
}
if (AllRemainingLinesContainTrivialData)
{
OpShouldBeTrivial = true;
break;
}
if (TrivLinePos != (StartPos + QtyLinesInBlock) )
{
return PostFilter(TrivLinePos, EndPos, Direction, QtyLinesInBlock - (TrivLinePos - StartPos), Op, FileNo, filtercommentsset);
}
}
}
}
if (OpShouldBeTrivial)
{
Op = OP_TRIVIAL;
}
return true;
}
/**
@brief The main entry for post filtering. Performs post-filtering, by setting comment blocks to trivial
@param [in] LineNumberLeft - First line number to read from left file
@param [in] QtyLinesLeft - Number of lines in the block for left file
@param [in] LineNumberRight - First line number to read from right file
@param [in] QtyLinesRight - Number of lines in the block for right file
@param [in,out] Op - This variable is set to trivial if block should be ignored.
@param [in] FileNameExt - The file name extension. Needs to be lower case string ("cpp", "java", "c")
*/
void CDiffWrapper::PostFilter(int LineNumberLeft, int QtyLinesLeft, int LineNumberRight,
int QtyLinesRight, OP_TYPE &Op, const String& FileNameExt) const
{
if (Op == OP_TRIVIAL || !m_pFilterCommentsManager)
return;
//First we need to get lowercase file name extension
FilterCommentsSet filtercommentsset = m_pFilterCommentsManager->GetSetForFileType(FileNameExt);
if (filtercommentsset.StartMarker.empty() &&
filtercommentsset.EndMarker.empty() &&
filtercommentsset.InlineMarker.empty())
{
return;
}
OP_TYPE LeftOp = OP_NONE;
OP_TYPE RightOp = OP_NONE;
if (QtyLinesRight == 0)
{ //Only check left side
if (PostFilter(LineNumberLeft, files[0].valid_lines, 1, QtyLinesLeft, LeftOp, 0, filtercommentsset))
PostFilter(LineNumberLeft, -1, -1, QtyLinesLeft, LeftOp, 0, filtercommentsset);
}
else if (QtyLinesLeft == 0)
{ //Only check right side
if (PostFilter(LineNumberRight, files[1].valid_lines, 1, QtyLinesRight, RightOp, 1, filtercommentsset))
PostFilter(LineNumberRight, -1, -1, QtyLinesRight, RightOp, 1, filtercommentsset);
}
else
{
if (PostFilter(LineNumberLeft, files[0].valid_lines, 1, QtyLinesLeft, LeftOp, 0, filtercommentsset))
PostFilter(LineNumberLeft, -1, -1, QtyLinesLeft, LeftOp, 0, filtercommentsset);
if (PostFilter(LineNumberRight, files[1].valid_lines, 1, QtyLinesRight, RightOp, 1, filtercommentsset))
PostFilter(LineNumberRight, -1, -1, QtyLinesRight, RightOp, 1, filtercommentsset);
}
std::list<std::string> LeftLines, RightLines;
for (int i = 0; (i < QtyLinesLeft) || (i < QtyLinesRight); i++)
{
//Lets test all lines if only a comment is different.
const char * LineStrLeft = "";
const char * EndLineLeft = LineStrLeft;
const char * LineStrRight = "";
const char * EndLineRight = LineStrRight;
if(i < QtyLinesLeft)
{
LineStrLeft = files[0].linbuf[LineNumberLeft + i];
EndLineLeft = files[0].linbuf[LineNumberLeft + i + 1];
}
if(i < QtyLinesRight)
{
LineStrRight = files[1].linbuf[LineNumberRight + i];
EndLineRight = files[1].linbuf[LineNumberRight + i + 1];
}
if (EndLineLeft && EndLineRight)
{
std::string LineDataLeft(LineStrLeft, EndLineLeft);
std::string LineDataRight(LineStrRight, EndLineRight);
if (!filtercommentsset.StartMarker.empty() && !filtercommentsset.EndMarker.empty())
{
const char * CommentStrLeftStart;
const char * CommentStrLeftEnd;
const char * CommentStrRightStart;
const char * CommentStrRightEnd;
bool bFirstLoop = true;
do {
//Lets remove block comments, and see if lines are equal
CommentStrLeftStart = FindCommentMarker(LineDataLeft.c_str(), filtercommentsset.StartMarker.c_str());
CommentStrLeftEnd = FindCommentMarker(LineDataLeft.c_str(), filtercommentsset.EndMarker.c_str());
CommentStrRightStart = FindCommentMarker(LineDataRight.c_str(), filtercommentsset.StartMarker.c_str());
CommentStrRightEnd = FindCommentMarker(LineDataRight.c_str(), filtercommentsset.EndMarker.c_str());
if (CommentStrLeftStart != NULL && CommentStrLeftEnd != NULL && CommentStrLeftStart < CommentStrLeftEnd)
LineDataLeft.erase(CommentStrLeftStart - LineDataLeft.c_str(), CommentStrLeftEnd + filtercommentsset.EndMarker.size() - CommentStrLeftStart);
else if (CommentStrLeftEnd != NULL)
LineDataLeft.erase(0, CommentStrLeftEnd + filtercommentsset.EndMarker.size() - LineDataLeft.c_str());
else if (CommentStrLeftStart != NULL)
LineDataLeft.erase(CommentStrLeftStart - LineDataLeft.c_str());
else if(LeftOp == OP_TRIVIAL && bFirstLoop)
LineDataLeft.erase(0); //This line is all in block comments
if (CommentStrRightStart != NULL && CommentStrRightEnd != NULL && CommentStrRightStart < CommentStrRightEnd)
LineDataRight.erase(CommentStrRightStart - LineDataRight.c_str(), CommentStrRightEnd + filtercommentsset.EndMarker.size() - CommentStrRightStart);
else if (CommentStrRightEnd != NULL)
LineDataRight.erase(0, CommentStrRightEnd + filtercommentsset.EndMarker.size() - LineDataRight.c_str());
else if (CommentStrRightStart != NULL)
LineDataRight.erase(CommentStrRightStart - LineDataRight.c_str());
else if(RightOp == OP_TRIVIAL && bFirstLoop)
LineDataRight.erase(0); //This line is all in block comments
bFirstLoop = false;
} while (CommentStrLeftStart != NULL || CommentStrLeftEnd != NULL
|| CommentStrRightStart != NULL || CommentStrRightEnd != NULL); //Loops until all blockcomments are lost
}
if (!filtercommentsset.InlineMarker.empty())
{
//Lets remove line comments
const char * CommentStrLeft = FindCommentMarker(LineDataLeft.c_str(), filtercommentsset.InlineMarker.c_str());
const char * CommentStrRight = FindCommentMarker(LineDataRight.c_str(), filtercommentsset.InlineMarker.c_str());
if (CommentStrLeft != NULL)
LineDataLeft.erase(CommentStrLeft - LineDataLeft.c_str());
if (CommentStrRight != NULL)
LineDataRight.erase(CommentStrRight - LineDataRight.c_str());
}
if (m_options.m_ignoreWhitespace == WHITESPACE_IGNORE_ALL)
{
//Ignore character case
ReplaceSpaces(LineDataLeft, "");
ReplaceSpaces(LineDataRight, "");
}
else if (m_options.m_ignoreWhitespace == WHITESPACE_IGNORE_CHANGE)
{
//Ignore change in whitespace char count
ReplaceSpaces(LineDataLeft, " ");
ReplaceSpaces(LineDataRight, " ");
}
if (m_options.m_bIgnoreCase)
{
//ignore case
std::transform(LineDataLeft.begin(), LineDataLeft.end(), LineDataLeft.begin(), ::toupper);
std::transform(LineDataRight.begin(), LineDataRight.end(), LineDataRight.begin(), ::toupper);
}
if (!LineDataLeft.empty())
LeftLines.push_back(LineDataLeft);
if (!LineDataRight.empty())
RightLines.push_back(LineDataRight);
}
}
if (LeftLines != RightLines)
return;
//only difference is trival
Op = OP_TRIVIAL;
}
/**
* @brief Set source paths for diffing two files.
* 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] files Files to compare
* @param [in] tempPaths Are given paths temporary (can be deleted)?.
*/
void CDiffWrapper::SetPaths(const PathContext &files,
bool tempPaths)
{
m_files = files;
m_bPathsAreTemp = tempPaths;
}
/**
* @brief Set source paths for original (NON-TEMP) diffing two files.
* Sets full paths to two (NON-TEMP) files we are diffing.
* @param [in] OriginalFile1 First file to compare "(NON-TEMP) file".
* @param [in] OriginalFile2 Second file to compare "(NON-TEMP) file".
*/
void CDiffWrapper::SetCompareFiles(const PathContext &originalFile)
{
m_originalFile = originalFile;
}
/**
* @brief Set alternative paths for compared files.
* Sets alternative paths for diff'ed files. These alternative paths might not
* be real paths. For example when creating a patch file from folder compare
* we want to use relative paths.
* @param [in] altPaths Alternative file paths.
*/
void CDiffWrapper::SetAlternativePaths(const PathContext &altPaths)
{
m_alternativePaths = altPaths;
}
/**
* @brief Runs diff-engine.
*/
bool CDiffWrapper::RunFileDiff()
{
PathContext files = m_files;
int file;
for (file = 0; file < m_files.GetSize(); file++)
files[file] = paths::ToWindowsPath(files[file]);
bool bRet = true;
String strFileTemp[3];
std::copy(m_files.begin(), m_files.end(), strFileTemp);
m_options.SetToDiffUtils();
if (m_bUseDiffList)
m_nDiffs = m_pDiffList->GetSize();
for (file = 0; file < files.GetSize(); file++)
{
if (m_bPluginsEnabled)
{
// 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.
// this can only fail if the data can not be saved back (no more
// place on disk ???) What to do then ??
if (!FileTransform::Prediffing(m_infoPrediffer.get(), strFileTemp[file], m_sToFindPrediffer, m_bPathsAreTemp))
{
// display a message box
String sError = strutils::format(
_T("An error occurred while prediffing the file '%s' with the plugin '%s'. The prediffing is not applied any more."),
strFileTemp[file].c_str(),
m_infoPrediffer->pluginName.c_str());
AppErrorMessageBox(sError);
// don't use any more this prediffer
m_infoPrediffer->bToBeScanned = false;
m_infoPrediffer->pluginName.erase();
}
// We use the same plugin for both files, so it must be defined before
// second file
assert(m_infoPrediffer->bToBeScanned == false);
}
}
struct change *script = NULL;
struct change *script10 = NULL;
struct change *script12 = NULL;
DiffFileData diffdata, diffdata10, diffdata12;
int bin_flag = 0, bin_flag10 = 0, bin_flag12 = 0;
if (files.GetSize() == 2)
{
diffdata.SetDisplayFilepaths(files[0], files[1]); // store true names for diff utils patch file
// This opens & fstats both files (if it succeeds)
if (!diffdata.OpenFiles(strFileTemp[0], strFileTemp[1]))
{
return false;
}
// Compare the files, if no error was found.
// Last param (bin_file) is NULL since we don't
// (yet) need info about binary sides.
bRet = Diff2Files(&script, &diffdata, &bin_flag, NULL);
// We don't anymore create diff-files for every rescan.
// User can create patch-file whenever one wants to.
// We don't need to waste time. But lets keep this as
// debugging aid. Sometimes it is very useful to see
// what differences diff-engine sees!
#ifdef _DEBUG
// throw the diff into a temp file
String sTempPath = env::GetTemporaryPath(); // get path to Temp folder
String path = paths::ConcatPath(sTempPath, _T("Diff.txt"));
outfile = _tfopen(path.c_str(), _T("w+"));
if (outfile != NULL)
{
print_normal_script(script);
fclose(outfile);
outfile = NULL;
}
#endif
}
else
{
diffdata10.SetDisplayFilepaths(files[1], files[0]); // store true names for diff utils patch file
diffdata12.SetDisplayFilepaths(files[1], files[2]); // store true names for diff utils patch file
if (!diffdata10.OpenFiles(strFileTemp[1], strFileTemp[0]))
{
return false;
}
bRet = Diff2Files(&script10, &diffdata10, &bin_flag10, NULL);
if (!diffdata12.OpenFiles(strFileTemp[1], strFileTemp[2]))
{
return false;
}
bRet = Diff2Files(&script12, &diffdata12, &bin_flag12, NULL);
}
// First determine what happened during comparison
// If there were errors or files were binaries, don't bother
// creating diff-lists or patches
// diff_2_files set bin_flag to -1 if different binary
// diff_2_files set bin_flag to +1 if same binary
file_data * inf = diffdata.m_inf;
file_data * inf10 = diffdata10.m_inf;
file_data * inf12 = diffdata12.m_inf;
if (files.GetSize() == 2)
{
if (bin_flag != 0)
{
m_status.bBinaries = true;
if (bin_flag != -1)
m_status.Identical = IDENTLEVEL_ALL;
else
m_status.Identical = IDENTLEVEL_NONE;
}
else
{ // text files according to diffutils, so change script exists
m_status.Identical = (script == 0) ? IDENTLEVEL_ALL : IDENTLEVEL_NONE;
m_status.bBinaries = false;
}
m_status.bMissingNL[0] = !!inf[0].missing_newline;
m_status.bMissingNL[1] = !!inf[1].missing_newline;
}
else
{
m_status.Identical = IDENTLEVEL_NONE;
if (bin_flag10 != 0 || bin_flag12 != 0)
{
m_status.bBinaries = true;
if (bin_flag10 != -1 && bin_flag12 != -1)
m_status.Identical = IDENTLEVEL_ALL;
else if (bin_flag10 != -1)
m_status.Identical = IDENTLEVEL_EXCEPTRIGHT;
else if (bin_flag12 != -1)
m_status.Identical = IDENTLEVEL_EXCEPTLEFT;
else
m_status.Identical = IDENTLEVEL_EXCEPTMIDDLE;
}
else
{ // text files according to diffutils, so change script exists
m_status.bBinaries = false;
if (script10 == 0 && script12 == 0)
m_status.Identical = IDENTLEVEL_ALL;
else if (script10 == 0)
m_status.Identical = IDENTLEVEL_EXCEPTRIGHT;
else if (script12 == 0)
m_status.Identical = IDENTLEVEL_EXCEPTLEFT;
else
m_status.Identical = IDENTLEVEL_EXCEPTMIDDLE;
}
m_status.bMissingNL[0] = !!inf10[1].missing_newline;
m_status.bMissingNL[1] = !!inf12[0].missing_newline;
m_status.bMissingNL[2] = !!inf12[1].missing_newline;
}
// Create patch file
if (!m_status.bBinaries && m_bCreatePatchFile && files.GetSize() == 2)
{
WritePatchFile(script, &inf[0]);
}
// Go through diffs adding them to WinMerge's diff list
// This is done on every WinMerge's doc rescan!
if (!m_status.bBinaries && m_bUseDiffList)
{
if (files.GetSize() == 2)
LoadWinMergeDiffsFromDiffUtilsScript(script, diffdata.m_inf);
else
LoadWinMergeDiffsFromDiffUtilsScript3(
script10, script12,
diffdata10.m_inf, diffdata12.m_inf);
}
// cleanup the script
if (files.GetSize() == 2)
FreeDiffUtilsScript(script);
else
FreeDiffUtilsScript3(script10, script12);
// Done with diffutils filedata
if (files.GetSize() == 2)
{
diffdata.Close();
}
else
{
diffdata10.Close();
diffdata12.Close();
}
if (m_bPluginsEnabled)
{
// Delete temp files transformation functions possibly created
for (file = 0; file < files.GetSize(); file++)
{
if (strutils::compare_nocase(files[file], strFileTemp[file]) != 0)
{
try
{
TFile(strFileTemp[file]).remove();
}
catch (Exception& e)
{
LogErrorStringUTF8(e.displayText());
}
strFileTemp[file].erase();
}
}
}
return bRet;
}
/**
* @brief Add diff to external diff-list
*/
void CDiffWrapper::AddDiffRange(DiffList *pDiffList, unsigned begin0, unsigned end0, unsigned begin1, unsigned end1, OP_TYPE op)
{
try
{
DIFFRANGE dr;
dr.begin[0] = begin0;
dr.end[0] = end0;
dr.begin[1] = begin1;
dr.end[1] = end1;
dr.begin[2] = -1;
dr.end[2] = -1;
dr.op = op;
dr.blank[0] = dr.blank[1] = dr.blank[2] = -1;
pDiffList->AddDiff(dr);
}
catch (std::exception& e)
{
AppErrorMessageBox(ucr::toTString(e.what()));
}
}
void CDiffWrapper::AddDiffRange(DiffList *pDiffList, DIFFRANGE &dr)
{
try
{
pDiffList->AddDiff(dr);
}
catch (std::exception& e)
{
AppErrorMessageBox(ucr::toTString(e.what()));
}
}
/**
* @brief Expand last DIFFRANGE of file by one line to contain last line after EOL.
* @param [in] leftBufferLines size of array pane left
* @param [in] rightBufferLines size of array pane right
* @param [in] left on whitch side we have to insert
* @param [in] bIgnoreBlankLines, if true we allways add a new diff and make as trivial
*/
void CDiffWrapper::FixLastDiffRange(int nFiles, int bufferLines[], bool bMissingNL[], bool bIgnoreBlankLines)
{
DIFFRANGE dr;
const int count = m_pDiffList->GetSize();
if (count > 0 && !bIgnoreBlankLines)
{
m_pDiffList->GetDiff(count - 1, dr);
for (int file = 0; file < nFiles; file++)
{
if (!bMissingNL[file])
dr.end[file]++;
}
m_pDiffList->SetDiff(count - 1, dr);
}
else
{
// we have to create the DIFF
for (int file = 0; file < nFiles; file++)
{
dr.end[file] = bufferLines[file] - 1;
if (bMissingNL[file])
dr.begin[file] = dr.end[file];
else
dr.begin[file] = dr.end[file] + 1;
dr.op = OP_DIFF;
assert(dr.begin[0] == dr.begin[file]);
}
if (bIgnoreBlankLines)
dr.op = OP_TRIVIAL;
AddDiffRange(m_pDiffList, dr);
}
}
/**
* @brief Returns status-data from diff-engine last run
*/
void CDiffWrapper::GetDiffStatus(DIFFSTATUS *status) const
{
std::memcpy(status, &m_status, sizeof(DIFFSTATUS));
}
/**
* @brief Formats command-line for diff-engine last run (like it was called from command-line)
*/
String CDiffWrapper::FormatSwitchString() const
{