2 This file is part of libmsppath
3 Copyright © 2006 Mikko Rasa, Mikkosoft Productions
4 Distributed under the LGPL
6 #include <msp/strings/utils.h>
16 Returns the number of components in the path.
18 unsigned Path::size() const
20 if(!path.size()) return 0;
21 if(path.size()==1 && path[0]==DIRCHAR) return 1;
23 for(string::const_iterator i=path.begin(); i!=path.end(); ++i)
24 if(*i==DIRCHAR) ++count;
28 bool Path::is_absolute() const
31 if(is_windows_drive((*this)[0])) return true;
33 if(path[0]==DIRCHAR) return true;
38 Extracts the given component range from the path.
40 Path Path::subpath(unsigned start, unsigned count) const
44 for(unsigned j=0; (j<start && i!=end()); ++j)
46 for(unsigned j=0; (j<count && i!=end()); ++j)
55 Attaches another path to the end of this one. An absolute path replaces the
56 existing data. ".." elements annihilate the last component and "." elements
59 Path &Path::operator/=(const Path &p)
65 for(iterator i=p.begin(); i!=p.end(); ++i)
72 Returns the path component at the specified index. Negative indices count from
75 string Path::operator[](int n) const
79 for(iterator i=begin(); i!=end(); ++i,--n)
84 for(iterator i=--end();; --i)
94 bool Path::operator==(const Path &p) const
97 return !strcasecmp(path, p.path);
103 void Path::init(const string &p)
106 if(p[0]=='/' || p[0]=='\\')
107 add_component(string(1, DIRCHAR));
110 unsigned slash=p.find_first_of("/\\",start);
113 add_component(p.substr(start, slash-start));
115 if(slash==string::npos)
122 Adds a single component to the path, emulating the cd command.
124 void Path::add_component(const string &comp)
128 else if(comp.size()==1 && comp[0]==DIRCHAR)
130 // Replace the path with the root directory
132 unsigned slash=path.find(DIRCHAR);
133 if(is_windows_drive(path.substr(0, slash)))
134 path=path.substr(0,2);
140 else if(is_windows_drive(comp))
147 // .. in root directory is a no-op
148 else if(path.size()==1 && path[0]==DIRCHAR)
151 else if(is_windows_drive(path))
156 unsigned slash=path.rfind(DIRCHAR);
157 unsigned start=(slash==string::npos)?0:slash+1;
158 if(!path.compare(start, string::npos, ".."))
160 // If the last component already is a .., add another
165 // Otherwise, erase the last component
166 path.erase(slash, string::npos);
169 else if(comp!="." || path.empty())
173 if(path.size()>1 || (path.size()==1 && path[0]!=DIRCHAR))
179 Path::iterator::iterator(const Path &p):
183 if(path.path.empty())
184 start=end=string::npos;
185 else if(path.path[0]==DIRCHAR)
188 else if(path.path.size()>2 && path.path[2]==DIRCHAR && is_windows_drive(path.path.substr(0,2)))
192 end=path.path.find(DIRCHAR);
195 Path::iterator &Path::iterator::operator++()
198 if(start>=path.path.size()) return *this;
199 if(path.path[start]==DIRCHAR) ++start;
200 end=path.path.find(DIRCHAR,start);
204 Path::iterator &Path::iterator::operator--()
207 if(end==0) return *this;
208 if(end>1 && end<path.path.size() && path.path[end]!=DIRCHAR) --end;
209 start=path.path.rfind(DIRCHAR,end-1);
210 if(start==string::npos)
217 string Path::iterator::operator*() const
219 if(start>=path.path.size()) return "";
220 if(start==end) return "";
221 return path.path.substr(start,end-start);