stream_util.h
Go to the documentation of this file.
1 
9 #pragma once
10 
11 #include <istream>
12 #include <cstddef>
13 #include <vector>
14 #include <cstring>
15 #include "interop/util/exception.h"
18 #include "interop/util/cstdint.h"
20 
21 namespace illumina { namespace interop { namespace io
22 {
23 
30  template<class T>
31  void read_binary(std::istream &in, T *buffer, const size_t n)
32  {
33  in.read(reinterpret_cast<char *>(buffer), n * sizeof(T));
34  }
35 
41  template<class T>
42  void read_binary(std::istream &in, T &buffer)
43  {
44  read_binary(in, &buffer, 1);
45  }
52  template<class T>
53  void read_binary(char*& in, T *buffer, const size_t n)
54  {
55  std::memcpy(reinterpret_cast<char *>(buffer), in, n * sizeof(T));
56  in+=n * sizeof(T);
57  }
63  template<class T>
64  void read_binary(char*& in, T &buffer)
65  {
66  read_binary(in, &buffer, 1);
67  }
68 
75  template<class T>
76  std::streamsize read_binary_with_count(std::istream &in, T &buffer)
77  {
78  read_binary(in, &buffer, 1);
79  return in.gcount();
80  }
81 
88  template<class T>
89  std::streamsize read_binary_with_count(char*& in, T &buffer)
90  {
91  read_binary(in, &buffer, 1);
92  return static_cast<std::streamsize >(sizeof(T));
93  }
94 
100  template<class T>
101  T read_binary(std::istream &in)
102  {
103  T val;
104  read_binary(in, &val, 1);
105  return val;
106  }
107 
114  template<class T>
115  void read_binary(std::istream &in, std::vector<T> &buffer, const size_t n)
116  {
117  INTEROP_ASSERT(!buffer.empty());
118  read_binary(in, &buffer.front(), n);
119  }
120 
126  template<class T>
127  void read_binary(std::istream &in, std::vector<T> &buffer)
128  {
129  INTEROP_ASSERT(!buffer.empty());
130  read_binary(in, &buffer.front(), buffer.size());
131  }
132 
139  inline void read_binary(std::istream &in, std::string &str, const std::string& default_val)
140  {
141  const size_t length = read_binary< ::uint16_t >(in);
142  if (in.fail()) INTEROP_THROW(incomplete_file_exception, "Unexpected end of file");
143  if (length > 0)
144  {
145  str.assign(length, ' ');
146  read_binary(in, const_cast<char *>(str.c_str()), length);
147  }
148  else str = default_val;
149  }
150 
157  template<class T>
158  void write_binary(std::ostream &out, const T *buffer, const size_t n)
159  {
160  out.write(reinterpret_cast<const char *>(buffer), n * sizeof(T));
161  }
162 
168  template<class T>
169  void write_binary(std::ostream &out, const T &buffer)
170  {
171  write_binary(out, &buffer, 1);
172  }
173 
180  template<class T>
181  void write_binary(std::ostream &out, const std::vector<T> &buffer, const size_t n)
182  {
183  if(buffer.empty())return;
184  write_binary(out, &buffer.front(), n);
185  }
186 
192  template<class T>
193  void write_binary(std::ostream &out, const std::vector<T> &buffer)
194  {
195  write_binary(out, buffer, buffer.size());
196  }
197 
203  inline void write_binary(std::ostream &out, const std::string &str)
204  {
205  const ::uint16_t len = static_cast< ::uint16_t >(str.size());
206  write_binary(out, len);
207  if (len > 0) write_binary(out, const_cast<char *>(str.c_str()), len);
208  }
209 
215  inline std::streamsize scount(std::istream &in)
216  {
217  return in.gcount();
218  }
219 
225  inline std::streamsize scount(std::ostream &out)
226  {
227  return out.tellp();
228  }
229 
230 }}}
231 
232 
Definition: enum_description.h:15
#define INTEROP_THROW(EXCEPTION, MESSAGE)
Definition: exception.h:18
#define INTEROP_ASSERT(TST)
Definition: assert.h:21
void write_binary(std::ostream &out, const T *buffer, const size_t n)
Definition: stream_util.h:158
Definition: enums.h:301
std::streamsize scount(std::istream &in)
Definition: stream_util.h:215
std::streamsize read_binary_with_count(std::istream &in, T &buffer)
Definition: stream_util.h:76
Definition: stream_exceptions.h:73
void read_binary(std::istream &in, T *buffer, const size_t n)
Definition: stream_util.h:31