Add option 'Show complete log'
[TortoiseGit.git] / src / TortoiseMerge / FileTextLines.h
blobac1c062a8a98d9922b5e0742b40a33a803ee067f
1 // TortoiseGitMerge - a Diff/Patch program
3 // Copyright (C) 2006-2007, 2012-2013 - 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>
23 // A template class to make an array which looks like a CStringArray or CDWORDArray but
24 // is in fact based on a STL vector, which is much faster at large sizes
25 template <typename T> class CStdArrayV
27 public:
28 int GetCount() const { return (int)m_vec.size(); }
29 const T& GetAt(int index) const { return m_vec[index]; }
30 void RemoveAt(int index) { m_vec.erase(m_vec.begin()+index); }
31 void InsertAt(int index, const T& strVal) { m_vec.insert(m_vec.begin()+index, strVal); }
32 void InsertAt(int index, const T& strVal, int nCopies) { m_vec.insert(m_vec.begin()+index, nCopies, strVal); }
33 void SetAt(int index, const T& strVal) { m_vec[index] = strVal; }
34 void Add(const T& strVal) {
35 if (m_vec.size()==m_vec.capacity()) {
36 m_vec.reserve(m_vec.capacity() ? m_vec.capacity()*2 : 256);
38 m_vec.push_back(strVal);
40 void RemoveAll() { m_vec.clear(); }
41 void Reserve(int nHintSize) { m_vec.reserve(nHintSize); }
43 private:
44 std::vector<T> m_vec;
47 // A template class to make an array which looks like a CStringArray or CDWORDArray but
48 // is in fact based on a STL deque, which is much faster at large sizes
49 template <typename T> class CStdArrayD
51 public:
52 int GetCount() const { return (int)m_vec.size(); }
53 const T& GetAt(int index) const { return m_vec[index]; }
54 void RemoveAt(int index) { m_vec.erase(m_vec.begin()+index); }
55 void InsertAt(int index, const T& strVal) { m_vec.insert(m_vec.begin()+index, strVal); }
56 void InsertAt(int index, const T& strVal, int nCopies) { m_vec.insert(m_vec.begin()+index, nCopies, strVal); }
57 void SetAt(int index, const T& strVal) { m_vec[index] = strVal; }
58 void Add(const T& strVal) { m_vec.push_back(strVal); }
59 void RemoveAll() { m_vec.clear(); }
60 void Reserve(int ) { }
62 private:
63 std::deque<T> m_vec;
66 typedef CStdArrayV<DWORD> CStdDWORDArray;
68 struct CFileTextLine {
69 CString sLine;
70 EOL eEnding;
72 typedef CStdArrayD<CFileTextLine> CStdFileLineArray;
73 /**
74 * \ingroup TortoiseMerge
76 * Represents an array of text lines which are read from a file.
77 * This class is also responsible for determining the encoding of
78 * the file (e.g. UNICODE(UTF16), UTF8, ASCII, ...).
80 class CFileTextLines : public CStdFileLineArray
82 public:
83 CFileTextLines(void);
84 ~CFileTextLines(void);
86 enum UnicodeType
88 AUTOTYPE,
89 BINARY,
90 ASCII,
91 UTF16_LE, //=1200,
92 UTF16_BE, //=1201,
93 UTF16_LEBOM, //=1200,
94 UTF16_BEBOM, //=1201,
95 UTF32_LE, //=12000,
96 UTF32_BE, //=12001,
97 UTF8, //=65001,
98 UTF8BOM, //=UTF8+65536,
101 struct SaveParams {
102 UnicodeType m_UnicodeType;
103 EOL m_LineEndings;
107 * Loads the text file and adds each line to the array
108 * \param sFilePath the path to the file
109 * \param lengthHint hint to create line array
111 BOOL Load(const CString& sFilePath, int lengthHint = 0);
113 * Saves the whole array of text lines to a file, preserving
114 * the line endings detected at Load()
115 * \param sFilePath the path to save the file to
116 * \param bSaveAsUTF8 enforce encoding for save
117 * \param bUseSVNCompatibleEOLs limit EOLs to CRLF, CR and LF, last one is used instead of all others
118 * \param dwIgnoreWhitespaces "enum" mode of removing whitespaces
119 * \param bIgnoreCase converts whole file to lower case
120 * \param bBlame limit line len
122 BOOL Save(const CString& sFilePath
123 , bool bSaveAsUTF8 = false
124 , bool bUseSVNCompatibleEOLs = false
125 , DWORD dwIgnoreWhitespaces = 0
126 , BOOL bIgnoreCase = FALSE
127 , bool bBlame = false) const;
129 * Returns an error string of the last failed operation
131 CString GetErrorString() const {return m_sErrorString;}
133 * Copies the settings of a file like the line ending styles
134 * to another CFileTextLines object.
136 void CopySettings(CFileTextLines * pFileToCopySettingsTo);
138 bool NeedsConversion() const { return m_bNeedsConversion; }
139 UnicodeType GetUnicodeType() const {return m_SaveParams.m_UnicodeType;}
140 EOL GetLineEndings() const {return m_SaveParams.m_LineEndings;}
142 void Add(const CString& sLine, EOL ending) { CFileTextLine temp={sLine, ending}; CStdFileLineArray::Add(temp); }
143 void InsertAt(int index, const CString& strVal, EOL ending) { CFileTextLine temp={strVal, ending}; CStdFileLineArray::InsertAt(index, temp); }
145 const CString& GetAt(int index) const { return CStdFileLineArray::GetAt(index).sLine; }
146 EOL GetLineEnding(int index) const { return CStdFileLineArray::GetAt(index).eEnding; }
147 void SetSaveParams(const SaveParams& sp) { m_SaveParams = sp; }
148 //void SetLineEnding(int index, EOL ending) { CStdFileLineArray::GetAt(index).eEnding = ending; }
150 static const wchar_t * GetEncodingName(UnicodeType);
153 * Checks the Unicode type in a text buffer
154 * Must be public for TortoiseGitBlame
155 * \param pBuffer pointer to the buffer containing text
156 * \param cb size of the text buffer in bytes
158 UnicodeType CheckUnicodeType(LPVOID pBuffer, int cb);
160 private:
161 void SetErrorString();
163 static void StripWhiteSpace(CString& sLine, DWORD dwIgnoreWhitespaces, bool blame);
166 private:
167 CString m_sErrorString;
168 bool m_bNeedsConversion;
169 SaveParams m_SaveParams;
174 class CBuffer
176 public:
177 CBuffer() {Init(); }
178 CBuffer(const CBuffer & Src) {Init(); Copy(Src); }
179 CBuffer(const CBuffer * const Src) {Init(); Copy(*Src); }
180 ~CBuffer() {Free(); }
182 CBuffer & operator =(const CBuffer & Src) { Copy(Src); return *this; }
183 operator bool () { return !IsEmpty(); }
184 template<typename T>
185 operator T () const { return (T)m_pBuffer; }
187 void Clear() { m_nUsed=0; }
188 void ExpandToAtLeast(int nNewSize);
189 int GetLength() const { return m_nUsed; }
190 bool IsEmpty() const { return GetLength()==0; }
191 void SetLength(int nUsed);
192 void Swap(CBuffer & Src);
194 private:
195 void Copy(const CBuffer & Src);
196 void Free() { delete [] m_pBuffer; }
197 void Init() { m_pBuffer=NULL; m_nUsed=0; m_nAllocated=0; }
199 BYTE * m_pBuffer;
200 int m_nUsed;
201 int m_nAllocated;
205 class CBaseFilter
207 public:
208 CBaseFilter(CStdioFile * p_File) { m_pFile=p_File; m_nCodePage=0; }
209 virtual ~CBaseFilter() {}
211 virtual bool Decode(/*in out*/ CBuffer & s);
212 virtual const CBuffer & Encode(const CString data);
213 const CBuffer & GetBuffer() {return m_oBuffer; }
214 void Write(const CString s) { Write(Encode(s)); } ///< encode into buffer and write
215 void Write() { Write(m_oBuffer); } ///< write preencoded internal buffer
216 void Write(const CBuffer & buffer) { if (buffer.GetLength()) m_pFile->Write((void*)buffer, buffer.GetLength()); } ///< write preencoded buffer
218 protected:
219 CBuffer m_oBuffer;
221 Code page for WideCharToMultiByte.
223 UINT m_nCodePage;
225 private:
226 CStdioFile * m_pFile;
230 class CAsciiFilter : public CBaseFilter
232 public:
233 CAsciiFilter(CStdioFile *pFile) : CBaseFilter(pFile){ m_nCodePage=CP_ACP; }
234 virtual ~CAsciiFilter() {}
238 class CUtf8Filter : public CBaseFilter
240 public:
241 CUtf8Filter(CStdioFile *pFile) : CBaseFilter(pFile){ m_nCodePage=CP_UTF8;}
242 virtual ~CUtf8Filter() {}
246 class CUtf16leFilter : public CBaseFilter
248 public:
249 CUtf16leFilter(CStdioFile *pFile) : CBaseFilter(pFile){}
250 virtual ~CUtf16leFilter() {}
252 virtual bool Decode(/*in out*/ CBuffer & data);
253 virtual const CBuffer & Encode(const CString s);
257 class CUtf16beFilter : public CUtf16leFilter
259 public:
260 CUtf16beFilter(CStdioFile *pFile) : CUtf16leFilter(pFile){}
261 virtual ~CUtf16beFilter() {}
263 virtual bool Decode(/*in out*/ CBuffer & data);
264 virtual const CBuffer & Encode(const CString s);
268 class CUtf32leFilter : public CBaseFilter
270 public:
271 CUtf32leFilter(CStdioFile *pFile) : CBaseFilter(pFile){}
272 virtual ~CUtf32leFilter() {}
274 virtual bool Decode(/*in out*/ CBuffer & data);
275 virtual const CBuffer & Encode(const CString s);
279 class CUtf32beFilter : public CUtf32leFilter
281 public:
282 CUtf32beFilter(CStdioFile *pFile) : CUtf32leFilter(pFile){}
283 virtual ~CUtf32beFilter() {}
285 virtual bool Decode(/*in out*/ CBuffer & data);
286 virtual const CBuffer & Encode(const CString s);