2 #include <msp/strings/utils.h>
11 inline bool is_windows_drive(const string &p)
12 { return (p.size()==2 && ((p[0]>='A' && p[0]<='Z') || (p[0]>='a' && p[0]<='z')) && p[1]==':'); }
20 Path::Path(const string &p)
24 string::size_type start = 0;
27 string::size_type slash = p.find_first_of("/\\", start);
28 if(slash>start || start==0)
29 add_component(p.substr(start, max<string::size_type>(slash-start, 1U)));
30 if(slash==string::npos)
36 unsigned Path::size() const
40 if(path.size()==1 && path[0]==DIRSEP)
43 return separators.size()+1;
46 bool Path::is_absolute() const
49 if(!empty() && is_windows_drive((*this)[0]))
52 return path[0]==DIRSEP;
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(const string &c: p)
88 void Path::add_component(const string &comp)
90 if(comp.size()==1 && (comp[0]=='/' || comp[0]=='\\'))
92 // Replace the path with the root directory
94 string::size_type slash = (separators.empty() ? string::npos : separators.front());
95 if(is_windows_drive(path.substr(0, slash)))
97 path = path.substr(0, 2);
103 path.assign(1, DIRSEP);
105 separators.push_back(0);
109 else if(is_windows_drive(comp))
117 if(path.empty() || path==".")
119 // .. in root directory is a no-op
120 else if(path.size()==1 && path[0]==DIRSEP)
123 else if(is_windows_drive(path))
128 string::size_type start = (separators.empty() ? 0 : separators.back()+1);
129 if(!path.compare(start, string::npos, ".."))
131 // If the last component already is a .., add another
132 separators.push_back(path.size());
138 /* Removing the last component of a relative path results in the
144 /* Removing the last component of an absolute path results in the
146 path.erase(start, string::npos);
150 // Otherwise, erase the last component and its separator
151 path.erase(separators.back(), string::npos);
152 separators.pop_back();
156 else if(comp!="." || path.empty())
158 if(comp!="." && path.empty())
160 if(path.size()>1 || (path.size()==1 && path[0]!=DIRSEP))
162 separators.push_back(path.size());
169 string Path::operator[](int n) const
173 for(auto i=begin(); i!=end(); ++i, --n)
179 for(auto i=end(); i!=begin();)
187 throw invalid_argument("Path::operator[]");
190 bool Path::operator==(const Path &other) const
193 return strcasecmp(path, other.path)==0;
195 return path==other.path;
199 bool Path::operator<(const Path &other) const
202 return strcasecmp(path, other.path)<0;
204 return path<other.path;
208 bool Path::operator>(const Path &other) const
211 return strcasecmp(path, other.path)>0;
213 return path>other.path;
218 Path::Iterator::Iterator(const Path &p, bool e):
220 iter(e ? path->separators.end() : path->separators.begin()),
221 end(e || path->path.empty())
226 Path::Iterator &Path::Iterator::operator++()
228 if(iter==path->separators.end())
233 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())
254 void Path::Iterator::update()
262 string::size_type start = 0;
263 if(iter!=path->separators.begin())
264 start = *prev(iter)+1;
266 string::size_type slash = string::npos;
267 if(iter!=path->separators.end())
271 current = path->path.substr(start, 1);
273 current = path->path.substr(start, slash-start);