2 This file is part of libmsppath
3 Copyright © 2006 Mikko Rasa, Mikkosoft Productions
4 Distributed under the LGPL
6 #include <msp/strutils.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;
37 Path Path::subpath(unsigned start, unsigned count) const
41 for(unsigned j=0; (j<start && i!=end()); ++j)
43 for(unsigned j=0; (j<count && i!=end()); ++j)
52 Attaches another path to the end of this one. An absolute path replaces the
53 existing data. ".." elements annihilate the last component and "." elements
56 Path &Path::operator/=(const Path &p)
62 for(iterator i=p.begin(); i!=p.end(); ++i)
66 unsigned slash=path.rfind(DIRCHAR);
68 if(is_windows_drive(path.substr(0,slash))) ++slash;
71 if(slash==string::npos)
78 if(path.size()>1 || (path.size()==1 && path[0]!=DIRCHAR))
88 Returns the path component at the specified index. Negative indices count from
91 string Path::operator[](int n) const
95 for(iterator i=begin(); i!=end(); ++i,--n)
100 for(iterator i=--end();; --i)
103 if(i==begin()) break;
110 bool Path::operator==(const Path &p) const
113 return !strcasecmp(path, p.path);
119 void Path::init(const std::string &p)
127 unsigned slash=p.find_first_of("/\\",start);
130 if(path.size() || absolute)
132 path+=p.substr(start,slash-start);
134 if(slash==string::npos)
140 Path::iterator::iterator(const Path &p):
144 if(path.path[0]==DIRCHAR)
147 else if(path.path[2]==DIRCHAR && is_windows_drive(path.path.substr(0,2)))
151 end=path.path.find(DIRCHAR);
154 Path::iterator &Path::iterator::operator++()
157 if(start>=path.path.size()) return *this;
158 if(path.path[start]==DIRCHAR) ++start;
159 end=path.path.find(DIRCHAR,start);
163 Path::iterator &Path::iterator::operator--()
166 if(end==0) return *this;
167 if(end>1 && end<path.path.size() && path.path[end]!=DIRCHAR) --end;
168 start=path.path.rfind(DIRCHAR,end-1);
169 if(start==string::npos)
176 string Path::iterator::operator*() const
178 if(start>=path.path.size()) return "";
179 if(start==end) return "";
180 return path.path.substr(start,end-start);