OSDN Git Service

Update TranslationsStatus
[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
17 class PluginInfo;
18
19 namespace FileTransform
20 {
21 extern bool AutoUnpacking;
22 extern bool AutoPrediffing;
23 }
24
25 /**
26  * @brief Plugin information for a given file
27  *
28  * @note Can be be passed/copied between threads
29  */
30 class PluginForFile
31 {
32 public:
33         struct PipelineItem
34         {
35                 String name;
36                 std::vector<String> args;
37                 TCHAR quoteChar;
38         };
39
40         void Initialize(bool automatic)
41         {
42                 // and init Plugin/Prediffer mode and Plugin name accordingly
43                 m_PluginPipeline = automatic ? _T("<Automatic>") : _T("");
44         }
45
46         explicit PluginForFile(bool automatic) 
47         {
48                 Initialize(automatic);
49         }
50
51         explicit PluginForFile(const String& pluginPipeline)
52         : m_PluginPipeline(pluginPipeline)
53         {
54         }
55
56         const String& GetPluginPipeline() const { return m_PluginPipeline; }
57         void SetPluginPipeline(const String& pluginPipeline) { m_PluginPipeline = pluginPipeline; }
58         void ClearPluginPipeline() { m_PluginPipeline.clear(); }
59
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);
64
65 protected:
66         /// plugin name when it is defined
67         String m_PluginPipeline;
68 };
69
70 /**
71  * @brief Unpacking/packing information for a given file
72  *
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
76  */
77 class PackingInfo : public PluginForFile
78 {
79 public:
80         explicit PackingInfo(bool automatic = FileTransform::AutoUnpacking)
81         : PluginForFile(automatic), m_bWebBrowser(false)
82         {
83         }
84
85         explicit PackingInfo(const String& pluginPipeline)
86         : PluginForFile(pluginPipeline), m_bWebBrowser(false)
87         {
88         }
89
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;
93
94         // Events handler
95         // WinMerge uses one of these entry points to call a plugin
96
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
99
100         /**
101          * @brief Prepare one file for loading, scan all available plugins (events+filename filtering) 
102          *
103          * @param filepath : [in, out] Most plugins change this filename
104          *
105          * @return Tells if WinMerge handles this file
106          *
107          * @note Event FILE_UNPACK
108          * Apply only the first correct handler
109          */
110         bool Unpacking(std::vector<int> * handlerSubcodes, String & filepath, const String& filteredText, const std::vector<StringView>& variables);
111
112         /**
113          * @brief Prepare one file for saving, known handler
114          *
115          * @return Tells if we can save the file (really hope we can)
116          *
117          * @param filepath : [in, out] Most plugins change this filename
118          *
119          * @note Event FILE_PACK
120          * Never do Unicode conversion, it was done in SaveFromFile
121          */
122         bool pack(String & filepath, const String& dstFilepath, const std::vector<int>& handlerSubcodes, const std::vector<StringView>& variables) const;
123
124         bool Packing(const String& srcFilepath, const String& dstFilepath, const std::vector<int>& handlerSubcodes, const std::vector<StringView>& variables) const;
125
126         String GetUnpackedFileExtension(const String& filteredFilenames, int& preferredWindowType) const;
127
128         void EnableWebBrowserMode() { m_bWebBrowser = true; }
129 private:
130         String m_URLHandler;
131         bool m_bWebBrowser;
132 };
133
134 /**
135  * @brief Prediffing information for a given file
136  *
137  * @note Can be be passed/copied between threads
138  */
139 class PrediffingInfo : public PluginForFile
140 {
141 public:
142         explicit PrediffingInfo(bool automatic = FileTransform::AutoPrediffing)
143         : PluginForFile(automatic)
144         {
145         }
146
147         explicit PrediffingInfo(const String& pluginPipeline)
148         : PluginForFile(pluginPipeline)
149         {
150         }
151
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;
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, bool bMayOverwrite, const std::vector<StringView>& variables);
168 };
169
170 /**
171  * @brief Editor script information
172  *
173  * @note Can be be passed/copied between threads
174  */
175 class EditorScriptInfo : public PluginForFile
176 {
177 public:
178         explicit EditorScriptInfo(const String& pluginPipeline)
179         : PluginForFile(pluginPipeline)
180         {
181         }
182
183         bool GetEditorScriptPlugin(std::vector<std::tuple<PluginInfo*, std::vector<String>, int>>& plugins,
184                 String& errorMessage) const;
185
186         bool TransformText(String & text, const std::vector<StringView>& variables, bool& changed);
187 };
188
189 namespace FileTransform
190 {
191 /**
192  * @brief Transform all files to UTF8 aslong possible
193  *
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
197  *
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
201  */
202 bool AnyCodepageToUTF8(int codepage, String & filepath, bool bMayOverwrite);
203
204 std::pair<
205         std::vector<std::tuple<String, String, unsigned, PluginInfo *>>,
206         std::map<String, std::vector<std::tuple<String, String, unsigned, PluginInfo *>>>
207 >
208 CreatePluginMenuInfos(const String& filteredFilenames, const std::vector<std::wstring>& events, unsigned baseId);
209
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" };
213
214 }