Move file utility function into pbd/file_utils.h and into PBD namespace
[ardour.git] / libs / pbd / filesystem.cc
index 229b22fcb54ed6e61f1bb6835d9d48f8ee3974fb..a154ed8b8eb70dbd454fc884c94312e11e52e096 100644 (file)
 #include <glib.h>
 #include <glib/gstdio.h>
 
+#include <giomm/file.h>
+
 #include <cerrno>
+#include <fstream>
 
 #include <glibmm/fileutils.h>
 #include <glibmm/miscutils.h>
 
-#include <pbd/filesystem.h>
-#include <pbd/error.h>
+#include "pbd/filesystem.h"
+#include "pbd/error.h"
+#include "pbd/compose.h"
+#include "pbd/pathscanner.h"
+
+#include "i18n.h"
+
+using namespace std;
 
 namespace PBD {
 
@@ -54,6 +63,30 @@ path::operator/=(const char* rhs)
        return *this;
 }
 
+string
+path::leaf () const
+{
+       return Glib::path_get_basename(m_path);
+}
+
+path
+path::branch_path () const
+{
+       string dir = Glib::path_get_dirname (m_path);
+
+       /*
+        * glib returns "." to signify that the path
+        * has no directory components(branch path)
+        * whereas boost::filesystem returns an empty
+        * string
+        */
+       if(dir == ".")
+       {
+               return "";
+       }
+       return dir;
+}
+
 bool
 exists (const path & p)
 {
@@ -94,11 +127,57 @@ create_directories(const path & p)
        return true;
 }
 
+bool
+remove(const path & p)
+{
+       if(!exists(p)) return false;
+
+       int error = g_unlink (p.to_string().c_str());
+
+       if(error == -1)
+       {
+               throw filesystem_error(g_strerror(errno), errno);
+       }
+       return true;
+}
+
+void
+rename (const path & from_path, const path & to_path)
+{
+       // g_rename is a macro that evaluates to ::rename on
+       // POSIX systems, without the global namespace qualifier
+       // it would evaluate to a recursive call(if it compiled)
+       if ( ::g_rename( from_path.to_string().c_str(),
+                               to_path.to_string().c_str() ) == -1 )
+       {
+               throw filesystem_error(g_strerror(errno), errno);
+       }
+}
+       
 string
 basename (const path & p)
 {
-       // I'm not sure if this works quite the same as boost::filesystem::basename
-       return Glib::path_get_basename (p.to_string ());
+       string base(p.leaf());
+
+       string::size_type n = base.rfind ('.');
+
+       return base.substr (0, n);
+}
+
+string
+extension (const path & p)
+{
+       string base(p.leaf());
+
+       string::size_type n = base.rfind ('.');
+
+       if (n != string::npos)
+       {
+               return base.substr(n);
+       }
+
+       return string();
+
 }
 
 } // namespace sys