]> git.tdb.fi Git - libs/core.git/blobdiff - source/utils.cpp
globcasematch takes a std::string, not Path
[libs/core.git] / source / utils.cpp
index 08ab9586a31700ed9a1399b35187caba5472f37c..31ab53be7b2ed210cf66dd9f2996972f0d33b25f 100644 (file)
@@ -8,8 +8,10 @@ Distributed under the LGPL
 #include <errno.h>
 #ifndef WIN32
 #include <fnmatch.h>
+#else
+#include <msp/strings/glob.h>
 #endif
-#include <msp/strutils.h>
+#include <msp/strings/utils.h>
 #include "path.h"
 #include "utils.h"
 
@@ -55,6 +57,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;
@@ -81,6 +91,7 @@ int mkpath(const Path &path, int mode)
                {
 #ifdef WIN32
                        // The win32 version of this function doesn't take the mode argument.  Go figure.
+                       (void)mode;
                        err=mkdir(p.str().c_str());
 #else
                        err=mkdir(p.str().c_str(),mode);
@@ -125,7 +136,8 @@ list<string> list_files(const Path &path)
                while(dirent *de=readdir(dir))
                {
                        const char *fn=de->d_name;
-                       if(fn[0]=='.' && (fn[1]==0 || (fn[1]=='.' && fn[2]==0))) continue;
+                       if(fn[0]=='.' && (fn[1]==0 || (fn[1]=='.' && fn[2]==0))) 
+                               continue;
                        result.push_back(fn);
                }
                closedir(dir);
@@ -136,14 +148,14 @@ list<string> list_files(const Path &path)
 bool exists(const Path &path)
 {
        struct stat st;
-       return !stat(path.str().c_str(),&st);
+       return !stat(path.str().c_str(), &st);
 }
 
 Filename splitext(const string &fn)
 {
        Filename result;
        unsigned dot=fn.rfind('.');
-       result.base=fn.substr(0,dot);
+       result.base=fn.substr(0, dot);
        if(dot!=string::npos)
                result.ext=fn.substr(dot);
        return result;
@@ -152,11 +164,39 @@ Filename splitext(const string &fn)
 int fnmatch(const string &pat, const Path &fn)
 {
 #ifdef WIN32
-       return -1;
+       return globcasematch(pat, fn.str());
 #else
        return ::fnmatch(pat.c_str(), fn.str().c_str(), FNM_PATHNAME);
 #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