0fe95d65df943a9f0b4b1b4bf006035a4e7dccee
[libs/core.git] / source / fs / path.cpp
1 #include <stdexcept>
2 #include <msp/strings/utils.h>
3 #include "path.h"
4 #include "utils.h"
5
6 using namespace std;
7
8 namespace {
9
10 #ifdef WIN32
11 inline bool is_windows_drive(const std::string &p)
12 { return (p.size()==2 && ((p[0]>='A' && p[0]<='Z') || (p[0]>='a' && p[0]<='z')) && p[1]==':'); }
13 #endif
14
15 }
16
17 namespace Msp {
18 namespace FS {
19
20 Path::Path()
21 { }
22
23 Path::Path(const string &p)
24 {
25         init(p);
26 }
27
28 Path::Path(const char *p)
29 {
30         init(p);
31 }
32
33 void Path::init(const string &p)
34 {
35         if(p.empty())
36                 return;
37         string::size_type start = 0;
38         while(start<p.size())
39         {
40                 string::size_type slash = p.find_first_of("/\\", start);
41                 if(slash>start || start==0)
42                         add_component(p.substr(start, max<string::size_type>(slash-start, 1U)));
43                 if(slash==string::npos)
44                         break;
45                 start = slash+1;
46         }
47 }
48
49 unsigned Path::size() const
50 {
51         if(path.empty())
52                 return 0;
53         if(path.size()==1 && path[0]==DIRSEP)
54                 return 1;
55
56         return separators.size()+1;
57 }
58
59 bool Path::is_absolute() const
60 {
61 #ifdef WIN32
62         if(is_windows_drive((*this)[0]))
63                 return true;
64 #endif
65         return path[0]==DIRSEP;
66 }
67
68 Path Path::subpath(unsigned start, unsigned count) const
69 {
70         Path result;
71         Iterator i = begin();
72         for(unsigned j=0; (j<start && i!=end()); ++j)
73                 ++i;
74         for(unsigned j=0; (j<count && i!=end()); ++j)
75         {
76                 result /= *i;
77                 ++i;
78         }
79         return result;
80 }
81
82 Path Path::operator/(const Path &p) const
83 {
84         Path a = *this;
85         a /= p;
86         return a;
87 }
88
89 Path &Path::operator/=(const Path &p)
90 {
91         if(p.is_absolute())
92                 *this = p;
93         else
94         {
95                 for(Iterator i=p.begin(); i!=p.end(); ++i)
96                         add_component(*i);
97         }
98         return *this;
99 }
100
101 void Path::add_component(const string &comp)
102 {
103         if(comp.size()==1 && (comp[0]=='/' || comp[0]=='\\'))
104         {
105                 // Replace the path with the root directory
106 #ifdef WIN32
107                 string::size_type slash = (separators.empty() ? string::npos : separators.front());
108                 if(is_windows_drive(path.substr(0, slash)))
109                 {
110                         path = path.substr(0, 2);
111                         separators.clear();
112                 }
113                 else
114 #endif
115                 {
116                         path.assign(1, DIRSEP);
117                         separators.clear();
118                         separators.push_back(0);
119                 }
120         }
121 #ifdef WIN32
122         else if(is_windows_drive(comp))
123         {
124                 path = comp;
125                 separators.clear();
126         }
127 #endif
128         else if(comp=="..")
129         {
130                 if(path.empty() || path==".")
131                         path = comp;
132                 // .. in root directory is a no-op
133                 else if(path.size()==1 && path[0]==DIRSEP)
134                         ;
135 #ifdef WIN32
136                 else if(is_windows_drive(path))
137                         ;
138 #endif
139                 else
140                 {
141                         string::size_type start = (separators.empty() ? 0 : separators.back()+1);
142                         if(!path.compare(start, string::npos, ".."))
143                         {
144                                 // If the last component already is a .., add another
145                                 separators.push_back(path.size());
146                                 path += DIRSEP;
147                                 path += comp;
148                         }
149                         else if(separators.empty())
150                                 path = ".";
151                         else
152                         {
153                                 // Otherwise, erase the last component
154                                 path.erase(separators.back(), string::npos);
155                                 separators.pop_back();
156                         }
157                 }
158         }
159         else if(comp!="." || path.empty())
160         {
161                 if(comp!="." && path.empty())
162                         path = ".";
163                 if(path.size()>1 || (path.size()==1 && path[0]!=DIRSEP))
164                 {
165                         separators.push_back(path.size());
166                         path += DIRSEP;
167                 }
168                 path += comp;
169         }
170 }
171
172 string Path::operator[](int n) const
173 {
174         if(n>=0)
175         {
176                 for(Iterator i=begin(); i!=end(); ++i, --n)
177                         if(!n)
178                                 return *i;
179         }
180         else
181         {
182                 for(Iterator i=end(); i!=begin();)
183                 {
184                         --i;
185                         if(!++n)
186                                 return *i;
187                 }
188         }
189
190         throw invalid_argument("Path::operator[]");
191 }
192
193 bool Path::operator==(const Path &other) const
194 {
195 #ifdef WIN32
196         return strcasecmp(path, other.path)==0;
197 #else
198         return path==other.path;
199 #endif
200 }
201
202 bool Path::operator<(const Path &other) const
203 {
204 #ifdef WIN32
205         return strcasecmp(path, other.path)<0;
206 #else
207         return path<other.path;
208 #endif
209 }
210
211 bool Path::operator>(const Path &other) const
212 {
213 #ifdef WIN32
214         return strcasecmp(path, other.path)>0;
215 #else
216         return path>other.path;
217 #endif
218 }
219
220
221 Path::Iterator::Iterator(const Path &p, bool e):
222         path(&p),
223         iter(e ? path->separators.end() : path->separators.begin()),
224         end(e || path->path.empty())
225 {
226         update();
227 }
228
229 Path::Iterator &Path::Iterator::operator++()
230 {
231         if(iter==path->separators.end())
232                 end = true;
233         else
234         {
235                 ++iter;
236                 if(path->path.size()==1 && path->separators.size()==1)
237                         end = true;
238         }
239         update();
240         return *this;
241 }
242
243 Path::Iterator &Path::Iterator::operator--()
244 {
245         if(end)
246         {
247                 end = false;
248                 if(path->path.size()==1 && path->separators.size()==1)
249                         --iter;
250         }
251         else if(iter!=path->separators.begin())
252                 --iter;
253         update();
254         return *this;
255 }
256
257 void Path::Iterator::update()
258 {
259         if(end)
260         {
261                 current.clear();
262                 return;
263         }
264
265         string::size_type start = 0;
266         if(iter!=path->separators.begin())
267         {
268                 PositionArray::const_iterator prev = iter;
269                 start = *--prev+1;
270         }
271
272         string::size_type slash = string::npos;
273         if(iter!=path->separators.end())
274                 slash = *iter;
275
276         if(slash==0)
277                 current = path->path.substr(start, 1);
278         else
279                 current = path->path.substr(start, slash-start);
280 }
281
282 } // namespace FS
283 } // namespace Msp