C++ Utilities  5.10.5
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 "./nativefilestream.h"
5 
6 #include <functional>
7 #include <iostream>
8 
9 namespace CppUtilities {
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  void copy(NativeFileStream &input, NativeFileStream &output, std::size_t count);
23  void callbackCopy(NativeFileStream &input, NativeFileStream &output, std::size_t count, const std::function<bool(void)> &isAborted,
24  const std::function<void(double)> &callback);
25  char *buffer();
26 
27 private:
28  char m_buffer[bufferSize];
29 };
30 
34 template <std::size_t bufferSize> CopyHelper<bufferSize>::CopyHelper()
35 {
36 }
37 
43 template <std::size_t bufferSize> void CopyHelper<bufferSize>::copy(std::istream &input, std::ostream &output, std::size_t count)
44 {
45  while (count > bufferSize) {
46  input.read(m_buffer, bufferSize);
47  output.write(m_buffer, bufferSize);
48  count -= bufferSize;
49  }
50  input.read(m_buffer, static_cast<std::streamsize>(count));
51  output.write(m_buffer, static_cast<std::streamsize>(count));
52 }
53 
64 template <std::size_t bufferSize>
65 void CopyHelper<bufferSize>::callbackCopy(std::istream &input, std::ostream &output, std::size_t count, const std::function<bool(void)> &isAborted,
66  const std::function<void(double)> &callback)
67 {
68  const auto totalBytes = count;
69  while (count > bufferSize) {
70  input.read(m_buffer, bufferSize);
71  output.write(m_buffer, bufferSize);
72  count -= bufferSize;
73  if (isAborted()) {
74  return;
75  }
76  callback(static_cast<double>(totalBytes - count) / static_cast<double>(totalBytes));
77  }
78  input.read(m_buffer, static_cast<std::streamsize>(count));
79  output.write(m_buffer, static_cast<std::streamsize>(count));
80  callback(1.0);
81 }
82 
90 template <std::size_t bufferSize> void CopyHelper<bufferSize>::copy(NativeFileStream &input, NativeFileStream &output, std::size_t count)
91 {
92  copy(static_cast<std::istream &>(input), static_cast<std::ostream &>(output), count);
93 }
94 
106 template <std::size_t bufferSize>
108  const std::function<bool(void)> &isAborted, const std::function<void(double)> &callback)
109 {
110  callbackCopy(static_cast<std::istream &>(input), static_cast<std::ostream &>(output), count, isAborted, callback);
111 }
112 
116 template <std::size_t bufferSize> char *CopyHelper<bufferSize>::buffer()
117 {
118  return m_buffer;
119 }
120 } // namespace CppUtilities
121 
122 #endif // IOUTILITIES_COPY_H
The CopyHelper class helps to copy bytes from one stream to another.
Definition: copy.h:16
char * buffer()
Returns the internal buffer.
Definition: copy.h:116
void copy(std::istream &input, std::ostream &output, std::size_t count)
Copies count bytes from input to output.
Definition: copy.h:43
CopyHelper()
Constructs a new copy helper.
Definition: copy.h:34
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:65
#define CPP_UTILITIES_EXPORT
Marks the symbol to be exported by the c++utilities library.
Contains all utilities provides by the c++utilities library.
std::fstream NativeFileStream