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