add Path::dir_entries()
This commit is contained in:
parent
e3cb7b15b4
commit
c001fca012
@ -3,6 +3,7 @@
|
||||
|
||||
#include "jes/Ref.h"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
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<std::string> dir_entries();
|
||||
protected:
|
||||
void clean();
|
||||
std::string m_path;
|
||||
|
@ -2,6 +2,7 @@
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#include <dirent.h>
|
||||
|
||||
namespace jes
|
||||
{
|
||||
@ -55,6 +56,24 @@ namespace jes
|
||||
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()
|
||||
{
|
||||
for (char & c : m_path)
|
||||
|
@ -1,5 +1,6 @@
|
||||
#include "gtest/gtest.h"
|
||||
#include "jes/Path.h"
|
||||
#include <algorithm>
|
||||
|
||||
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());
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user