From: Mikko Rasa Date: Wed, 27 Feb 2008 10:00:55 +0000 (+0000) Subject: Add is_dir and get_home_dir functions X-Git-Tag: fs-1.0~9 X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=commitdiff_plain;h=3004842aa3281f26137c7332089068da7edb34af Add is_dir and get_home_dir functions --- diff --git a/source/utils.cpp b/source/utils.cpp index 721fc69..18784b3 100644 --- a/source/utils.cpp +++ b/source/utils.cpp @@ -158,6 +158,14 @@ bool exists(const Path &path) return access(path.str().c_str(), F_OK)==0; } +bool is_dir(const Path &path) +{ + struct stat st; + if(stat(path, st)==0) + return S_ISDIR(st.st_mode); + return false; +} + Filename splitext(const string &fn) { Filename result; @@ -224,4 +232,16 @@ Path getcwd() return ::getcwd(buf, sizeof(buf)); } +Path get_home_dir() +{ +#ifndef WIN32 + const char *home=getenv("HOME"); + if(home) + return home; + return "."; +#else + return "."; +#endif +} + } // namespace Msp diff --git a/source/utils.h b/source/utils.h index 47efcac..db6a32c 100644 --- a/source/utils.h +++ b/source/utils.h @@ -53,6 +53,9 @@ std::string basename(const std::string &); /// Tests for existence of a file bool exists(const Path &path); +/// Tests whether a path refers to an existing directory +bool is_dir(const Path &paht); + /** Gets information about a file. Returns 0 on success or -1 on error. This version can be used to check for file existence and get information in one @@ -68,6 +71,9 @@ struct stat stat(const Path &fn); Path getcwd(); +/// Returns the user's home directory +Path get_home_dir(); + inline bool is_windows_drive(const std::string &p) { return (p.size()==2 && ((p[0]>='A' && p[0]<='Z') || (p[0]>='a' && p[0]<='z')) && p[1]==':'); }