add Path::dir_entries()

This commit is contained in:
Josh Holtrop 2014-06-22 15:42:26 -04:00
parent e3cb7b15b4
commit c001fca012
3 changed files with 29 additions and 0 deletions

View File

@ -3,6 +3,7 @@
#include "jes/Ref.h" #include "jes/Ref.h"
#include <string> #include <string>
#include <vector>
namespace jes namespace jes
{ {
@ -15,6 +16,7 @@ namespace jes
Path join(const Path & other); Path join(const Path & other);
const std::string & to_s() { return m_path; } const std::string & to_s() { return m_path; }
bool exists(); bool exists();
std::vector<std::string> dir_entries();
protected: protected:
void clean(); void clean();
std::string m_path; std::string m_path;

View File

@ -2,6 +2,7 @@
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <unistd.h> #include <unistd.h>
#include <dirent.h>
namespace jes namespace jes
{ {
@ -55,6 +56,24 @@ namespace jes
return stat(m_path.c_str(), &st) == 0; return stat(m_path.c_str(), &st) == 0;
} }
std::vector<std::string> Path::dir_entries()
{
std::vector<std::string> 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() void Path::clean()
{ {
for (char & c : m_path) for (char & c : m_path)

View File

@ -1,5 +1,6 @@
#include "gtest/gtest.h" #include "gtest/gtest.h"
#include "jes/Path.h" #include "jes/Path.h"
#include <algorithm>
using namespace jes; using namespace jes;
@ -44,3 +45,10 @@ TEST(PathTest, exists)
EXPECT_TRUE(Path("runtime").exists()); EXPECT_TRUE(Path("runtime").exists());
EXPECT_FALSE(Path("foobar").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());
}