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