mirror of
https://github.com/CHN-beta/nixos.git
synced 2026-01-12 04:39:23 +08:00
packages.biu: add read file
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
# pragma once
|
||||
# include <fstream>
|
||||
# include <boost/functional/hash.hpp>
|
||||
# include <zpp_bits.h>
|
||||
# include <biu/common.hpp>
|
||||
|
||||
@@ -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()};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user