packages.biu: add read file

This commit is contained in:
2024-09-06 15:07:45 +08:00
parent 0aacc62638
commit 4fd2a7973d
3 changed files with 26 additions and 1 deletions

View File

@@ -105,9 +105,20 @@ namespace biu
template <typename Array> concurrencpp::generator<std::pair<Array, std::size_t>> sequence(Array from, Array to);
template <typename Array> concurrencpp::generator<std::pair<Array, std::size_t>> sequence(Array to);
namespace detail_
{
template <typename Byte> struct ReadFileReturnType;
template <> struct ReadFileReturnType<std::byte> { using Type = std::vector<std::byte>; };
template <> struct ReadFileReturnType<char> { using Type = std::string; };
}
template <typename Byte = std::byte> detail_::ReadFileReturnType<Byte>::Type
read_file(const std::filesystem::path& path);
template<> std::vector<std::byte> read_file<std::byte>(const std::filesystem::path& path);
template<> std::string read_file<char>(const std::filesystem::path& path);
}
using common::hash, common::unused, common::block_forever, common::is_interactive, common::env, common::int128_t,
common::uint128_t, common::Empty, common::CaseInsensitiveStringLessComparator, common::RemoveMemberPointer,
common::MoveQualifiers, common::FallbackIfNoTypeDeclared, common::exec, common::serialize, common::deserialize,
common::sequence;
common::sequence, common::read_file;
}

View File

@@ -1,4 +1,5 @@
# pragma once
# include <fstream>
# include <boost/functional/hash.hpp>
# include <zpp_bits.h>
# include <biu/common.hpp>

View File

@@ -67,5 +67,18 @@ namespace biu
template detail_::ExecResult<detail_::ExecMode##i> \
exec<detail_::ExecMode##i>(detail_::ExecInput<detail_::ExecMode##i>);
BOOST_PP_FOR(0, BIU_EXEC_PRED, BIU_EXEC_OP, BIU_EXEC_MACRO)
template<> std::vector<std::byte> read_file<std::byte>(const std::filesystem::path& path)
{
auto length = std::filesystem::file_size(path);
std::vector<std::byte> buffer(length);
std::ifstream in(path, std::ios_base::binary);
in.read(reinterpret_cast<char*>(buffer.data()), length);
return buffer;
}
template<> std::string read_file<char>(const std::filesystem::path& path)
{
auto buffer = read_file<std::byte>(path);
return std::string{reinterpret_cast<char*>(buffer.data()), buffer.size()};
}
}
}