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 void Path::init(const string &p)
33 string::size_type start = 0;
34 if(p[0]=='/' || p[0]=='\\')
35 add_component(string(1, DIRSEP));
38 string::size_type slash = p.find_first_of("/\\", start);
40 add_component(p.substr(start, slash-start));
41 if(slash==string::npos)
47 unsigned Path::size() const
51 if(path.size()==1 && path[0]==DIRSEP)
55 for(string::const_iterator i=path.begin(); i!=path.end(); ++i)
56 if(*i==DIRSEP) ++count;
60 bool Path::is_absolute() const
63 if(is_windows_drive((*this)[0]))
71 Path Path::subpath(unsigned start, unsigned count) const
75 for(unsigned j=0; (j<start && i!=end()); ++j)
77 for(unsigned j=0; (j<count && i!=end()); ++j)
85 Path Path::operator/(const Path &p) const
92 Path &Path::operator/=(const Path &p)
98 for(Iterator i=p.begin(); i!=p.end(); ++i)
104 void Path::add_component(const string &comp)
108 else if(comp.size()==1 && comp[0]==DIRSEP)
110 // Replace the path with the root directory
112 unsigned slash = path.find(DIRSEP);
113 if(is_windows_drive(path.substr(0, slash)))
114 path = path.substr(0, 2);
120 else if(is_windows_drive(comp))
125 if(path.empty() || path==".")
127 // .. in root directory is a no-op
128 else if(path.size()==1 && path[0]==DIRSEP)
131 else if(is_windows_drive(path))
136 string::size_type slash = path.rfind(DIRSEP);
137 string::size_type start = (slash==string::npos ? 0 : slash+1);
138 if(!path.compare(start, string::npos, ".."))
140 // If the last component already is a .., add another
144 else if(slash==string::npos)
150 // Otherwise, erase the last component
151 path.erase(slash, string::npos);
155 else if(comp!="." || path.empty())
157 if(comp!="." && path.empty())
159 if(path.size()>1 || (path.size()==1 && path[0]!=DIRSEP))
165 string Path::operator[](int n) const
169 for(Iterator i=begin(); i!=end(); ++i, --n)
175 for(Iterator i=end(); i!=begin();)
183 throw invalid_argument("Path::operator[]");
186 bool Path::operator==(const Path &p) const
189 return !strcasecmp(path, p.path);
195 Path::Iterator Path::begin() const
197 return Iterator(*this);
200 Path::Iterator Path::end() const
203 i.start = i.end = std::string::npos;
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);