2 * @file DiffFileData.cpp
4 * @brief Code for DiffFileData class
6 * @date Created: 2003-08-22
10 #include "DiffFileData.h"
14 #include "FileLocation.h"
17 #include "FileTransform.h"
22 * @brief Simple initialization of DiffFileData
23 * @note Diffcounts are initialized to invalid values, not zeros.
25 DiffFileData::DiffFileData()
26 : m_inf(new file_data[2]{})
32 /** @brief deallocate member data */
33 DiffFileData::~DiffFileData()
39 /** @brief Open file descriptors in the inf structure (return false if failure) */
40 bool DiffFileData::OpenFiles(const String& szFilepath1, const String& szFilepath2)
42 m_FileLocation[0].setPath(szFilepath1);
43 m_FileLocation[1].setPath(szFilepath2);
44 bool b = DoOpenFiles();
50 /** @brief stash away true names for display, before opening files */
51 void DiffFileData::SetDisplayFilepaths(const String& szTrueFilepath1, const String& szTrueFilepath2)
53 m_sDisplayFilepath[0] = szTrueFilepath1;
54 m_sDisplayFilepath[1] = szTrueFilepath2;
58 /** @brief Open file descriptors in the inf structure (return false if failure) */
59 bool DiffFileData::DoOpenFiles()
63 for (int i = 0; i < 2; ++i)
65 // Fill in 8-bit versions of names for diffutils (WinMerge doesn't use these)
66 // Actual paths are m_FileLocation[i].filepath
67 // but these are often temporary files
68 // Displayable (original) paths are m_sDisplayFilepath[i]
69 m_inf[i].name = _strdup(ucr::toSystemCP(m_sDisplayFilepath[i]).c_str());
70 if (m_inf[i].name == nullptr)
73 // Open up file descriptors
74 // Always use O_BINARY mode, to avoid terminating file read on ctrl-Z (DOS EOF)
75 // Also, WinMerge-modified diffutils handles all three major eol styles
76 if (m_inf[i].desc == 0)
78 _tsopen_s(&m_inf[i].desc, TFile(m_FileLocation[i].filepath).wpath().c_str(),
79 O_RDONLY | O_BINARY, _SH_DENYNO, _S_IREAD);
81 if (m_inf[i].desc < 0)
84 // Get file stats (diffutils uses these)
85 if (myfstat(m_inf[i].desc, &m_inf[i].stat) != 0)
90 if (strutils::compare_nocase(m_FileLocation[0].filepath,
91 m_FileLocation[1].filepath) == 0)
93 m_inf[1].desc = m_inf[0].desc;
101 /** @brief Clear inf structure to pristine */
102 void DiffFileData::Reset()
104 assert(m_inf != nullptr);
105 // If diffutils put data in, have it cleanup
108 cleanup_file_buffers(m_inf);
111 // clean up any open file handles, and zero stuff out
112 // open file handles might be leftover from a failure in DiffFileData::OpenFiles
113 for (int i = 0; i < 2; ++i)
115 if (m_inf[1].desc == m_inf[0].desc)
119 free((void *)m_inf[i].name);
120 m_inf[i].name = nullptr;
122 if (m_inf[i].desc > 0)
124 _close(m_inf[i].desc);
132 * @brief Invoke appropriate plugins for prediffing
133 * return false if anything fails
134 * caller has to DeleteFile filepathTransformed, if it differs from filepath
136 bool DiffFileData::Filepath_Transform(bool bForceUTF8,
137 const FileTextEncoding & encoding, const String & filepath, String & filepathTransformed,
138 const String& filteredFilenames, PrediffingInfo * infoPrediffer)
140 // third step : prediff (plugins)
141 bool bMayOverwrite = // temp variable set each time it is used
142 (filepathTransformed != filepath); // may overwrite if we've already copied to temp file
144 // FileTransform_Prediffing tries each prediffer for the pointed out filteredFilenames
145 // if a prediffer fails, we consider it is not the good one, that's all
146 // FileTransform_Prediffing returns `false` only if the prediffer works,
147 // but the data can not be saved to disk (no more place ??)
148 if (!FileTransform::Prediffing(infoPrediffer, filepathTransformed, filteredFilenames, bMayOverwrite))
151 if ((encoding.m_unicoding && encoding.m_unicoding != ucr::UTF8) || bForceUTF8)
153 // fourth step : prepare for diffing
154 // may overwrite if we've already copied to temp file
155 bool bMayOverwrite1 = 0 != strutils::compare_nocase(filepathTransformed, filepath);
156 if (!FileTransform::AnyCodepageToUTF8(encoding.m_codepage, filepathTransformed, bMayOverwrite1))