X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Fpath.cpp;h=5424cb1c79e31936a7628cef52a85f1eb74306d0;hp=c0b91d6c82f8321df9c31acd1e045bd026a08304;hb=9c48f72f02e3112f4193cc8b3f12b135aa483de1;hpb=a2a494043d5cf228cf1b3e5551e3abb486a60191 diff --git a/source/path.cpp b/source/path.cpp index c0b91d6..5424cb1 100644 --- a/source/path.cpp +++ b/source/path.cpp @@ -5,6 +5,7 @@ Copyright © 2006-2008 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ +#include #include #include "path.h" #include "utils.h" @@ -106,16 +107,15 @@ string Path::operator[](int n) const } else { - for(Iterator i=--end();; --i) + for(Iterator i=end(); i!=begin();) { + --i; if(!++n) return *i; - if(i==begin()) - break; } } - return string(); + throw InvalidParameterValue("Path component index out of range"); } bool Path::operator==(const Path &p) const @@ -180,7 +180,7 @@ void Path::add_component(const string &comp) #endif else if(comp=="..") { - if(path.empty()) + if(path.empty() || path==".") path=comp; // .. in root directory is a no-op else if(path.size()==1 && path[0]==DIRSEP) @@ -199,15 +199,21 @@ void Path::add_component(const string &comp) path+=DIRSEP; path+=comp; } + else if(slash==string::npos) + path="."; else + { + if(slash==0) + slash=1; // Otherwise, erase the last component path.erase(slash, string::npos); + } } } else if(comp!="." || path.empty()) { - if(path==".") - path=""; + if(comp!="." && path.empty()) + path="."; if(path.size()>1 || (path.size()==1 && path[0]!=DIRSEP)) path+=DIRSEP; path+=comp;