[gnote] Add directory_get_directories and directory_delete



commit 7c5f5a565a65076a8fb754209f4d50585c401d7a
Author: Aurimas Äernius <aurisc4 gmail com>
Date:   Tue Jan 24 22:55:17 2012 +0200

    Add directory_get_directories and directory_delete
    
    Two new functions to sharp/directory.

 src/sharp/directory.cpp |   34 +++++++++++++++++++++++++++++++++-
 src/sharp/directory.hpp |    6 ++++++
 2 files changed, 39 insertions(+), 1 deletions(-)
---
diff --git a/src/sharp/directory.cpp b/src/sharp/directory.cpp
index 072971a..02d821a 100644
--- a/src/sharp/directory.cpp
+++ b/src/sharp/directory.cpp
@@ -1,7 +1,7 @@
 /*
  * gnote
  *
- * Copyright (C) 2011 Aurimas Cernius
+ * Copyright (C) 2011-2012 Aurimas Cernius
  * Copyright (C) 2011 Debarshi Ray
  * Copyright (C) 2009 Hubert Figuiere
  * 
@@ -26,6 +26,7 @@
 
 
 
+#include <glib/gstdio.h>
 #include <glibmm.h>
 
 #include "sharp/directory.hpp"
@@ -59,6 +60,24 @@ namespace sharp {
     }
   }
 
+  void directory_get_directories(const std::string & dir,
+                                 std::list<std::string>  & files)
+  {
+    if(!Glib::file_test(dir, Glib::FILE_TEST_EXISTS | Glib::FILE_TEST_IS_DIR)) {
+      return;
+    }
+
+    Glib::Dir d(dir);
+
+    for(Glib::Dir::iterator iter = d.begin(); iter != d.end(); ++iter) {
+      const std::string file(dir + "/" + *iter);
+
+      if(Glib::file_test(file, Glib::FILE_TEST_IS_DIR)) {
+        files.push_back(file);
+      }
+    }
+  }
+
   void directory_get_files(const std::string & dir, std::list<std::string>  & files)
   {
     directory_get_files_with_ext(dir, "", files);
@@ -112,4 +131,17 @@ namespace sharp {
     return Gio::File::create_for_path(dir)->make_directory_with_parents();
   }
 
+  bool directory_delete(const std::string & dir, bool recursive)
+  {
+    if(!recursive) {
+      std::list<std::string> files;
+      directory_get_files(dir, files);
+      if(files.size()) {
+        return false;
+      }
+    }
+
+    return g_remove(dir.c_str()) == 0;
+  }
+
 }
diff --git a/src/sharp/directory.hpp b/src/sharp/directory.hpp
index 5ab1b3a..545cebc 100644
--- a/src/sharp/directory.hpp
+++ b/src/sharp/directory.hpp
@@ -1,6 +1,7 @@
 /*
  * gnote
  *
+ * Copyright (C) 2012 Aurimas Cernius
  * Copyright (C) 2009 Hubert Figuiere
  * 
  * Permission is hereby granted, free of charge, to any person obtaining a
@@ -45,6 +46,9 @@ namespace sharp {
                                     const std::string & ext,
                                     std::list<std::string>  & files);
 
+  void directory_get_directories(const std::string & dir,
+                                 std::list<std::string>  & files);
+
   void directory_get_files(const std::string & dir, 
                            std::list<std::string>  & files);
 
@@ -60,6 +64,8 @@ namespace sharp {
 
   bool directory_create(const std::string & dir);
 
+  bool directory_delete(const std::string & dir, bool recursive);
+
 }
 
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]