OSDN Git Service

Implement multithreaded compare
[winmerge-jp/winmerge-jp.git] / Src / DiffContext.h
1 /**
2  *  @file DiffContext.h
3  *
4  *  @brief Declarations of CDiffContext and diff structures
5  */
6 // ID line follows -- this is updated by SVN
7 // $Id: DiffContext.h 6910 2009-07-12 09:06:54Z kimmov $
8
9 #if !defined(AFX_DIFFCONTEXT_H__D3CC86BE_F11E_11D2_826C_00A024706EDC__INCLUDED_)
10 #define AFX_DIFFCONTEXT_H__D3CC86BE_F11E_11D2_826C_00A024706EDC__INCLUDED_
11 #pragma once
12 #define POCO_NO_UNWINDOWS 1
13 #include <Poco/Mutex.h>
14 #include <Poco/ThreadLocal.h>
15 #include <boost/scoped_ptr.hpp>
16 #include "PathContext.h"
17 #include "DiffFileInfo.h"
18 #include "DiffItemList.h"
19
20 class PackingInfo;
21 class PrediffingInfo;
22 class IDiffFilter;
23 struct DIFFITEM;
24 class CompareStats;
25 class IAbortable;
26 class CDiffWrapper;
27 class FilterList;
28 class CompareOptions;
29 struct DIFFOPTIONS;
30
31 /** Interface to a provider of plugin info */
32 class IPluginInfos
33 {
34 public:
35         virtual void FetchPluginInfos(const String& filteredFilenames, 
36                                       PackingInfo ** infoUnpacker, 
37                                       PrediffingInfo ** infoPrediffer) = 0;
38 };
39
40 /**
41  * The folder compare context.
42  * This class holds data of the current folder compare. There are paths
43  * to compare, filters used, compare options etc. And compare results list
44  * is also contained in this class. Many compare classes and functions have
45  * a pointer to instance of this class. 
46  *
47  * @note If you add new member variables, remember to copy values in
48  * CDiffContext::CDiffContext(..,CDiffContext) constructor!
49  */
50 class CDiffContext : public DiffItemList
51 {
52 public:
53         /** @brief Special values for difference counts. */
54         enum
55         {
56                 DIFFS_UNKNOWN = -1, /**< Difference count unknown (generally). */
57                 DIFFS_UNKNOWN_QUICKCOMPARE = -9, /**< Unknown because of quick-compare method. */
58         };
59
60         CDiffContext(const PathContext & paths, int compareMethod);
61         ~CDiffContext();
62
63         void UpdateVersion(DIFFITEM & di, int nIndex) const;
64
65         /**
66          * Get the main compare method used in this compare.
67          * @return Compare method used.
68          */
69         int GetCompareMethod(void) const { return m_nCompMethod; }
70
71         //@{
72         /**
73          * @name Path accessor functions.
74          *
75          * These functions return left/right path associated to DiffContext.
76          * There is no setter fuctions and path can be set only via constructor.
77          * Normalized paths are preferred to use - short paths are expanded
78          * and trailing slashes removed (except from root path).
79          */
80         /**
81          * Get left-side compare path.
82          * @return full path in left-side.
83          */
84         String GetLeftPath() const { return m_paths.GetLeft(false); }
85         String GetMiddlePath() const { return m_paths.GetMiddle(false); }
86         /**
87          * Get right-side compare path.
88          * @return full path in right-side.
89          */
90         String GetRightPath() const { return m_paths.GetRight(false); }
91         String GetPath(int nIndex) const { return m_paths.GetPath(nIndex, false); }
92         /**
93          * Get left-side compare path in normalized form.
94          * @return full path in left-side.
95          */
96         String GetNormalizedLeft() const { return m_paths.GetLeft(); }
97         String GetNormalizedMiddle() const { return m_paths.GetMiddle(); }
98         /**
99          * Get right-side compare path in normalized form.
100          * @return full path in left-side.
101          */
102         String GetNormalizedRight() const { return m_paths.GetRight(); }
103         String GetNormalizedPath(int nIndex) const { return m_paths.GetPath(nIndex, true); }
104         PathContext GetNormalizedPaths() const
105         {
106                 PathContext paths;
107                 for (int nIndex = 0; nIndex < m_paths.GetSize(); nIndex++)
108                         paths.SetPath(nIndex, m_paths.GetPath(nIndex, true));
109                 return paths;
110         }
111         //@}
112
113         // change an existing difference
114         bool UpdateInfoFromDiskHalf(DIFFITEM & di, int nIndex);
115         void UpdateStatusFromDisk(Poco::UIntPtr diffpos, bool bLeft, bool bRight);
116         void UpdateStatusFromDisk(Poco::UIntPtr diffpos, bool bLeft, bool bMiddle, bool bRight);
117
118         bool CreateCompareOptions(int compareMethod, const DIFFOPTIONS & options);
119         CompareOptions * GetCompareOptions(int compareMethod);
120
121         // retrieve or manufacture plugin info for specified file comparison
122         void FetchPluginInfos(const String& filteredFilenames,
123                 PackingInfo ** infoUnpacker, PrediffingInfo ** infoPrediffer);
124
125         //@{
126         /**
127          * @name Compare aborting interface.
128          * These functions handle compare aborting using IAbortable interface.
129          */
130         bool ShouldAbort() const;
131
132         /**
133          * Set pointer to IAbortable interface.
134          * This function sets pointer to interface used to abort the compare when
135          * user wants to.
136          * @param [in] piAbortable Pointer to interface.
137          */
138         void SetAbortable(IAbortable * piAbortable) { m_piAbortable = piAbortable; }
139
140         /**
141          * Returns a pointer to current IAbortable interface.
142          * This function returns a pointer to interface used to abort the compare.
143          * @return Pointer to current IAbortable interface.
144          */
145         const IAbortable * GetAbortable() const { return m_piAbortable; }
146         //@}
147
148         int GetCompareDirs() const { return m_paths.GetSize(); }
149
150         IDiffFilter * m_piFilterGlobal; /**< Interface for file filtering. */
151         IPluginInfos * m_piPluginInfos;
152         int m_iGuessEncodingType;
153
154         /**
155          * The current and effective compare method.
156          * During the compare we may change compare method per file. This may
157          * happen for various reasons. But the change is always temporarily and
158          * we must return to the main method indicated by m_nCompMethod.
159          */
160         Poco::ThreadLocal<int> m_nCurrentCompMethod;
161
162         bool m_bIgnoreSmallTimeDiff; /**< Ignore small timedifferences when comparing by date */
163         CompareStats *m_pCompareStats; /**< Pointer to compare statistics */
164
165         /**
166          * Optimize compare by stopping after first difference.
167          * In some compare methods (currently quick compare) we can stop the
168          * compare right after finding the first difference. This speeds up the
169          * compare, but also causes compare statistics to be inaccurate.
170          */
171         bool m_bStopAfterFirstDiff;
172
173         /**
174          * Threshold size for switching to quick compare.
175          * When diffutils compare is selected, files bigger (in bytes) than this
176          * value are compared using Quick compare. This is because diffutils simply
177          * cannot compare large files. And large files are usually binary files.
178          */
179         int m_nQuickCompareLimit;
180
181         /**
182          * Walk into unique folders and add contents.
183          * This enables/disables walking into unique folders. If we don't walk into
184          * unique folders, they are shown as such in folder compare results. If we
185          * walk into unique folders, we'll show all files in the unique folder and
186          * in possible subfolders.
187          *
188          * This value is true by default.
189          */
190         bool m_bWalkUniques;
191
192         bool m_bRecursive; /**< Do we include subfolders to compare? */
193         bool m_bPluginsEnabled; /**< Are plugins enabled? */
194         boost::scoped_ptr<FilterList> m_pFilterList; /**< Filter list for line filters */
195         CDiffWrapper *m_pDiffWrapper;
196
197 private:
198         /**
199          * The main compare method used.
200          * This is the main compare method set when compare is started. There
201          * can be temporary switches to other method (e.g. for large file) but
202          * this main method must be set back for next file.
203          */
204         int m_nCompMethod;
205
206         boost::scoped_ptr<DIFFOPTIONS> m_pOptions; /**< Generalized compare options. */
207         boost::scoped_ptr<CompareOptions> m_pContentCompareOptions; /**< Per compare method compare options. */
208         boost::scoped_ptr<CompareOptions> m_pQuickCompareOptions;   /**< Per compare method compare options. */
209         PathContext m_paths; /**< (root) paths for this context */
210         IAbortable *m_piAbortable; /**< Interface for aborting the compare. */
211         Poco::FastMutex m_mutex;
212 };
213
214 #endif // !defined(AFX_DIFFCONTEXT_H__D3CC86BE_F11E_11D2_826C_00A024706EDC__INCLUDED_)