3 This file is part of libmspfs
4 Copyright © 2006-2008 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
8 #include <msp/core/except.h>
9 #include <msp/strings/utils.h>
17 inline bool is_windows_drive(const std::string &p)
18 { return (p.size()==2 && ((p[0]>='A' && p[0]<='Z') || (p[0]>='a' && p[0]<='z')) && p[1]==':'); }
28 Path::Path(const string &p)
33 Path::Path(const char *p)
38 unsigned Path::size() const
42 if(path.size()==1 && path[0]==DIRSEP)
46 for(string::const_iterator i=path.begin(); i!=path.end(); ++i)
47 if(*i==DIRSEP) ++count;
51 bool Path::is_absolute() const
54 if(is_windows_drive((*this)[0]))
62 Path Path::subpath(unsigned start, unsigned count) const
66 for(unsigned j=0; (j<start && i!=end()); ++j)
68 for(unsigned j=0; (j<count && i!=end()); ++j)
76 Path Path::operator/(const Path &p) const
84 Attaches another path to the end of this one. An absolute path replaces the
85 existing data. ".." elements annihilate the last component and "." elements
88 Path &Path::operator/=(const Path &p)
94 for(Iterator i=p.begin(); i!=p.end(); ++i)
100 string Path::operator[](int n) const
104 for(Iterator i=begin(); i!=end(); ++i, --n)
110 for(Iterator i=end(); i!=begin();)
118 throw InvalidParameterValue("Path component index out of range");
121 bool Path::operator==(const Path &p) const
124 return !strcasecmp(path, p.path);
130 Path::Iterator Path::begin() const
132 return Iterator(*this);
135 Path::Iterator Path::end() const
138 i.start=i.end = std::string::npos;
142 void Path::init(const string &p)
144 string::size_type start = 0;
145 if(p[0]=='/' || p[0]=='\\')
146 add_component(string(1, DIRSEP));
149 string::size_type slash = p.find_first_of("/\\", start);
151 add_component(p.substr(start, slash-start));
152 if(slash==string::npos)
159 Adds a single component to the path, emulating the cd command. Fails horribly
160 if comp contains a separator character.
162 void Path::add_component(const string &comp)
166 else if(comp.size()==1 && comp[0]==DIRSEP)
168 // Replace the path with the root directory
170 unsigned slash = path.find(DIRSEP);
171 if(is_windows_drive(path.substr(0, slash)))
172 path = path.substr(0, 2);
178 else if(is_windows_drive(comp))
183 if(path.empty() || path==".")
185 // .. in root directory is a no-op
186 else if(path.size()==1 && path[0]==DIRSEP)
189 else if(is_windows_drive(path))
194 string::size_type slash = path.rfind(DIRSEP);
195 string::size_type start = (slash==string::npos ? 0 : slash+1);
196 if(!path.compare(start, string::npos, ".."))
198 // If the last component already is a .., add another
202 else if(slash==string::npos)
208 // Otherwise, erase the last component
209 path.erase(slash, string::npos);
213 else if(comp!="." || path.empty())
215 if(comp!="." && path.empty())
217 if(path.size()>1 || (path.size()==1 && path[0]!=DIRSEP))
224 Path::Iterator::Iterator(const Path &p):
228 if(path.path.empty())
229 start=end = string::npos;
230 else if(path.path[0]==DIRSEP)
233 else if(path.path.size()>2 && path.path[2]==DIRSEP && is_windows_drive(path.path.substr(0, 2)))
237 end = path.path.find(DIRSEP);
240 Path::Iterator &Path::Iterator::operator++()
243 if(start>=path.path.size())
245 if(path.path[start]==DIRSEP)
247 end = path.path.find(DIRSEP, start);
251 Path::Iterator &Path::Iterator::operator--()
257 if(end>1 && end<path.path.size() && path.path[end]!=DIRSEP)
260 start = path.path.rfind(DIRSEP, end-1);
261 if(start==string::npos)
269 string Path::Iterator::operator*() const
271 if(start>=path.path.size())
275 return path.path.substr(start, end-start);