2021-01-01 05:43:23 +08:00
|
|
|
#include <blah/core/filesystem.h>
|
2021-03-21 17:08:28 +08:00
|
|
|
#include <blah/streams/filestream.h>
|
2021-01-01 05:43:23 +08:00
|
|
|
#include "../internal/platform_backend.h"
|
2020-08-26 15:38:01 +08:00
|
|
|
|
|
|
|
using namespace Blah;
|
|
|
|
|
|
|
|
bool File::exists(const FilePath& path)
|
|
|
|
{
|
2020-12-24 08:16:09 +08:00
|
|
|
return PlatformBackend::file_exists(path.cstr());
|
2020-08-26 15:38:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool File::remove(const FilePath& path)
|
|
|
|
{
|
2020-12-24 08:16:09 +08:00
|
|
|
return PlatformBackend::file_delete(path.cstr());
|
2020-08-26 15:38:01 +08:00
|
|
|
}
|
|
|
|
|
2021-03-21 17:08:28 +08:00
|
|
|
FileStream File::open(const FilePath& path , FileMode mode)
|
|
|
|
{
|
|
|
|
return FileStream(path, mode);
|
|
|
|
}
|
|
|
|
|
2020-08-26 15:38:01 +08:00
|
|
|
bool Directory::create(const FilePath& path)
|
|
|
|
{
|
2020-12-24 08:16:09 +08:00
|
|
|
return PlatformBackend::dir_create(path.cstr());
|
2020-08-26 15:38:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Directory::exists(const FilePath& path)
|
|
|
|
{
|
2020-12-24 08:16:09 +08:00
|
|
|
return PlatformBackend::dir_exists(path.cstr());
|
2020-08-26 15:38:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Directory::remove(const FilePath& path)
|
|
|
|
{
|
2020-12-24 08:16:09 +08:00
|
|
|
return PlatformBackend::dir_delete(path.cstr());
|
2020-08-26 15:38:01 +08:00
|
|
|
}
|
|
|
|
|
2020-10-25 06:21:36 +08:00
|
|
|
Vector<FilePath> Directory::enumerate(const FilePath& path, bool recursive)
|
2020-08-26 15:38:01 +08:00
|
|
|
{
|
2020-10-25 06:21:36 +08:00
|
|
|
Vector<FilePath> list;
|
2020-08-26 15:38:01 +08:00
|
|
|
|
|
|
|
// get files
|
2020-12-24 08:16:09 +08:00
|
|
|
PlatformBackend::dir_enumerate(list, path.cstr(), recursive);
|
2020-08-26 15:38:01 +08:00
|
|
|
|
|
|
|
// normalize path names
|
|
|
|
for (auto& it : list)
|
|
|
|
it.replace('\\', '/');
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Directory::explore(const FilePath& path)
|
|
|
|
{
|
2020-12-24 08:16:09 +08:00
|
|
|
PlatformBackend::dir_explore(path);
|
2020-08-26 15:38:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
FilePath Path::get_file_name(const FilePath& path)
|
|
|
|
{
|
|
|
|
const char* cstr = path.cstr();
|
|
|
|
for (int n = path.length() - 2; n >= 0; n --)
|
|
|
|
if (*(cstr + n) == '/')
|
|
|
|
{
|
|
|
|
return FilePath(cstr + n + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
FilePath Path::get_file_name_no_ext(const FilePath& path)
|
|
|
|
{
|
|
|
|
FilePath filename = get_file_name(path);
|
|
|
|
return get_path_no_ext(filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
FilePath Path::get_path_no_ext(const FilePath& path)
|
|
|
|
{
|
|
|
|
const char* cstr = path.cstr();
|
|
|
|
for (int n = 0; n < path.length(); n++)
|
|
|
|
if (*(cstr + n) == '.')
|
|
|
|
return FilePath(cstr, cstr + n);
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
FilePath Path::get_directory_name(const FilePath& path)
|
|
|
|
{
|
|
|
|
FilePath directory = path;
|
|
|
|
while (directory.ends_with("/"))
|
|
|
|
directory = directory.substr(0, -1);
|
|
|
|
int last = directory.last_index_of('/');
|
|
|
|
if (last >= 0)
|
|
|
|
directory = directory.substr(0, last + 1);
|
|
|
|
return directory;
|
|
|
|
}
|
|
|
|
|
|
|
|
FilePath Path::get_path_after(const FilePath& path, const FilePath& after)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < path.length(); i++)
|
|
|
|
{
|
|
|
|
FilePath substr(path.begin() + i, path.end());
|
|
|
|
if (substr.starts_with(after, false))
|
|
|
|
return FilePath(path.begin() + i + after.length(), path.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
FilePath Path::normalize(const FilePath& path)
|
|
|
|
{
|
|
|
|
FilePath normalized;
|
|
|
|
|
|
|
|
int len = path.length();
|
|
|
|
for (int n = 0; n < len; n++)
|
|
|
|
{
|
|
|
|
// normalize slashes
|
|
|
|
if (path[n] == '\\' || path[n] == '/')
|
|
|
|
{
|
|
|
|
if (normalized.length() == 0 || normalized[normalized.length() - 1] != '/')
|
|
|
|
normalized.append('/');
|
|
|
|
}
|
|
|
|
// move up a directory
|
|
|
|
else if (path[n] == '.' && n < len - 1 && path[n + 1] == '.')
|
|
|
|
{
|
|
|
|
// search backwards for last /
|
|
|
|
bool couldMoveUp = false;
|
|
|
|
for (int k = normalized.length() - 2; k >= 0; k--)
|
|
|
|
if (normalized[k] == '/')
|
|
|
|
{
|
|
|
|
normalized = normalized.substr(0, k);
|
|
|
|
couldMoveUp = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!couldMoveUp)
|
|
|
|
normalized.append('.');
|
|
|
|
else
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
normalized.append(path[n]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return normalized;
|
2021-01-15 10:42:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
FilePath Path::join(const FilePath& a, const FilePath& b)
|
|
|
|
{
|
|
|
|
return normalize(FilePath(a).append("/").append(b));
|
2020-08-26 15:38:01 +08:00
|
|
|
}
|