X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Futils.cpp;h=45cd614b150ccaaea463f3c9860dabbe3bc82e1d;hb=da904316fb2ed4e2d084726e20f514f6faf956f7;hp=f5fb3abb846d4b6e126346c91b9164e1fa6200a3;hpb=66bdaa1f9f833e18a07d061208ce6080f3abac06;p=libs%2Fcore.git diff --git a/source/utils.cpp b/source/utils.cpp index f5fb3ab..45cd614 100644 --- a/source/utils.cpp +++ b/source/utils.cpp @@ -6,7 +6,9 @@ Distributed under the LGPL #include #include #include +#ifndef WIN32 #include +#endif #include #include "path.h" #include "utils.h" @@ -53,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; @@ -149,7 +159,39 @@ Filename splitext(const string &fn) int fnmatch(const string &pat, const Path &fn) { +#ifdef WIN32 + return -1; +#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