3 This file is part of libmspfs
4 Copyright © 2006-2008 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
8 #include <msp/strings/utils.h>
18 Returns the number of components in the path.
20 unsigned Path::size() const
24 if(path.size()==1 && path[0]==DIRCHAR)
28 for(string::const_iterator i=path.begin(); i!=path.end(); ++i)
29 if(*i==DIRCHAR) ++count;
33 bool Path::is_absolute() const
36 if(is_windows_drive((*this)[0]))
45 Extracts the given component range from the path.
47 Path Path::subpath(unsigned start, unsigned count) const
51 for(unsigned j=0; (j<start && i!=end()); ++j)
53 for(unsigned j=0; (j<count && i!=end()); ++j)
62 Attaches another path to the end of this one. An absolute path replaces the
63 existing data. ".." elements annihilate the last component and "." elements
66 Path &Path::operator/=(const Path &p)
72 for(Iterator i=p.begin(); i!=p.end(); ++i)
79 Returns the path component at the specified index. Negative indices count from
82 string Path::operator[](int n) const
86 for(Iterator i=begin(); i!=end(); ++i, --n)
91 for(Iterator i=--end();; --i)
101 bool Path::operator==(const Path &p) const
104 return !strcasecmp(path, p.path);
110 void Path::init(const string &p)
113 if(p[0]=='/' || p[0]=='\\')
114 add_component(string(1, DIRCHAR));
117 unsigned slash=p.find_first_of("/\\", start);
119 add_component(p.substr(start, slash-start));
120 if(slash==string::npos)
127 Adds a single component to the path, emulating the cd command.
129 void Path::add_component(const string &comp)
133 else if(comp.size()==1 && comp[0]==DIRCHAR)
135 // Replace the path with the root directory
137 unsigned slash=path.find(DIRCHAR);
138 if(is_windows_drive(path.substr(0, slash)))
139 path=path.substr(0, 2);
145 else if(is_windows_drive(comp))
152 // .. in root directory is a no-op
153 else if(path.size()==1 && path[0]==DIRCHAR)
156 else if(is_windows_drive(path))
161 unsigned slash=path.rfind(DIRCHAR);
162 unsigned start=(slash==string::npos)?0:slash+1;
163 if(!path.compare(start, string::npos, ".."))
165 // If the last component already is a .., add another
170 // Otherwise, erase the last component
171 path.erase(slash, string::npos);
174 else if(comp!="." || path.empty())
178 if(path.size()>1 || (path.size()==1 && path[0]!=DIRCHAR))
184 Path::Iterator::Iterator(const Path &p):
188 if(path.path.empty())
189 start=end=string::npos;
190 else if(path.path[0]==DIRCHAR)
193 else if(path.path.size()>2 && path.path[2]==DIRCHAR && is_windows_drive(path.path.substr(0, 2)))
197 end=path.path.find(DIRCHAR);
200 Path::Iterator &Path::Iterator::operator++()
203 if(start>=path.path.size()) return *this;
204 if(path.path[start]==DIRCHAR) ++start;
205 end=path.path.find(DIRCHAR, start);
209 Path::Iterator &Path::Iterator::operator--()
212 if(end==0) return *this;
213 if(end>1 && end<path.path.size() && path.path[end]!=DIRCHAR) --end;
214 start=path.path.rfind(DIRCHAR, end-1);
215 if(start==string::npos)
222 string Path::Iterator::operator*() const
224 if(start>=path.path.size()) return "";
225 if(start==end) return "";
226 return path.path.substr(start, end-start);