]> git.tdb.fi Git - libs/core.git/blobdiff - source/utils.cpp
strutils.h -> strings/utils.h
[libs/core.git] / source / utils.cpp
index 08ab9586a31700ed9a1399b35187caba5472f37c..5c7d6a230803bcaf47c08b9e43b21aaeb2e55ae6 100644 (file)
@@ -9,7 +9,7 @@ Distributed under the LGPL
 #ifndef WIN32
 #include <fnmatch.h>
 #endif
-#include <msp/strutils.h>
+#include <msp/strings/utils.h>
 #include "path.h"
 #include "utils.h"
 
@@ -55,6 +55,14 @@ Path fix_case(const Path &path)
        return result;
 }
 
+/**
+Creates the given directory and any parent directories if needed.
+
+@param   path  The path to create
+@param   mode  Access mode for new directories
+
+@return  0 on success, -1 on error
+*/
 int mkpath(const Path &path, int mode)
 {
        Path p;
@@ -158,5 +166,33 @@ int fnmatch(const string &pat, const Path &fn)
 #endif
 }
 
+Path relative(const Path &path, const Path &base)
+{
+       Path::iterator i=path.begin();
+       Path::iterator j=base.begin();
+       for(; (i!=path.end() && j!=base.end() && *i==*j); ++i,++j);
+
+       Path result;
+       for(; j!=base.end(); ++j)
+               result/="..";
+       for(; i!=path.end(); ++i)
+               result/=*i;
+
+       return result;
+}
+
+/**
+Extracts the basename from the given path.  Same thing as Path::Path(p)[-1],
+but faster.
+*/
+string basename(const std::string &p)
+{
+       unsigned slash=p.rfind(DIRCHAR);
+       if(slash==string::npos)
+               return p;
+       else
+               return p.substr(slash+1);
+}
+
 } // namespace Path
 } // namespace Msp