1#ifndef IOUTILITIES_COPY_H
2#define IOUTILITIES_COPY_H
5#if defined(CPP_UTILITIES_USE_PLATFORM_SPECIFIC_API_FOR_OPTIMIZING_COPY_HELPER) && defined(CPP_UTILITIES_USE_NATIVE_FILE_BUFFER) \
6 && defined(PLATFORM_LINUX)
7#define CPP_UTILITIES_USE_SEND_FILE
11#ifdef CPP_UTILITIES_USE_SEND_FILE
13#include <sys/sendfile.h>
18#ifdef CPP_UTILITIES_USE_SEND_FILE
33 void copy(std::istream &input, std::ostream &output, std::uint64_t count);
34 void callbackCopy(std::istream &input, std::ostream &output, std::uint64_t count,
const std::function<
bool(
void)> &isAborted,
35 const std::function<
void(
double)> &callback);
38 const std::function<
void(
double)> &callback);
42 char m_buffer[bufferSize];
64 input.read(m_buffer,
static_cast<std::streamsize
>(
count));
65 output.write(m_buffer,
static_cast<std::streamsize
>(
count));
78template <std::
size_t bufferSize>
80 const std::function<
void(
double)> &callback)
92 input.read(m_buffer,
static_cast<std::streamsize
>(
count));
93 output.write(m_buffer,
static_cast<std::streamsize
>(
count));
106#ifdef CPP_UTILITIES_USE_SEND_FILE
107 if (
output.fileDescriptor() != -1 &&
input.fileDescriptor() != -1 &&
output.fileDescriptor() !=
input.fileDescriptor()) {
122 throw std::ios_base::failure(
argsToString(
"sendfile64() failed: ", std::strerror(
errno)));
136 copy(
static_cast<std::istream &
>(
input),
static_cast<std::ostream &
>(
output),
count);
150template <std::
size_t bufferSize>
152 const std::function<
bool(
void)> &
isAborted,
const std::function<
void(
double)> &callback)
154#ifdef CPP_UTILITIES_USE_SEND_FILE
155 if (
output.fileDescriptor() != -1 &&
input.fileDescriptor() != -1 &&
output.fileDescriptor() !=
input.fileDescriptor()) {
171 throw std::ios_base::failure(
argsToString(
"sendfile64() failed: ", std::strerror(
errno)));
The CopyHelper class helps to copy bytes from one stream to another.
char * buffer()
Returns the internal buffer.
void copy(std::istream &input, std::ostream &output, std::uint64_t count)
Copies count bytes from input to output.
void callbackCopy(std::istream &input, std::ostream &output, std::uint64_t count, const std::function< bool(void)> &isAborted, const std::function< void(double)> &callback)
Copies count bytes from input to output.
CopyHelper()
Constructs a new copy helper.
#define CPP_UTILITIES_EXPORT
Marks the symbol to be exported by the c++utilities library.
Contains all utilities provides by the c++utilities library.
IntegralType stringToNumber(const StringType &string, BaseType base=10)
Converts the given string to an unsigned/signed number assuming string uses the specified base.
std::fstream NativeFileStream
StringType argsToString(Args &&...args)