cpp-utilities/io/copy.h

90 lines
2.6 KiB
C
Raw Normal View History

2015-04-22 18:36:40 +02:00
#ifndef IOUTILITIES_COPY_H
#define IOUTILITIES_COPY_H
#include "../global.h"
2015-04-22 18:36:40 +02:00
#include <iostream>
#include <functional>
namespace IoUtilities {
/*!
* \class IoUtilities::CopyHelper
* \brief The copy helper class helps to copy bytes from one stream to another.
* \tparam Specifies the buffer size.
*/
template<std::size_t bufferSize>
class CPP_UTILITIES_EXPORT CopyHelper
2015-04-22 18:36:40 +02:00
{
public:
CopyHelper();
void copy(std::istream &input, std::ostream &output, std::size_t count);
void callbackCopy(std::istream &input, std::ostream &output, std::size_t count, const std::function<bool (void)> &isAborted, const std::function<void (double)> &callback);
2016-01-18 23:41:30 +01:00
char *buffer();
2015-04-22 18:36:40 +02:00
private:
char m_buffer[bufferSize];
};
/*!
* \brief Constructs a new copy helper.
*/
template<std::size_t bufferSize>
CopyHelper<bufferSize>::CopyHelper()
{}
/*!
* \brief Copies \a count bytes from \a input to \a output.
* \remarks Set an exception mask using std::ios::exceptions() to get
* a std::ios_base::failure exception when an IO error occurs.
*/
template<std::size_t bufferSize>
void CopyHelper<bufferSize>::copy(std::istream &input, std::ostream &output, std::size_t count)
{
while(count > bufferSize) {
input.read(m_buffer, bufferSize);
output.write(m_buffer, bufferSize);
count -= bufferSize;
}
input.read(m_buffer, count);
output.write(m_buffer, count);
}
/*!
* \brief Copies \a count bytes from \a input to \a output. The procedure might be abortet. Progress updates will be reportet.
*
2016-01-18 23:41:30 +01:00
* Copying is aborted when \a isAborted returns true. The current progress is reported by calling the specified \a callback function.
2015-04-22 18:36:40 +02:00
*
2016-01-18 23:41:30 +01:00
* \remarks Set an exception mask using std::ios::exceptions() to get a std::ios_base::failure exception when an IO error occurs.
2015-04-22 18:36:40 +02:00
*/
template<std::size_t bufferSize>
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)
{
std::size_t totalBytes = count;
while(count > bufferSize) {
input.read(m_buffer, bufferSize);
output.write(m_buffer, bufferSize);
count -= bufferSize;
if(isAborted()) {
return;
}
callback(static_cast<double>(totalBytes - count) / totalBytes);
}
input.read(m_buffer, count);
output.write(m_buffer, count);
callback(1.0);
}
2016-01-18 23:41:30 +01:00
/*!
* \brief Returns the internal buffer.
*/
template<std::size_t bufferSize>
char *CopyHelper<bufferSize>::buffer()
{
return m_buffer;
}
2015-04-22 18:36:40 +02:00
}
#endif // IOUTILITIES_COPY_H