2 This file is part of libmsppath
3 Copyright © 2006 Mikko Rasa, Mikkosoft Productions
4 Distributed under the LGPL
6 #include <msp/strutils.h>
17 Returns the number of components in the path.
19 unsigned Path::size() const
21 if(!path.size()) return 0;
22 if(path.size()==1 && path[0]==DIRCHAR) return 1;
24 for(string::const_iterator i=path.begin(); i!=path.end(); ++i)
25 if(*i==DIRCHAR) ++count;
29 bool Path::is_absolute() const
32 if(is_windows_drive((*this)[0])) return true;
34 if(path[0]==DIRCHAR) return true;
38 Path Path::subpath(unsigned start, unsigned count) const
42 for(unsigned j=0; (j<start && i!=end()); ++j)
44 for(unsigned j=0; (j<count && i!=end()); ++j)
53 Attaches another path to the end of this one. An absolute path replaces the
54 existing data. ".." elements annihilate the last component and "." elements
57 Path &Path::operator/=(const Path &p)
63 for(iterator i=p.begin(); i!=p.end(); ++i)
67 unsigned slash=path.rfind(DIRCHAR);
69 if(is_windows_drive(path.substr(0,slash))) ++slash;
72 if(slash==string::npos)
79 if(path.size()>1 || (path.size()==1 && path[0]!=DIRCHAR))
89 Returns the path component at the specified index. Negative indices count from
92 string Path::operator[](int n) const
96 for(iterator i=begin(); i!=end(); ++i,--n)
101 for(iterator i=--end();; --i)
104 if(i==begin()) break;
111 bool Path::operator==(const Path &p) const
114 return !strcasecmp(path, p.path);
120 void Path::init(const std::string &p)
128 unsigned slash=p.find_first_of("/\\",start);
131 if(path.size() || absolute)
133 path+=p.substr(start,slash-start);
135 if(slash==string::npos)
141 Path::iterator::iterator(const Path &p):
145 if(path.path[0]==DIRCHAR)
148 else if(path.path[2]==DIRCHAR && is_windows_drive(path.path.substr(0,2)))
152 end=path.path.find(DIRCHAR);
155 Path::iterator &Path::iterator::operator++()
158 if(start>=path.path.size()) return *this;
159 if(path.path[start]==DIRCHAR) ++start;
160 end=path.path.find(DIRCHAR,start);
164 Path::iterator &Path::iterator::operator--()
167 if(end==0) return *this;
168 if(end>1 && end<path.path.size() && path.path[end]!=DIRCHAR) --end;
169 start=path.path.rfind(DIRCHAR,end-1);
170 if(start==string::npos)
177 string Path::iterator::operator*() const
179 if(start>=path.path.size()) return "";
180 if(start==end) return "";
181 return path.path.substr(start,end-start);