X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Fpath.cpp;h=d8a2c71e830f08052d9b45573546ba241a4c4df9;hp=c0b91d6c82f8321df9c31acd1e045bd026a08304;hb=d3c9b9abb9e3c69aeecbca1044b43e7ec83f3b8c;hpb=a17d6ad286e19e2222ab8b6c9a762a83bf2c6c56 diff --git a/source/path.cpp b/source/path.cpp index c0b91d6..d8a2c71 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 @@ -141,12 +141,12 @@ Path::Iterator Path::end() const void Path::init(const string &p) { - unsigned start=0; + string::size_type start=0; if(p[0]=='/' || p[0]=='\\') add_component(string(1, DIRSEP)); while(1) { - unsigned slash=p.find_first_of("/\\", start); + string::size_type slash=p.find_first_of("/\\", start); if(slash>start) add_component(p.substr(start, slash-start)); if(slash==string::npos) @@ -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) @@ -191,23 +191,29 @@ void Path::add_component(const string &comp) #endif else { - unsigned slash=path.rfind(DIRSEP); - unsigned start=(slash==string::npos ? 0 : slash+1); + string::size_type slash=path.rfind(DIRSEP); + string::size_type start=(slash==string::npos ? 0 : slash+1); if(!path.compare(start, string::npos, "..")) { // If the last component already is a .., add another 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;