OSDN Git Service

Make Unpacker plugins available for image compare and binary compare
[winmerge-jp/winmerge-jp.git] / Src / FileTransform.h
1 /////////////////////////////////////////////////////////////////////////////
2 //    WinMerge:  an interactive diff/merge utility
3 //    Copyright (C) 1997-2000  Thingamahoochie Software
4 //    Author: Dean Grimm
5 //    SPDX-License-Identifier: GPL-2.0-or-later
6 /////////////////////////////////////////////////////////////////////////////
7 /**
8  *  @file FileTransform.h
9  *
10  *  @brief Declaration of file transformations
11  */ 
12 #pragma once
13
14 #include <vector>
15 #include "UnicodeString.h"
16 #include "MergeApp.h"
17
18 /**
19  * @brief Modes for plugin (Modes for prediffing included)
20  */
21 enum class PLUGIN_MODE
22 {
23         // Modes for "unpacking"
24         PLUGIN_MANUAL,
25         PLUGIN_AUTO,
26         PLUGIN_BUILTIN_XML,
27
28         // Modes for "prediffing"
29         PREDIFF_MANUAL = PLUGIN_MANUAL,
30         PREDIFF_AUTO = PLUGIN_AUTO,
31 };
32
33 namespace FileTransform
34 {
35 extern PLUGIN_MODE g_UnpackerMode;
36 extern PLUGIN_MODE g_PredifferMode;
37 }
38
39 class UniFile;
40
41 /**
42  * @brief Plugin information for a given file
43  *
44  * @note Can be be passed/copied between threads
45  */
46 class PluginForFile
47 {
48 public:
49         void Initialize(PLUGIN_MODE Mode)
50         {
51                 // and init Plugin/Prediffer mode and Plugin name accordingly
52                 m_PluginOrPredifferMode = Mode;
53                 if (Mode != PLUGIN_MODE::PLUGIN_AUTO)
54                 {
55                         m_PluginName.erase();
56                 }
57                 else
58                 {
59                         m_PluginName = _("<Automatic>");
60                 }
61         };
62         explicit PluginForFile(PLUGIN_MODE Mode) 
63         {
64                 Initialize(Mode);
65         };
66 public:
67         /// PLUGIN_AUTO if the plugin will be defined during the first use (via scan of all available plugins)
68         PLUGIN_MODE     m_PluginOrPredifferMode;
69
70         /// plugin name when it is defined
71         String          m_PluginName;
72 };
73
74 /**
75  * @brief Unpacking/packing information for a given file
76  *
77  * @note Can be be copied between threads
78  * Each thread really needs its own instance so that subcode is really defined
79  * during the unpacking (open file) of the thread
80  */
81 class PackingInfo : public PluginForFile
82 {
83 public:
84         explicit PackingInfo(PLUGIN_MODE Mode = FileTransform::g_UnpackerMode)
85         : PluginForFile(Mode)
86         , m_pufile(nullptr)
87         , m_bDisallowMixedEOL(false)
88         {
89         }
90 public:
91         /// text type to override syntax highlighting
92         String          m_textType;
93         /// custom UniFile
94         UniFile*        m_pufile;
95         bool            m_bDisallowMixedEOL;
96 };
97
98 /**
99  * @brief Prediffing information for a given file
100  *
101  * @note Can be be passed/copied between threads
102  */
103 class PrediffingInfo : public PluginForFile
104 {
105 public:
106         explicit PrediffingInfo(PLUGIN_MODE Mode = FileTransform::g_PredifferMode)
107         : PluginForFile(Mode)
108         {
109         }
110 };
111
112 namespace FileTransform
113 {
114
115 // Events handler
116 // WinMerge uses one of these entry points to call a plugin
117
118 // bMayOverwrite : tells if we can overwrite the source file
119 // if we don't, don't forget do delete the temp file after use
120
121 /**
122  * @brief Prepare one file for loading, scan all available plugins (events+filename filtering) 
123  *
124  * @param filepath : [in, out] Most plugins change this filename
125  * @param handler : unpacking handler, to keep to pack again
126  *
127  * @return Tells if WinMerge handles this file
128  *
129  * @note Event FILE_UNPACK
130  * Apply only the first correct handler
131  */
132 bool Unpacking(String & filepath, const String& filteredText, PackingInfo * handler, int * handlerSubcode);
133 /**
134  * @brief Prepare one file for loading, known handler
135  *
136  * @param filepath : [in, out] Most plugins change this filename
137  */
138 bool Unpacking(String & filepath, const PackingInfo * handler, int * handlerSubcode);
139
140 bool Unpacking(PackingInfo * handler, int * handlerSubcode, String & filepath, const String& filteredText);
141
142 /**
143  * @brief Prepare one file for saving, known handler
144  *
145  * @return Tells if we can save the file (really hope we can)
146  *
147  * @param filepath : [in, out] Most plugins change this filename
148  *
149  * @note Event FILE_PACK
150  * Never do Unicode conversion, it was done in SaveFromFile
151  */
152 bool Packing(String & filepath, const PackingInfo& handler, int handlerSubcode);
153
154 bool Packing(const String& srcFilepath, const String& dstFilepath, const PackingInfo& handler, int handlerSubcode);
155
156 /**
157  * @brief Prepare one file for diffing, scan all available plugins (events+filename filtering) 
158  *
159  * @param filepath : [in, out] Most plugins change this filename
160  * @param handler : unpacking handler, to keep to pack again
161  *
162  * @return Tells if WinMerge handles this file
163  *
164  * @note Event FILE_PREDIFF BUFFER_PREDIFF
165  * Apply only the first correct handler
166  */
167 bool Prediffing(String & filepath, const String& filteredText, PrediffingInfo * handler, bool bMayOverwrite);
168 /**
169  * @brief Prepare one file for diffing, known handler
170  *
171  * @param filepath : [in, out] Most plugins change this filename
172  */
173 bool Prediffing(String & filepath, PrediffingInfo handler, bool bMayOverwrite);
174
175 bool Prediffing(PrediffingInfo * handler, String & filepath, const String& filteredText, bool bMayOverwrite);
176
177 /**
178  * @brief Transform all files to UTF8 aslong possible
179  *
180  * @param codepage : [in] codepage of source file
181  * @param filepath : [in,out] path of file to be prepared. This filename is updated if bMayOverwrite is false
182  * @param bMayOverwrite : [in] True only if the filepath points out a temp file
183  *
184  * @return Tells if we can go on with diffutils
185  * convert all Ansi or unicode-files to UTF8 
186  * if other file is unicode or uses a different codepage
187  */
188 bool AnyCodepageToUTF8(int codepage, String & filepath, bool bMayOverwrite);
189
190
191 /**
192  * @brief Get the list of all the free functions in all the scripts for this event :
193  * 
194  * @note the order is :
195  * 1st script file, 1st function name
196  * 1st script file, 2nd function name
197  * 1st script file, ...
198  * 1st script file, last function name
199  * 2nd script file, 1st function name
200  * 2nd script file, 2nd function name
201  * 2nd script file, ...
202  * 2nd script file, last function name
203  * ... script file
204  * last script file, 1st function name
205  * last script file, 2nd function name
206  * last script file, ...
207  * last script file, last function name
208  */
209 std::vector<String> GetFreeFunctionsInScripts(const wchar_t* TransformationEvent);
210
211 /** 
212  * @brief : Execute one free function from one script
213  *
214  * @param iFncChosen : index of the function in the list returned by GetFreeFunctionsInScripts
215  *
216  * @return Tells if the text has been changed 
217  *
218  * @note Event EDITOR_SCRIPT, ?
219  */
220 bool Interactive(String & text, const wchar_t *TransformationEvent, int iFncChosen);
221
222 String GetUnpackedFileExtension(const String& filteredFilenames, const PackingInfo* handler);
223
224 }