Path::dirname() and Path::join() return PathRef

This commit is contained in:
josh committed 2014-06-22 16:08:40 -04:00
1 parent 6a51c5a880
commit f94b754b0a
4 files changed
+29 -26

No files matched your search

+4 -3
View File
@@ -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
+8 -8
View File
@@ -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()
+8 -6
View File
@@ -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;