]> git.tdb.fi Git - libs/core.git/blobdiff - source/utils.cpp
Rename the library as mspfs
[libs/core.git] / source / utils.cpp
index 721fc69249a98f8d7a85a471eb39b6db167bf9a8..ca6d6f346469c6f4f980e601f6339fd9f587083f 100644 (file)
@@ -1,12 +1,13 @@
 /* $Id$
 
-This file is part of libmsppath
-Copyright © 2006-2007  Mikko Rasa, Mikkosoft Productions
+This file is part of libmspfs
+Copyright © 2006-2008  Mikko Rasa, Mikkosoft Productions
 Distributed under the LGPL
 */
 
 #include <dirent.h>
-#include <errno.h>
+#include <cerrno>
+#include <cstdlib>
 #include <msp/core/except.h>
 #ifndef WIN32
 #include <fnmatch.h>
@@ -158,6 +159,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;
@@ -181,7 +190,7 @@ 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);
+       for(; (i!=path.end() && j!=base.end() && *i==*j); ++i, ++j) ;
 
        Path result;
        for(; j!=base.end(); ++j)
@@ -224,4 +233,22 @@ 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
+}
+
+void chdir(const Path &path)
+{
+       if(::chdir(path.str().c_str())==-1)
+               throw SystemError("chdir failed", errno);
+}
+
 } // namespace Msp