C++ Utilities  4.6.1
Common C++ classes and routines used by my applications 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 <iostream>
7 #include <functional>
8 
9 namespace IoUtilities {
10 
16 template<std::size_t bufferSize>
18 {
19 public:
20  CopyHelper();
21  void copy(std::istream &input, std::ostream &output, std::size_t count);
22  void callbackCopy(std::istream &input, std::ostream &output, std::size_t count, const std::function<bool (void)> &isAborted, const std::function<void (double)> &callback);
23  char *buffer();
24 private:
25  char m_buffer[bufferSize];
26 };
27 
31 template<std::size_t bufferSize>
33 {}
34 
40 template<std::size_t bufferSize>
41 void CopyHelper<bufferSize>::copy(std::istream &input, std::ostream &output, std::size_t count)
42 {
43  while(count > bufferSize) {
44  input.read(m_buffer, bufferSize);
45  output.write(m_buffer, bufferSize);
46  count -= bufferSize;
47  }
48  input.read(m_buffer, count);
49  output.write(m_buffer, count);
50 }
51 
52 
63 template<std::size_t bufferSize>
64 void CopyHelper<bufferSize>::callbackCopy(std::istream &input, std::ostream &output, std::size_t count, const std::function<bool (void)> &isAborted, const std::function<void (double)> &callback)
65 {
66  const std::size_t totalBytes = count;
67  while(count > bufferSize) {
68  input.read(m_buffer, bufferSize);
69  output.write(m_buffer, bufferSize);
70  count -= bufferSize;
71  if(isAborted()) {
72  return;
73  }
74  callback(static_cast<double>(totalBytes - count) / totalBytes);
75  }
76  input.read(m_buffer, count);
77  output.write(m_buffer, count);
78  callback(1.0);
79 }
80 
84 template<std::size_t bufferSize>
86 {
87  return m_buffer;
88 }
89 
90 }
91 
92 #endif // IOUTILITIES_COPY_H
char * buffer()
Returns the internal buffer.
Definition: copy.h:85
The CopyHelper class helps to copy bytes from one stream to another.
Definition: copy.h:17
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:64
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:41
#define CPP_UTILITIES_EXPORT
Marks the symbol to be exported by the c++utilities library.
CopyHelper()
Constructs a new copy helper.
Definition: copy.h:32