diff -r d7c4ba50b7d8 -r bf19378aed58 ucx/utils.h --- a/ucx/utils.h Fri Nov 18 13:39:20 2016 +0100 +++ b/ucx/utils.h Fri Nov 18 15:27:45 2016 +0100 @@ -1,7 +1,7 @@ /* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * - * Copyright 2015 Olaf Wintermann. All rights reserved. + * Copyright 2016 Olaf Wintermann. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: @@ -45,11 +45,16 @@ #include "ucx.h" #include "string.h" #include "allocator.h" -#include +#include #include #include /** + * Default buffer size for ucx_stream_copy() and ucx_stream_ncopy(). + */ +#define UCX_STREAM_COPY_BUFSIZE 4096 + +/** * Copies a string. * @param s the string to copy * @param data omitted @@ -82,23 +87,26 @@ * @param n the maximum number of bytes that shall be copied * @return the total number of bytes copied */ -size_t ucx_stream_copy(void *src, void *dest, read_func rfnc, write_func wfnc, +size_t ucx_stream_bncopy(void *src, void *dest, read_func rfnc, write_func wfnc, char* buf, size_t bufsize, size_t n); /** - * Shorthand for ucx_stream_copy using the default copy buffer. + * Shorthand for an unbounded ucx_stream_bncopy call using a default buffer. * * @param src the source stream * @param dest the destination stream * @param rfnc the read function * @param wfnc the write function * @return total number of bytes copied + * + * @see #UCX_STREAM_COPY_BUFSIZE */ -#define ucx_stream_hcopy(src,dest,rfnc,wfnc) ucx_stream_copy(\ - src, dest, (read_func)rfnc, (write_func)wfnc, NULL, 0x100, SIZE_MAX) +#define ucx_stream_copy(src,dest,rfnc,wfnc) ucx_stream_bncopy(\ + src, dest, (read_func)rfnc, (write_func)wfnc, \ + NULL, UCX_STREAM_COPY_BUFSIZE, (size_t)-1) /** - * Shorthand for ucx_stream_copy using the default copy buffer and a copy limit. + * Shorthand for ucx_stream_bncopy using a default copy buffer. * * @param src the source stream * @param dest the destination stream @@ -107,8 +115,27 @@ * @param n maximum number of bytes that shall be copied * @return total number of bytes copied */ -#define ucx_stream_ncopy(src,dest,rfnc,wfnc, n) ucx_stream_copy(\ - src, dest, (read_func)rfnc, (write_func)wfnc, NULL, 0x100, n) +#define ucx_stream_ncopy(src,dest,rfnc,wfnc, n) ucx_stream_bncopy(\ + src, dest, (read_func)rfnc, (write_func)wfnc, \ + NULL, UCX_STREAM_COPY_BUFSIZE, n) + +/** + * Shorthand for an unbounded ucx_stream_bncopy call using the specified buffer. + * + * @param src the source stream + * @param dest the destination stream + * @param rfnc the read function + * @param wfnc the write function + * @param buf a pointer to the copy buffer or NULL if a buffer + * shall be implicitly created on the heap + * @param bufsize the size of the copy buffer - if NULL was + * provided for buf, this is the size of the buffer that shall be + * implicitly created + * @return total number of bytes copied + */ +#define ucx_stream_bcopy(src,dest,rfnc,wfnc, buf, bufsize) ucx_stream_bncopy(\ + src, dest, (read_func)rfnc, (write_func)wfnc, \ + buf, bufsize, (size_t)-1) /** * Wraps the strcmp function. @@ -219,10 +246,6 @@ */ sstr_t ucx_asprintf(UcxAllocator *allocator, const char *fmt, ...); -/** Shortcut for ucx_asprintf() with default allocator. */ -#define ucx_sprintf(fmt, ...) \ - ucx_asprintf(ucx_default_allocator(), fmt, __VA_ARGS__) - /** * va_list version of ucx_asprintf(). * @@ -234,8 +257,12 @@ */ sstr_t ucx_vasprintf(UcxAllocator *allocator, const char *fmt, va_list ap); +/** Shortcut for ucx_asprintf() with default allocator. */ +#define ucx_sprintf(...) \ + ucx_asprintf(ucx_default_allocator(), __VA_ARGS__) + /** - * A printf() like function which writes the output to an + * A printf() like function which writes the output to a * UcxBuffer. * * @param buffer the buffer the data is written to