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]==':'); }
23 Path::Path(const string &p)
27 string::size_type start = 0;
30 string::size_type slash = p.find_first_of("/\\", start);
31 if(slash>start || start==0)
32 add_component(p.substr(start, max<string::size_type>(slash-start, 1U)));
33 if(slash==string::npos)
39 unsigned Path::size() const
43 if(path.size()==1 && path[0]==DIRSEP)
46 return separators.size()+1;
49 bool Path::is_absolute() const
52 if(!empty() && is_windows_drive((*this)[0]))
55 return path[0]==DIRSEP;
58 Path Path::subpath(unsigned start, unsigned count) const
62 for(unsigned j=0; (j<start && i!=end()); ++j)
64 for(unsigned j=0; (j<count && i!=end()); ++j)
72 Path Path::operator/(const Path &p) const
79 Path &Path::operator/=(const Path &p)
85 for(const string &c: p)
91 void Path::add_component(const string &comp)
93 if(comp.size()==1 && (comp[0]=='/' || comp[0]=='\\'))
95 // Replace the path with the root directory
97 string::size_type slash = (separators.empty() ? string::npos : separators.front());
98 if(is_windows_drive(path.substr(0, slash)))
100 path = path.substr(0, 2);
106 path.assign(1, DIRSEP);
108 separators.push_back(0);
112 else if(is_windows_drive(comp))
120 if(path.empty() || path==".")
122 // .. in root directory is a no-op
123 else if(path.size()==1 && path[0]==DIRSEP)
126 else if(is_windows_drive(path))
131 string::size_type start = (separators.empty() ? 0 : separators.back()+1);
132 if(!path.compare(start, string::npos, ".."))
134 // If the last component already is a .., add another
135 separators.push_back(path.size());
139 else if(separators.empty())
143 // Otherwise, erase the last component
144 path.erase(separators.back(), string::npos);
145 separators.pop_back();
149 else if(comp!="." || path.empty())
151 if(comp!="." && path.empty())
153 if(path.size()>1 || (path.size()==1 && path[0]!=DIRSEP))
155 separators.push_back(path.size());
162 string Path::operator[](int n) const
166 for(auto i=begin(); i!=end(); ++i, --n)
172 for(auto i=end(); i!=begin();)
180 throw invalid_argument("Path::operator[]");
183 bool Path::operator==(const Path &other) const
186 return strcasecmp(path, other.path)==0;
188 return path==other.path;
192 bool Path::operator<(const Path &other) const
195 return strcasecmp(path, other.path)<0;
197 return path<other.path;
201 bool Path::operator>(const Path &other) const
204 return strcasecmp(path, other.path)>0;
206 return path>other.path;
211 Path::Iterator::Iterator(const Path &p, bool e):
213 iter(e ? path->separators.end() : path->separators.begin()),
214 end(e || path->path.empty())
219 Path::Iterator &Path::Iterator::operator++()
221 if(iter==path->separators.end())
226 if(path->path.size()==1 && path->separators.size()==1)
233 Path::Iterator &Path::Iterator::operator--()
238 if(path->path.size()==1 && path->separators.size()==1)
241 else if(iter!=path->separators.begin())
247 void Path::Iterator::update()
255 string::size_type start = 0;
256 if(iter!=path->separators.begin())
257 start = *prev(iter)+1;
259 string::size_type slash = string::npos;
260 if(iter!=path->separators.end())
264 current = path->path.substr(start, 1);
266 current = path->path.substr(start, slash-start);