diff --git a/src/filesystem.cc b/src/filesystem.cc index 43357ed..37907ef 100644 --- a/src/filesystem.cc +++ b/src/filesystem.cc @@ -169,3 +169,38 @@ bool filesystem::write(const std::string &path, Glib::RefPtr bu } return false; } + +std::vector filesystem::get_directory_content(const boost::filesystem::path &path, int filter) { + std::vector dirs; + if (boost::filesystem::is_directory(path)) { + for (boost::filesystem::directory_iterator it(path); it != boost::filesystem::directory_iterator(); it++) { + if (filter & DIRS && boost::filesystem::is_directory(it->path())) + dirs.emplace_back(it->path()); + if (filter & FILES && boost::filesystem::is_regular_file(it->path())) + dirs.emplace_back(it->path()); + } + } + if (filter & SORTED) + std::sort(dirs.begin(), dirs.end()); + return dirs; +}; + +std::vector filesystem::dirs(const boost::filesystem::path &path) { + return filesystem::get_directory_content(path, DIRS | SORTED); +} + +std::vector filesystem::files(const boost::filesystem::path &path) { + return filesystem::get_directory_content(path, FILES | SORTED); +} + +std::vector filesystem::contents(const boost::filesystem::path &path) { + return filesystem::get_directory_content(path, ALL | SORTED); +} + +std::vector filesystem::seperate_contents(const boost::filesystem::path &path) { + auto a = dirs(path); + auto b = files(path); + a.insert(a.end(), b.begin(), b.end()); + return a; +} + diff --git a/src/filesystem.h b/src/filesystem.h index a417825..4472a12 100644 --- a/src/filesystem.h +++ b/src/filesystem.h @@ -5,13 +5,12 @@ #include #include -namespace juci { - class filesystem { - public: - static std::string read(const std::string &path); - static std::string read(const boost::filesystem::path &path) { return read(path.string()); } - static int read(const std::string &path, Glib::RefPtr text_buffer); - static int read(const boost::filesystem::path &path, Glib::RefPtr text_buffer) { return read(path.string(), text_buffer); } +enum filesystem_options { + DIRS = 1, + FILES = 2, + ALL = DIRS | FILES, + SORTED = 4 +}; class filesystem { public: @@ -32,6 +31,11 @@ public: static bool write(const boost::filesystem::path &path) { return write(path, ""); }; static bool write(const std::string &path, Glib::RefPtr text_buffer); static bool write(const boost::filesystem::path &path, Glib::RefPtr text_buffer) { return write(path.string(), text_buffer); } + static std::vector get_directory_content(const boost::filesystem::path &path, int filter); + static std::vector dirs(const boost::filesystem::path &path); + static std::vector files(const boost::filesystem::path &path); + static std::vector contents(const boost::filesystem::path &path); + static std::vector seperate_contents(const boost::filesystem::path &path); static std::string get_home_folder(); static std::string get_tmp_folder();