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)
56 return separators.size()+1;
59 bool Path::is_absolute() const
62 if(is_windows_drive((*this)[0]))
65 return path[0]==DIRSEP;
68 Path Path::subpath(unsigned start, unsigned count) const
72 for(unsigned j=0; (j<start && i!=end()); ++j)
74 for(unsigned j=0; (j<count && i!=end()); ++j)
82 Path Path::operator/(const Path &p) const
89 Path &Path::operator/=(const Path &p)
95 for(Iterator i=p.begin(); i!=p.end(); ++i)
101 void Path::add_component(const string &comp)
105 else if(comp.size()==1 && comp[0]==DIRSEP)
107 // Replace the path with the root directory
109 string::size_type slash = (separators.empty() ? string::npos : separators.front());
110 if(is_windows_drive(path.substr(0, slash)))
112 path = path.substr(0, 2);
120 separators.push_back(0);
124 else if(is_windows_drive(comp))
132 if(path.empty() || path==".")
134 // .. in root directory is a no-op
135 else if(path.size()==1 && path[0]==DIRSEP)
138 else if(is_windows_drive(path))
143 string::size_type start = (separators.empty() ? 0 : separators.back()+1);
144 if(!path.compare(start, string::npos, ".."))
146 // If the last component already is a .., add another
147 separators.push_back(path.size());
151 else if(separators.empty())
155 // Otherwise, erase the last component
156 path.erase(separators.back(), string::npos);
157 separators.pop_back();
161 else if(comp!="." || path.empty())
163 if(comp!="." && path.empty())
165 if(path.size()>1 || (path.size()==1 && path[0]!=DIRSEP))
167 separators.push_back(path.size());
174 string Path::operator[](int n) const
178 for(Iterator i=begin(); i!=end(); ++i, --n)
184 for(Iterator i=end(); i!=begin();)
192 throw invalid_argument("Path::operator[]");
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 bool Path::operator>(const Path &other) const
216 return strcasecmp(path, other.path)>0;
218 return path>other.path;
223 Path::Iterator::Iterator(const Path &p, bool e):
225 iter(e ? path->separators.end() : path->separators.begin()),
226 end(e || path->path.empty())
229 Path::Iterator &Path::Iterator::operator++()
231 if(iter==path->separators.end())
236 if(path->path.size()==1 && path->separators.size()==1)
242 Path::Iterator &Path::Iterator::operator--()
247 if(path->path.size()==1 && path->separators.size()==1)
250 else if(iter!=path->separators.begin())
255 string Path::Iterator::operator*() const
258 throw logic_error("Path::Iterator::operator*");
260 string::size_type start = 0;
261 if(iter!=path->separators.begin())
263 PositionArray::const_iterator prev = iter;
267 string::size_type slash = string::npos;
268 if(iter!=path->separators.end())
272 return path->path.substr(start, 1);
273 return path->path.substr(start, slash-start);