3 This file is part of libmspfs
4 Copyright © 2006-2008 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
8 #include <msp/strings/utils.h>
16 inline bool is_windows_drive(const std::string &p)
17 { return (p.size()==2 && ((p[0]>='A' && p[0]<='Z') || (p[0]>='a' && p[0]<='z')) && p[1]==':'); }
27 Path::Path(const string &p)
32 Path::Path(const char *p)
37 unsigned Path::size() const
41 if(path.size()==1 && path[0]==DIRSEP)
45 for(string::const_iterator i=path.begin(); i!=path.end(); ++i)
46 if(*i==DIRSEP) ++count;
50 bool Path::is_absolute() const
53 if(is_windows_drive((*this)[0]))
61 Path Path::subpath(unsigned start, unsigned count) const
65 for(unsigned j=0; (j<start && i!=end()); ++j)
67 for(unsigned j=0; (j<count && i!=end()); ++j)
75 Path Path::operator/(const Path &p) const
83 Attaches another path to the end of this one. An absolute path replaces the
84 existing data. ".." elements annihilate the last component and "." elements
87 Path &Path::operator/=(const Path &p)
93 for(Iterator i=p.begin(); i!=p.end(); ++i)
99 string Path::operator[](int n) const
103 for(Iterator i=begin(); i!=end(); ++i, --n)
109 for(Iterator i=--end();; --i)
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)
145 if(p[0]=='/' || p[0]=='\\')
146 add_component(string(1, DIRSEP));
149 unsigned 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))
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 unsigned slash=path.rfind(DIRSEP);
195 unsigned start=(slash==string::npos ? 0 : slash+1);
196 if(!path.compare(start, string::npos, ".."))
198 // If the last component already is a .., add another
203 // Otherwise, erase the last component
204 path.erase(slash, string::npos);
207 else if(comp!="." || path.empty())
211 if(path.size()>1 || (path.size()==1 && path[0]!=DIRSEP))
218 Path::Iterator::Iterator(const Path &p):
222 if(path.path.empty())
223 start=end=string::npos;
224 else if(path.path[0]==DIRSEP)
227 else if(path.path.size()>2 && path.path[2]==DIRSEP && is_windows_drive(path.path.substr(0, 2)))
231 end=path.path.find(DIRSEP);
234 Path::Iterator &Path::Iterator::operator++()
237 if(start>=path.path.size())
239 if(path.path[start]==DIRSEP)
241 end=path.path.find(DIRSEP, start);
245 Path::Iterator &Path::Iterator::operator--()
251 if(end>1 && end<path.path.size() && path.path[end]!=DIRSEP)
254 start=path.path.rfind(DIRSEP, end-1);
255 if(start==string::npos)
263 string Path::Iterator::operator*() const
265 if(start>=path.path.size())
269 return path.path.substr(start, end-start);