3#include <clod/stream.h>
5int clod_stream_copy(
clod_stream *dst,
clod_stream *src,
void *
const buffer,
const size_t buffer_size,
size_t *total_transferred) {
6 if (buffer_size == 0) {
7 debug(CLOD_DEBUG,
"Invalid buffer size for stream copy.");
8 return CLOD_STREAM_INVALID;
11 if (dst->
write ==
nullptr) {
12 debug(CLOD_DEBUG,
"Provided stream %ptr doesn't support writing.", (
void*)dst);
13 return CLOD_STREAM_INVALID;
16 if (src->
read ==
nullptr) {
17 debug(CLOD_DEBUG,
"Provided stream %ptr doesn't support reading.", (
void*)src);
18 return CLOD_STREAM_INVALID;
21 size_t transferred = 0;
24 int read_res = src->
read(src, &buff);
28 write_res = dst->
write(dst, &buff);
29 transferred += (size_t)buff.
ptr - (
size_t)buffer;
33 debug(CLOD_DEBUG,
"Short write to stream %ptr.", (
void*)dst);
36 if (read_res == CLOD_STREAM_EOF) {
37 if (total_transferred) *total_transferred = transferred;
41 if (read_res != CLOD_STREAM_OK) {
42 if (total_transferred) *total_transferred = transferred;
45 if (write_res != CLOD_STREAM_OK) {
46 if (total_transferred) *total_transferred = transferred;
int(* write)(clod_stream *self, struct clod_string *src)
int(* read)(clod_stream *self, struct clod_string *dst)