diff -r 11f3bb408051 -r 62921b370c60 ucx/utils.h --- a/ucx/utils.h Wed Nov 22 12:59:13 2017 +0100 +++ b/ucx/utils.h Sun Jan 21 12:13:09 2018 +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,17 +45,22 @@ #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 * @return a pointer to a copy of s1 that can be passed to free(void*) */ -void *ucx_strcpy(void *s, void *data); +void *ucx_strcpy(const void *s, void *data); /** * Copies a memory area. @@ -64,7 +69,7 @@ * @return a pointer to a copy of the specified memory area that can * be passed to free(void*) */ -void *ucx_memcpy(void *m, void *n); +void *ucx_memcpy(const void *m, void *n); /** @@ -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. @@ -117,7 +144,7 @@ * @param data omitted * @return the result of strcmp(s1, s2) */ -int ucx_strcmp(void *s1, void *s2, void *data); +int ucx_strcmp(const void *s1, const void *s2, void *data); /** * Wraps the strncmp function. @@ -126,7 +153,7 @@ * @param n a pointer to the size_t containing the third strncmp parameter * @return the result of strncmp(s1, s2, *n) */ -int ucx_strncmp(void *s1, void *s2, void *n); +int ucx_strncmp(const void *s1, const void *s2, void *n); /** * Compares two integers of type int. @@ -136,7 +163,7 @@ * @return -1, if *i1 is less than *i2, 0 if both are equal, * 1 if *i1 is greater than *i2 */ -int ucx_intcmp(void *i1, void *i2, void *data); +int ucx_intcmp(const void *i1, const void *i2, void *data); /** * Compares two real numbers of type float. @@ -147,7 +174,7 @@ * 1 if *f1 is greater than *f2 */ -int ucx_floatcmp(void *f1, void *f2, void *data); +int ucx_floatcmp(const void *f1, const void *f2, void *data); /** * Compares two real numbers of type double. @@ -157,7 +184,7 @@ * @return -1, if *d1 is less than *d2, 0 if both are equal, * 1 if *d1 is greater than *d2 */ -int ucx_doublecmp(void *d1, void *d2, void *data); +int ucx_doublecmp(const void *d1, const void *d2, void *data); /** * Compares two pointers. @@ -167,7 +194,7 @@ * @return -1 if ptr1 is less than ptr2, 0 if both are equal, * 1 if ptr1 is greater than ptr2 */ -int ucx_ptrcmp(void *ptr1, void *ptr2, void *data); +int ucx_ptrcmp(const void *ptr1, const void *ptr2, void *data); /** * Compares two memory areas. @@ -176,7 +203,7 @@ * @param n a pointer to the size_t containing the third parameter for memcmp * @return the result of memcmp(ptr1, ptr2, *n) */ -int ucx_memcmp(void *ptr1, void *ptr2, void *n); +int ucx_memcmp(const void *ptr1, const void *ptr2, void *n); /** * A printf() like function which writes the output to a stream by @@ -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