This commit is contained in:
151
support/include/utils/file.hpp
Normal file
151
support/include/utils/file.hpp
Normal file
@ -0,0 +1,151 @@
|
||||
/*
|
||||
Copyright <2018-2024> <scott.e.graves@protonmail.com>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
#ifndef REPERTORY_INCLUDE_UTILS_FILE_HPP_
|
||||
#define REPERTORY_INCLUDE_UTILS_FILE_HPP_
|
||||
|
||||
#include "utils/config.hpp"
|
||||
|
||||
namespace repertory::utils::file {
|
||||
class file final {
|
||||
public:
|
||||
[[nodiscard]] static auto open_file(std::filesystem::path path) -> file;
|
||||
|
||||
[[nodiscard]] static auto
|
||||
open_or_create_file(std::filesystem::path path) -> file;
|
||||
|
||||
protected:
|
||||
file(std::fstream stream, std::filesystem::path path)
|
||||
: path_(std::move(path)), stream_(std::move(stream)) {}
|
||||
file() = default;
|
||||
|
||||
public:
|
||||
file(const file &) = delete;
|
||||
file(file &&file_) noexcept = default;
|
||||
|
||||
~file() { close(); }
|
||||
|
||||
auto operator=(const file &) noexcept -> file & = delete;
|
||||
auto operator=(file &&file_) noexcept -> file & = default;
|
||||
|
||||
private:
|
||||
std::error_code error_{};
|
||||
std::filesystem::path path_;
|
||||
std::fstream stream_;
|
||||
|
||||
public:
|
||||
void close();
|
||||
|
||||
[[nodiscard]] auto get_error_code() const -> std::error_code {
|
||||
return error_;
|
||||
}
|
||||
|
||||
[[nodiscard]] auto get_path() const -> std::filesystem::path { return path_; }
|
||||
|
||||
[[nodiscard]] auto move_to(std::filesystem::path new_path) -> bool;
|
||||
|
||||
[[nodiscard]] auto read(data_buffer &data, std::uint64_t offset,
|
||||
std::size_t *total_read = nullptr) -> bool {
|
||||
return read_(reinterpret_cast<unsigned char *>(data.data()), data.size(),
|
||||
offset, total_read);
|
||||
}
|
||||
|
||||
[[nodiscard]] auto remove() -> bool;
|
||||
|
||||
[[nodiscard]] auto truncate() -> bool { return truncate(0U); }
|
||||
|
||||
[[nodiscard]] auto truncate(std::size_t size) -> bool;
|
||||
|
||||
#if defined(PROJECT_ENABLE_JSON)
|
||||
[[nodiscard]] auto write(const nlohmann::json &data,
|
||||
std::size_t *total_written = nullptr) -> bool {
|
||||
auto str_data = data.dump();
|
||||
return write_(reinterpret_cast<const unsigned char *>(str_data.c_str()),
|
||||
str_data.size(), 0U, total_written);
|
||||
}
|
||||
#endif // defined(PROJECT_ENABLE_JSON)
|
||||
|
||||
[[nodiscard]] auto write(const data_buffer &data, std::uint64_t offset,
|
||||
std::size_t *total_written = nullptr) -> bool {
|
||||
return write_(reinterpret_cast<const unsigned char *>(data.data()),
|
||||
data.size(), offset, total_written);
|
||||
}
|
||||
|
||||
[[nodiscard]] operator bool() const { return stream_.is_open(); }
|
||||
|
||||
private:
|
||||
[[nodiscard]] auto read_(unsigned char *data, std::size_t to_read,
|
||||
std::uint64_t offset,
|
||||
std::size_t *total_read) -> bool;
|
||||
|
||||
[[nodiscard]] auto write_(const unsigned char *data, std::size_t to_write,
|
||||
std::size_t offset,
|
||||
std::size_t *total_written) -> bool;
|
||||
};
|
||||
|
||||
[[nodiscard]] auto get_file_size(std::string_view path,
|
||||
std::uint64_t &file_size) -> bool;
|
||||
|
||||
[[nodiscard]] auto get_file_size(std::wstring_view path,
|
||||
std::uint64_t &file_size) -> bool;
|
||||
|
||||
[[nodiscard]] auto is_directory(std::string_view path) -> bool;
|
||||
|
||||
[[nodiscard]] auto is_directory(std::wstring_view path) -> bool;
|
||||
|
||||
[[nodiscard]] auto is_file(std::string_view path) -> bool;
|
||||
|
||||
[[nodiscard]] auto is_file(std::wstring_view path) -> bool;
|
||||
|
||||
#if defined(PROJECT_ENABLE_JSON)
|
||||
#if defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST)
|
||||
[[nodiscard]] auto
|
||||
read_json_file(std::string_view path, nlohmann::json &data,
|
||||
std::optional<std::string_view> password = std::nullopt) -> bool;
|
||||
|
||||
[[nodiscard]] auto read_json_file(
|
||||
std::wstring_view path, nlohmann::json &data,
|
||||
std::optional<std::wstring_view> password = std::nullopt) -> bool;
|
||||
|
||||
[[nodiscard]] auto write_json_file(
|
||||
std::string_view path, const nlohmann::json &data,
|
||||
std::optional<std::string_view> password = std::nullopt) -> bool;
|
||||
|
||||
[[nodiscard]] auto write_json_file(
|
||||
std::wstring_view path, const nlohmann::json &data,
|
||||
std::optional<std::wstring_view> password = std::nullopt) -> bool;
|
||||
#else // !defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST)
|
||||
[[nodiscard]] auto read_json_file(std::string_view path,
|
||||
nlohmann::json &data) -> bool;
|
||||
|
||||
[[nodiscard]] auto read_json_file(std::wstring_view path,
|
||||
nlohmann::json &data) -> bool;
|
||||
|
||||
[[nodiscard]] auto write_json_file(std::string_view path,
|
||||
const nlohmann::json &data) -> bool;
|
||||
|
||||
[[nodiscard]] auto write_json_file(std::wstring_view path,
|
||||
const nlohmann::json &data) -> bool;
|
||||
#endif // defined(PROJECT_ENABLE_LIBSODIUM) && defined(PROJECT_ENABLE_BOOST)
|
||||
#endif // defined(PROJECT_ENABLE_JSON)
|
||||
} // namespace repertory::utils::file
|
||||
|
||||
#endif // REPERTORY_INCLUDE_UTILS_FILE_HPP_
|
Reference in New Issue
Block a user