1 /////////////////////////////////////////////////////////////////////////////
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 (at
6 // your option) any later version.
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
11 // GNU General Public License for more details.
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 /////////////////////////////////////////////////////////////////////////////
20 * @brief Code file routines
24 #include "PatchTool.h"
25 #include "UnicodeString.h"
26 #include "DiffWrapper.h"
27 #include "PathContext.h"
39 * @brief Default constructor.
41 CPatchTool::CPatchTool() : m_bOpenToEditor(false)
46 * @brief Default destructor.
48 CPatchTool::~CPatchTool()
53 * @brief Adds files to list for patching.
54 * @param [in] file1 First file to add.
55 * @param [in] file2 Second file to add.
57 void CPatchTool::AddFiles(const String &file1, const String &file2)
63 // TODO: Read and add file's timestamps
64 m_fileList.push_back(tFiles);
68 * @brief Add files with alternative paths.
69 * This function adds files with alternative paths. Alternative path is the
70 * one that is added to the patch file. So while @p file1 and @p file2 are
71 * paths in disk (can be temp file names), @p altPath1 and @p altPath2 are
72 * "visible " paths printed to the patch file.
73 * @param [in] file1 First path in disk.
74 * @param [in] altPath1 First path as printed to the patch file.
75 * @param [in] file2 Second path in disk.
76 * @param [in] altPath2 Second path as printed to the patch file.
78 void CPatchTool::AddFiles(const String &file1, const String &altPath1,
79 const String &file2, const String &altPath2)
84 tFiles.pathLeft = altPath1;
85 tFiles.pathRight = altPath2;
87 // TODO: Read and add file's timestamps
88 m_fileList.push_back(tFiles);
92 * @brief Create a patch from files given.
93 * @note Files can be given using AddFiles() or selecting using
96 int CPatchTool::CreatePatch()
103 // If files already inserted, add them to dialog
104 for(std::vector<PATCHFILES>::iterator iter = m_fileList.begin(); iter != m_fileList.end(); ++iter)
106 dlgPatch.AddItem(*iter);
109 if (ShowDialog(&dlgPatch))
113 if (!paths::CreateIfNeeded(paths::GetPathOnly(dlgPatch.m_fileResult)))
115 LangMessageBox(IDS_FOLDER_NOTEXIST, MB_OK | MB_ICONSTOP);
119 // Select patch create -mode
120 m_diffWrapper.SetCreatePatchFile(dlgPatch.m_fileResult);
121 m_diffWrapper.SetAppendFiles(dlgPatch.m_appendFile);
122 m_diffWrapper.SetPrediffer(nullptr);
124 size_t fileCount = dlgPatch.GetItemCount();
126 m_diffWrapper.WritePatchFileHeader(dlgPatch.m_outputStyle, dlgPatch.m_appendFile);
127 m_diffWrapper.SetAppendFiles(true);
131 TFile file(emptyFile.GetPath());
132 file.setLastModified(Poco::Timestamp::fromEpochTime(0));
134 for (size_t index = 0; index < fileCount; index++)
136 const PATCHFILES& tFiles = dlgPatch.GetItemAt(index);
137 String filename1 = tFiles.lfile.length() == 0 ? emptyFile.GetPath() : tFiles.lfile;
138 String filename2 = tFiles.rfile.length() == 0 ? emptyFile.GetPath() : tFiles.rfile;
140 // Set up DiffWrapper
141 m_diffWrapper.SetPaths(PathContext(filename1, filename2), false);
142 m_diffWrapper.SetAlternativePaths(PathContext(tFiles.pathLeft, tFiles.pathRight));
143 m_diffWrapper.SetCompareFiles(PathContext(tFiles.lfile, tFiles.rfile));
144 bool bDiffSuccess = m_diffWrapper.RunFileDiff();
145 m_diffWrapper.GetDiffStatus(&status);
149 LangMessageBox(IDS_FILEERROR, MB_ICONSTOP);
153 else if (status.bBinaries)
155 LangMessageBox(IDS_CANNOT_CREATE_BINARYPATCH, MB_ICONSTOP);
159 else if (status.bPatchFileFailed)
161 String errMsg = strutils::format_string1(_("Could not write to file %1."), dlgPatch.m_fileResult);
162 AfxMessageBox(errMsg.c_str(), MB_ICONSTOP);
168 m_diffWrapper.WritePatchFileTerminator(dlgPatch.m_outputStyle);
170 if (bResult && fileCount > 0)
172 LangMessageBox(IDS_DIFF_SUCCEEDED, MB_ICONINFORMATION|MB_DONT_DISPLAY_AGAIN,
175 m_sPatchFile = dlgPatch.m_fileResult;
176 m_bOpenToEditor = dlgPatch.m_openToEditor;
180 dlgPatch.ClearItems();
184 theApp.OpenFileToExternalEditor(m_sPatchFile);
190 * @brief Show patch options dialog and check options selected.
191 * @return `true` if user wants to create a patch (didn't cancel dialog).
193 bool CPatchTool::ShowDialog(CPatchDlg *pDlgPatch)
195 DIFFOPTIONS diffOptions = {0};
196 PATCHOPTIONS patchOptions;
199 if (pDlgPatch->DoModal() == IDOK)
201 // There must be one filepair
202 if (pDlgPatch->GetItemCount() < 1)
205 // These two are from dropdown list - can't be wrong
206 patchOptions.outputStyle = pDlgPatch->m_outputStyle;
207 patchOptions.nContext = pDlgPatch->m_contextLines;
209 // Checkbox - can't be wrong
210 patchOptions.bAddCommandline = pDlgPatch->m_includeCmdLine;
211 m_diffWrapper.SetPatchOptions(&patchOptions);
213 // These are from checkboxes and radiobuttons - can't be wrong
214 diffOptions.nIgnoreWhitespace = pDlgPatch->m_whitespaceCompare;
215 diffOptions.bIgnoreBlankLines = pDlgPatch->m_ignoreBlanks;
216 m_diffWrapper.SetAppendFiles(pDlgPatch->m_appendFile);
218 // Use this because non-sensitive setting can't write
219 // patch file EOLs correctly
220 diffOptions.bIgnoreEol = pDlgPatch->m_ignoreEOLDifference;
222 diffOptions.bIgnoreCase = !pDlgPatch->m_caseSensitive;
223 diffOptions.nDiffAlgorithm = pDlgPatch->m_diffAlgorithm;
224 diffOptions.bIndentHeuristic = pDlgPatch->m_indentHeuristic;
225 m_diffWrapper.SetOptions(&diffOptions);