Path::dirname() and Path::join() return PathRef
This commit is contained in:
parent
6a51c5a880
commit
f94b754b0a
@ -7,13 +7,15 @@
|
|||||||
|
|
||||||
namespace jes
|
namespace jes
|
||||||
{
|
{
|
||||||
|
class Path;
|
||||||
|
typedef Ref<Path> PathRef;
|
||||||
class Path
|
class Path
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Path(const char * path);
|
Path(const char * path);
|
||||||
Path(const std::string & path);
|
Path(const std::string & path);
|
||||||
Path dirname();
|
PathRef dirname();
|
||||||
Path join(const Path & other);
|
PathRef join(const Path & other);
|
||||||
const std::string & to_s() { return m_path; }
|
const std::string & to_s() { return m_path; }
|
||||||
bool exists();
|
bool exists();
|
||||||
std::vector<std::string> dir_entries();
|
std::vector<std::string> dir_entries();
|
||||||
@ -21,7 +23,6 @@ namespace jes
|
|||||||
void clean();
|
void clean();
|
||||||
std::string m_path;
|
std::string m_path;
|
||||||
};
|
};
|
||||||
typedef Ref<Path> PathRef;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -18,36 +18,36 @@ namespace jes
|
|||||||
clean();
|
clean();
|
||||||
}
|
}
|
||||||
|
|
||||||
Path Path::dirname()
|
PathRef Path::dirname()
|
||||||
{
|
{
|
||||||
size_t i = m_path.size() - 1;
|
size_t i = m_path.size() - 1;
|
||||||
while (m_path[i] == '/')
|
while (m_path[i] == '/')
|
||||||
{
|
{
|
||||||
if (i == 0)
|
if (i == 0)
|
||||||
return "/";
|
return new Path("/");
|
||||||
i--;
|
i--;
|
||||||
}
|
}
|
||||||
while (m_path[i] != '/')
|
while (m_path[i] != '/')
|
||||||
{
|
{
|
||||||
if (i == 0)
|
if (i == 0)
|
||||||
return ".";
|
return new Path(".");
|
||||||
i--;
|
i--;
|
||||||
}
|
}
|
||||||
while (m_path[i] == '/')
|
while (m_path[i] == '/')
|
||||||
{
|
{
|
||||||
if (i == 0)
|
if (i == 0)
|
||||||
return "/";
|
return new Path("/");
|
||||||
i--;
|
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() == '/')
|
if (m_path.size() > 0 && *m_path.rbegin() == '/')
|
||||||
return Path(m_path + other.m_path);
|
return new Path(m_path + other.m_path);
|
||||||
else
|
else
|
||||||
return Path(m_path + '/' + other.m_path);
|
return new Path(m_path + '/' + other.m_path);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Path::exists()
|
bool Path::exists()
|
||||||
|
@ -9,25 +9,27 @@ namespace jes
|
|||||||
|
|
||||||
PathRef Runtime::locate(int type, const std::string & name)
|
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;
|
PathRef rv = NULL;
|
||||||
switch (type)
|
switch (type)
|
||||||
{
|
{
|
||||||
case Runtime::SHADER:
|
case Runtime::SHADER:
|
||||||
rv = new Path(path.join(name));
|
rv = path->join(name);
|
||||||
|
break;
|
||||||
case Runtime::FONT:
|
case Runtime::FONT:
|
||||||
{
|
{
|
||||||
std::vector<std::string> dirents = path.dir_entries();
|
std::vector<std::string> dirents = path->dir_entries();
|
||||||
for (auto de : dirents)
|
for (auto de : dirents)
|
||||||
{
|
{
|
||||||
auto test_path = path.join(de).join(name);
|
PathRef test_path = path->join(de)->join(name);
|
||||||
if (test_path.exists())
|
if (test_path->exists())
|
||||||
{
|
{
|
||||||
rv = new Path(test_path);
|
rv = test_path;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
if (rv->exists())
|
if (rv->exists())
|
||||||
return rv;
|
return rv;
|
||||||
|
@ -25,19 +25,19 @@ TEST(PathTest, convert_backslashes_to_forward_slashes)
|
|||||||
|
|
||||||
TEST(PathTest, joins_two_paths_together)
|
TEST(PathTest, joins_two_paths_together)
|
||||||
{
|
{
|
||||||
EXPECT_EQ("C:/foo/../baz", Path("C:\\foo").join("../baz").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());
|
EXPECT_EQ("/xyz/f.txt", Path("/xyz/").join("f.txt")->to_s());
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(PathTest, dirname)
|
TEST(PathTest, dirname)
|
||||||
{
|
{
|
||||||
EXPECT_EQ("/one/two", Path("/one/two/f.c").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("C:/a", Path("C:\\a\\b\\").dirname()->to_s());
|
||||||
EXPECT_EQ("/", Path("/").dirname().to_s());
|
EXPECT_EQ("/", Path("/").dirname()->to_s());
|
||||||
EXPECT_EQ("/", Path("///foo").dirname().to_s());
|
EXPECT_EQ("/", Path("///foo").dirname()->to_s());
|
||||||
EXPECT_EQ(".", Path("file.txt").dirname().to_s());
|
EXPECT_EQ(".", Path("file.txt").dirname()->to_s());
|
||||||
EXPECT_EQ(".", Path("folder//").dirname().to_s());
|
EXPECT_EQ(".", Path("folder//").dirname()->to_s());
|
||||||
EXPECT_EQ("a", Path("a/b").dirname().to_s());
|
EXPECT_EQ("a", Path("a/b").dirname()->to_s());
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(PathTest, exists)
|
TEST(PathTest, exists)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user