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.
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
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
); }
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
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 ) { }
66 typedef CStdArrayV
<DWORD
> CStdDWORDArray
;
68 struct CFileTextLine
{
72 typedef CStdArrayD
<CFileTextLine
> CStdFileLineArray
;
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
84 ~CFileTextLines(void);
98 UTF8BOM
, //=UTF8+65536,
102 UnicodeType m_UnicodeType
;
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
);
161 void SetErrorString();
163 static void StripWhiteSpace(CString
& sLine
, DWORD dwIgnoreWhitespaces
, bool blame
);
167 CString m_sErrorString
;
168 bool m_bNeedsConversion
;
169 SaveParams m_SaveParams
;
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(); }
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
);
195 void Copy(const CBuffer
& Src
);
196 void Free() { delete [] m_pBuffer
; }
197 void Init() { m_pBuffer
=NULL
; m_nUsed
=0; m_nAllocated
=0; }
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
221 Code page for WideCharToMultiByte.
226 CStdioFile
* m_pFile
;
230 class CAsciiFilter
: public CBaseFilter
233 CAsciiFilter(CStdioFile
*pFile
) : CBaseFilter(pFile
){ m_nCodePage
=CP_ACP
; }
234 virtual ~CAsciiFilter() {}
238 class CUtf8Filter
: public CBaseFilter
241 CUtf8Filter(CStdioFile
*pFile
) : CBaseFilter(pFile
){ m_nCodePage
=CP_UTF8
;}
242 virtual ~CUtf8Filter() {}
246 class CUtf16leFilter
: public CBaseFilter
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
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
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
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
);