1 #include <msp/core/except.h>
2 #include <msp/strings/utils.h>
10 inline bool is_windows_drive(const std::string &p)
11 { return (p.size()==2 && ((p[0]>='A' && p[0]<='Z') || (p[0]>='a' && p[0]<='z')) && p[1]==':'); }
21 Path::Path(const string &p)
26 Path::Path(const char *p)
31 unsigned Path::size() const
35 if(path.size()==1 && path[0]==DIRSEP)
39 for(string::const_iterator i=path.begin(); i!=path.end(); ++i)
40 if(*i==DIRSEP) ++count;
44 bool Path::is_absolute() const
47 if(is_windows_drive((*this)[0]))
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
77 Attaches another path to the end of this one. An absolute path replaces the
78 existing data. ".." elements annihilate the last component and "." elements
81 Path &Path::operator/=(const Path &p)
87 for(Iterator i=p.begin(); i!=p.end(); ++i)
93 string Path::operator[](int n) const
97 for(Iterator i=begin(); i!=end(); ++i, --n)
103 for(Iterator i=end(); i!=begin();)
111 throw InvalidParameterValue("Path component index out of range");
114 bool Path::operator==(const Path &p) const
117 return !strcasecmp(path, p.path);
123 Path::Iterator Path::begin() const
125 return Iterator(*this);
128 Path::Iterator Path::end() const
131 i.start=i.end = std::string::npos;
135 void Path::init(const string &p)
137 string::size_type start = 0;
138 if(p[0]=='/' || p[0]=='\\')
139 add_component(string(1, DIRSEP));
142 string::size_type slash = p.find_first_of("/\\", start);
144 add_component(p.substr(start, slash-start));
145 if(slash==string::npos)
152 Adds a single component to the path, emulating the cd command. Fails horribly
153 if comp contains a separator character.
155 void Path::add_component(const string &comp)
159 else if(comp.size()==1 && comp[0]==DIRSEP)
161 // Replace the path with the root directory
163 unsigned slash = path.find(DIRSEP);
164 if(is_windows_drive(path.substr(0, slash)))
165 path = path.substr(0, 2);
171 else if(is_windows_drive(comp))
176 if(path.empty() || path==".")
178 // .. in root directory is a no-op
179 else if(path.size()==1 && path[0]==DIRSEP)
182 else if(is_windows_drive(path))
187 string::size_type slash = path.rfind(DIRSEP);
188 string::size_type start = (slash==string::npos ? 0 : slash+1);
189 if(!path.compare(start, string::npos, ".."))
191 // If the last component already is a .., add another
195 else if(slash==string::npos)
201 // Otherwise, erase the last component
202 path.erase(slash, string::npos);
206 else if(comp!="." || path.empty())
208 if(comp!="." && path.empty())
210 if(path.size()>1 || (path.size()==1 && path[0]!=DIRSEP))
217 Path::Iterator::Iterator(const Path &p):
221 if(path.path.empty())
222 start=end = string::npos;
223 else if(path.path[0]==DIRSEP)
226 else if(path.path.size()>2 && path.path[2]==DIRSEP && is_windows_drive(path.path.substr(0, 2)))
230 end = path.path.find(DIRSEP);
233 Path::Iterator &Path::Iterator::operator++()
236 if(start>=path.path.size())
238 if(path.path[start]==DIRSEP)
240 end = path.path.find(DIRSEP, start);
244 Path::Iterator &Path::Iterator::operator--()
250 if(end>1 && end<path.path.size() && path.path[end]!=DIRSEP)
253 start = path.path.rfind(DIRSEP, end-1);
254 if(start==string::npos)
262 string Path::Iterator::operator*() const
264 if(start>=path.path.size())
268 return path.path.substr(start, end-start);