2 #include <msp/strings/utils.h>
10 inline bool is_windows_drive(const std::string &p)
11 { return (p.size()==2 && ((p[0]>='A' && p[0]<='Z') || (p[0]>='a' && p[0]<='z')) && p[1]==':'); }
21 Path::Path(const string &p)
26 Path::Path(const char *p)
31 unsigned Path::size() const
35 if(path.size()==1 && path[0]==DIRSEP)
39 for(string::const_iterator i=path.begin(); i!=path.end(); ++i)
40 if(*i==DIRSEP) ++count;
44 bool Path::is_absolute() const
47 if(is_windows_drive((*this)[0]))
55 Path Path::subpath(unsigned start, unsigned count) const
59 for(unsigned j=0; (j<start && i!=end()); ++j)
61 for(unsigned j=0; (j<count && i!=end()); ++j)
69 Path Path::operator/(const Path &p) const
76 Path &Path::operator/=(const Path &p)
82 for(Iterator i=p.begin(); i!=p.end(); ++i)
88 string Path::operator[](int n) const
92 for(Iterator i=begin(); i!=end(); ++i, --n)
98 for(Iterator i=end(); i!=begin();)
106 throw invalid_argument("Path::operator[]");
109 bool Path::operator==(const Path &p) const
112 return !strcasecmp(path, p.path);
118 Path::Iterator Path::begin() const
120 return Iterator(*this);
123 Path::Iterator Path::end() const
126 i.start=i.end = std::string::npos;
130 void Path::init(const string &p)
132 string::size_type start = 0;
133 if(p[0]=='/' || p[0]=='\\')
134 add_component(string(1, DIRSEP));
137 string::size_type slash = p.find_first_of("/\\", start);
139 add_component(p.substr(start, slash-start));
140 if(slash==string::npos)
146 void Path::add_component(const string &comp)
150 else if(comp.size()==1 && comp[0]==DIRSEP)
152 // Replace the path with the root directory
154 unsigned slash = path.find(DIRSEP);
155 if(is_windows_drive(path.substr(0, slash)))
156 path = path.substr(0, 2);
162 else if(is_windows_drive(comp))
167 if(path.empty() || path==".")
169 // .. in root directory is a no-op
170 else if(path.size()==1 && path[0]==DIRSEP)
173 else if(is_windows_drive(path))
178 string::size_type slash = path.rfind(DIRSEP);
179 string::size_type start = (slash==string::npos ? 0 : slash+1);
180 if(!path.compare(start, string::npos, ".."))
182 // If the last component already is a .., add another
186 else if(slash==string::npos)
192 // Otherwise, erase the last component
193 path.erase(slash, string::npos);
197 else if(comp!="." || path.empty())
199 if(comp!="." && path.empty())
201 if(path.size()>1 || (path.size()==1 && path[0]!=DIRSEP))
208 Path::Iterator::Iterator(const Path &p):
212 if(path.path.empty())
213 start=end = string::npos;
214 else if(path.path[0]==DIRSEP)
217 else if(path.path.size()>2 && path.path[2]==DIRSEP && is_windows_drive(path.path.substr(0, 2)))
221 end = path.path.find(DIRSEP);
224 Path::Iterator &Path::Iterator::operator++()
227 if(start>=path.path.size())
229 if(path.path[start]==DIRSEP)
231 end = path.path.find(DIRSEP, start);
235 Path::Iterator &Path::Iterator::operator--()
241 if(end>1 && end<path.path.size() && path.path[end]!=DIRSEP)
244 start = path.path.rfind(DIRSEP, end-1);
245 if(start==string::npos)
253 string Path::Iterator::operator*() const
255 if(start>=path.path.size())
259 return path.path.substr(start, end-start);