Add override specifier
[TortoiseGit.git] / src / TortoiseMerge / FileTextLines.h
blob99ed624240a974d72aa9f33232cf06428465bb9b
1 // TortoiseGitMerge - a Diff/Patch program
3 // Copyright (C) 2006-2007, 2012-2016 - TortoiseSVN
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software Foundation,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #pragma once
20 #include "EOL.h"
21 #include <deque>
22 #include <regex>
24 // A template class to make an array which looks like a CStringArray or CDWORDArray but
25 // is in fact based on a STL vector, which is much faster at large sizes
26 template <typename T> class CStdArrayV
28 public:
29 int GetCount() const { return (int)m_vec.size(); }
30 const T& GetAt(int index) const { return m_vec[index]; }
31 void RemoveAt(int index) { m_vec.erase(m_vec.begin()+index); }
32 void InsertAt(int index, const T& strVal) { m_vec.insert(m_vec.begin()+index, strVal); }
33 void InsertAt(int index, const T& strVal, int nCopies) { m_vec.insert(m_vec.begin()+index, nCopies, strVal); }
34 void SetAt(int index, const T& strVal) { m_vec[index] = strVal; }
35 void Add(const T& strVal) {
36 if (m_vec.size()==m_vec.capacity()) {
37 m_vec.reserve(m_vec.capacity() ? m_vec.capacity()*2 : 256);
39 m_vec.push_back(strVal);
41 void RemoveAll() { m_vec.clear(); }
42 void Reserve(int nHintSize) { m_vec.reserve(nHintSize); }
44 private:
45 std::vector<T> m_vec;
48 // A template class to make an array which looks like a CStringArray or CDWORDArray but
49 // is in fact based on a STL deque, which is much faster at large sizes
50 template <typename T> class CStdArrayD
52 public:
53 int GetCount() const { return (int)m_vec.size(); }
54 const T& GetAt(int index) const { return m_vec[index]; }
55 void RemoveAt(int index) { m_vec.erase(m_vec.begin()+index); }
56 void InsertAt(int index, const T& strVal) { m_vec.insert(m_vec.begin()+index, strVal); }
57 void InsertAt(int index, const T& strVal, int nCopies) { m_vec.insert(m_vec.begin()+index, nCopies, strVal); }
58 void SetAt(int index, const T& strVal) { m_vec[index] = strVal; }
59 void Add(const T& strVal) { m_vec.push_back(strVal); }
60 void RemoveAll() { m_vec.clear(); }
61 void Reserve(int ) { }
63 private:
64 std::deque<T> m_vec;
67 typedef CStdArrayV<DWORD> CStdDWORDArray;
69 struct CFileTextLine {
70 CString sLine;
71 EOL eEnding;
73 typedef CStdArrayD<CFileTextLine> CStdFileLineArray;
74 /**
75 * \ingroup TortoiseMerge
77 * Represents an array of text lines which are read from a file.
78 * This class is also responsible for determining the encoding of
79 * the file (e.g. UNICODE(UTF16), UTF8, ASCII, ...).
81 class CFileTextLines : public CStdFileLineArray
83 public:
84 CFileTextLines(void);
85 ~CFileTextLines(void);
87 enum UnicodeType
89 AUTOTYPE,
90 BINARY,
91 ASCII,
92 UTF16_LE, //=1200,
93 UTF16_BE, //=1201,
94 UTF16_LEBOM, //=1200,
95 UTF16_BEBOM, //=1201,
96 UTF32_LE, //=12000,
97 UTF32_BE, //=12001,
98 UTF8, //=65001,
99 UTF8BOM, //=UTF8+65536,
102 struct SaveParams {
103 UnicodeType m_UnicodeType;
104 EOL m_LineEndings;
108 * Loads the text file and adds each line to the array
109 * \param sFilePath the path to the file
110 * \param lengthHint hint to create line array
112 BOOL Load(const CString& sFilePath, int lengthHint = 0);
114 * Saves the whole array of text lines to a file, preserving
115 * the line endings detected at Load()
116 * \param sFilePath the path to save the file to
117 * \param bSaveAsUTF8 enforce encoding for save
118 * \param bUseSVNCompatibleEOLs limit EOLs to CRLF, CR and LF, last one is used instead of all others
119 * \param dwIgnoreWhitespaces "enum" mode of removing whitespaces
120 * \param bIgnoreCase converts whole file to lower case
121 * \param bBlame limit line len
123 BOOL Save(const CString& sFilePath
124 , bool bSaveAsUTF8 = false
125 , bool bUseSVNCompatibleEOLs = false
126 , DWORD dwIgnoreWhitespaces = 0
127 , BOOL bIgnoreCase = FALSE
128 , bool bBlame = false
129 , bool bIgnoreComments = false
130 , const CString& linestart = CString()
131 , const CString& blockstart = CString()
132 , const CString& blockend = CString()
133 , const std::wregex& rx = std::wregex(L"")
134 , const std::wstring& replacement = L"");
136 * Returns an error string of the last failed operation
138 CString GetErrorString() const {return m_sErrorString;}
140 * Copies the settings of a file like the line ending styles
141 * to another CFileTextLines object.
143 void CopySettings(CFileTextLines * pFileToCopySettingsTo) const;
145 bool NeedsConversion() const { return m_bNeedsConversion; }
146 UnicodeType GetUnicodeType() const {return m_SaveParams.m_UnicodeType;}
147 EOL GetLineEndings() const {return m_SaveParams.m_LineEndings;}
149 void Add(const CString& sLine, EOL ending) { CFileTextLine temp={sLine, ending}; CStdFileLineArray::Add(temp); }
150 void InsertAt(int index, const CString& strVal, EOL ending) { CFileTextLine temp={strVal, ending}; CStdFileLineArray::InsertAt(index, temp); }
152 const CString& GetAt(int index) const { return CStdFileLineArray::GetAt(index).sLine; }
153 EOL GetLineEnding(int index) const { return CStdFileLineArray::GetAt(index).eEnding; }
154 void SetSaveParams(const SaveParams& sp) { m_SaveParams = sp; }
155 SaveParams GetSaveParams() const { return m_SaveParams; }
156 void KeepEncoding(bool bKeep = true) { m_bKeepEncoding = bKeep; }
157 //void SetLineEnding(int index, EOL ending) { CStdFileLineArray::GetAt(index).eEnding = ending; }
159 static const wchar_t * GetEncodingName(UnicodeType);
162 * Checks the Unicode type in a text buffer
163 * Must be public for TortoiseGitBlame
164 * \param pBuffer pointer to the buffer containing text
165 * \param cb size of the text buffer in bytes
167 UnicodeType CheckUnicodeType(LPVOID pBuffer, int cb);
169 private:
170 void SetErrorString();
172 static void StripWhiteSpace(CString& sLine, DWORD dwIgnoreWhitespaces, bool blame);
173 bool StripComments(CString& sLine, bool bInBlockComment);
174 void LineRegex(CString& sLine, const std::wregex& rx, const std::wstring& replacement) const;
177 private:
178 CString m_sErrorString;
179 bool m_bNeedsConversion;
180 bool m_bKeepEncoding;
181 SaveParams m_SaveParams;
182 CString m_sCommentLine;
183 CString m_sCommentBlockStart;
184 CString m_sCommentBlockEnd;
189 class CBuffer
191 public:
192 CBuffer() {Init(); }
193 CBuffer(const CBuffer & Src) {Init(); Copy(Src); }
194 CBuffer(const CBuffer * const Src) {Init(); Copy(*Src); }
195 ~CBuffer() {Free(); }
197 CBuffer & operator =(const CBuffer & Src) { Copy(Src); return *this; }
198 operator bool () const { return !IsEmpty(); }
199 template<typename T>
200 operator T () const { return (T)m_pBuffer; }
202 void Clear() { m_nUsed=0; }
203 void ExpandToAtLeast(int nNewSize);
204 int GetLength() const { return m_nUsed; }
205 bool IsEmpty() const { return GetLength()==0; }
206 void SetLength(int nUsed);
207 void Swap(CBuffer & Src);
209 private:
210 void Copy(const CBuffer & Src);
211 void Free() { delete [] m_pBuffer; }
212 void Init() { m_pBuffer = nullptr; m_nUsed = 0; m_nAllocated = 0; }
214 BYTE * m_pBuffer;
215 int m_nUsed;
216 int m_nAllocated;
220 class CBaseFilter
222 public:
223 CBaseFilter(CStdioFile * p_File) { m_pFile=p_File; m_nCodePage=0; }
224 virtual ~CBaseFilter() {}
226 virtual bool Decode(/*in out*/ CBuffer & s);
227 virtual const CBuffer& Encode(const CString& data);
228 const CBuffer & GetBuffer() const {return m_oBuffer; }
229 void Write(const CString& s) { Write(Encode(s)); } ///< encode into buffer and write
230 void Write() { Write(m_oBuffer); } ///< write preencoded internal buffer
231 void Write(const CBuffer & buffer) { if (buffer.GetLength()) m_pFile->Write((void*)buffer, buffer.GetLength()); } ///< write preencoded buffer
233 protected:
234 CBuffer m_oBuffer;
236 Code page for WideCharToMultiByte.
238 UINT m_nCodePage;
240 private:
241 CStdioFile * m_pFile;
245 class CAsciiFilter : public CBaseFilter
247 public:
248 CAsciiFilter(CStdioFile *pFile) : CBaseFilter(pFile){ m_nCodePage=CP_ACP; }
249 virtual ~CAsciiFilter() {}
253 class CUtf8Filter : public CBaseFilter
255 public:
256 CUtf8Filter(CStdioFile *pFile) : CBaseFilter(pFile){ m_nCodePage=CP_UTF8;}
257 virtual ~CUtf8Filter() {}
261 class CUtf16leFilter : public CBaseFilter
263 public:
264 CUtf16leFilter(CStdioFile *pFile) : CBaseFilter(pFile){}
265 virtual ~CUtf16leFilter() {}
267 virtual bool Decode(/*in out*/ CBuffer& data) override;
268 virtual const CBuffer& Encode(const CString& s) override;
272 class CUtf16beFilter : public CUtf16leFilter
274 public:
275 CUtf16beFilter(CStdioFile *pFile) : CUtf16leFilter(pFile){}
276 virtual ~CUtf16beFilter() {}
278 virtual bool Decode(/*in out*/ CBuffer& data) override;
279 virtual const CBuffer& Encode(const CString& s) override;
283 class CUtf32leFilter : public CBaseFilter
285 public:
286 CUtf32leFilter(CStdioFile *pFile) : CBaseFilter(pFile){}
287 virtual ~CUtf32leFilter() {}
289 virtual bool Decode(/*in out*/ CBuffer& data) override;
290 virtual const CBuffer& Encode(const CString& s) override;
294 class CUtf32beFilter : public CUtf32leFilter
296 public:
297 CUtf32beFilter(CStdioFile *pFile) : CUtf32leFilter(pFile){}
298 virtual ~CUtf32beFilter() {}
300 virtual bool Decode(/*in out*/ CBuffer& data) override;
301 virtual const CBuffer& Encode(const CString& s) override;