#include "File.h" #include #include #include "StringHelper.h" #pragma warning(disable: 4267) #pragma warning(disable: 4244) #pragma warning(disable: 4018) bool File::Exists(const std::string path) { return std::filesystem::exists(path); } void File::Delete(const std::string path) { std::filesystem::remove(path); } void File::Copy(const std::string src, const std::string dest) { std::filesystem::copy(src, dest); } void File::Move(const std::string src, const std::string dest) { std::filesystem::rename(src, dest); } void File::Create(const std::string path) { std::ofstream fout(path); fout.close(); } std::string File::ReadAllText(const std::string path) { if (std::ifstream file(path, std::ios::binary | std::ios::ate); file) { std::string data(file.tellg(), '\0'); file.seekg(0); file.read(reinterpret_cast(data.data()), data.size()); return data; } return {}; } std::vector File::ReadAllBytes(const std::string path) { if (std::ifstream file(path, std::ios::binary | std::ios::ate); file) { std::vector data(file.tellg()); file.seekg(0); file.read(reinterpret_cast(data.data()), data.size()); return data; } return {}; } std::vector File::ReadAllLines(const std::string path) { std::string str = File::ReadAllText(path); return StringHelper::Split(str, {'\r','\n'}); } void File::WriteAllText(const std::string path, const std::string content) { if (std::ofstream fout(path); fout) { fout << content; } } void File::WriteAllBytes(const std::string path, const std::vector content) { if (std::ofstream fout(path); fout) { fout.write((char*)content.data(), content.size()); } } void File::WriteAllBytes(const std::string path, const uint8_t* content, size_t size) { if (std::ofstream fout(path); fout) { fout.write((char*)content, size); } } void File::WriteAllLines(const std::string path, const std::vector content) { auto str = StringHelper::Join(content, "\n"); File::WriteAllText(path, str); } void File::AppendAllText(const std::string path, const std::string content) { if (std::ofstream fout(path, std::ios::app); fout) { fout.write((char*)content.data(), content.size()); } } void File::AppendAllBytes(const std::string path, const std::vector content) { if (std::ofstream fout(path, std::ios::app); fout) { fout.write((char*)content.data(), content.size()); } } void File::AppendAllLines(const std::string path, const std::vector content) { auto str = StringHelper::Join(content, "\n"); File::AppendAllText(path, str); } void File::SetAttributes(const std::string path, FileAttributes attributes) { SetFileAttributesA(path.c_str(), (DWORD)attributes); } FileAttributes File::GetAttributes(const std::string path) { return (FileAttributes)GetFileAttributesA(path.c_str()); } void File::SetCreationTime(const std::string path, FILETIME time) { HANDLE pFile = CreateFileA(path.c_str(), GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (pFile == INVALID_HANDLE_VALUE) { CloseHandle(pFile); return; } SetFileTime(pFile, &time, NULL, NULL); CloseHandle(pFile); } FILETIME File::GetCreationTime(const std::string path) { HANDLE pFile = CreateFileA(path.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (pFile == INVALID_HANDLE_VALUE) { CloseHandle(pFile); return {}; } FILETIME time; GetFileTime(pFile, &time, NULL, NULL); CloseHandle(pFile); return time; } void File::SetLastAccessTime(const std::string path, FILETIME time) { HANDLE pFile = CreateFileA(path.c_str(), GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (pFile == INVALID_HANDLE_VALUE) { CloseHandle(pFile); return; } SetFileTime(pFile, NULL, &time, NULL); CloseHandle(pFile); } FILETIME File::GetLastAccessTime(const std::string path) { HANDLE pFile = CreateFileA(path.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (pFile == INVALID_HANDLE_VALUE) { CloseHandle(pFile); return {}; } FILETIME time; GetFileTime(pFile, NULL, &time, NULL); CloseHandle(pFile); return time; } void File::SetLastWriteTime(const std::string path, FILETIME time) { HANDLE pFile = CreateFileA(path.c_str(), GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (pFile == INVALID_HANDLE_VALUE) { CloseHandle(pFile); return; } SetFileTime(pFile, NULL, NULL, &time); CloseHandle(pFile); } FILETIME File::GetLastWriteTime(const std::string path) { HANDLE pFile = CreateFileA(path.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (pFile == INVALID_HANDLE_VALUE) { CloseHandle(pFile); return {}; } FILETIME time; GetFileTime(pFile, NULL, NULL, &time); CloseHandle(pFile); return time; }