C++ Utilities  4.17.0
Useful C++ classes and routines such as argument parser, IO and conversion utilities
copy.h
Go to the documentation of this file.
1 #ifndef IOUTILITIES_COPY_H
2 #define IOUTILITIES_COPY_H
3 
4 #include "../global.h"
5 
6 #include <functional>
7 #include <iostream>
8 
9 namespace IoUtilities {
10 
16 template <std::size_t bufferSize> class CPP_UTILITIES_EXPORT CopyHelper {
17 public:
18  CopyHelper();
19  void copy(std::istream &input, std::ostream &output, std::size_t count);
20  void callbackCopy(std::istream &input, std::ostream &output, std::size_t count, const std::function<bool(void)> &isAborted,
21  const std::function<void(double)> &callback);
22  char *buffer();
23 
24 private:
25  char m_buffer[bufferSize];
26 };
27 
31 template <std::size_t bufferSize> CopyHelper<bufferSize>::CopyHelper()
32 {
33 }
34 
40 template <std::size_t bufferSize> void CopyHelper<bufferSize>::copy(std::istream &input, std::ostream &output, std::size_t count)
41 {
42  while (count > bufferSize) {
43  input.read(m_buffer, bufferSize);
44  output.write(m_buffer, bufferSize);
45  count -= bufferSize;
46  }
47  input.read(m_buffer, count);
48  output.write(m_buffer, count);
49 }
50 
61 template <std::size_t bufferSize>
62 void CopyHelper<bufferSize>::callbackCopy(std::istream &input, std::ostream &output, std::size_t count, const std::function<bool(void)> &isAborted,
63  const std::function<void(double)> &callback)
64 {
65  const std::size_t totalBytes = count;
66  while (count > bufferSize) {
67  input.read(m_buffer, bufferSize);
68  output.write(m_buffer, bufferSize);
69  count -= bufferSize;
70  if (isAborted()) {
71  return;
72  }
73  callback(static_cast<double>(totalBytes - count) / totalBytes);
74  }
75  input.read(m_buffer, count);
76  output.write(m_buffer, count);
77  callback(1.0);
78 }
79 
83 template <std::size_t bufferSize> char *CopyHelper<bufferSize>::buffer()
84 {
85  return m_buffer;
86 }
87 } // namespace IoUtilities
88 
89 #endif // IOUTILITIES_COPY_H
#define CPP_UTILITIES_EXPORT
char * buffer()
Returns the internal buffer.
Definition: copy.h:83
The CopyHelper class helps to copy bytes from one stream to another.
Definition: copy.h:16
void callbackCopy(std::istream &input, std::ostream &output, std::size_t count, const std::function< bool(void)> &isAborted, const std::function< void(double)> &callback)
Copies count bytes from input to output.
Definition: copy.h:62
Contains utility classes helping to read and write streams.
Definition: binaryreader.h:10
void copy(std::istream &input, std::ostream &output, std::size_t count)
Copies count bytes from input to output.
Definition: copy.h:40
CopyHelper()
Constructs a new copy helper.
Definition: copy.h:31