5 namespace tdc {
namespace io {
7 class InputStreamInternal {
10 virtual std::istream& stream() = 0;
14 class Memory:
public InputStreamInternal::Variant {
15 InputAllocChunkHandle m_handle;
18 friend class InputStreamInternal;
20 inline Memory(Memory&& other):
21 m_handle(other.m_handle),
22 m_stream(
std::move(other.m_stream))
25 inline Memory(InputAllocChunkHandle handle,
View view):
30 inline std::istream& stream()
override {
31 return m_stream.stream();
34 inline Memory(
const Memory& other) =
delete;
35 inline Memory() =
delete;
38 unregister_alloc_chunk_handle(m_handle);
41 class File:
public InputStreamInternal::Variant {
43 std::unique_ptr<std::ifstream> m_stream;
45 friend class InputStreamInternal;
47 inline File(std::string&& path,
size_t offset):
48 m_path(
std::move(path)),
49 m_stream(
std::make_unique<
std::ifstream>(
50 m_path,
std::ios::in |
std::ios::binary))
53 s.seekg(offset, std::ios::beg);
55 throw tdc_input_file_not_found_error(m_path);
59 inline File(File&& other):
60 m_path(
std::move(other.m_path)),
61 m_stream(
std::move(other.m_stream))
64 inline std::istream& stream()
override {
68 inline File(
const File& other) =
delete;
69 inline File() =
delete;
72 std::unique_ptr<InputStreamInternal::Variant> m_variant;
73 std::unique_ptr<RestrictedIStreamBuf> m_restricted_istream;
75 friend class InputStream;
78 inline InputStreamInternal(
const InputStreamInternal& other) =
delete;
79 inline InputStreamInternal() =
delete;
81 inline InputStreamInternal(InputStreamInternal::Memory&& mem,
82 const InputRestrictions& restrictions):
83 m_variant(
std::make_unique<InputStreamInternal::Memory>(
std::move(mem)))
85 if (!restrictions.has_no_restrictions()) {
86 m_restricted_istream = std::make_unique<RestrictedIStreamBuf>(
92 inline InputStreamInternal(InputStreamInternal::File&& f,
93 const InputRestrictions& restrictions):
94 m_variant(
std::make_unique<InputStreamInternal::File>(
std::move(f)))
96 if (!restrictions.has_no_restrictions()) {
97 m_restricted_istream = std::make_unique<RestrictedIStreamBuf>(
103 inline InputStreamInternal(InputStreamInternal&& s):
104 m_variant(
std::move(s.m_variant)),
105 m_restricted_istream(
std::move(s.m_restricted_istream)) {}
107 inline std::streambuf* internal_rdbuf() {
108 if (m_restricted_istream) {
109 return &*m_restricted_istream;
111 return m_variant->stream().rdbuf();
122 InputStreamInternal(
std::move(mem)),
123 std::istream(InputStreamInternal::internal_rdbuf()) {
128 InputStreamInternal(
std::move(mem)),
129 std::istream(mem.rdbuf()) {}
146 inline InputStream Input::Variant::as_stream()
const {
150 if (source().is_file()) {
152 <<
"TODO: Can not yet slice the trailing end of a stream";
155 InputStreamInternal {
157 std::string(source().file()),
163 }
if (source().is_view()) {
165 InputStreamInternal {
166 InputStream::Memory {
168 source().view().slice(from(), to())
174 auto h = alloc().find_or_construct(
175 source(), from(), to(), restrictions());
179 InputStreamInternal {
180 InputStream::Memory {
Contains the text compression and encoding framework.
io::Input Input
Convenience shortcut to io::Input.