Mantid
Loading...
Searching...
No Matches
BinaryStreamReader.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
7//------------------------------------------------------------------------------
8// Includes
9//------------------------------------------------------------------------------
11#include "MantidKernel/Matrix.h"
12
13#include <cassert>
14#include <istream>
15#include <stdexcept>
16
17namespace Mantid::Kernel {
18
19//------------------------------------------------------------------------------
20// Anonymous functions
21//------------------------------------------------------------------------------
22namespace {
23
29template <typename T> inline void readFromStream(std::istream &stream, T &value) {
30 stream.read(reinterpret_cast<char *>(&value), sizeof(T));
31}
32
41template <typename T> inline void readFromStream(std::istream &stream, std::vector<T> &value, size_t nvals) {
42 if (value.size() < nvals)
43 value.resize(nvals);
44 stream.read(reinterpret_cast<char *>(value.data()), nvals * sizeof(T));
45}
46
56template <typename T>
57inline void readFromStream(std::istream &stream, Matrix<T> &value, const std::vector<int32_t> &shape,
59 assert(2 <= shape.size());
60 const size_t s0(shape[0]), s1(shape[1]), totalLength(s0 * s1);
61 std::vector<T> buffer;
62 readFromStream(stream, buffer, totalLength);
63 value = Matrix<T>(s0, s1);
65 for (size_t i = 0; i < s0; ++i) {
66 auto row = value[i];
67 const size_t offset = i * s1;
68 for (size_t j = 0; j < s1; ++j) {
69 row[j] = buffer[offset + j];
70 }
71 }
72 } else {
73 for (size_t i = 0; i < s0; ++i) {
74 auto row = value[i];
75 for (size_t j = 0; j < s1; ++j) {
76 row[j] = buffer[j * s0 + i];
77 }
78 }
79 }
80}
81} // namespace
82
83//------------------------------------------------------------------------------
84// Public members
85//------------------------------------------------------------------------------
86
94 : m_istrm(istrm), m_strLengthSize(static_cast<uint64_t>(sizeof(int32_t))) {
95 if (!istrm) {
96 throw std::runtime_error("BinaryStreamReader: Input stream is in a bad state. Cannot continue.");
97 }
98}
99
106 readFromStream(m_istrm, value);
107 return *this;
108}
109
116 readFromStream(m_istrm, value);
117 return *this;
118}
119
126 readFromStream(m_istrm, value);
127 return *this;
128}
129
136 readFromStream(m_istrm, value);
137 return *this;
138}
139
146 readFromStream(m_istrm, value);
147 return *this;
148}
149
159 // First read the size.
160 decltype(m_strLengthSize) length(0);
161 m_istrm.read(reinterpret_cast<char *>(&length), m_strLengthSize);
162 // Now the value
163 value.resize(static_cast<std::string::size_type>(length));
164 m_istrm.read(const_cast<char *>(value.data()), static_cast<size_t>(length));
165 return *this;
166}
167
174 readFromStream(m_istrm, value);
175 return *this;
176}
177
184 readFromStream(m_istrm, value);
185 return *this;
186}
187
194BinaryStreamReader &BinaryStreamReader::read(std::vector<int16_t> &value, const size_t nvals) {
195 readFromStream(m_istrm, value, nvals);
196 return *this;
197}
198
205BinaryStreamReader &BinaryStreamReader::read(std::vector<int32_t> &value, const size_t nvals) {
206 readFromStream(m_istrm, value, nvals);
207 return *this;
208}
209
216BinaryStreamReader &BinaryStreamReader::read(std::vector<int64_t> &value, const size_t nvals) {
217 readFromStream(m_istrm, value, nvals);
218 return *this;
219}
220
227BinaryStreamReader &BinaryStreamReader::read(std::vector<float> &value, const size_t nvals) {
228 readFromStream(m_istrm, value, nvals);
229 return *this;
230}
231
238BinaryStreamReader &BinaryStreamReader::read(std::vector<double> &value, const size_t nvals) {
239 readFromStream(m_istrm, value, nvals);
240 return *this;
241}
242
249BinaryStreamReader &BinaryStreamReader::read(std::string &value, const size_t length) {
250 value.resize(length);
251 m_istrm.read(const_cast<char *>(value.data()), length);
252 return *this;
253}
254
264BinaryStreamReader &BinaryStreamReader::read(std::vector<std::string> &value, const std::vector<int32_t> &shape,
266 assert(2 <= shape.size());
267
268 const size_t s0(shape[0]), s1(shape[1]), totalLength(s0 * s1);
269 std::string buffer;
270 this->read(buffer, totalLength);
271 value.resize(s0);
272 if (order == MatrixOrdering::RowMajor) {
273 size_t pos(0);
274 for (auto &str : value) {
275 str = buffer.substr(pos, s1);
276 pos += s1;
277 }
278 } else {
279 for (size_t i = 0; i < s0; ++i) {
280 auto &str = value[i];
281 str.resize(s1);
282 for (size_t j = 0; j < s1; ++j) {
283 str[j] = buffer[j * s0 + i];
284 }
285 }
286 }
287 return *this;
288}
289
301 readFromStream(m_istrm, value, shape, order);
302 return *this;
303}
304
316 readFromStream(m_istrm, value, shape, order);
317 return *this;
318}
319
324void BinaryStreamReader::moveStreamToPosition(size_t nbytes) { m_istrm.seekg(nbytes, std::ios_base::beg); }
325
326} // namespace Mantid::Kernel
double value
The value of the point.
Definition: FitMW.cpp:51
Assists with reading a binary file by providing standard overloads for the istream operators (>>) to ...
BinaryStreamReader(std::istream &istrm)
Constructor taking the stream to read.
BinaryStreamReader & operator>>(int16_t &value)
Read a int16_t from the stream.
std::istream & m_istrm
Reference to the stream being read.
uint64_t m_strLengthSize
The default size in bytes of the type used to encode the length of a string in the file.
MatrixOrdering
Define the ordering of 2D structures in the file.
BinaryStreamReader & read(std::vector< int16_t > &value, const size_t nvals)
Read an array of int16_t into the given vector.
void moveStreamToPosition(size_t nbytes)
Move the stream to nbytes past the beginning of the file.
Numerical Matrix class.
Definition: Matrix.h:42