From e3cb7b15b4cb933147cf8535e387ceb40d28a9bb Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Sun, 22 Jun 2014 15:30:05 -0400 Subject: [PATCH] add Path::exists() --- src/lib/include/jes/Path.h | 1 + src/lib/src/Path.cc | 9 +++++++++ test/src/test_Path.cc | 6 ++++++ 3 files changed, 16 insertions(+) diff --git a/src/lib/include/jes/Path.h b/src/lib/include/jes/Path.h index 3be9084..b21bdf8 100644 --- a/src/lib/include/jes/Path.h +++ b/src/lib/include/jes/Path.h @@ -14,6 +14,7 @@ namespace jes Path dirname(); Path join(const Path & other); const std::string & to_s() { return m_path; } + bool exists(); protected: void clean(); std::string m_path; diff --git a/src/lib/src/Path.cc b/src/lib/src/Path.cc index 7a344b1..6f19aa7 100644 --- a/src/lib/src/Path.cc +++ b/src/lib/src/Path.cc @@ -1,4 +1,7 @@ #include "jes/Path.h" +#include +#include +#include namespace jes { @@ -46,6 +49,12 @@ namespace jes return Path(m_path + '/' + other.m_path); } + bool Path::exists() + { + struct stat st; + return stat(m_path.c_str(), &st) == 0; + } + void Path::clean() { for (char & c : m_path) diff --git a/test/src/test_Path.cc b/test/src/test_Path.cc index 96aae3a..1dddfc5 100644 --- a/test/src/test_Path.cc +++ b/test/src/test_Path.cc @@ -38,3 +38,9 @@ TEST(PathTest, dirname) EXPECT_EQ(".", Path("folder//").dirname().to_s()); EXPECT_EQ("a", Path("a/b").dirname().to_s()); } + +TEST(PathTest, exists) +{ + EXPECT_TRUE(Path("runtime").exists()); + EXPECT_FALSE(Path("foobar").exists()); +}