1 /////////////////////////////////////////////////////////////////////////////
2 // WinMerge: an interactive diff/merge utility
3 // Copyright (C) 1997-2000 Thingamahoochie Software
5 // SPDX-License-Identifier: GPL-2.0-or-later
6 /////////////////////////////////////////////////////////////////////////////
8 * @file FileTransform.h
10 * @brief Declaration of file transformations
15 #include "UnicodeString.h"
19 namespace FileTransform
21 extern bool AutoUnpacking;
22 extern bool AutoPrediffing;
26 * @brief Plugin information for a given file
28 * @note Can be be passed/copied between threads
36 std::vector<String> args;
40 void Initialize(bool automatic)
42 // and init Plugin/Prediffer mode and Plugin name accordingly
43 m_PluginPipeline = automatic ? _T("<Automatic>") : _T("");
46 explicit PluginForFile(bool automatic)
48 Initialize(automatic);
51 explicit PluginForFile(const String& pluginPipeline)
52 : m_PluginPipeline(pluginPipeline)
56 const String& GetPluginPipeline() const { return m_PluginPipeline; }
57 void SetPluginPipeline(const String& pluginPipeline) { m_PluginPipeline = pluginPipeline; }
58 void ClearPluginPipeline() { m_PluginPipeline.clear(); }
60 std::vector<PipelineItem> ParsePluginPipeline(String& errorMessage) const;
61 static std::vector<PipelineItem> ParsePluginPipeline(const String& pluginPipeline, String& errorMessage);
62 static String MakePluginPipeline(const std::vector<PipelineItem>& list);
63 static String MakeArguments(const std::vector<String>& args, const std::vector<StringView>& variables);
66 /// plugin name when it is defined
67 String m_PluginPipeline;
71 * @brief Unpacking/packing information for a given file
73 * @note Can be be copied between threads
74 * Each thread really needs its own instance so that subcode is really defined
75 * during the unpacking (open file) of the thread
77 class PackingInfo : public PluginForFile
80 explicit PackingInfo(bool automatic = FileTransform::AutoUnpacking)
81 : PluginForFile(automatic), m_bWebBrowser(false)
85 explicit PackingInfo(const String& pluginPipeline)
86 : PluginForFile(pluginPipeline), m_bWebBrowser(false)
90 bool GetPackUnpackPlugin(const String& filteredFilenames, bool bUrl, bool bReverse,
91 std::vector<std::tuple<PluginInfo*, std::vector<String>, bool>>& plugins,
92 String *pPluginPipelineResolved, String *pURLHandlerResolved, String& errorMessage) const;
95 // WinMerge uses one of these entry points to call a plugin
97 // bMayOverwrite : tells if we can overwrite the source file
98 // if we don't, don't forget do delete the temp file after use
101 * @brief Prepare one file for loading, scan all available plugins (events+filename filtering)
103 * @param filepath : [in, out] Most plugins change this filename
105 * @return Tells if WinMerge handles this file
107 * @note Event FILE_UNPACK
108 * Apply only the first correct handler
110 bool Unpacking(std::vector<int> * handlerSubcodes, String & filepath, const String& filteredText, const std::vector<StringView>& variables);
113 * @brief Prepare one file for saving, known handler
115 * @return Tells if we can save the file (really hope we can)
117 * @param filepath : [in, out] Most plugins change this filename
119 * @note Event FILE_PACK
120 * Never do Unicode conversion, it was done in SaveFromFile
122 bool pack(String & filepath, const String& dstFilepath, const std::vector<int>& handlerSubcodes, const std::vector<StringView>& variables) const;
124 bool Packing(const String& srcFilepath, const String& dstFilepath, const std::vector<int>& handlerSubcodes, const std::vector<StringView>& variables) const;
126 String GetUnpackedFileExtension(const String& filteredFilenames, int& preferredWindowType) const;
128 void EnableWebBrowserMode() { m_bWebBrowser = true; }
135 * @brief Prediffing information for a given file
137 * @note Can be be passed/copied between threads
139 class PrediffingInfo : public PluginForFile
142 explicit PrediffingInfo(bool automatic = FileTransform::AutoPrediffing)
143 : PluginForFile(automatic)
147 explicit PrediffingInfo(const String& pluginPipeline)
148 : PluginForFile(pluginPipeline)
152 bool GetPrediffPlugin(const String& filteredFilenames, bool bReverse,
153 std::vector<std::tuple<PluginInfo*, std::vector<String>, bool>>& plugins,
154 String* pPluginPipelineResolved, String& errorMessage) const;
157 * @brief Prepare one file for diffing, scan all available plugins (events+filename filtering)
159 * @param filepath : [in, out] Most plugins change this filename
160 * @param handler : unpacking handler, to keep to pack again
162 * @return Tells if WinMerge handles this file
164 * @note Event FILE_PREDIFF BUFFER_PREDIFF
165 * Apply only the first correct handler
167 bool Prediffing(String & filepath, const String& filteredText, bool bMayOverwrite, const std::vector<StringView>& variables);
171 * @brief Editor script information
173 * @note Can be be passed/copied between threads
175 class EditorScriptInfo : public PluginForFile
178 explicit EditorScriptInfo(const String& pluginPipeline)
179 : PluginForFile(pluginPipeline)
183 bool GetEditorScriptPlugin(std::vector<std::tuple<PluginInfo*, std::vector<String>, int>>& plugins,
184 String& errorMessage) const;
186 bool TransformText(String & text, const std::vector<StringView>& variables, bool& changed);
189 namespace FileTransform
192 * @brief Transform all files to UTF8 aslong possible
194 * @param codepage : [in] codepage of source file
195 * @param filepath : [in,out] path of file to be prepared. This filename is updated if bMayOverwrite is false
196 * @param bMayOverwrite : [in] True only if the filepath points out a temp file
198 * @return Tells if we can go on with diffutils
199 * convert all Ansi or unicode-files to UTF8
200 * if other file is unicode or uses a different codepage
202 bool AnyCodepageToUTF8(int codepage, String & filepath, bool bMayOverwrite);
205 std::vector<std::tuple<String, String, unsigned, PluginInfo *>>,
206 std::map<String, std::vector<std::tuple<String, String, unsigned, PluginInfo *>>>
208 CreatePluginMenuInfos(const String& filteredFilenames, const std::vector<std::wstring>& events, unsigned baseId);
210 inline const std::vector<String> UnpackerEventNames = { L"BUFFER_PACK_UNPACK", L"FILE_PACK_UNPACK", L"FILE_FOLDER_PACK_UNPACK" };
211 inline const std::vector<String> PredifferEventNames = { L"BUFFER_PREDIFF", L"FILE_PREDIFF" };
212 inline const std::vector<String> EditorScriptEventNames = { L"EDITOR_SCRIPT" };