diff --git a/src/common/utils/io.cpp b/src/common/utils/io.cpp index 45f2ab91..e981315d 100644 --- a/src/common/utils/io.cpp +++ b/src/common/utils/io.cpp @@ -6,7 +6,7 @@ namespace utils::io { bool remove_file(const std::filesystem::path& file) { - if(DeleteFileW(file.wstring().data()) != FALSE) + if (DeleteFileW(file.wstring().data()) != FALSE) { return true; } @@ -109,22 +109,108 @@ namespace utils::io return std::filesystem::is_empty(directory); } - std::vector list_files(const std::filesystem::path& directory) - { - std::vector 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(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(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(stream.tellg()); + } + } + + return 0; + } + + std::vector list_files(const std::filesystem::path& directory, const bool recursive) + { + std::vector 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; + } } diff --git a/src/common/utils/io.hpp b/src/common/utils/io.hpp index 52476834..7065e21d 100644 --- a/src/common/utils/io.hpp +++ b/src/common/utils/io.hpp @@ -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 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 list_files(const std::filesystem::path& directory, bool recursive = false); }