X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;ds=sidebyside;f=source%2Ffs%2Fpath.cpp;h=0fe95d65df943a9f0b4b1b4bf006035a4e7dccee;hb=497eaf090b1665511c795d5764c06a2bcd4b90b6;hp=93805277749d96ed35cccaa86ecd130774f9174e;hpb=05f7e9b4c6f804102f261517c8ecdd338e1f67d3;p=libs%2Fcore.git
diff --git a/source/fs/path.cpp b/source/fs/path.cpp
index 9380527..0fe95d6 100644
--- a/source/fs/path.cpp
+++ b/source/fs/path.cpp
@@ -32,14 +32,14 @@ Path::Path(const char *p)
void Path::init(const string &p)
{
+ if(p.empty())
+ return;
string::size_type start = 0;
- if(p[0]=='/' || p[0]=='\\')
- add_component(string(1, DIRSEP));
- while(1)
+ while(start
start)
- add_component(p.substr(start, slash-start));
+ if(slash>start || start==0)
+ add_component(p.substr(start, max(slash-start, 1U)));
if(slash==string::npos)
break;
start = slash+1;
@@ -89,7 +89,7 @@ Path Path::operator/(const Path &p) const
Path &Path::operator/=(const Path &p)
{
if(p.is_absolute())
- path = p.path;
+ *this = p;
else
{
for(Iterator i=p.begin(); i!=p.end(); ++i)
@@ -100,9 +100,7 @@ Path &Path::operator/=(const Path &p)
void Path::add_component(const string &comp)
{
- if(comp.empty())
- ;
- else if(comp.size()==1 && comp[0]==DIRSEP)
+ if(comp.size()==1 && (comp[0]=='/' || comp[0]=='\\'))
{
// Replace the path with the root directory
#ifdef WIN32
@@ -115,7 +113,7 @@ void Path::add_component(const string &comp)
else
#endif
{
- path = comp;
+ path.assign(1, DIRSEP);
separators.clear();
separators.push_back(0);
}
@@ -224,7 +222,9 @@ Path::Iterator::Iterator(const Path &p, bool e):
path(&p),
iter(e ? path->separators.end() : path->separators.begin()),
end(e || path->path.empty())
-{ }
+{
+ update();
+}
Path::Iterator &Path::Iterator::operator++()
{
@@ -236,6 +236,7 @@ Path::Iterator &Path::Iterator::operator++()
if(path->path.size()==1 && path->separators.size()==1)
end = true;
}
+ update();
return *this;
}
@@ -249,13 +250,17 @@ Path::Iterator &Path::Iterator::operator--()
}
else if(iter!=path->separators.begin())
--iter;
+ update();
return *this;
}
-string Path::Iterator::operator*() const
+void Path::Iterator::update()
{
if(end)
- throw logic_error("Path::Iterator::operator*");
+ {
+ current.clear();
+ return;
+ }
string::size_type start = 0;
if(iter!=path->separators.begin())
@@ -269,8 +274,9 @@ string Path::Iterator::operator*() const
slash = *iter;
if(slash==0)
- return path->path.substr(start, 1);
- return path->path.substr(start, slash-start);
+ current = path->path.substr(start, 1);
+ else
+ current = path->path.substr(start, slash-start);
}
} // namespace FS