1 // TortoiseMerge - a Diff/Patch program
3 // Copyright (C) 2006-2007 - 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.
22 // A template class to make an array which looks like a CStringArray or CDWORDArray but
23 // is in fact based on a STL array, which is much faster at large sizes
24 template <typename T
> class CStdArray
27 int GetCount() const { return (int)m_vec
.size(); }
28 const T
& GetAt(int index
) const { return m_vec
[index
]; }
29 void RemoveAt(int index
) { m_vec
.erase(m_vec
.begin()+index
); }
30 void InsertAt(int index
, const T
& strVal
) { m_vec
.insert(m_vec
.begin()+index
, strVal
); }
31 void InsertAt(int index
, const T
& strVal
, int nCopies
) { m_vec
.insert(m_vec
.begin()+index
, nCopies
, strVal
); }
32 void SetAt(int index
, const T
& strVal
) { m_vec
[index
] = strVal
; }
33 void Add(const T
& strVal
) { m_vec
.push_back(strVal
); }
34 void RemoveAll() { m_vec
.clear(); }
35 void Reserve(int lengthHint
) { m_vec
.reserve(lengthHint
); }
41 typedef CStdArray
<CString
> CStdCStringArray
;
42 typedef CStdArray
<DWORD
> CStdDWORDArray
;
45 * \ingroup TortoiseMerge
47 * Represents an array of text lines which are read from a file.
48 * This class is also responsible for determining the encoding of
49 * the file (e.g. UNICODE, UTF8, ASCII, ...).
51 class CFileTextLines
: public CStdCStringArray
55 ~CFileTextLines(void);
68 * Loads the text file and adds each line to the array
69 * \param sFilePath the path to the file
71 BOOL
Load(const CString
& sFilePath
, int lengthHint
= 0);
73 * Saves the whole array of text lines to a file, preserving
74 * the line endings detected at Load()
75 * \param sFilePath the path to save the file to
77 BOOL
Save(const CString
& sFilePath
, bool bSaveAsUTF8
, DWORD dwIgnoreWhitespaces
=0, BOOL bIgnoreCase
= FALSE
, bool bBlame
= false);
79 * Returns an error string of the last failed operation
81 CString
GetErrorString() const {return m_sErrorString
;}
83 * Copies the settings of a file like the line ending styles
84 * to another CFileTextLines object.
86 void CopySettings(CFileTextLines
* pFileToCopySettingsTo
);
88 CFileTextLines::UnicodeType
GetUnicodeType() const {return m_UnicodeType
;}
89 EOL
GetLineEndings() const {return m_LineEndings
;}
91 void Add(const CString
& sLine
, EOL ending
) {CStdCStringArray::Add(sLine
); m_endings
.push_back(ending
);}
92 void RemoveAt(int index
) {CStdCStringArray::RemoveAt(index
); m_endings
.erase(m_endings
.begin()+index
);}
93 void InsertAt(int index
, const CString
& strVal
, EOL ending
) {CStdCStringArray::InsertAt(index
, strVal
); m_endings
.insert(m_endings
.begin()+index
, ending
);}
95 EOL
GetLineEnding(int index
) {return m_endings
[index
];}
96 void SetLineEnding(int index
, EOL ending
) {m_endings
[index
] = ending
;}
98 void RemoveAll() {CStdCStringArray::RemoveAll(); m_endings
.clear();}
100 CFileTextLines::UnicodeType
CheckUnicodeType(LPVOID pBuffer
, int cb
);
103 * Checks the line endings in a text buffer
104 * \param pBuffer pointer to the buffer containing text
105 * \param cd size of the text buffer in bytes
107 EOL
CheckLineEndings(LPVOID pBuffer
, int cb
);
109 * Checks the Unicode type in a text buffer
110 * \param pBuffer pointer to the buffer containing text
111 * \param cd size of the text buffer in bytes
115 void SetErrorString();
117 void StripAsciiWhiteSpace(CStringA
& sLine
);
119 void StripWhiteSpace(CString
& sLine
,DWORD dwIgnoreWhitespaces
, bool blame
);
120 void StripAsciiWhiteSpace(CStringA
& sLine
,DWORD dwIgnoreWhitespaces
, bool blame
);
124 std::vector
<EOL
> m_endings
;
125 CString m_sErrorString
;
126 CFileTextLines::UnicodeType m_UnicodeType
;