OSDN Git Service

Installer: Textual change
[tortoisegit/TortoiseGitJp.git] / src / TortoiseMerge / FileTextLines.h
1 // TortoiseMerge - a Diff/Patch program\r
2 \r
3 // Copyright (C) 2006-2007 - TortoiseSVN\r
4 \r
5 // This program is free software; you can redistribute it and/or\r
6 // modify it under the terms of the GNU General Public License\r
7 // as published by the Free Software Foundation; either version 2\r
8 // of the License, or (at your option) any later version.\r
9 \r
10 // This program is distributed in the hope that it will be useful,\r
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13 // GNU General Public License for more details.\r
14 \r
15 // You should have received a copy of the GNU General Public License\r
16 // along with this program; if not, write to the Free Software Foundation,\r
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.\r
18 //\r
19 #pragma once\r
20 #include "EOL.h"\r
21 \r
22 // A template class to make an array which looks like a CStringArray or CDWORDArray but\r
23 // is in fact based on a STL array, which is much faster at large sizes\r
24 template <typename T> class CStdArray\r
25 {\r
26 public:\r
27         int GetCount() const { return (int)m_vec.size(); }\r
28         const T& GetAt(int index) const { return m_vec[index]; }\r
29         void RemoveAt(int index)        { m_vec.erase(m_vec.begin()+index); }\r
30         void InsertAt(int index, const T& strVal)       { m_vec.insert(m_vec.begin()+index, strVal); }\r
31         void InsertAt(int index, const T& strVal, int nCopies)  { m_vec.insert(m_vec.begin()+index, nCopies, strVal); }\r
32         void SetAt(int index, const T& strVal)  { m_vec[index] = strVal; }\r
33         void Add(const T& strVal)        { m_vec.push_back(strVal); }\r
34         void RemoveAll()                                { m_vec.clear(); }\r
35         void Reserve(int lengthHint)    { m_vec.reserve(lengthHint); }\r
36 \r
37 private:\r
38         std::vector<T> m_vec;\r
39 };\r
40 \r
41 typedef CStdArray<CString> CStdCStringArray;\r
42 typedef CStdArray<DWORD> CStdDWORDArray;\r
43 \r
44 /**\r
45  * \ingroup TortoiseMerge\r
46  *\r
47  * Represents an array of text lines which are read from a file.\r
48  * This class is also responsible for determining the encoding of\r
49  * the file (e.g. UNICODE, UTF8, ASCII, ...).\r
50  */\r
51 class CFileTextLines  : public CStdCStringArray\r
52 {\r
53 public:\r
54         CFileTextLines(void);\r
55         ~CFileTextLines(void);\r
56 \r
57         enum UnicodeType\r
58         {\r
59                 AUTOTYPE,\r
60                 BINARY,\r
61                 ASCII,\r
62                 UNICODE_LE,\r
63                 UTF8,\r
64                 UTF8BOM,\r
65         };\r
66 \r
67         /**\r
68          * Loads the text file and adds each line to the array\r
69          * \param sFilePath the path to the file\r
70          */\r
71         BOOL            Load(const CString& sFilePath, int lengthHint = 0);\r
72         /**\r
73          * Saves the whole array of text lines to a file, preserving\r
74          * the line endings detected at Load()\r
75          * \param sFilePath the path to save the file to\r
76          */\r
77         BOOL            Save(const CString& sFilePath, bool bSaveAsUTF8, DWORD dwIgnoreWhitespaces=0, BOOL bIgnoreCase = FALSE, bool bBlame = false);\r
78         /**\r
79          * Returns an error string of the last failed operation\r
80          */\r
81         CString         GetErrorString() const  {return m_sErrorString;}\r
82         /**\r
83          * Copies the settings of a file like the line ending styles\r
84          * to another CFileTextLines object.\r
85          */\r
86         void            CopySettings(CFileTextLines * pFileToCopySettingsTo);\r
87 \r
88         CFileTextLines::UnicodeType GetUnicodeType() const  {return m_UnicodeType;}\r
89         EOL GetLineEndings() const {return m_LineEndings;}\r
90 \r
91         void            Add(const CString& sLine, EOL ending) {CStdCStringArray::Add(sLine); m_endings.push_back(ending);}\r
92         void            RemoveAt(int index)     {CStdCStringArray::RemoveAt(index); m_endings.erase(m_endings.begin()+index);}\r
93         void            InsertAt(int index, const CString& strVal, EOL ending) {CStdCStringArray::InsertAt(index, strVal); m_endings.insert(m_endings.begin()+index, ending);}\r
94 \r
95         EOL                     GetLineEnding(int index) {return m_endings[index];}\r
96         void            SetLineEnding(int index, EOL ending) {m_endings[index] = ending;}\r
97         \r
98         void            RemoveAll() {CStdCStringArray::RemoveAll(); m_endings.clear();}\r
99 private:\r
100         /**\r
101          * Checks the line endings in a text buffer\r
102          * \param pBuffer pointer to the buffer containing text\r
103          * \param cd size of the text buffer in bytes\r
104          */\r
105         EOL CheckLineEndings(LPVOID pBuffer, int cb); \r
106         /**\r
107          * Checks the Unicode type in a text buffer\r
108          * \param pBuffer pointer to the buffer containing text\r
109          * \param cd size of the text buffer in bytes\r
110          */\r
111         CFileTextLines::UnicodeType CheckUnicodeType(LPVOID pBuffer, int cb); \r
112 \r
113         void            SetErrorString();\r
114 \r
115         void StripAsciiWhiteSpace(CStringA& sLine);\r
116 \r
117         void StripWhiteSpace(CString& sLine,DWORD dwIgnoreWhitespaces, bool blame);\r
118         void StripAsciiWhiteSpace(CStringA& sLine,DWORD dwIgnoreWhitespaces, bool blame);\r
119 \r
120 \r
121 private:\r
122         std::vector<EOL>                                                        m_endings;\r
123         CString                                                                         m_sErrorString;\r
124         CFileTextLines::UnicodeType                                     m_UnicodeType;\r
125         EOL                                                                                     m_LineEndings;\r
126         bool                                                                            m_bReturnAtEnd;\r
127 };\r