|
ferencd@0
|
1 //
|
|
ferencd@0
|
2 // VMime library (http://www.vmime.org)
|
|
ferencd@0
|
3 // Copyright (C) 2002-2013 Vincent Richard <vincent@vmime.org>
|
|
ferencd@0
|
4 //
|
|
ferencd@0
|
5 // This program is free software; you can redistribute it and/or
|
|
ferencd@0
|
6 // modify it under the terms of the GNU General Public License as
|
|
ferencd@0
|
7 // published by the Free Software Foundation; either version 3 of
|
|
ferencd@0
|
8 // the License, or (at your option) any later version.
|
|
ferencd@0
|
9 //
|
|
ferencd@0
|
10 // This program is distributed in the hope that it will be useful,
|
|
ferencd@0
|
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
ferencd@0
|
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
ferencd@0
|
13 // General Public License for more details.
|
|
ferencd@0
|
14 //
|
|
ferencd@0
|
15 // You should have received a copy of the GNU General Public License along
|
|
ferencd@0
|
16 // with this program; if not, write to the Free Software Foundation, Inc.,
|
|
ferencd@0
|
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
ferencd@0
|
18 //
|
|
ferencd@0
|
19 // Linking this library statically or dynamically with other modules is making
|
|
ferencd@0
|
20 // a combined work based on this library. Thus, the terms and conditions of
|
|
ferencd@0
|
21 // the GNU General Public License cover the whole combination.
|
|
ferencd@0
|
22 //
|
|
ferencd@0
|
23
|
|
ferencd@0
|
24 #include "vmime/utility/path.hpp"
|
|
ferencd@0
|
25
|
|
ferencd@0
|
26 #include <algorithm>
|
|
ferencd@0
|
27
|
|
ferencd@0
|
28
|
|
ferencd@0
|
29 namespace vmime {
|
|
ferencd@0
|
30 namespace utility {
|
|
ferencd@0
|
31
|
|
ferencd@0
|
32
|
|
ferencd@0
|
33 path::path()
|
|
ferencd@0
|
34 {
|
|
ferencd@0
|
35 }
|
|
ferencd@0
|
36
|
|
ferencd@0
|
37
|
|
ferencd@0
|
38 path::path(const component& c)
|
|
ferencd@0
|
39 {
|
|
ferencd@0
|
40 m_list.push_back(c);
|
|
ferencd@0
|
41 }
|
|
ferencd@0
|
42
|
|
ferencd@0
|
43
|
|
ferencd@0
|
44 path::path(const path& p)
|
|
ferencd@0
|
45 : object()
|
|
ferencd@0
|
46 {
|
|
ferencd@0
|
47 m_list.resize(p.m_list.size());
|
|
ferencd@0
|
48 std::copy(p.m_list.begin(), p.m_list.end(), m_list.begin());
|
|
ferencd@0
|
49 }
|
|
ferencd@0
|
50
|
|
ferencd@0
|
51
|
|
ferencd@0
|
52 path::path(const string& s)
|
|
ferencd@0
|
53 {
|
|
ferencd@0
|
54 m_list.push_back(component(s));
|
|
ferencd@0
|
55 }
|
|
ferencd@0
|
56
|
|
ferencd@0
|
57
|
|
ferencd@0
|
58 path path::operator/(const path& p) const
|
|
ferencd@0
|
59 {
|
|
ferencd@0
|
60 path pr(*this);
|
|
ferencd@0
|
61 pr /= p;
|
|
ferencd@0
|
62
|
|
ferencd@0
|
63 return (pr);
|
|
ferencd@0
|
64 }
|
|
ferencd@0
|
65
|
|
ferencd@0
|
66
|
|
ferencd@0
|
67 path path::operator/(const component& c) const
|
|
ferencd@0
|
68 {
|
|
ferencd@0
|
69 path pr(*this);
|
|
ferencd@0
|
70 pr /= c;
|
|
ferencd@0
|
71
|
|
ferencd@0
|
72 return (pr);
|
|
ferencd@0
|
73 }
|
|
ferencd@0
|
74
|
|
ferencd@0
|
75
|
|
ferencd@0
|
76 path& path::operator/=(const path& p)
|
|
ferencd@0
|
77 {
|
|
ferencd@0
|
78 const list::size_type size = m_list.size();
|
|
ferencd@0
|
79
|
|
ferencd@0
|
80 m_list.resize(size + p.m_list.size());
|
|
ferencd@0
|
81 std::copy(p.m_list.begin(), p.m_list.end(), m_list.begin() + size);
|
|
ferencd@0
|
82
|
|
ferencd@0
|
83 return (*this);
|
|
ferencd@0
|
84 }
|
|
ferencd@0
|
85
|
|
ferencd@0
|
86
|
|
ferencd@0
|
87 path& path::operator/=(const component& c)
|
|
ferencd@0
|
88 {
|
|
ferencd@0
|
89 m_list.push_back(c);
|
|
ferencd@0
|
90 return (*this);
|
|
ferencd@0
|
91 }
|
|
ferencd@0
|
92
|
|
ferencd@0
|
93
|
|
ferencd@0
|
94 path path::getParent() const
|
|
ferencd@0
|
95 {
|
|
ferencd@0
|
96 path p;
|
|
ferencd@0
|
97
|
|
ferencd@0
|
98 if (!isEmpty())
|
|
ferencd@0
|
99 {
|
|
ferencd@0
|
100 p.m_list.resize(m_list.size() - 1);
|
|
ferencd@0
|
101 std::copy(m_list.begin(), m_list.end() - 1, p.m_list.begin());
|
|
ferencd@0
|
102 }
|
|
ferencd@0
|
103
|
|
ferencd@0
|
104 return (p);
|
|
ferencd@0
|
105 }
|
|
ferencd@0
|
106
|
|
ferencd@0
|
107
|
|
ferencd@0
|
108 path& path::operator=(const path& p)
|
|
ferencd@0
|
109 {
|
|
ferencd@0
|
110 m_list.resize(p.m_list.size());
|
|
ferencd@0
|
111 std::copy(p.m_list.begin(), p.m_list.end(), m_list.begin());
|
|
ferencd@0
|
112
|
|
ferencd@0
|
113 return (*this);
|
|
ferencd@0
|
114 }
|
|
ferencd@0
|
115
|
|
ferencd@0
|
116
|
|
ferencd@0
|
117 path& path::operator=(const component& c)
|
|
ferencd@0
|
118 {
|
|
ferencd@0
|
119 m_list.resize(1);
|
|
ferencd@0
|
120 m_list[0] = c;
|
|
ferencd@0
|
121
|
|
ferencd@0
|
122 return (*this);
|
|
ferencd@0
|
123 }
|
|
ferencd@0
|
124
|
|
ferencd@0
|
125
|
|
ferencd@0
|
126 bool path::operator==(const path& p) const
|
|
ferencd@0
|
127 {
|
|
ferencd@0
|
128 if (m_list.size() != p.m_list.size())
|
|
ferencd@0
|
129 return (false);
|
|
ferencd@0
|
130
|
|
ferencd@0
|
131 list::const_iterator i = m_list.begin();
|
|
ferencd@0
|
132 list::const_iterator j = p.m_list.begin();
|
|
ferencd@0
|
133
|
|
ferencd@0
|
134 bool equal = true;
|
|
ferencd@0
|
135
|
|
ferencd@0
|
136 for ( ; equal && i != m_list.end() ; ++i, ++j)
|
|
ferencd@0
|
137 equal = ((*i).isEquivalent(*j));
|
|
ferencd@0
|
138
|
|
ferencd@0
|
139 return (equal);
|
|
ferencd@0
|
140 }
|
|
ferencd@0
|
141
|
|
ferencd@0
|
142
|
|
ferencd@0
|
143 bool path::operator!=(const path& p) const
|
|
ferencd@0
|
144 {
|
|
ferencd@0
|
145 return (!(*this == p));
|
|
ferencd@0
|
146 }
|
|
ferencd@0
|
147
|
|
ferencd@0
|
148
|
|
ferencd@0
|
149 bool path::isEmpty() const
|
|
ferencd@0
|
150 {
|
|
ferencd@0
|
151 return (m_list.empty());
|
|
ferencd@0
|
152 }
|
|
ferencd@0
|
153
|
|
ferencd@0
|
154
|
|
ferencd@0
|
155 bool path::isRoot() const
|
|
ferencd@0
|
156 {
|
|
ferencd@0
|
157 return (m_list.empty());
|
|
ferencd@0
|
158 }
|
|
ferencd@0
|
159
|
|
ferencd@0
|
160
|
|
ferencd@0
|
161 const path::component path::getLastComponent() const
|
|
ferencd@0
|
162 {
|
|
ferencd@0
|
163 return (m_list[m_list.size() - 1]);
|
|
ferencd@0
|
164 }
|
|
ferencd@0
|
165
|
|
ferencd@0
|
166
|
|
ferencd@0
|
167 path::component& path::getLastComponent()
|
|
ferencd@0
|
168 {
|
|
ferencd@0
|
169 return (m_list[m_list.size() - 1]);
|
|
ferencd@0
|
170 }
|
|
ferencd@0
|
171
|
|
ferencd@0
|
172
|
|
ferencd@0
|
173 size_t path::getSize() const
|
|
ferencd@0
|
174 {
|
|
ferencd@0
|
175 return (m_list.size());
|
|
ferencd@0
|
176 }
|
|
ferencd@0
|
177
|
|
ferencd@0
|
178
|
|
ferencd@0
|
179 const path::component& path::operator[](const size_t x) const
|
|
ferencd@0
|
180 {
|
|
ferencd@0
|
181 return (m_list[x]);
|
|
ferencd@0
|
182 }
|
|
ferencd@0
|
183
|
|
ferencd@0
|
184
|
|
ferencd@0
|
185 path::component& path::operator[](const size_t x)
|
|
ferencd@0
|
186 {
|
|
ferencd@0
|
187 return (m_list[x]);
|
|
ferencd@0
|
188 }
|
|
ferencd@0
|
189
|
|
ferencd@0
|
190
|
|
ferencd@0
|
191 bool path::isDirectParentOf(const path& p) const
|
|
ferencd@0
|
192 {
|
|
ferencd@0
|
193 if (p.getSize() != getSize() + 1)
|
|
ferencd@0
|
194 return (false);
|
|
ferencd@0
|
195
|
|
ferencd@0
|
196 bool equal = true;
|
|
ferencd@0
|
197
|
|
ferencd@0
|
198 for (list::size_type i = 0 ; equal && i < m_list.size() ; ++i)
|
|
ferencd@0
|
199 equal = (m_list[i].isEquivalent(p.m_list[i]));
|
|
ferencd@0
|
200
|
|
ferencd@0
|
201 return (equal);
|
|
ferencd@0
|
202 }
|
|
ferencd@0
|
203
|
|
ferencd@0
|
204
|
|
ferencd@0
|
205 bool path::isParentOf(const path& p) const
|
|
ferencd@0
|
206 {
|
|
ferencd@0
|
207 if (p.getSize() < getSize() + 1)
|
|
ferencd@0
|
208 return (false);
|
|
ferencd@0
|
209
|
|
ferencd@0
|
210 bool equal = true;
|
|
ferencd@0
|
211
|
|
ferencd@0
|
212 for (list::size_type i = 0 ; equal && i < m_list.size() ; ++i)
|
|
ferencd@0
|
213 equal = (m_list[i].isEquivalent(p.m_list[i]));
|
|
ferencd@0
|
214
|
|
ferencd@0
|
215 return (equal);
|
|
ferencd@0
|
216 }
|
|
ferencd@0
|
217
|
|
ferencd@0
|
218
|
|
ferencd@0
|
219 void path::renameParent(const path& oldPath, const path& newPath)
|
|
ferencd@0
|
220 {
|
|
ferencd@0
|
221 if (isEmpty() || oldPath.getSize() > getSize())
|
|
ferencd@0
|
222 return;
|
|
ferencd@0
|
223
|
|
ferencd@0
|
224 bool equal = true;
|
|
ferencd@0
|
225 list::size_type i;
|
|
ferencd@0
|
226
|
|
ferencd@0
|
227 for (i = 0 ; equal && i < oldPath.m_list.size() ; ++i)
|
|
ferencd@0
|
228 equal = (m_list[i].isEquivalent(oldPath.m_list[i]));
|
|
ferencd@0
|
229
|
|
ferencd@0
|
230 if (i != oldPath.m_list.size())
|
|
ferencd@0
|
231 return;
|
|
ferencd@0
|
232
|
|
ferencd@0
|
233 list newList;
|
|
ferencd@0
|
234
|
|
ferencd@0
|
235 for (list::size_type j = 0 ; j < newPath.m_list.size() ; ++j)
|
|
ferencd@0
|
236 newList.push_back(newPath.m_list[j]);
|
|
ferencd@0
|
237
|
|
ferencd@0
|
238 for (list::size_type j = i ; j < m_list.size() ; ++j)
|
|
ferencd@0
|
239 newList.push_back(m_list[j]);
|
|
ferencd@0
|
240
|
|
ferencd@0
|
241 m_list.resize(newList.size());
|
|
ferencd@0
|
242 std::copy(newList.begin(), newList.end(), m_list.begin());
|
|
ferencd@0
|
243 }
|
|
ferencd@0
|
244
|
|
ferencd@0
|
245
|
|
ferencd@0
|
246 void path::appendComponent(const path::component& c)
|
|
ferencd@0
|
247 {
|
|
ferencd@0
|
248 m_list.push_back(c);
|
|
ferencd@0
|
249 }
|
|
ferencd@0
|
250
|
|
ferencd@0
|
251
|
|
ferencd@0
|
252 const path::component& path::getComponentAt(const size_t pos) const
|
|
ferencd@0
|
253 {
|
|
ferencd@0
|
254 return (m_list[pos]);
|
|
ferencd@0
|
255 }
|
|
ferencd@0
|
256
|
|
ferencd@0
|
257
|
|
ferencd@0
|
258 path::component& path::getComponentAt(const size_t pos)
|
|
ferencd@0
|
259 {
|
|
ferencd@0
|
260 return (m_list[pos]);
|
|
ferencd@0
|
261 }
|
|
ferencd@0
|
262
|
|
ferencd@0
|
263
|
|
ferencd@0
|
264 // static
|
|
ferencd@0
|
265 path path::fromString(const string& str, const string& sep, const charset& cset)
|
|
ferencd@0
|
266 {
|
|
ferencd@0
|
267 path p;
|
|
ferencd@0
|
268
|
|
ferencd@0
|
269 size_t start = 0;
|
|
ferencd@0
|
270 size_t end = 0;
|
|
ferencd@0
|
271
|
|
ferencd@0
|
272 do
|
|
ferencd@0
|
273 {
|
|
ferencd@0
|
274 end = str.find(sep, start);
|
|
ferencd@0
|
275
|
|
ferencd@0
|
276 string comp;
|
|
ferencd@0
|
277
|
|
ferencd@0
|
278 if (end == string::npos)
|
|
ferencd@0
|
279 comp = str.substr(start);
|
|
ferencd@0
|
280 else
|
|
ferencd@0
|
281 comp = str.substr(start, end - start);
|
|
ferencd@0
|
282
|
|
ferencd@0
|
283 // Skip leading or trailing separators
|
|
ferencd@0
|
284 if (comp.length())
|
|
ferencd@0
|
285 p.appendComponent(component(comp, cset));
|
|
ferencd@0
|
286
|
|
ferencd@0
|
287 start = end + 1;
|
|
ferencd@0
|
288 }
|
|
ferencd@0
|
289 while (end != string::npos);
|
|
ferencd@0
|
290
|
|
ferencd@0
|
291 return p;
|
|
ferencd@0
|
292 }
|
|
ferencd@0
|
293
|
|
ferencd@0
|
294
|
|
ferencd@0
|
295 const string path::toString(const string& sep, const charset& cset) const
|
|
ferencd@0
|
296 {
|
|
ferencd@0
|
297 string str;
|
|
ferencd@0
|
298
|
|
ferencd@0
|
299 for (size_t i = 0 ; i < m_list.size() ; ++i)
|
|
ferencd@0
|
300 {
|
|
ferencd@0
|
301 if (i != 0)
|
|
ferencd@0
|
302 str += sep;
|
|
ferencd@0
|
303
|
|
ferencd@0
|
304 str += m_list[i].getConvertedText(cset);
|
|
ferencd@0
|
305 }
|
|
ferencd@0
|
306
|
|
ferencd@0
|
307 return str;
|
|
ferencd@0
|
308 }
|
|
ferencd@0
|
309
|
|
ferencd@0
|
310
|
|
ferencd@0
|
311 } // utility
|
|
ferencd@0
|
312 } // vmime
|