]> git.tdb.fi Git - libs/core.git/blobdiff - source/path.cpp
Style update: spaces around assignments
[libs/core.git] / source / path.cpp
index c0b91d6c82f8321df9c31acd1e045bd026a08304..e514d36bfe490f5a4349f9a46946847221caa5f5 100644 (file)
@@ -5,6 +5,7 @@ Copyright © 2006-2008  Mikko Rasa, Mikkosoft Productions
 Distributed under the LGPL
 */
 
+#include <msp/core/except.h>
 #include <msp/strings/utils.h>
 #include "path.h"
 #include "utils.h"
@@ -41,7 +42,7 @@ unsigned Path::size() const
        if(path.size()==1 && path[0]==DIRSEP)
                return 1;
 
-       unsigned count=1;
+       unsigned count = 1;
        for(string::const_iterator i=path.begin(); i!=path.end(); ++i)
                if(*i==DIRSEP) ++count;
        return count;
@@ -61,12 +62,12 @@ bool Path::is_absolute() const
 Path Path::subpath(unsigned start, unsigned count) const
 {
        Path result;
-       Iterator i=begin();
+       Iterator i = begin();
        for(unsigned j=0; (j<start && i!=end()); ++j)
                ++i;
        for(unsigned j=0; (j<count && i!=end()); ++j)
        {
-               result/=*i;
+               result /= *i;
                ++i;
        }
        return result;
@@ -74,8 +75,8 @@ Path Path::subpath(unsigned start, unsigned count) const
 
 Path Path::operator/(const Path &p) const
 {
-       Path a=*this;
-       a/=p;
+       Path a = *this;
+       a /= p;
        return a;
 }
 
@@ -87,7 +88,7 @@ are ignored.
 Path &Path::operator/=(const Path &p)
 {
        if(p.is_absolute())
-               path=p.path;
+               path = p.path;
        else
        {
                for(Iterator i=p.begin(); i!=p.end(); ++i)
@@ -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
@@ -135,23 +135,23 @@ Path::Iterator Path::begin() const
 Path::Iterator Path::end() const
 {
        Iterator i(*this);
-       i.start=i.end=std::string::npos;
+       i.start=i.end = std::string::npos;
        return i;
 }
 
 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)
                        break;
-               start=slash+1;
+               start = slash+1;
        }
 }
 
@@ -167,21 +167,21 @@ void Path::add_component(const string &comp)
        {
                // Replace the path with the root directory
 #ifdef WIN32
-               unsigned slash=path.find(DIRSEP);
+               unsigned slash = path.find(DIRSEP);
                if(is_windows_drive(path.substr(0, slash)))
-                       path=path.substr(0, 2);
+                       path = path.substr(0, 2);
                else
 #endif
-               path=comp;
+               path = comp;
        }
 #ifdef WIN32
        else if(is_windows_drive(comp))
-               path=comp;
+               path = comp;
 #endif
        else if(comp=="..")
        {
-               if(path.empty())
-                       path=comp;
+               if(path.empty() || path==".")
+                       path = comp;
                // .. in root directory is a no-op
                else if(path.size()==1 && path[0]==DIRSEP)
                        ;
@@ -191,26 +191,32 @@ 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;
+                               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;
+                       path += DIRSEP;
+               path += comp;
        }
 }
 
@@ -220,25 +226,25 @@ Path::Iterator::Iterator(const Path &p):
        start(0)
 {
        if(path.path.empty())
-               start=end=string::npos;
+               start=end = string::npos;
        else if(path.path[0]==DIRSEP)
-               end=1;
+               end = 1;
 #ifdef WIN32
        else if(path.path.size()>2 && path.path[2]==DIRSEP && is_windows_drive(path.path.substr(0, 2)))
-               end=2;
+               end = 2;
 #endif
        else
-               end=path.path.find(DIRSEP);
+               end = path.path.find(DIRSEP);
 }
 
 Path::Iterator &Path::Iterator::operator++()
 {
-       start=end;
+       start = end;
        if(start>=path.path.size())
                return *this;
        if(path.path[start]==DIRSEP)
                ++start;
-       end=path.path.find(DIRSEP, start);
+       end = path.path.find(DIRSEP, start);
        return *this;
 }
 
@@ -247,13 +253,13 @@ Path::Iterator &Path::Iterator::operator--()
        if(start==0)
                return *this;
 
-       end=start;
+       end = start;
        if(end>1 && end<path.path.size() && path.path[end]!=DIRSEP)
                --end;
 
-       start=path.path.rfind(DIRSEP, end-1);
+       start = path.path.rfind(DIRSEP, end-1);
        if(start==string::npos)
-               start=0;
+               start = 0;
        else if(start<end-1)
                ++start;