C++17ification.
C++17ification! - Filesystem interaction now uses std::filesystem::path. - Usage of const char*, std::string have been changed to std::string_view where appropriate. - Usage of printf-style functions changed to use fmt.
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
******************************************************************************
|
||||
* Xenia : Xbox 360 Emulator Research Project *
|
||||
******************************************************************************
|
||||
* Copyright 2017 Ben Vanik. All rights reserved. *
|
||||
* Copyright 2020 Ben Vanik. All rights reserved. *
|
||||
* Released under the BSD license - see LICENSE in the root for more details. *
|
||||
******************************************************************************
|
||||
*/
|
||||
@@ -26,54 +26,64 @@
|
||||
#include <iostream>
|
||||
|
||||
namespace xe {
|
||||
|
||||
std::string path_to_utf8(const std::filesystem::path& path) {
|
||||
return path.string();
|
||||
}
|
||||
|
||||
std::u16string path_to_utf16(const std::filesystem::path& path) {
|
||||
return xe::to_utf16(path.string());
|
||||
}
|
||||
|
||||
std::filesystem::path to_path(const std::string_view source) { return source; }
|
||||
|
||||
std::filesystem::path to_path(const std::u16string_view source) {
|
||||
return xe::to_utf8(source);
|
||||
}
|
||||
|
||||
namespace filesystem {
|
||||
|
||||
std::wstring GetExecutablePath() {
|
||||
std::filesystem::path GetExecutablePath() {
|
||||
char buff[FILENAME_MAX] = "";
|
||||
readlink("/proc/self/exe", buff, FILENAME_MAX);
|
||||
std::string s(buff);
|
||||
return to_wstring(s);
|
||||
return s;
|
||||
}
|
||||
|
||||
std::wstring GetExecutableFolder() {
|
||||
auto path = GetExecutablePath();
|
||||
return xe::find_base_path(path);
|
||||
std::filesystem::path GetExecutableFolder() {
|
||||
return GetExecutablePath().parent_path();
|
||||
}
|
||||
|
||||
std::wstring GetUserFolder() {
|
||||
std::filesystem::path GetUserFolder() {
|
||||
// get preferred data home
|
||||
char* dataHome = std::getenv("XDG_DATA_HOME");
|
||||
|
||||
// if XDG_DATA_HOME not set, fallback to HOME directory
|
||||
if (dataHome == NULL) {
|
||||
dataHome = std::getenv("HOME");
|
||||
} else {
|
||||
std::string home(dataHome);
|
||||
return to_wstring(home);
|
||||
char* home = std::getenv("XDG_DATA_HOME");
|
||||
if (home) {
|
||||
return std::string(home);
|
||||
}
|
||||
|
||||
// if XDG_DATA_HOME not set, fallback to HOME directory
|
||||
home = std::getenv("HOME");
|
||||
|
||||
// if HOME not set, fall back to this
|
||||
if (dataHome == NULL) {
|
||||
if (home == NULL) {
|
||||
struct passwd pw1;
|
||||
struct passwd* pw;
|
||||
char buf[4096]; // could potentionally lower this
|
||||
getpwuid_r(getuid(), &pw1, buf, sizeof(buf), &pw);
|
||||
assert(&pw1 == pw); // sanity check
|
||||
dataHome = pw->pw_dir;
|
||||
home = pw->pw_dir;
|
||||
}
|
||||
|
||||
std::string home(dataHome);
|
||||
return to_wstring(home + "/.local/share");
|
||||
return std::filesystem::path(home) / ".local" / "share";
|
||||
}
|
||||
|
||||
bool PathExists(const std::wstring& path) {
|
||||
bool PathExists(const std::filesystem::path& path) {
|
||||
struct stat st;
|
||||
return stat(xe::to_string(path).c_str(), &st) == 0;
|
||||
return stat(path.c_str(), &st) == 0;
|
||||
}
|
||||
|
||||
FILE* OpenFile(const std::wstring& path, const char* mode) {
|
||||
auto fixed_path = xe::fix_path_separators(path);
|
||||
return fopen(xe::to_string(fixed_path).c_str(), mode);
|
||||
FILE* OpenFile(const std::filesystem::path& path, const std::string_view mode) {
|
||||
return fopen(path.c_str(), std::string(mode).c_str());
|
||||
}
|
||||
|
||||
bool Seek(FILE* file, int64_t offset, int origin) {
|
||||
@@ -101,8 +111,8 @@ bool TruncateStdioFile(FILE* file, uint64_t length) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CreateFolder(const std::wstring& path) {
|
||||
return mkdir(xe::to_string(path).c_str(), 0774);
|
||||
bool CreateFolder(const std::filesystem::path& path) {
|
||||
return mkdir(path.c_str(), 0774);
|
||||
}
|
||||
|
||||
static int removeCallback(const char* fpath, const struct stat* sb,
|
||||
@@ -111,9 +121,8 @@ static int removeCallback(const char* fpath, const struct stat* sb,
|
||||
return rv;
|
||||
}
|
||||
|
||||
bool DeleteFolder(const std::wstring& path) {
|
||||
return nftw(xe::to_string(path).c_str(), removeCallback, 64,
|
||||
FTW_DEPTH | FTW_PHYS) == 0
|
||||
bool DeleteFolder(const std::filesystem::path& path) {
|
||||
return nftw(path.c_str(), removeCallback, 64, FTW_DEPTH | FTW_PHYS) == 0
|
||||
? true
|
||||
: false;
|
||||
}
|
||||
@@ -128,16 +137,16 @@ static uint64_t convertUnixtimeToWinFiletime(time_t unixtime) {
|
||||
return filetime;
|
||||
}
|
||||
|
||||
bool IsFolder(const std::wstring& path) {
|
||||
bool IsFolder(const std::filesystem::path& path) {
|
||||
struct stat st;
|
||||
if (stat(xe::to_string(path).c_str(), &st) == 0) {
|
||||
if (stat(path.c_str(), &st) == 0) {
|
||||
if (S_ISDIR(st.st_mode)) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool CreateFile(const std::wstring& path) {
|
||||
int file = creat(xe::to_string(path).c_str(), 0774);
|
||||
bool CreateFile(const std::filesystem::path& path) {
|
||||
int file = creat(path.c_str(), 0774);
|
||||
if (file >= 0) {
|
||||
close(file);
|
||||
return true;
|
||||
@@ -145,13 +154,14 @@ bool CreateFile(const std::wstring& path) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool DeleteFile(const std::wstring& path) {
|
||||
return (xe::to_string(path).c_str()) == 0 ? true : false;
|
||||
bool DeleteFile(const std::filesystem::path& path) {
|
||||
// TODO: proper implementation.
|
||||
return (path.c_str()) == 0 ? true : false;
|
||||
}
|
||||
|
||||
class PosixFileHandle : public FileHandle {
|
||||
public:
|
||||
PosixFileHandle(std::wstring path, int handle)
|
||||
PosixFileHandle(std::filesystem::path path, int handle)
|
||||
: FileHandle(std::move(path)), handle_(handle) {}
|
||||
~PosixFileHandle() override {
|
||||
close(handle_);
|
||||
@@ -178,8 +188,8 @@ class PosixFileHandle : public FileHandle {
|
||||
int handle_ = -1;
|
||||
};
|
||||
|
||||
std::unique_ptr<FileHandle> FileHandle::OpenExisting(std::wstring path,
|
||||
uint32_t desired_access) {
|
||||
std::unique_ptr<FileHandle> FileHandle::OpenExisting(
|
||||
const std::filesystem::path& path, uint32_t desired_access) {
|
||||
int open_access = 0;
|
||||
if (desired_access & FileAccess::kGenericRead) {
|
||||
open_access |= O_RDONLY;
|
||||
@@ -202,7 +212,7 @@ std::unique_ptr<FileHandle> FileHandle::OpenExisting(std::wstring path,
|
||||
if (desired_access & FileAccess::kFileAppendData) {
|
||||
open_access |= O_APPEND;
|
||||
}
|
||||
int handle = open(xe::to_string(path).c_str(), open_access);
|
||||
int handle = open(path.c_str(), open_access);
|
||||
if (handle == -1) {
|
||||
// TODO(benvanik): pick correct response.
|
||||
return nullptr;
|
||||
@@ -210,9 +220,9 @@ std::unique_ptr<FileHandle> FileHandle::OpenExisting(std::wstring path,
|
||||
return std::make_unique<PosixFileHandle>(path, handle);
|
||||
}
|
||||
|
||||
bool GetInfo(const std::wstring& path, FileInfo* out_info) {
|
||||
bool GetInfo(const std::filesystem::path& path, FileInfo* out_info) {
|
||||
struct stat st;
|
||||
if (stat(xe::to_string(path).c_str(), &st) == 0) {
|
||||
if (stat(path.c_str(), &st) == 0) {
|
||||
if (S_ISDIR(st.st_mode)) {
|
||||
out_info->type = FileInfo::Type::kDirectory;
|
||||
} else {
|
||||
@@ -226,10 +236,10 @@ bool GetInfo(const std::wstring& path, FileInfo* out_info) {
|
||||
return false;
|
||||
}
|
||||
|
||||
std::vector<FileInfo> ListFiles(const std::wstring& path) {
|
||||
std::vector<FileInfo> ListFiles(const std::filesystem::path& path) {
|
||||
std::vector<FileInfo> result;
|
||||
|
||||
DIR* dir = opendir(xe::to_string(path).c_str());
|
||||
DIR* dir = opendir(path.c_str());
|
||||
if (!dir) {
|
||||
return result;
|
||||
}
|
||||
@@ -237,9 +247,9 @@ std::vector<FileInfo> ListFiles(const std::wstring& path) {
|
||||
while (auto ent = readdir(dir)) {
|
||||
FileInfo info;
|
||||
|
||||
info.name = xe::to_wstring(ent->d_name);
|
||||
info.name = ent->d_name;
|
||||
struct stat st;
|
||||
stat((xe::to_string(path) + xe::to_string(info.name)).c_str(), &st);
|
||||
stat((path / info.name).c_str(), &st);
|
||||
info.create_timestamp = convertUnixtimeToWinFiletime(st.st_ctime);
|
||||
info.access_timestamp = convertUnixtimeToWinFiletime(st.st_atime);
|
||||
info.write_timestamp = convertUnixtimeToWinFiletime(st.st_mtime);
|
||||
|
||||
Reference in New Issue
Block a user