iw4x-client/src/Utils/IO.cpp

115 lines
2.3 KiB
C++
Raw Normal View History

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);
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;
}
std::size_t FileSize(const std::string& file)
{
if (FileExists(file))
{
std::ifstream stream(file, std::ios::binary);
if (stream.good())
{
stream.seekg(0, std::ios::end);
return static_cast<std::size_t>(stream.tellg());
}
}
return 0;
}
2018-12-17 08:29:18 -05:00
bool CreateDir(const std::string& dir)
2017-01-19 16:23:59 -05:00
{
return std::filesystem::create_directories(dir);
2017-01-19 16:23:59 -05:00
}
bool DirectoryExists(const std::filesystem::path& directory)
{
return std::filesystem::is_directory(directory);
}
bool DirectoryIsEmpty(const std::filesystem::path& directory)
{
return std::filesystem::is_empty(directory);
}
std::vector<std::string> ListFiles(const std::filesystem::path& directory)
2017-01-19 16:23:59 -05:00
{
std::vector<std::string> files;
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;
}
}
}