2022-02-27 07:53:44 -05:00
|
|
|
#include <STDInclude.hpp>
|
2017-01-19 16:23:59 -05:00
|
|
|
|
|
|
|
namespace Utils
|
|
|
|
{
|
|
|
|
namespace IO
|
|
|
|
{
|
2018-12-17 08:29:18 -05:00
|
|
|
bool FileExists(const std::string& file)
|
2017-01-19 16:23:59 -05:00
|
|
|
{
|
2017-06-02 16:18:00 -04:00
|
|
|
//return std::ifstream(file).good();
|
|
|
|
return GetFileAttributesA(file.data()) != INVALID_FILE_ATTRIBUTES;
|
2017-01-19 16:23:59 -05:00
|
|
|
}
|
|
|
|
|
2018-12-17 08:29:18 -05:00
|
|
|
bool WriteFile(const std::string& file, const std::string& data, bool append)
|
2017-01-19 16:23:59 -05:00
|
|
|
{
|
|
|
|
auto pos = file.find_last_of("/\\");
|
|
|
|
if (pos != std::string::npos)
|
|
|
|
{
|
2017-02-10 16:02:13 -05:00
|
|
|
CreateDir(file.substr(0, pos));
|
2017-01-19 16:23:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
std::ofstream stream(file, std::ios::binary | std::ofstream::out | (append ? std::ofstream::app : std::ofstream::out));
|
|
|
|
|
|
|
|
if (stream.is_open())
|
|
|
|
{
|
|
|
|
stream.write(data.data(), data.size());
|
|
|
|
stream.close();
|
2017-02-10 09:18:11 -05:00
|
|
|
return true;
|
2017-01-19 16:23:59 -05:00
|
|
|
}
|
2017-02-10 09:18:11 -05:00
|
|
|
|
|
|
|
return false;
|
2017-01-19 16:23:59 -05:00
|
|
|
}
|
|
|
|
|
2018-12-17 08:29:18 -05:00
|
|
|
std::string ReadFile(const std::string& file)
|
2017-01-19 16:23:59 -05:00
|
|
|
{
|
2017-02-10 09:18:11 -05:00
|
|
|
std::string data;
|
|
|
|
ReadFile(file, &data);
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2018-12-17 08:29:18 -05:00
|
|
|
bool ReadFile(const std::string& file, std::string* data)
|
2017-02-10 09:18:11 -05:00
|
|
|
{
|
|
|
|
if (!data) return false;
|
|
|
|
data->clear();
|
2017-01-19 16:23:59 -05:00
|
|
|
|
|
|
|
if (FileExists(file))
|
|
|
|
{
|
|
|
|
std::ifstream stream(file, std::ios::binary);
|
2017-02-10 09:18:11 -05:00
|
|
|
if (!stream.is_open()) return false;
|
2017-01-19 16:23:59 -05:00
|
|
|
|
|
|
|
stream.seekg(0, std::ios::end);
|
2017-01-20 16:41:03 -05:00
|
|
|
std::streamsize size = stream.tellg();
|
2017-01-19 16:23:59 -05:00
|
|
|
stream.seekg(0, std::ios::beg);
|
|
|
|
|
|
|
|
if (size > -1)
|
|
|
|
{
|
2017-02-10 09:18:11 -05:00
|
|
|
data->resize(static_cast<uint32_t>(size));
|
2022-08-24 17:46:07 -04:00
|
|
|
stream.read(data->data(), size);
|
2017-02-10 09:18:11 -05:00
|
|
|
stream.close();
|
|
|
|
return true;
|
2017-01-19 16:23:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-10 09:18:11 -05:00
|
|
|
return false;
|
2017-01-19 16:23:59 -05:00
|
|
|
}
|
|
|
|
|
2021-12-04 13:34:19 -05:00
|
|
|
bool RemoveFile(const std::string& file)
|
|
|
|
{
|
|
|
|
return DeleteFileA(file.data()) == TRUE;
|
|
|
|
}
|
|
|
|
|
2022-11-21 19:34:17 -05:00
|
|
|
std::size_t FileSize(const std::string& file)
|
2017-02-10 15:46:03 -05:00
|
|
|
{
|
|
|
|
if (FileExists(file))
|
|
|
|
{
|
|
|
|
std::ifstream stream(file, std::ios::binary);
|
|
|
|
|
2017-06-14 06:06:04 -04:00
|
|
|
if (stream.good())
|
2017-02-10 15:46:03 -05:00
|
|
|
{
|
|
|
|
stream.seekg(0, std::ios::end);
|
2022-11-21 19:34:17 -05:00
|
|
|
return static_cast<std::size_t>(stream.tellg());
|
2017-02-10 15:46:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-12-17 08:29:18 -05:00
|
|
|
bool CreateDir(const std::string& dir)
|
2017-01-19 16:23:59 -05:00
|
|
|
{
|
2019-10-02 05:37:08 -04:00
|
|
|
return std::filesystem::create_directories(dir);
|
2017-01-19 16:23:59 -05:00
|
|
|
}
|
|
|
|
|
2022-11-21 19:34:17 -05:00
|
|
|
bool DirectoryExists(const std::filesystem::path& directory)
|
2017-03-15 14:30:15 -04:00
|
|
|
{
|
2019-10-02 05:37:08 -04:00
|
|
|
return std::filesystem::is_directory(directory);
|
2017-03-15 14:30:15 -04:00
|
|
|
}
|
|
|
|
|
2022-11-21 19:34:17 -05:00
|
|
|
bool DirectoryIsEmpty(const std::filesystem::path& directory)
|
2017-03-15 14:30:15 -04:00
|
|
|
{
|
2019-10-02 05:37:08 -04:00
|
|
|
return std::filesystem::is_empty(directory);
|
2017-03-15 14:30:15 -04:00
|
|
|
}
|
|
|
|
|
2022-11-21 19:34:17 -05:00
|
|
|
std::vector<std::string> ListFiles(const std::filesystem::path& directory)
|
2017-01-19 16:23:59 -05:00
|
|
|
{
|
|
|
|
std::vector<std::string> files;
|
|
|
|
|
2022-11-21 19:34:17 -05:00
|
|
|
for (auto& file : std::filesystem::directory_iterator(directory))
|
2017-01-19 16:23:59 -05:00
|
|
|
{
|
|
|
|
files.push_back(file.path().generic_string());
|
|
|
|
}
|
|
|
|
|
|
|
|
return files;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|