tudocomp
– The TU Dortmund Compression Framework
Input.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <cstddef>
4 #include <cstdint>
5 #include <fstream>
6 #include <iostream>
7 #include <string>
8 #include <utility>
9 #include <vector>
10 #include <iterator>
11 #include <memory>
12 
13 #include <tudocomp/util.hpp>
14 
15 #include <tudocomp/io/Path.hpp>
18 #include <tudocomp/io/IOUtil.hpp>
20 
21 namespace tdc {namespace io {
22  class InputView;
23  class InputStream;
24 
37  class Input {
38  public:
40  static constexpr size_t npos = -1;
42  private:
43  class Variant {
44  InputSource m_source;
45  InputAllocHandle m_handle;
46  InputRestrictions m_input_restrictions;
47  size_t m_from = 0;
48  size_t m_to = npos;
49  mutable size_t m_escaped_size_cache = npos;
50  protected:
51  inline void set_escaped_size(size_t size) const {
52  m_escaped_size_cache = size;
53  }
54  inline Variant(const Variant& other,
55  size_t from,
56  size_t to): Variant(other) {
57  m_from = from;
58  m_to = to;
59  m_escaped_size_cache = npos;
60  }
61  inline Variant(const Variant& other,
62  const InputRestrictions& restrictions): Variant(other) {
63  m_input_restrictions = restrictions;
64  m_escaped_size_cache = npos;
65  }
66  public:
67  inline Variant(const InputSource& src): m_source(src) {}
68 
69  inline const InputAllocHandle& alloc() const {
70  return m_handle;
71  }
72 
73  inline const InputRestrictions& restrictions() const {
74  return m_input_restrictions;
75  }
76 
77  inline size_t from() const {
78  return m_from;
79  }
80 
81  inline size_t to() const {
82  return m_to;
83  }
84 
85  inline bool to_unknown() const {
86  return m_to == npos;
87  }
88 
89  inline bool escaped_size_unknown() const {
90  return m_escaped_size_cache == npos;
91  }
92 
93  inline size_t escaped_size() const {
94  DCHECK(!escaped_size_unknown());
95  return m_escaped_size_cache;
96  }
97 
98  inline const InputSource source() const {
99  return m_source;
100  }
101 
104  inline std::shared_ptr<Variant> slice(size_t from, size_t to) const;
105  inline std::shared_ptr<Variant> restrict(const InputRestrictions& rest) const;
106  inline size_t size() const;
107  inline InputView as_view() const;
108  inline InputStream as_stream() const;
109  };
110 
111  friend class InputStream;
112  friend class InputStreamInternal;
113  friend class InputView;
114 
115  std::shared_ptr<Variant> m_data;
116  public:
118  inline Input():
119  m_data(std::make_shared<Variant>(InputSource(""_v))) {}
120 
127  inline Input(const Input& other):
128  m_data(other.m_data) {}
129 
131  inline Input(Input&& other):
132  m_data(std::move(other.m_data)) {}
133 
138  Input(Path&& path):
139  m_data(std::make_shared<Variant>(InputSource(path.path))) {}
140 
144  Input(const string_ref& buf):
145  m_data(std::make_shared<Variant>(View(buf))) {}
146 
150  Input(const std::vector<uint8_t>& buf):
151  Input(string_ref(buf)) {}
152 
158  Input(std::istream& stream):
159  m_data(std::make_shared<Variant>(InputSource(&stream))) {}
160 
162  Input& operator=(Input&& other) {
163  m_data = std::move(other.m_data);
164  return *this;
165  }
166 
168  Input& operator=(const Input& other) {
169  Input cpy(other);
170  *this = std::move(cpy);
171  return *this;
172  }
173 
179  static Input from_path(std::string path) {
180  return Input(Path { path });
181  }
182 
187  static Input from_memory(const std::vector<uint8_t>& buf) {
188  return Input(buf);
189  }
190 
195  static Input from_memory(const string_ref buf) {
196  return Input(buf);
197  }
198 
208  inline InputView as_view() const;
209 
221  inline InputStream as_stream() const;
222 
233  inline size_t size() const {
234  return m_data->size();
235  }
236 
241  inline Input(const Input& other, size_t from, size_t to = npos):
242  m_data(other.m_data->slice(from, to)) {}
243 
248  inline Input(const Input& other, const InputRestrictions& restrictions):
249  m_data(other.m_data->restrict(restrictions)) {}
251  };
252 
253 }}
254 
255 #include <tudocomp/io/InputView.hpp>
257 #include <tudocomp/io/InputSize.hpp>
258 
259 namespace tdc {namespace io {
260  inline InputView Input::as_view() const {
261  return m_data->as_view();
262  }
263 
264  inline InputStream Input::as_stream() const {
265  return m_data->as_stream();
266  }
267 }}
Contains the text compression and encoding framework.
Definition: namespaces.hpp:11
A const view into a slice of memory.
Input(Path &&path)
Constructs a file input reading from the file at the given path.
Definition: Input.hpp:138
Describes a set of restrictions placed on input data.
InputStream as_stream() const
Creates a stream that allows for character-wise reading of the input.
Definition: Input.hpp:264
Input(const std::vector< uint8_t > &buf)
Constructs an input reading from the specified byte buffer.
Definition: Input.hpp:150
static Input from_memory(const std::vector< uint8_t > &buf)
Constructs a file input reading from a byte buffer.
Definition: Input.hpp:187
Represents a file path.
Definition: Path.hpp:8
Provides a character stream of the underlying input.
Input()
Constructs an empty input.
Definition: Input.hpp:118
len_compact_t src
Definition: LZSSFactors.hpp:38
Input & operator=(const Input &other)
Copy assignment operator.
Definition: Input.hpp:168
Class that stores the source of input data.
Definition: InputSource.hpp:12
uint64_t m_data
Definition: uint_t.hpp:30
Input(const Input &other)
Constructs an input from another input, retaining its internal state ("cursor").
Definition: Input.hpp:127
static Input from_memory(const string_ref buf)
Constructs a file input reading from a string in memory.
Definition: Input.hpp:195
friend class InputStreamInternal
Definition: Input.hpp:112
size_t size() const
Yields the total amount of characters in the input.
Definition: Input.hpp:233
Input(Input &&other)
Move constructor.
Definition: Input.hpp:131
InputView as_view() const
Provides a view on the input that allows for random access.
Definition: Input.hpp:260
Provides a view on the input that allows for random access.
Definition: InputView.hpp:29
static Input from_path(std::string path)
Constructs a file input reading from the file at the given path.
Definition: Input.hpp:179
Input & operator=(Input &&other)
Move assignment operator.
Definition: Input.hpp:162
Input(const string_ref &buf)
Constructs an input reading from a string in memory.
Definition: Input.hpp:144
Input(std::istream &stream)
Constructs an input reading from a stream.
Definition: Input.hpp:158
An abstraction layer for algorithm input.
Definition: Input.hpp:37