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)
37 string::size_type start = 0;
40 string::size_type slash = p.find_first_of("/\\", start);
41 if(slash>start || start==0)
42 add_component(p.substr(start, max(slash-start, 1U)));
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)
103 if(comp.size()==1 && comp[0]==DIRSEP)
105 // Replace the path with the root directory
107 string::size_type slash = (separators.empty() ? string::npos : separators.front());
108 if(is_windows_drive(path.substr(0, slash)))
110 path = path.substr(0, 2);
118 separators.push_back(0);
122 else if(is_windows_drive(comp))
130 if(path.empty() || path==".")
132 // .. in root directory is a no-op
133 else if(path.size()==1 && path[0]==DIRSEP)
136 else if(is_windows_drive(path))
141 string::size_type start = (separators.empty() ? 0 : separators.back()+1);
142 if(!path.compare(start, string::npos, ".."))
144 // If the last component already is a .., add another
145 separators.push_back(path.size());
149 else if(separators.empty())
153 // Otherwise, erase the last component
154 path.erase(separators.back(), string::npos);
155 separators.pop_back();
159 else if(comp!="." || path.empty())
161 if(comp!="." && path.empty())
163 if(path.size()>1 || (path.size()==1 && path[0]!=DIRSEP))
165 separators.push_back(path.size());
172 string Path::operator[](int n) const
176 for(Iterator i=begin(); i!=end(); ++i, --n)
182 for(Iterator i=end(); i!=begin();)
190 throw invalid_argument("Path::operator[]");
193 bool Path::operator==(const Path &other) const
196 return strcasecmp(path, other.path)==0;
198 return path==other.path;
202 bool Path::operator<(const Path &other) const
205 return strcasecmp(path, other.path)<0;
207 return path<other.path;
211 bool Path::operator>(const Path &other) const
214 return strcasecmp(path, other.path)>0;
216 return path>other.path;
221 Path::Iterator::Iterator(const Path &p, bool e):
223 iter(e ? path->separators.end() : path->separators.begin()),
224 end(e || path->path.empty())
227 Path::Iterator &Path::Iterator::operator++()
229 if(iter==path->separators.end())
234 if(path->path.size()==1 && path->separators.size()==1)
240 Path::Iterator &Path::Iterator::operator--()
245 if(path->path.size()==1 && path->separators.size()==1)
248 else if(iter!=path->separators.begin())
253 string Path::Iterator::operator*() const
256 throw logic_error("Path::Iterator::operator*");
258 string::size_type start = 0;
259 if(iter!=path->separators.begin())
261 PositionArray::const_iterator prev = iter;
265 string::size_type slash = string::npos;
266 if(iter!=path->separators.end())
270 return path->path.substr(start, 1);
271 return path->path.substr(start, slash-start);