Extend io

This commit is contained in:
momo5502 2023-02-18 19:13:36 +01:00
parent 21ce390a53
commit e4b54a2dae
2 changed files with 107 additions and 14 deletions

View File

@ -109,22 +109,108 @@ namespace utils::io
return std::filesystem::is_empty(directory);
}
std::vector<std::string> list_files(const std::filesystem::path& directory)
{
std::vector<std::string> files;
for (auto& file : std::filesystem::directory_iterator(directory))
{
files.push_back(file.path().generic_string());
}
return files;
}
void copy_folder(const std::filesystem::path& src, const std::filesystem::path& target)
{
std::filesystem::copy(src, target,
std::filesystem::copy_options::overwrite_existing |
std::filesystem::copy_options::recursive);
}
bool file_exists(const std::wstring& file)
{
return std::ifstream(file).good();
}
bool write_file(const std::wstring& file, const std::string& data, const bool append)
{
const auto pos = file.find_last_of(L"/\\");
if (pos != std::string::npos)
{
create_directory(file.substr(0, pos));
}
std::ofstream stream(
file, std::ios::binary | std::ofstream::out | (append ? std::ofstream::app : 0));
if (stream.is_open())
{
stream.write(data.data(), static_cast<std::streamsize>(data.size()));
stream.close();
return true;
}
return false;
}
std::string read_file(const std::wstring& file)
{
std::string data;
read_file(file, &data);
return data;
}
bool read_file(const std::wstring& file, std::string* data)
{
if (!data) return false;
data->clear();
if (file_exists(file))
{
std::ifstream stream(file, std::ios::binary);
if (!stream.is_open()) return false;
stream.seekg(0, std::ios::end);
const std::streamsize size = stream.tellg();
stream.seekg(0, std::ios::beg);
if (size > -1)
{
data->resize(static_cast<uint32_t>(size));
stream.read(data->data(), size);
stream.close();
return true;
}
}
return false;
}
std::size_t file_size(const std::wstring& file)
{
if (file_exists(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;
}
std::vector<std::filesystem::path> list_files(const std::filesystem::path& directory, const bool recursive)
{
std::vector<std::filesystem::path> files;
if (recursive)
{
for (auto& file : std::filesystem::recursive_directory_iterator(directory))
{
files.push_back(file.path());
}
}
else
{
for (auto& file : std::filesystem::directory_iterator(directory))
{
files.push_back(file.path());
}
}
return files;
}
}

View File

@ -16,6 +16,13 @@ namespace utils::io
bool create_directory(const std::filesystem::path& directory);
bool directory_exists(const std::filesystem::path& directory);
bool directory_is_empty(const std::filesystem::path& directory);
std::vector<std::string> list_files(const std::filesystem::path& directory);
void copy_folder(const std::filesystem::path& src, const std::filesystem::path& target);
bool file_exists(const std::wstring& file);
bool write_file(const std::wstring& file, const std::string& data, bool append = false);
bool read_file(const std::wstring& file, std::string* data);
std::string read_file(const std::wstring& file);
std::size_t file_size(const std::wstring& file);
std::vector<std::filesystem::path> list_files(const std::filesystem::path& directory, bool recursive = false);
}