8
8
9
9
#pragma once
10
10
11
- #include < system_error>
12
-
13
- #include < gsl/span>
14
-
15
- #include " Text/Fmt/Offset.h"
16
- #include " Log/Log.h"
17
- #include " Utils/BufferView.h"
18
- #include " Utils/TypeTraits.h"
19
- #include " Stream/SeekDirection.h"
20
-
21
11
namespace Orc {
22
12
23
13
/*
24
- * StreamConcept interface
14
+ * Stream concept
25
15
*
26
16
* class StreamConcept
27
17
* {
@@ -30,174 +20,7 @@ namespace Orc {
30
20
* size_t Write(BufferView& input, std::error_code& ec);
31
21
* uint64_t Seek(SeekDirection direction, int64_t value, std::error_code& ec);
32
22
* };
33
- */
34
-
35
- /* !
36
- * \brief Read 'stream' and shrink output to read size.
37
- */
38
- template <typename InputStreamT, typename CharT>
39
- size_t Read (InputStreamT& stream, gsl::span<CharT> output, std::error_code& ec)
40
- {
41
- gsl::span<uint8_t > span (reinterpret_cast <uint8_t *>(output.data ()), output.size () * sizeof (CharT));
42
- return stream.Read (span, ec);
43
- }
44
-
45
- /* !
46
- * \brief Read 'stream' until 'output' is completely filled.
47
- */
48
- template <typename InputStreamT, typename CharT>
49
- size_t ReadChunk (InputStreamT& stream, gsl::span<CharT> output, std::error_code& ec)
50
- {
51
- uint64_t processed = 0 ;
52
-
53
- while (processed != output.size ())
54
- {
55
- // TODO: subview
56
- gsl::span<uint8_t > span (
57
- reinterpret_cast <uint8_t *>(output.data () + processed), output.size () * sizeof (CharT) - processed);
58
-
59
- const auto lastReadSize = stream.Read (span, ec);
60
- if (ec)
61
- {
62
- return processed;
63
- }
64
-
65
- if (lastReadSize == 0 )
66
- {
67
- Log::Debug (" Input stream does not provide enough bytes to fill buffer" );
68
- ec = std::make_error_code (std::errc::message_size);
69
- return processed;
70
- }
71
-
72
- processed += lastReadSize;
73
- }
74
-
75
- return processed;
76
- }
77
-
78
- /* !
79
- * \brief Read 'stream' until 'output' is completely filled.
80
- */
81
- template <typename InputStreamT, typename ContainerT>
82
- size_t Read (InputStreamT& stream, ContainerT& output, std::error_code& ec)
83
- {
84
- gsl::span<uint8_t > span (reinterpret_cast <uint8_t *>(output.data ()), output.size () * sizeof (ContainerT::value_type));
85
-
86
- size_t processed = stream.Read (span, ec);
87
- if (ec)
88
- {
89
- return processed;
90
- }
91
-
92
- output.resize (processed / sizeof (ContainerT::value_type));
93
- return processed;
94
- }
95
-
96
- /* !
97
- * \brief Read 'stream' until 'output' is completely filled.
98
23
*
99
- * TODO: C++20: use std::is_contiguous_iterator
100
24
*/
101
- template <typename InputStreamT, typename ContainerT>
102
- size_t ReadChunk (InputStreamT& stream, ContainerT& output, std::error_code& ec)
103
- {
104
- gsl::span<uint8_t > span (reinterpret_cast <uint8_t *>(output.data ()), output.size () * sizeof (ContainerT::value_type));
105
- return ReadChunk (stream, span, ec);
106
- }
107
-
108
- /* !
109
- * \brief Read at specified position the available bytes and store them in container of type ContainerT
110
- *
111
- * TODO: C++20: use std::is_contiguous_iterator
112
- */
113
- template <typename InputStreamT, typename ContainerT>
114
- size_t ReadAt (InputStreamT& stream, uint64_t offset, ContainerT& output, std::error_code& ec)
115
- {
116
- stream.Seek (SeekDirection::kBegin , offset, ec);
117
- if (ec)
118
- {
119
- Log::Debug (" Failed to seek to position: {} [{}]" , Traits::Offset (offset), ec);
120
- return 0 ;
121
- }
122
-
123
- return Read (stream, output, ec);
124
- }
125
-
126
- /* !
127
- * \brief Read at specified position in 'stream' until 'output' is completely filled.
128
- *
129
- * TODO: C++20: use std::is_contiguous_iterator
130
- */
131
- template <typename InputStreamT, typename ContainerT>
132
- size_t ReadChunkAt (InputStreamT& stream, uint64_t offset, ContainerT& output, std::error_code& ec)
133
- {
134
- stream.Seek (SeekDirection::kBegin , offset, ec);
135
- if (ec)
136
- {
137
- Log::Debug (" Failed to seek to position: {} [{}]" , Traits::Offset (offset), ec);
138
- return 0 ;
139
- }
140
-
141
- gsl::span<uint8_t > span (reinterpret_cast <uint8_t *>(output.data ()), output.size () * sizeof (ContainerT::value_type));
142
- return ReadChunk (stream, span, ec);
143
- }
144
-
145
- /* !
146
- * \brief Read at specified position in 'stream' until 'output' is completely filled.
147
- *
148
- * TODO: C++20: use std::is_contiguous_iterator
149
- */
150
- template <typename InputStreamT, typename CharT>
151
- size_t
152
- ReadChunkAt (InputStreamT& stream, uint64_t offset, size_t chunkSizeCb, gsl::span<CharT> output, std::error_code& ec)
153
- {
154
- stream.Seek (SeekDirection::kBegin , offset, ec);
155
- if (ec)
156
- {
157
- Log::Debug (" Failed to seek to position: {} [{}]" , Traits::Offset (offset), ec);
158
- return 0 ;
159
- }
160
-
161
- assert (chunkSizeCb < output.size () * sizeof (CharT) && " Buffer 'output' overflow" );
162
-
163
- gsl::span<uint8_t > span (reinterpret_cast <uint8_t *>(output.data ()), chunkSizeCb);
164
- return ReadChunk (stream, span, ec);
165
- }
166
-
167
- /* !
168
- * \brief Fill an item of type 'T' and fail if there is not enough data to read.
169
- */
170
- template <typename InputStreamT, typename ItemT>
171
- void ReadItem (InputStreamT& stream, ItemT& output, std::error_code& ec)
172
- {
173
- uint64_t processed = stream.Read (gsl::span<uint8_t >(reinterpret_cast <uint8_t *>(&output), sizeof (output)), ec);
174
- if (ec)
175
- {
176
- return ;
177
- }
178
-
179
- if (processed != sizeof (output))
180
- {
181
- ec = std::make_error_code (std::errc::interrupted);
182
- Log::Debug (" Failed to read expected size ({}/{})" , processed, sizeof (ItemT));
183
- return ;
184
- }
185
- }
186
-
187
- /* !
188
- * \brief Fill an item of type 'T' and fail if there is not enough data to read.
189
- */
190
- template <typename InputStreamT, typename ItemT>
191
- void ReadItemAt (InputStreamT& stream, uint64_t offset, ItemT& output, std::error_code& ec)
192
- {
193
- stream.Seek (SeekDirection::kBegin , offset, ec);
194
- if (ec)
195
- {
196
- Log::Debug (" Failed to seek to position: {} [{}]" , Traits::Offset (offset), ec);
197
- return ;
198
- }
199
-
200
- ReadItem (stream, output, ec);
201
- }
202
25
203
26
} // namespace Orc
0 commit comments