Path::dirname() and Path::join() return PathRef
This commit is contained in:
parent
6a51c5a880
commit
f94b754b0a
@ -7,13 +7,15 @@
|
||||
|
||||
namespace jes
|
||||
{
|
||||
class Path;
|
||||
typedef Ref<Path> 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<std::string> dir_entries();
|
||||
@ -21,7 +23,6 @@ namespace jes
|
||||
void clean();
|
||||
std::string m_path;
|
||||
};
|
||||
typedef Ref<Path> PathRef;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@ -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()
|
||||
|
@ -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<std::string> dirents = path.dir_entries();
|
||||
std::vector<std::string> 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;
|
||||
|
@ -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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user