X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Futils.cpp;h=af8ea8a37008eca04ce6d5e5403e53f09b1fdea3;hb=e48e54c5690d086c3f3ffb6f34f424f253bfb6b1;hp=bae91cc50898ad83838749f58987a9c3a182d61a;hpb=6f4ac56492d45c04df0f205252b3c3210e319b4f;p=libs%2Fcore.git diff --git a/source/utils.cpp b/source/utils.cpp index bae91cc..af8ea8a 100644 --- a/source/utils.cpp +++ b/source/utils.cpp @@ -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; @@ -149,12 +157,42 @@ Filename splitext(const string &fn) return result; } -#ifndef WIN32 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 } // namespace Msp