2 #include <msp/strings/utils.h>
11 inline bool is_windows_drive(const std::string &p)
12 { return (p.size()==2 && ((p[0]>='A' && p[0]<='Z') || (p[0]>='a' && p[0]<='z')) && p[1]==':'); }
23 Path::Path(const string &p)
28 Path::Path(const char *p)
33 void Path::init(const string &p)
35 string::size_type start = 0;
36 if(p[0]=='/' || p[0]=='\\')
37 add_component(string(1, DIRSEP));
40 string::size_type slash = p.find_first_of("/\\", start);
42 add_component(p.substr(start, slash-start));
43 if(slash==string::npos)
49 unsigned Path::size() const
53 if(path.size()==1 && path[0]==DIRSEP)
57 for(string::const_iterator i=path.begin(); i!=path.end(); ++i)
58 if(*i==DIRSEP) ++count;
62 bool Path::is_absolute() const
65 if(is_windows_drive((*this)[0]))
68 return path[0]==DIRSEP;
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 &other) const
189 return strcasecmp(path, other.path)==0;
191 return path==other.path;
195 bool Path::operator<(const Path &other) const
198 return strcasecmp(path, other.path)<0;
200 return path<other.path;
204 bool Path::operator>(const Path &other) const
207 return strcasecmp(path, other.path)>0;
209 return path>other.path;
213 Path::Iterator Path::begin() const
215 return Iterator(*this);
218 Path::Iterator Path::end() const
221 i.start = i.end = std::string::npos;
226 Path::Iterator::Iterator(const Path &p):
230 if(path.path.empty())
231 start = end = string::npos;
232 else if(path.path[0]==DIRSEP)
235 else if(path.path.size()>2 && path.path[2]==DIRSEP && is_windows_drive(path.path.substr(0, 2)))
239 end = path.path.find(DIRSEP);
242 Path::Iterator &Path::Iterator::operator++()
245 if(start>=path.path.size())
247 if(path.path[start]==DIRSEP)
249 end = path.path.find(DIRSEP, start);
253 Path::Iterator &Path::Iterator::operator--()
259 if(end>1 && end<path.path.size() && path.path[end]!=DIRSEP)
262 start = path.path.rfind(DIRSEP, end-1);
263 if(start==string::npos)
271 string Path::Iterator::operator*() const
273 if(start>=path.path.size())
277 return path.path.substr(start, end-start);