OSDN Git Service

Avoid an assertion failure when loading settings from winmerge.ini
[winmerge-jp/winmerge-jp.git] / Src / PathContext.cpp
1 /**
2  * @file  PathContext.cpp
3  *
4  * @brief Implementation file for PathInfo and PathContext
5  *
6  */
7
8 #include "pch.h"
9 #include "PathContext.h"
10 #include <cassert>
11 #include "paths.h"
12
13 /**
14  * @brief Copy constructor.
15  */
16 PathInfo::PathInfo(const PathInfo &pi)
17 : m_sPath(pi.m_sPath)
18 {
19 }
20
21 /**
22  * @brief Get path.
23  * @param [in] sbNormalized true if path is wanted in normalized format.
24  */
25 String PathInfo::GetPath(bool bNormalized /*= true*/) const
26
27         if (!bNormalized)
28                 return paths::AddTrailingSlash(m_sPath);
29         else
30                 return m_sPath;
31 }
32
33 /**
34  * @brief Normalize path.
35  */
36 void PathInfo::NormalizePath()
37 {
38         paths::normalize(m_sPath);
39 }
40
41 PathContext::PathContext()
42 {
43         m_nFiles = 0;
44 }
45
46 PathContext::PathContext(const String& sLeft)
47 {
48         m_nFiles = 1;
49         m_path[0].SetPath(sLeft);
50 }
51
52 PathContext::PathContext(const String& sLeft, const String& sRight)
53 {
54         m_nFiles = 2;
55         m_path[0].SetPath(sLeft);
56         m_path[1].SetPath(sRight);
57 }
58
59 PathContext::PathContext(const String& sLeft, const String& sMiddle, const String& sRight)
60 {
61         m_nFiles = 3;
62         m_path[0].SetPath(sLeft);
63         m_path[1].SetPath(sMiddle);
64         m_path[2].SetPath(sRight);
65 }
66
67 PathContext::PathContext(const PathContext &paths)
68 {
69         m_nFiles = paths.m_nFiles;
70         std::copy_n(paths.m_path, m_nFiles, m_path);
71 }
72
73 PathContext::PathContext(const std::vector<String> &paths)
74 {
75         m_nFiles = static_cast<int>(paths.size());
76         for (size_t i = 0; i < paths.size(); i++)
77                 m_path[i].SetPath(paths[i]);
78 }
79
80 String PathContext::GetAt(int nIndex) const
81 {
82         assert(nIndex < m_nFiles);
83         return m_path[nIndex].GetPath();
84 }
85
86 String& PathContext::GetElement(int nIndex)
87 {
88         assert(nIndex < m_nFiles);
89         return m_path[nIndex].GetRef();
90 }
91
92 void PathContext::SetAt(int nIndex, const String& newElement)
93 {
94         assert(nIndex < m_nFiles);
95         m_path[nIndex].SetPath(newElement);
96 }
97
98 /**
99  * @brief Empty m_path array
100  */
101 void PathContext::RemoveAll()
102 {
103         m_nFiles = 0;
104         m_path[0].SetPath(_T(""));
105         m_path[1].SetPath(_T(""));
106         m_path[2].SetPath(_T(""));
107 }
108
109 /**
110  * @brief Return left path.
111  * @param [in] sNormalized If true normalized path is returned.
112  */
113 String PathContext::GetLeft(bool bNormalized) const
114 {
115         if (m_nFiles == 0)
116                 return _T("");
117         return m_path[0].GetPath(bNormalized);
118 }
119
120 /**
121  * @brief Return right path.
122  * @param [in] sNormalized If true normalized path is returned.
123  */
124 String PathContext::GetRight(bool bNormalized) const
125 {
126         if (m_nFiles < 2)
127                 return _T("");
128         return m_path[m_nFiles - 1].GetPath(bNormalized);
129 }
130
131 /**
132  * @brief Return middle path.
133  * @param [in] sNormalized If true normalized path is returned.
134  */
135 String PathContext::GetMiddle(bool bNormalized) const
136 {
137         if (m_nFiles < 3)
138                 return _T("");
139         return m_path[1].GetPath(bNormalized);
140 }
141
142 /**
143  * @brief Return path
144  * @param [in] index index of path to return
145  * @param [in] sNormalized If true normalized path is returned.
146  */
147 String PathContext::GetPath(int index, bool bNormalized) const
148 {
149         return m_path[index].GetPath(bNormalized);
150 }
151
152 /**
153  * @brief Set left path.
154  * @param [in] path New path for item.
155  */
156 void PathContext::SetLeft(const String& path, bool bNormalized)
157 {
158         if (m_nFiles == 0)
159                 m_nFiles = 1;
160         m_path[0].SetPath(path);
161         if (bNormalized)
162                 m_path[0].NormalizePath();
163 }
164
165 /**
166  * @brief Set right path.
167  * @param [in] path New path for item.
168  */
169 void PathContext::SetRight(const String& path, bool bNormalized)
170 {
171         if (m_nFiles < 2)
172                 m_nFiles = 2;
173         m_path[m_nFiles - 1].SetPath(path);
174         if (bNormalized)
175                 m_path[m_nFiles - 1].NormalizePath();
176 }
177
178 /**
179  * @brief Set middle path.
180  * @param [in] path New path for item.
181  */
182 void PathContext::SetMiddle(const String& path, bool bNormalized)
183 {
184         if (m_nFiles < 3)
185         {
186                 m_nFiles = 3;
187                 m_path[2] = m_path[1];
188         }
189         m_path[1].SetPath(path);
190         if (bNormalized)
191                 m_path[1].NormalizePath();
192 }
193
194 /**
195  * @brief Set path
196  * @param [in] index index of path to set
197  * @param [in] path New path for item.
198  */
199 void PathContext::SetPath(int index, const String& path, bool bNormalized)
200 {
201         if (index >= sizeof(m_path)/sizeof(m_path[0]))
202                 return;
203         if (index >= m_nFiles)
204                 m_nFiles = index + 1;
205         m_path[index].SetPath(path);
206         if (bNormalized)
207                 m_path[index].NormalizePath();
208 }
209
210 /**
211  * @brief Swap paths.
212  */
213 void PathContext::Swap(int nFromIndex, int nToIndex)
214 {
215         if ((nFromIndex >= 0 && nFromIndex < m_nFiles) && (nToIndex >= 0 && nToIndex < m_nFiles))
216                 m_path[nFromIndex].m_sPath.swap(m_path[nToIndex].m_sPath);
217 }