add Path::exists()

This commit is contained in:
Josh Holtrop 2014-06-22 15:30:05 -04:00
parent b55d8d0f09
commit e3cb7b15b4
3 changed files with 16 additions and 0 deletions

View File

@ -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;

View File

@ -1,4 +1,7 @@
#include "jes/Path.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
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)

View File

@ -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());
}