[gnote] Add directory_get_files operating on Gio::File



commit 268008048a3e299f32e06bd1ebbb7d23495f676d
Author: Aurimas Černius <aurisc4 gmail com>
Date:   Mon Jul 30 22:55:11 2018 +0300

    Add directory_get_files operating on Gio::File

 src/sharp/directory.cpp          | 41 ++++++++++++++++++
 src/sharp/directory.hpp          |  5 +++
 src/test/unit/directorytests.cpp | 90 ++++++++++++++++++++++++++++++++++++----
 3 files changed, 128 insertions(+), 8 deletions(-)
---
diff --git a/src/sharp/directory.cpp b/src/sharp/directory.cpp
index 721f5434..4f23aa2b 100644
--- a/src/sharp/directory.cpp
+++ b/src/sharp/directory.cpp
@@ -61,6 +61,41 @@ namespace sharp {
     }
   }
 
+  void directory_get_files_with_ext(const Glib::RefPtr<Gio::File> & dir,
+                                    const Glib::ustring & ext,
+                                    std::vector<Glib::RefPtr<Gio::File>> & files)
+  {
+    if(!dir || !dir->query_exists()) {
+      return;
+    }
+    auto info = dir->query_info();
+    if(!info || info->get_file_type() != Gio::FILE_TYPE_DIRECTORY) {
+      return;
+    }
+
+    auto children = dir->enumerate_children();
+    while(true) {
+      auto fileinfo = children->next_file();
+      if(!fileinfo) {
+        break;
+      }
+      if(fileinfo->get_file_type() & Gio::FILE_TYPE_REGULAR) {
+        if(ext.size()) {
+          Glib::ustring name = fileinfo->get_name();
+          auto pos = name.find_last_of('.');
+          if(pos != Glib::ustring::npos && name.substr(pos) == ext) {
+            auto child = Gio::File::create_for_uri(Glib::build_filename(dir->get_uri(), name));
+            files.push_back(child);
+          }
+        }
+        else {
+          auto child = Gio::File::create_for_uri(Glib::build_filename(dir->get_uri(), fileinfo->get_name()));
+          files.push_back(child);
+        }
+      }
+    }
+  }
+
   void directory_get_directories(const Glib::ustring & dir,
                                  std::list<Glib::ustring> & files)
   {
@@ -108,6 +143,12 @@ namespace sharp {
     directory_get_files_with_ext(dir, "", files);
   }
 
+  void directory_get_files(const Glib::RefPtr<Gio::File> & dir,
+                           std::vector<Glib::RefPtr<Gio::File>> & files)
+  {
+    directory_get_files_with_ext(dir, "", files);
+  }
+
   bool directory_exists(const Glib::ustring & dir)
   {
     return Glib::file_test(dir, Glib::FILE_TEST_EXISTS) && Glib::file_test(dir, Glib::FILE_TEST_IS_DIR);
diff --git a/src/sharp/directory.hpp b/src/sharp/directory.hpp
index ce94356b..8562dd70 100644
--- a/src/sharp/directory.hpp
+++ b/src/sharp/directory.hpp
@@ -44,6 +44,9 @@ namespace sharp {
   void directory_get_files_with_ext(const Glib::ustring & dir,
                                     const Glib::ustring & ext,
                                     std::list<Glib::ustring> & files);
+  void directory_get_files_with_ext(const Glib::RefPtr<Gio::File> & dir,
+                                    const Glib::ustring & ext,
+                                    std::vector<Glib::RefPtr<Gio::File>> & files);
 
   void directory_get_directories(const Glib::ustring & dir,
                                  std::list<Glib::ustring> & files);
@@ -52,6 +55,8 @@ namespace sharp {
 
   void directory_get_files(const Glib::ustring & dir,
                            std::list<Glib::ustring> & files);
+  void directory_get_files(const Glib::RefPtr<Gio::File> & dir,
+                           std::vector<Glib::RefPtr<Gio::File>> & files);
 
   bool directory_exists(const Glib::ustring & dir);
 
diff --git a/src/test/unit/directorytests.cpp b/src/test/unit/directorytests.cpp
index b3e55100..01dce2a5 100644
--- a/src/test/unit/directorytests.cpp
+++ b/src/test/unit/directorytests.cpp
@@ -99,6 +99,20 @@ SUITE(directory)
     CHECK_EQUAL(1, directories.size());
   }
 
+  void remove_matching_files(const std::vector<Glib::RefPtr<Gio::File>> & dirsf,
+      std::list<Glib::ustring> & dirss)
+  {
+    for(auto f : dirsf) {
+      auto name = Glib::path_get_basename(f->get_path());
+      for(auto iter = dirss.begin(); iter != dirss.end(); ++iter) {
+        if(name == Glib::path_get_basename(*iter)) {
+          dirss.erase(iter);
+          break;
+        }
+      }
+    }
+  }
+
   TEST(get_directories__same_return)
   {
     auto dir = Glib::path_get_dirname(Glib::path_get_dirname(__FILE__));
@@ -113,16 +127,76 @@ SUITE(directory)
 
     CHECK(0 < dirss.size());
     CHECK_EQUAL(dirss.size(), dirsf.size());
-    for(auto f : dirsf) {
-      auto name = Glib::path_get_basename(f->get_path());
-      for(auto iter = dirss.begin(); iter != dirss.end(); ++iter) {
-        if(name == Glib::path_get_basename(*iter)) {
-          dirss.erase(iter);
-          break;
-        }
+    remove_matching_files(dirsf, dirss);
+    CHECK_EQUAL(0, dirss.size());
+  }
+
+  TEST(directory_get_files_with_ext__ustr__non_existent_dir)
+  {
+    Glib::ustring dir = Glib::build_filename(Glib::path_get_dirname(__FILE__), "nonexistent");
+
+    std::list<Glib::ustring> files;
+    sharp::directory_get_files_with_ext(dir, "", files);
+    CHECK_EQUAL(0, files.size());
+  }
+
+  TEST(directory_get_files_with_ext__File__non_existent_dir)
+  {
+    auto dir = Gio::File::create_for_path(Glib::build_filename(Glib::path_get_dirname(__FILE__), 
"nonexistent"));
+
+    std::vector<Glib::RefPtr<Gio::File>> files;
+    sharp::directory_get_files_with_ext(dir, "", files);
+    CHECK_EQUAL(0, files.size());
+  }
+
+  TEST(directory_get_files_with_ext__ustr__regular_file)
+  {
+    Glib::ustring dir(__FILE__);
+
+    std::list<Glib::ustring> files;
+    sharp::directory_get_files_with_ext(dir, "", files);
+    CHECK_EQUAL(0, files.size());
+  }
+
+  TEST(directory_get_files_with_ext__File__regular_file)
+  {
+    auto dir = Gio::File::create_for_path(__FILE__);
+
+    std::vector<Glib::RefPtr<Gio::File>> files;
+    sharp::directory_get_files_with_ext(dir, "", files);
+    CHECK_EQUAL(0, files.size());
+  }
+
+  void directory_get_files_with_ext__same_return_test(const Glib::ustring & ext)
+  {
+    Glib::ustring dir = Glib::path_get_dirname(__FILE__);
+
+    std::list<Glib::ustring> filess;
+    sharp::directory_get_files_with_ext(dir, ext, filess);
+    CHECK(0 < filess.size());
+    if(ext.size()) {
+      for(auto f : filess) {
+        auto pos = f.find_last_of('.');
+        CHECK_EQUAL(ext, f.substr(pos));
       }
     }
-    CHECK_EQUAL(0, dirss.size());
+
+    auto file = Gio::File::create_for_path(dir);
+    std::vector<Glib::RefPtr<Gio::File>> filesf;
+    sharp::directory_get_files_with_ext(file, ext, filesf);
+    CHECK_EQUAL(filess.size(), filesf.size());
+    remove_matching_files(filesf, filess);
+    CHECK_EQUAL(0, filess.size());
+  }
+
+  TEST(directory_get_files_with_ext__same_return)
+  {
+    directory_get_files_with_ext__same_return_test("");
+  }
+
+  TEST(directory_get_files_with_ext__same_return_filterred)
+  {
+    directory_get_files_with_ext__same_return_test(".cpp");
   }
 }
 


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