OSDN Git Service

- Use Poco::RegularExpression instead of PCRE library. (Poco::RegularExpression inter...
[winmerge-jp/winmerge-jp.git] / Src / DiffWrapper.h
1 /////////////////////////////////////////////////////////////////////////////
2 //    License (GPLv2+):
3 //    This program is free software; you can redistribute it and/or modify
4 //    it under the terms of the GNU General Public License as published by
5 //    the Free Software Foundation; either version 2 of the License, or
6 //    (at your option) any later version.
7 //
8 //    This program is distributed in the hope that it will be useful, but
9 //    WITHOUT ANY WARRANTY; without even the implied warranty of
10 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 //    General Public License for more details.
12 //
13 //    You should have received a copy of the GNU General Public License
14 //    along with this program; if not, write to the Free Software
15 //    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16 /////////////////////////////////////////////////////////////////////////////
17 /** 
18  * @file  DiffWrapper.h
19  *
20  * @brief Declaration file for CDiffWrapper.
21  *
22  * @date  Created: 2003-08-22
23  */
24 // ID line follows -- this is updated by SVN
25 // $Id: DiffWrapper.h 7091 2010-01-11 20:27:43Z kimmov $
26
27 #ifndef _DIFFWRAPPER_H
28 #define _DIFFWRAPPER_H
29
30 #include "FileLocation.h"
31 #include "PathContext.h"
32 #include "FileTextStats.h"
33 #include "CompareOptions.h"
34
35 class CDiffContext;
36 class PrediffingInfo;
37 struct DIFFRANGE;
38 class DiffList;
39 struct DiffFileData;
40 class PathContext;
41 struct file_data;
42 class FilterCommentsManager;
43 struct FilterCommentsSet;
44 class MovedLines;
45 class FilterList;
46 enum OP_TYPE;
47
48 /** @enum COMPARE_TYPE
49  * @brief Different foldercompare methods.
50  * These values are the foldercompare methods WinMerge supports.
51  */
52
53 /** @var CMP_CONTENT
54  * @brief Normal by content compare.
55  * This compare type is first, default and all-seeing compare type.
56  * diffutils is used for producing compare results. So all limitations
57  * of diffutils (like buffering) apply to this compare method. But this
58  * is also currently only compare method that produces difference lists
59  * we can use in file compare.
60  */
61
62 /** @var CMP_QUICK_CONTENT
63  * @brief Faster byte per byte -compare.
64  * This type of compare was a response for needing faster compare results
65  * in folder compare. It independent from diffutils, and fully customised
66  * for WinMerge. It basically does byte-per-byte compare, still implementing
67  * different whitespace ignore options.
68  *
69  * Optionally this compare type can be stopped when first difference is found.
70  * Which gets compare as fast as possible. But misses sometimes binary files
71  * if zero bytes aren't found before first difference. Also difference counts
72  * are not useful with that option.
73  */
74
75 /** @var CMP_DATE
76  * @brief Compare by modified date.
77  * This compare type was added after requests and realization that in some
78  * situations difference in file's timestamps is enough to judge them
79  * different. E.g. when modifying files in local machine, file timestamps
80  * are certainly different after modifying them. This method doesn't even
81  * open files for reading them. It only reads file's infos for timestamps
82  * and compares them.
83  *
84  * This is no doubt fastest way to compare files.
85  */
86
87 /** @var CMP_DATE_SIZE
88  * @brief Compare by date and then by size.
89  * This method is basically same than CMP_DATE, but it adds check for file
90  * sizes if timestamps are identical. This is because there are situations
91  * timestamps can't be trusted alone, especially with network shares. Adding
92  * checking for file sizes adds some more reliability for results with
93  * minimal increase in compare time.
94  */
95
96 /** @var CMP_SIZE
97  * @brief Compare by file size.
98  * This compare method compares file sizes. This isn't quite accurate method,
99  * other than it can detect files that certainly differ. But it can show lot of
100  * different files as identical too. Advantage is in some use cases where different
101  * size always means files are different. E.g. automatically created logs - when
102  * more data is added size increases.
103  */
104 enum COMPARE_TYPE
105 {
106         CMP_CONTENT = 0,
107         CMP_QUICK_CONTENT,
108         CMP_DATE,
109         CMP_DATE_SIZE,
110         CMP_SIZE,
111 };
112
113 /**
114  * @brief Additional options for creating patch files
115  */
116 struct PATCHOPTIONS
117 {
118         enum output_style outputStyle; /**< Patch file style. */
119         int nContext; /**< Number of context lines. */
120         BOOL bAddCommandline; /**< Add diff-style commandline to patch file. */
121 };
122
123 typedef enum {
124         IDENTLEVEL_NONE,
125         IDENTLEVEL_ALL,
126         IDENTLEVEL_EXCEPTLEFT,
127         IDENTLEVEL_EXCEPTMIDDLE,
128         IDENTLEVEL_EXCEPTRIGHT,
129 } IDENTLEVEL;
130 /**
131  * @brief Diffutils returns this statusdata about files compared
132  */
133 struct DIFFSTATUS
134 {
135         BOOL bMissingNL[3]; /**< file is missing EOL before EOF */
136         BOOL bBinaries; /**< Files are binaries */
137         IDENTLEVEL Identical; /**< diffutils said files are identical */
138         BOOL bPatchFileFailed; /**< Creating patch file failed */
139
140         DIFFSTATUS() { memset(this, 0, sizeof(*this)); } // start out with all flags clear
141 };
142
143 class FilterCommentsManager;
144
145 /**
146  * @brief Wrapper class for diffengine (diffutils and ByteComparator).
147  * Diffwappre class is used to run selected diffengine. For folder compare
148  * there are several methods (COMPARE_TYPE), but for file compare diffutils
149  * is used always. For file compare diffutils can output results to external
150  * DiffList or to patch file. Output type must be selected with member
151  * functions SetCreatePatchFile() and SetCreateDiffList().
152  */
153 class CDiffWrapper
154 {
155 public:
156         CDiffWrapper();
157         ~CDiffWrapper();
158         void SetCreatePatchFile(const String &filename);
159         void SetCreateDiffList(DiffList *diffList);
160         void SetDiffList(DiffList *diffList);
161         void GetOptions(DIFFOPTIONS *options);
162         void SetOptions(const DIFFOPTIONS *options);
163         void SetTextForAutomaticPrediff(const String &text);
164         void SetPrediffer(PrediffingInfo * prediffer =NULL);
165         void GetPrediffer(PrediffingInfo * prediffer);
166         void SetPatchOptions(const PATCHOPTIONS *options);
167         void SetDetectMovedBlocks(bool bDetectMovedBlocks);
168         bool GetDetectMovedBlocks() { return (m_pMovedLines[0] != NULL); }
169         void SetAppendFiles(BOOL bAppendFiles);
170         void SetPaths(const PathContext &files, BOOL tempPaths);
171         void SetAlternativePaths(const PathContext &altPaths);
172         void SetCodepage(int codepage) { m_codepage = codepage; }
173         BOOL RunFileDiff();
174         void GetDiffStatus(DIFFSTATUS *status);
175         void AddDiffRange(DiffList *pDiffList, UINT begin0, UINT end0, UINT begin1, UINT end1, OP_TYPE op);
176         void AddDiffRange(DiffList *pDiffList, DIFFRANGE &dr);
177         void FixLastDiffRange(int nFiles, int bufferLines[], int bMissingNL[], bool bIgnoreBlankLines);
178         MovedLines * GetMovedLines(int index) { return m_pMovedLines[index]; }
179         void SetCompareFiles(const PathContext &originalFile);
180         int Make3wayDiff(DiffList& diff3, DiffList& diff02, DiffList& diff10, DiffList& diff21);
181         void WritePatchFileHeader(enum output_style output_style, BOOL bAppendFiles);
182         void WritePatchFileTerminator(enum output_style output_style);
183         void SetFilterList(const String& filterStr);
184         void EnablePlugins(bool enable);
185         bool IsTrivialBytes(const char* Start, const char* End,
186                 const FilterCommentsSet& filtercommentsset);
187         bool IsTrivialLine(const std::string &Line, const char * StartOfComment,
188            const char * EndOfComment, const char * InLineComment,
189            const FilterCommentsSet& filtercommentsset);
190         bool PostFilter(int StartPos, int EndPos, int Direction,
191                 int QtyLinesInBlock, OP_TYPE &Op, int FileNo,
192                 const FilterCommentsSet& filtercommentsset);
193         void PostFilter(int LineNumberLeft, int QtyLinesLeft, int LineNumberRight,
194                 int QtyLinesRight, OP_TYPE &Op, const FilterCommentsManager &filtercommentsmanager,
195                 const TCHAR *FileNameExt);
196
197 protected:
198         String FormatSwitchString();
199         BOOL Diff2Files(struct change ** diffs, DiffFileData *diffData,
200                 int * bin_status, int * bin_file);
201         void LoadWinMergeDiffsFromDiffUtilsScript(struct change * script, const file_data * inf);
202         void WritePatchFile(struct change * script, file_data * inf);
203 public:
204         void LoadWinMergeDiffsFromDiffUtilsScript3(
205                 struct change * script10, struct change * script02, struct change * script12,
206                 const file_data * inf10, const file_data * inf02, const file_data * inf12);
207         void FreeDiffUtilsScript3(struct change * & script10, struct change * & script12, struct change * & script02);
208         bool RegExpFilter(int StartPos, int EndPos, int FileNo);
209
210 private:
211         DiffutilsOptions m_options;
212         DIFFSTATUS m_status; /**< Status of last compare */
213         FilterList * m_pFilterList; /**< List of linefilters. */
214         PathContext m_files; /**< Full path to diff'ed file. */
215         PathContext m_alternativePaths; /**< file's alternative path (may be relative). */
216         PathContext m_originalFile; /**< file's original (NON-TEMP) path. */
217
218         String m_sPatchFile; /**< Full path to created patch file. */
219         BOOL m_bPathsAreTemp; /**< Are compared paths temporary? */
220         /// prediffer info are stored only for MergeDoc
221         PrediffingInfo * m_infoPrediffer;
222         /// prediffer info are stored only for MergeDoc
223         String m_sToFindPrediffer;
224         BOOL m_bUseDiffList; /**< Are results returned in difflist? */
225         BOOL m_bCreatePatchFile; /**< Do we create a patch file? */
226         BOOL m_bAddCmdLine; /**< Do we add commandline to patch file? */
227         BOOL m_bAppendFiles; /**< Do we append to existing patch file? */
228         int m_nDiffs; /**< Difference count */
229         int m_codepage; /**< Codepage used in line filter */
230         DiffList *m_pDiffList; /**< Pointer to external DiffList */
231         MovedLines * m_pMovedLines[3];
232         FilterCommentsManager * m_FilterCommentsManager; /**< Comments filtering manager */
233         bool m_bPluginsEnabled; /**< Are plugins enabled? */
234 };
235
236
237 #endif // _DIFFWRAPPER_H