OSDN Git Service

Reduce compilation warnings
[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 // ID line follows -- this is updated by SVN
8 // $Id: PathContext.cpp 4929 2008-01-18 20:03:57Z kimmov $
9
10 #include "PathContext.h"
11 #include <cassert>
12 #include "paths.h"
13 #include "Environment.h"
14
15 /**
16  * @brief Copy constructor.
17  */
18 PathInfo::PathInfo(const PathInfo &pi)
19 {
20         m_sPath = pi.m_sPath;
21 }
22
23 /**
24  * @brief Get path.
25  * @param [in] sbNormalized true if path is wanted in normalized format.
26  */
27 String PathInfo::GetPath(bool bNormalized /*= true*/) const
28
29         if (!bNormalized)
30                 return paths_AddTrailingSlash(m_sPath);
31         else
32                 return m_sPath;
33 }
34
35 String& PathInfo::GetRef()
36
37         return m_sPath;
38 }
39
40 /**
41  * @brief Set path.
42  * @param [in] sPath New path for item.
43  */
44 void PathInfo::SetPath(const TCHAR *sPath)
45 {
46         m_sPath = sPath;
47 }
48
49 void PathInfo::SetPath(const String & sPath)
50 {
51         m_sPath = sPath;
52 }
53
54 /**
55  * @brief Normalize path.
56  */
57 void PathInfo::NormalizePath()
58 {
59         paths_normalize(m_sPath);
60 }
61
62 PathContext::PathContext()
63 {
64         m_nFiles = 0;
65 }
66
67 PathContext::PathContext(const String& sLeft)
68 {
69         m_nFiles = 1;
70         m_path[0].SetPath(sLeft);
71 }
72
73 PathContext::PathContext(const String& sLeft, const String& sRight)
74 {
75         m_nFiles = 2;
76         m_path[0].SetPath(sLeft);
77         m_path[1].SetPath(sRight);
78 }
79
80 PathContext::PathContext(const String& sLeft, const String& sMiddle, const String& sRight)
81 {
82         m_nFiles = 3;
83         m_path[0].SetPath(sLeft);
84         m_path[1].SetPath(sMiddle);
85         m_path[2].SetPath(sRight);
86 }
87
88 PathContext::PathContext(const PathContext &paths)
89 {
90         m_nFiles = paths.m_nFiles;
91         for (int i = 0; i < m_nFiles; i++)
92                 m_path[i].SetPath(paths[i]);
93 }
94
95 PathContext::PathContext(const std::vector<String> &paths)
96 {
97         m_nFiles = paths.size();
98         for (size_t i = 0; i < paths.size(); i++)
99                 m_path[i].SetPath(paths[i]);
100 }
101
102 String PathContext::GetAt(int nIndex) const
103 {
104         assert(nIndex < m_nFiles);
105         return m_path[nIndex].GetPath();
106 }
107
108 String& PathContext::GetElement(int nIndex)
109 {
110         assert(nIndex < m_nFiles);
111         return m_path[nIndex].GetRef();
112 }
113
114 void PathContext::SetAt(int nIndex, const String& newElement)
115 {
116         assert(nIndex < m_nFiles);
117         m_path[nIndex].SetPath(newElement);
118 }
119
120 String PathContext::operator[](int nIndex) const
121 {
122         return GetAt(nIndex);
123 }
124
125 String& PathContext::operator[](int nIndex)
126 {
127         return GetElement(nIndex);
128 }
129
130 /**
131  * @brief set number of files.
132  */
133 void PathContext::SetSize(int nFiles)
134 {
135         m_nFiles = nFiles;
136 }
137
138 /**
139  * @brief Return number of files.
140  */
141 int PathContext::GetSize() const
142 {
143         return m_nFiles;
144 }
145
146 /**
147  * @brief Empty m_path array
148  */
149 void PathContext::RemoveAll()
150 {
151         m_nFiles = 0;
152         m_path[0].SetPath(_T(""));
153         m_path[1].SetPath(_T(""));
154         m_path[2].SetPath(_T(""));
155 }
156
157 /**
158  * @brief Return left path.
159  * @param [in] sNormalized If true normalized path is returned.
160  */
161 String PathContext::GetLeft(bool bNormalized) const
162 {
163         if (m_nFiles == 0)
164                 return _T("");
165         return m_path[0].GetPath(bNormalized);
166 }
167
168 /**
169  * @brief Return right path.
170  * @param [in] sNormalized If true normalized path is returned.
171  */
172 String PathContext::GetRight(bool bNormalized) const
173 {
174         if (m_nFiles < 2)
175                 return _T("");
176         return m_path[m_nFiles - 1].GetPath(bNormalized);
177 }
178
179 /**
180  * @brief Return middle path.
181  * @param [in] sNormalized If true normalized path is returned.
182  */
183 String PathContext::GetMiddle(bool bNormalized) const
184 {
185         if (m_nFiles < 3)
186                 return _T("");
187         return m_path[1].GetPath(bNormalized);
188 }
189
190 /**
191  * @brief Return path
192  * @param [in] index index of path to return
193  * @param [in] sNormalized If true normalized path is returned.
194  */
195 String PathContext::GetPath(int index, bool bNormalized) const
196 {
197         return m_path[index].GetPath(bNormalized);
198 }
199
200 /**
201  * @brief Set left path.
202  * @param [in] path New path for item.
203  */
204 void PathContext::SetLeft(const String& path, bool bNormalized)
205 {
206         if (m_nFiles == 0)
207                 m_nFiles = 1;
208         m_path[0].SetPath(path);
209         if (bNormalized)
210                 m_path[0].NormalizePath();
211 }
212
213 /**
214  * @brief Set right path.
215  * @param [in] path New path for item.
216  */
217 void PathContext::SetRight(const String& path, bool bNormalized)
218 {
219         if (m_nFiles < 2)
220                 m_nFiles = 2;
221         m_path[m_nFiles - 1].SetPath(path);
222         if (bNormalized)
223                 m_path[m_nFiles - 1].NormalizePath();
224 }
225
226 /**
227  * @brief Set middle path.
228  * @param [in] path New path for item.
229  */
230 void PathContext::SetMiddle(const String& path, bool bNormalized)
231 {
232         if (m_nFiles < 3)
233         {
234                 m_nFiles = 3;
235                 m_path[2] = m_path[1];
236         }
237         m_path[1].SetPath(path);
238         if (bNormalized)
239                 m_path[1].NormalizePath();
240 }
241
242 /**
243  * @brief Set path
244  * @param [in] index index of path to set
245  * @param [in] path New path for item.
246  */
247 void PathContext::SetPath(int index, const String& path, bool bNormalized)
248 {
249         if (index >= sizeof(m_path)/sizeof(m_path[0]))
250                 return;
251         if (index >= m_nFiles)
252                 m_nFiles = index + 1;
253         m_path[index].SetPath(path);
254         if (bNormalized)
255                 m_path[index].NormalizePath();
256 }
257
258 /**
259  * @brief Swap paths.
260  */
261 void PathContext::Swap()
262 {
263         if (m_nFiles < 3)
264                 m_path[0].m_sPath.swap(m_path[1].m_sPath);
265         else
266                 m_path[0].m_sPath.swap(m_path[2].m_sPath);
267 }