add Path::is_file() and Path::is_dir()
This commit is contained in:
parent
1f9a981e6a
commit
2012591b27
@ -1,4 +1,7 @@
|
|||||||
#include "Path.h"
|
#include "Path.h"
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
std::string Path::dirname(const std::string & s)
|
std::string Path::dirname(const std::string & s)
|
||||||
{
|
{
|
||||||
@ -31,3 +34,23 @@ std::string Path::join(const std::string & first, const std::string & second)
|
|||||||
}
|
}
|
||||||
return first + "/" + second;
|
return first + "/" + second;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool Path::is_file(const std::string & s)
|
||||||
|
{
|
||||||
|
struct stat st;
|
||||||
|
if (stat(s.c_str(), &st) != 0)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return S_ISREG(st.st_mode);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Path::is_dir(const std::string & s)
|
||||||
|
{
|
||||||
|
struct stat st;
|
||||||
|
if (stat(s.c_str(), &st) != 0)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return S_ISDIR(st.st_mode);
|
||||||
|
}
|
||||||
|
@ -8,6 +8,8 @@ class Path
|
|||||||
public:
|
public:
|
||||||
static std::string dirname(const std::string & s);
|
static std::string dirname(const std::string & s);
|
||||||
static std::string join(const std::string & first, const std::string & second);
|
static std::string join(const std::string & first, const std::string & second);
|
||||||
|
static bool is_file(const std::string & s);
|
||||||
|
static bool is_dir(const std::string & s);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -61,3 +61,35 @@ TEST(Path_join, does_not_add_extra_slash_when_first_path_already_ends_with_one)
|
|||||||
{
|
{
|
||||||
EXPECT_EQ("/var/run", Path::join("/var/", "run"));
|
EXPECT_EQ("/var/run", Path::join("/var/", "run"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
TEST(Path_is_file, returns_true_for_file)
|
||||||
|
{
|
||||||
|
EXPECT_TRUE(Path::is_file("test/src/test_Path.cc"));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(Path_is_file, returns_false_for_directory)
|
||||||
|
{
|
||||||
|
EXPECT_FALSE(Path::is_file("test"));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(Path_is_file, returns_false_for_nonexistent_path)
|
||||||
|
{
|
||||||
|
EXPECT_FALSE(Path::is_file("non.existent"));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
TEST(Path_is_dir, returns_true_for_directory)
|
||||||
|
{
|
||||||
|
EXPECT_TRUE(Path::is_dir("test/src"));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(Path_is_dir, returns_false_for_file)
|
||||||
|
{
|
||||||
|
EXPECT_FALSE(Path::is_dir("test/src/test_Path.cc"));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(Path_is_dir, returns_false_for_nonexistent_path)
|
||||||
|
{
|
||||||
|
EXPECT_FALSE(Path::is_dir("non.existent"));
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user