OSDN Git Service

Leave the class name as CIniOptionsMgr, but rename the filename to IniOptionsMgr.*
[winmerge-jp/winmerge-jp.git] / Src / DiffFileData.cpp
1 /** 
2  * @file DiffFileData.cpp
3  *
4  * @brief Code for DiffFileData class
5  *
6  * @date  Created: 2003-08-22
7  */
8
9 #include "pch.h"
10 #include "DiffFileData.h"
11 #include <io.h>
12 #include <memory>
13 #include "DiffItem.h"
14 #include "FileLocation.h"
15 #include "diff.h"
16 #include "TFile.h"
17 #include "FileTransform.h"
18 #include "unicoder.h"
19 #include "DebugNew.h"
20
21 /**
22  * @brief Simple initialization of DiffFileData
23  * @note Diffcounts are initialized to invalid values, not zeros.
24  */
25 DiffFileData::DiffFileData()
26 : m_inf(new file_data[2]{})
27 , m_used(false)
28 {
29         Reset();
30 }
31
32 /** @brief deallocate member data */
33 DiffFileData::~DiffFileData()
34 {
35         Reset();
36         delete [] m_inf;
37 }
38
39 /** @brief Open file descriptors in the inf structure (return false if failure) */
40 bool DiffFileData::OpenFiles(const String& szFilepath1, const String& szFilepath2)
41 {
42         m_FileLocation[0].setPath(szFilepath1);
43         m_FileLocation[1].setPath(szFilepath2);
44         bool b = DoOpenFiles();
45         if (!b)
46                 Reset();
47         return b;
48 }
49
50 /** @brief stash away true names for display, before opening files */
51 void DiffFileData::SetDisplayFilepaths(const String& szTrueFilepath1, const String& szTrueFilepath2)
52 {
53         m_sDisplayFilepath[0] = szTrueFilepath1;
54         m_sDisplayFilepath[1] = szTrueFilepath2;
55 }
56
57
58 /** @brief Open file descriptors in the inf structure (return false if failure) */
59 bool DiffFileData::DoOpenFiles()
60 {
61         Reset();
62
63         for (int i = 0; i < 2; ++i)
64         {
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)
71                         return false;
72
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)
77                 {
78                         _tsopen_s(&m_inf[i].desc, TFile(m_FileLocation[i].filepath).wpath().c_str(),
79                                         O_RDONLY | O_BINARY, _SH_DENYNO, _S_IREAD);
80                 }
81                 if (m_inf[i].desc < 0)
82                         return false;
83
84                 // Get file stats (diffutils uses these)
85                 if (myfstat(m_inf[i].desc, &m_inf[i].stat) != 0)
86                 {
87                         return false;
88                 }
89                 
90                 if (strutils::compare_nocase(m_FileLocation[0].filepath,
91                                 m_FileLocation[1].filepath) == 0)
92                 {
93                         m_inf[1].desc = m_inf[0].desc;
94                 }
95         }
96
97         m_used = true;
98         return true;
99 }
100
101 /** @brief Clear inf structure to pristine */
102 void DiffFileData::Reset()
103 {
104         assert(m_inf != nullptr);
105         // If diffutils put data in, have it cleanup
106         if (m_used)
107         {
108                 cleanup_file_buffers(m_inf);
109                 m_used = false;
110         }
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)
114         {
115                 if (m_inf[1].desc == m_inf[0].desc)
116                 {
117                         m_inf[1].desc = 0;
118                 }
119                 free((void *)m_inf[i].name);
120                 m_inf[i].name = nullptr;
121
122                 if (m_inf[i].desc > 0)
123                 {
124                         _close(m_inf[i].desc);
125                 }
126                 m_inf[i].desc = 0;
127                 m_inf[i] = {};
128         }
129 }
130
131 /**
132  * @brief Invoke appropriate plugins for prediffing
133  * return false if anything fails
134  * caller has to DeleteFile filepathTransformed, if it differs from filepath
135  */
136 bool DiffFileData::Filepath_Transform(bool bForceUTF8,
137         const FileTextEncoding & encoding, const String & filepath, String & filepathTransformed,
138         const String& filteredFilenames, PrediffingInfo * infoPrediffer)
139 {
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
143
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))
149                 return false;
150
151         if ((encoding.m_unicoding && encoding.m_unicoding != ucr::UTF8) || bForceUTF8)
152         {
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))
157                         return false;
158         }
159         return true;
160 }