From c001fca0122365e06449a74bcef57156ccab7827 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Sun, 22 Jun 2014 15:42:26 -0400 Subject: [PATCH] add Path::dir_entries() --- src/lib/include/jes/Path.h | 2 ++ src/lib/src/Path.cc | 19 +++++++++++++++++++ test/src/test_Path.cc | 8 ++++++++ 3 files changed, 29 insertions(+) diff --git a/src/lib/include/jes/Path.h b/src/lib/include/jes/Path.h index b21bdf8..9631b3d 100644 --- a/src/lib/include/jes/Path.h +++ b/src/lib/include/jes/Path.h @@ -3,6 +3,7 @@ #include "jes/Ref.h" #include +#include namespace jes { @@ -15,6 +16,7 @@ namespace jes Path join(const Path & other); const std::string & to_s() { return m_path; } bool exists(); + std::vector dir_entries(); protected: void clean(); std::string m_path; diff --git a/src/lib/src/Path.cc b/src/lib/src/Path.cc index 6f19aa7..82d5780 100644 --- a/src/lib/src/Path.cc +++ b/src/lib/src/Path.cc @@ -2,6 +2,7 @@ #include #include #include +#include namespace jes { @@ -55,6 +56,24 @@ namespace jes return stat(m_path.c_str(), &st) == 0; } + std::vector Path::dir_entries() + { + std::vector rv; + DIR * dir = opendir(m_path.c_str()); + if (dir != NULL) + { + for (;;) + { + struct dirent * de = readdir(dir); + if (de == NULL) + break; + rv.push_back(std::string(de->d_name)); + } + closedir(dir); + } + return rv; + } + void Path::clean() { for (char & c : m_path) diff --git a/test/src/test_Path.cc b/test/src/test_Path.cc index 1dddfc5..cf5efa4 100644 --- a/test/src/test_Path.cc +++ b/test/src/test_Path.cc @@ -1,5 +1,6 @@ #include "gtest/gtest.h" #include "jes/Path.h" +#include using namespace jes; @@ -44,3 +45,10 @@ TEST(PathTest, exists) EXPECT_TRUE(Path("runtime").exists()); EXPECT_FALSE(Path("foobar").exists()); } + +TEST(PathTest, dir_entries) +{ + auto dir_entries = Path(".").dir_entries(); + EXPECT_TRUE(std::find(dir_entries.begin(), dir_entries.end(), "runtime") != dir_entries.end()); + EXPECT_FALSE(std::find(dir_entries.begin(), dir_entries.end(), "foobar") != dir_entries.end()); +}