From f94b754b0ab8cd703c287f01e7c0db4dc5b52c72 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Sun, 22 Jun 2014 16:08:40 -0400 Subject: [PATCH] Path::dirname() and Path::join() return PathRef --- src/lib/include/jes/Path.h | 7 ++++--- src/lib/src/Path.cc | 16 ++++++++-------- src/lib/src/Runtime.cc | 14 ++++++++------ test/src/test_Path.cc | 18 +++++++++--------- 4 files changed, 29 insertions(+), 26 deletions(-) diff --git a/src/lib/include/jes/Path.h b/src/lib/include/jes/Path.h index 9631b3d..b92ce3e 100644 --- a/src/lib/include/jes/Path.h +++ b/src/lib/include/jes/Path.h @@ -7,13 +7,15 @@ namespace jes { + class Path; + typedef Ref PathRef; class Path { public: Path(const char * path); Path(const std::string & path); - Path dirname(); - Path join(const Path & other); + PathRef dirname(); + PathRef join(const Path & other); const std::string & to_s() { return m_path; } bool exists(); std::vector dir_entries(); @@ -21,7 +23,6 @@ namespace jes void clean(); std::string m_path; }; - typedef Ref PathRef; } #endif diff --git a/src/lib/src/Path.cc b/src/lib/src/Path.cc index 82d5780..2773f74 100644 --- a/src/lib/src/Path.cc +++ b/src/lib/src/Path.cc @@ -18,36 +18,36 @@ namespace jes clean(); } - Path Path::dirname() + PathRef Path::dirname() { size_t i = m_path.size() - 1; while (m_path[i] == '/') { if (i == 0) - return "/"; + return new Path("/"); i--; } while (m_path[i] != '/') { if (i == 0) - return "."; + return new Path("."); i--; } while (m_path[i] == '/') { if (i == 0) - return "/"; + return new Path("/"); i--; } - return std::string(m_path, 0, i + 1); + return new Path(std::string(m_path, 0, i + 1)); } - Path Path::join(const Path & other) + PathRef Path::join(const Path & other) { if (m_path.size() > 0 && *m_path.rbegin() == '/') - return Path(m_path + other.m_path); + return new Path(m_path + other.m_path); else - return Path(m_path + '/' + other.m_path); + return new Path(m_path + '/' + other.m_path); } bool Path::exists() diff --git a/src/lib/src/Runtime.cc b/src/lib/src/Runtime.cc index 7830459..5e7ac00 100644 --- a/src/lib/src/Runtime.cc +++ b/src/lib/src/Runtime.cc @@ -9,25 +9,27 @@ namespace jes PathRef Runtime::locate(int type, const std::string & name) { - auto path = Core::instance.get_bin_path()->dirname().join("runtime").join(runtime_directories[type]); + PathRef path = Core::instance.get_bin_path()->dirname()->join("runtime")->join(runtime_directories[type]); PathRef rv = NULL; switch (type) { case Runtime::SHADER: - rv = new Path(path.join(name)); + rv = path->join(name); + break; case Runtime::FONT: { - std::vector dirents = path.dir_entries(); + std::vector dirents = path->dir_entries(); for (auto de : dirents) { - auto test_path = path.join(de).join(name); - if (test_path.exists()) + PathRef test_path = path->join(de)->join(name); + if (test_path->exists()) { - rv = new Path(test_path); + rv = test_path; break; } } } + break; } if (rv->exists()) return rv; diff --git a/test/src/test_Path.cc b/test/src/test_Path.cc index cf5efa4..9a1a92d 100644 --- a/test/src/test_Path.cc +++ b/test/src/test_Path.cc @@ -25,19 +25,19 @@ TEST(PathTest, convert_backslashes_to_forward_slashes) TEST(PathTest, joins_two_paths_together) { - EXPECT_EQ("C:/foo/../baz", Path("C:\\foo").join("../baz").to_s()); - EXPECT_EQ("/xyz/f.txt", Path("/xyz/").join("f.txt").to_s()); + EXPECT_EQ("C:/foo/../baz", Path("C:\\foo").join("../baz")->to_s()); + EXPECT_EQ("/xyz/f.txt", Path("/xyz/").join("f.txt")->to_s()); } TEST(PathTest, dirname) { - EXPECT_EQ("/one/two", Path("/one/two/f.c").dirname().to_s()); - EXPECT_EQ("C:/a", Path("C:\\a\\b\\").dirname().to_s()); - EXPECT_EQ("/", Path("/").dirname().to_s()); - EXPECT_EQ("/", Path("///foo").dirname().to_s()); - EXPECT_EQ(".", Path("file.txt").dirname().to_s()); - EXPECT_EQ(".", Path("folder//").dirname().to_s()); - EXPECT_EQ("a", Path("a/b").dirname().to_s()); + EXPECT_EQ("/one/two", Path("/one/two/f.c").dirname()->to_s()); + EXPECT_EQ("C:/a", Path("C:\\a\\b\\").dirname()->to_s()); + EXPECT_EQ("/", Path("/").dirname()->to_s()); + EXPECT_EQ("/", Path("///foo").dirname()->to_s()); + EXPECT_EQ(".", Path("file.txt").dirname()->to_s()); + EXPECT_EQ(".", Path("folder//").dirname()->to_s()); + EXPECT_EQ("a", Path("a/b").dirname()->to_s()); } TEST(PathTest, exists)