From: Mikko Rasa Date: Tue, 29 Aug 2006 11:17:11 +0000 (+0000) Subject: Add basename function X-Git-Tag: fs-1.0~20 X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=e48e54c5690d086c3f3ffb6f34f424f253bfb6b1;p=libs%2Fcore.git Add basename function --- diff --git a/source/utils.cpp b/source/utils.cpp index 204f33b..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; @@ -173,5 +181,18 @@ Path relative(const Path &path, const Path &base) 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 diff --git a/source/utils.h b/source/utils.h index faf5ef7..2b2f23c 100644 --- a/source/utils.h +++ b/source/utils.h @@ -29,6 +29,7 @@ extern bool exists(const Path &); extern Filename splitext(const std::string &); extern int fnmatch(const std::string &, const Path &); extern Path relative(const Path &, const Path &); +extern std::string basename(const std::string &); inline int stat(const Path &fn, struct stat &st) { return ::stat(fn.str().c_str(), &st); }