]> git.tdb.fi Git - libs/core.git/blob - source/fs/path.cpp
Store an array of separators alongside with the path string
[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         string::size_type start = 0;
36         if(p[0]=='/' || p[0]=='\\')
37                 add_component(string(1, DIRSEP));
38         while(1)
39         {
40                 string::size_type slash = p.find_first_of("/\\", start);
41                 if(slash>start)
42                         add_component(p.substr(start, slash-start));
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                 path = p.path;
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.empty())
104                 ;
105         else if(comp.size()==1 && comp[0]==DIRSEP)
106         {
107                 // Replace the path with the root directory
108 #ifdef WIN32
109                 string::size_type slash = (separators.empty() ? string::npos : separators.front());
110                 if(is_windows_drive(path.substr(0, slash)))
111                 {
112                         path = path.substr(0, 2);
113                         separators.clear();
114                 }
115                 else
116 #endif
117                 {
118                         path = comp;
119                         separators.clear();
120                         separators.push_back(0);
121                 }
122         }
123 #ifdef WIN32
124         else if(is_windows_drive(comp))
125         {
126                 path = comp;
127                 separators.clear();
128         }
129 #endif
130         else if(comp=="..")
131         {
132                 if(path.empty() || path==".")
133                         path = comp;
134                 // .. in root directory is a no-op
135                 else if(path.size()==1 && path[0]==DIRSEP)
136                         ;
137 #ifdef WIN32
138                 else if(is_windows_drive(path))
139                         ;
140 #endif
141                 else
142                 {
143                         string::size_type start = (separators.empty() ? 0 : separators.back()+1);
144                         if(!path.compare(start, string::npos, ".."))
145                         {
146                                 // If the last component already is a .., add another
147                                 separators.push_back(path.size());
148                                 path += DIRSEP;
149                                 path += comp;
150                         }
151                         else if(separators.empty())
152                                 path = ".";
153                         else
154                         {
155                                 // Otherwise, erase the last component
156                                 path.erase(separators.back(), string::npos);
157                                 separators.pop_back();
158                         }
159                 }
160         }
161         else if(comp!="." || path.empty())
162         {
163                 if(comp!="." && path.empty())
164                         path = ".";
165                 if(path.size()>1 || (path.size()==1 && path[0]!=DIRSEP))
166                 {
167                         separators.push_back(path.size());
168                         path += DIRSEP;
169                 }
170                 path += comp;
171         }
172 }
173
174 string Path::operator[](int n) const
175 {
176         if(n>=0)
177         {
178                 for(Iterator i=begin(); i!=end(); ++i, --n)
179                         if(!n)
180                                 return *i;
181         }
182         else
183         {
184                 for(Iterator i=end(); i!=begin();)
185                 {
186                         --i;
187                         if(!++n)
188                                 return *i;
189                 }
190         }
191
192         throw invalid_argument("Path::operator[]");
193 }
194
195 bool Path::operator==(const Path &other) const
196 {
197 #ifdef WIN32
198         return strcasecmp(path, other.path)==0;
199 #else
200         return path==other.path;
201 #endif
202 }
203
204 bool Path::operator<(const Path &other) const
205 {
206 #ifdef WIN32
207         return strcasecmp(path, other.path)<0;
208 #else
209         return path<other.path;
210 #endif
211 }
212
213 bool Path::operator>(const Path &other) const
214 {
215 #ifdef WIN32
216         return strcasecmp(path, other.path)>0;
217 #else
218         return path>other.path;
219 #endif
220 }
221
222
223 Path::Iterator::Iterator(const Path &p, bool e):
224         path(&p),
225         iter(e ? path->separators.end() : path->separators.begin()),
226         end(e || path->path.empty())
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         return *this;
240 }
241
242 Path::Iterator &Path::Iterator::operator--()
243 {
244         if(end)
245         {
246                 end = false;
247                 if(path->path.size()==1 && path->separators.size()==1)
248                         --iter;
249         }
250         else if(iter!=path->separators.begin())
251                 --iter;
252         return *this;
253 }
254
255 string Path::Iterator::operator*() const
256 {
257         if(end)
258                 throw logic_error("Path::Iterator::operator*");
259
260         string::size_type start = 0;
261         if(iter!=path->separators.begin())
262         {
263                 PositionArray::const_iterator prev = iter;
264                 start = *--prev+1;
265         }
266
267         string::size_type slash = string::npos;
268         if(iter!=path->separators.end())
269                 slash = *iter;
270
271         if(slash==0)
272                 return path->path.substr(start, 1);
273         return path->path.substr(start, slash-start);
274 }
275
276 } // namespace FS
277 } // namespace Msp