rename utils.h to streams.h

Sat, 02 Nov 2024 13:48:53 +0100

author
Mike Becker <universe@uap-core.de>
date
Sat, 02 Nov 2024 13:48:53 +0100
changeset 971
cc204fc56c9c
parent 970
c9b02747cfc5
child 972
a9a1d07a6840

rename utils.h to streams.h

resolves #470

CHANGELOG file | annotate | diff | comparison | revisions
msvc/libucx/libucx.vcxproj file | annotate | diff | comparison | revisions
msvc/libucx/libucx.vcxproj.filters file | annotate | diff | comparison | revisions
msvc/ucxtest/ucxtest.vcxproj file | annotate | diff | comparison | revisions
msvc/ucxtest/ucxtest.vcxproj.filters file | annotate | diff | comparison | revisions
src/Makefile file | annotate | diff | comparison | revisions
src/cx/streams.h file | annotate | diff | comparison | revisions
src/cx/utils.h file | annotate | diff | comparison | revisions
src/streams.c file | annotate | diff | comparison | revisions
src/szmul.c file | annotate | diff | comparison | revisions
src/utils.c file | annotate | diff | comparison | revisions
tests/Makefile file | annotate | diff | comparison | revisions
tests/test_streams.c file | annotate | diff | comparison | revisions
tests/test_utils.c file | annotate | diff | comparison | revisions
tests/ucxtest.c file | annotate | diff | comparison | revisions
--- a/CHANGELOG	Sat Nov 02 13:38:51 2024 +0100
+++ b/CHANGELOG	Sat Nov 02 13:48:53 2024 +0100
@@ -12,14 +12,16 @@
  * adds runtime constants to read out the actual SBO sizes
  * adds improved version of UCX 2 Test framework (now a self-contained header)
  * adds cx_nmemb() utility function to common.h
- * move cx_szmul() to common.h
- * the cx_compare_func symbol is now declared by compare.h
- * fixes wrong link from UCX 2 documentation to UCX 3 documentation
- * fixes critical bug that produced wrong results when comparing lists of different type but same size
+ * moves cx_compare_func typedef to compare.h
+ * moves cx_szmul() to common.h
+ * moves stream copy functions to new streams.h
+ * removes utils.h
  * removes flag_removal function from iterator (unfortunately breaks binary compatibility)
  * removes CMake
  * removes GTest dependency
  * removes flags to disable SBO in tests
+ * fixes wrong link from UCX 2 documentation to UCX 3 documentation
+ * fixes critical bug that produced wrong results when comparing lists of different type but same size
 
 Version 3.0 - 2023-07-09
 ------------------------
--- a/msvc/libucx/libucx.vcxproj	Sat Nov 02 13:38:51 2024 +0100
+++ b/msvc/libucx/libucx.vcxproj	Sat Nov 02 13:48:53 2024 +0100
@@ -38,7 +38,7 @@
     <ClInclude Include="..\..\src\cx\string.h" />
     <ClInclude Include="..\..\src\cx\test.h" />
     <ClInclude Include="..\..\src\cx\tree.h" />
-    <ClInclude Include="..\..\src\cx\utils.h" />
+    <ClInclude Include="..\..\src\cx\streams.h" />
   </ItemGroup>
   <ItemGroup>
     <ClCompile Include="..\..\src\allocator.c" />
@@ -57,7 +57,7 @@
     <ClCompile Include="..\..\src\properties.c" />
     <ClCompile Include="..\..\src\string.c" />
     <ClCompile Include="..\..\src\tree.c" />
-    <ClCompile Include="..\..\src\utils.c" />
+    <ClCompile Include="..\..\src\streams.c" />
   </ItemGroup>
   <PropertyGroup Label="Globals">
     <VCProjectVersion>17.0</VCProjectVersion>
--- a/msvc/libucx/libucx.vcxproj.filters	Sat Nov 02 13:38:51 2024 +0100
+++ b/msvc/libucx/libucx.vcxproj.filters	Sat Nov 02 13:48:53 2024 +0100
@@ -68,7 +68,7 @@
     <ClInclude Include="..\..\src\cx\tree.h">
       <Filter>Header</Filter>
     </ClInclude>
-    <ClInclude Include="..\..\src\cx\utils.h">
+    <ClInclude Include="..\..\src\cx\streams.h">
       <Filter>Header</Filter>
     </ClInclude>
   </ItemGroup>
@@ -121,7 +121,7 @@
     <ClCompile Include="..\..\src\tree.c">
       <Filter>Source</Filter>
     </ClCompile>
-    <ClCompile Include="..\..\src\utils.c">
+    <ClCompile Include="..\..\src\streams.c">
       <Filter>Source</Filter>
     </ClCompile>
   </ItemGroup>
--- a/msvc/ucxtest/ucxtest.vcxproj	Sat Nov 02 13:38:51 2024 +0100
+++ b/msvc/ucxtest/ucxtest.vcxproj	Sat Nov 02 13:48:53 2024 +0100
@@ -33,7 +33,7 @@
     <ClCompile Include="..\..\tests\test_string.c" />
     <ClCompile Include="..\..\tests\test_szmul.c" />
     <ClCompile Include="..\..\tests\test_tree.c" />
-    <ClCompile Include="..\..\tests\test_utils.c" />
+    <ClCompile Include="..\..\tests\test_streams.c" />
     <ClCompile Include="..\..\tests\ucxtest.c" />
     <ClCompile Include="..\..\tests\util_allocator.c" />
   </ItemGroup>
--- a/msvc/ucxtest/ucxtest.vcxproj.filters	Sat Nov 02 13:38:51 2024 +0100
+++ b/msvc/ucxtest/ucxtest.vcxproj.filters	Sat Nov 02 13:48:53 2024 +0100
@@ -53,7 +53,7 @@
     <ClCompile Include="..\..\tests\test_tree.c">
       <Filter>Source</Filter>
     </ClCompile>
-    <ClCompile Include="..\..\tests\test_utils.c">
+    <ClCompile Include="..\..\tests\test_streams.c">
       <Filter>Source</Filter>
     </ClCompile>
     <ClCompile Include="..\..\tests\ucxtest.c">
--- a/src/Makefile	Sat Nov 02 13:38:51 2024 +0100
+++ b/src/Makefile	Sat Nov 02 13:48:53 2024 +0100
@@ -25,7 +25,7 @@
 
 SRC = allocator.c array_list.c buffer.c compare.c hash_key.c hash_map.c \
   iterator.c linked_list.c list.c map.c mempool.c printf.c string.c tree.c \
-  utils.c properties.c json.c
+  streams.c szmul.c properties.c json.c
 
 OBJ_EXT=.o
 OBJ=$(SRC:%.c=$(build_dir)/%$(OBJ_EXT))
@@ -137,12 +137,16 @@
 	@echo "Compiling $<"
 	$(CC) -o $@ $(CFLAGS) -c $<
 
+$(build_dir)/streams$(OBJ_EXT): streams.c cx/streams.h cx/common.h
+	@echo "Compiling $<"
+	$(CC) -o $@ $(CFLAGS) -c $<
+
 $(build_dir)/string$(OBJ_EXT): string.c cx/string.h cx/common.h \
  cx/allocator.h
 	@echo "Compiling $<"
 	$(CC) -o $@ $(CFLAGS) -c $<
 
-$(build_dir)/szmul$(OBJ_EXT): szmul.c
+$(build_dir)/szmul$(OBJ_EXT): szmul.c cx/common.h
 	@echo "Compiling $<"
 	$(CC) -o $@ $(CFLAGS) -c $<
 
@@ -151,7 +155,3 @@
 	@echo "Compiling $<"
 	$(CC) -o $@ $(CFLAGS) -c $<
 
-$(build_dir)/utils$(OBJ_EXT): utils.c cx/utils.h cx/common.h
-	@echo "Compiling $<"
-	$(CC) -o $@ $(CFLAGS) -c $<
-
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/cx/streams.h	Sat Nov 02 13:48:53 2024 +0100
@@ -0,0 +1,130 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2021 Mike Becker, 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:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/**
+ * \file streams.h
+ *
+ * \brief Utility functions for data streams.
+ *
+ * \author Mike Becker
+ * \author Olaf Wintermann
+ * \copyright 2-Clause BSD License
+ */
+
+#ifndef UCX_STREAMS_H
+#define UCX_STREAMS_H
+
+#include "common.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * Reads data from a stream and writes it to another stream.
+ *
+ * @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 \c NULL if a buffer
+ * shall be implicitly created on the heap
+ * @param bufsize the size of the copy buffer - if \p buf is \c NULL you can
+ * set this to zero to let the implementation decide
+ * @param n the maximum number of bytes that shall be copied.
+ * If this is larger than \p bufsize, the content is copied over multiple
+ * iterations.
+ * @return the total number of bytes copied
+ */
+__attribute__((__nonnull__(1, 2, 3, 4)))
+size_t cx_stream_bncopy(
+        void *src,
+        void *dest,
+        cx_read_func rfnc,
+        cx_write_func wfnc,
+        char *buf,
+        size_t bufsize,
+        size_t n
+);
+
+/**
+ * Reads data from a stream and writes it to another stream.
+ *
+ * @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 \c NULL if a buffer
+ * shall be implicitly created on the heap
+ * @param bufsize the size of the copy buffer - if \p buf is \c NULL you can
+ * set this to zero to let the implementation decide
+ * @return total number of bytes copied
+ */
+#define cx_stream_bcopy(src, dest, rfnc, wfnc, buf, bufsize) \
+    cx_stream_bncopy(src, dest, rfnc, wfnc, buf, bufsize, SIZE_MAX)
+
+/**
+ * Reads data from a stream and writes it to another stream.
+ *
+ * The data is temporarily stored in a stack allocated buffer.
+ *
+ * @param src the source stream
+ * @param dest the destination stream
+ * @param rfnc the read function
+ * @param wfnc the write function
+ * @param n the maximum number of bytes that shall be copied.
+ * @return total number of bytes copied
+ */
+__attribute__((__nonnull__))
+size_t cx_stream_ncopy(
+        void *src,
+        void *dest,
+        cx_read_func rfnc,
+        cx_write_func wfnc,
+        size_t n
+);
+
+/**
+ * Reads data from a stream and writes it to another stream.
+ *
+ * The data is temporarily stored in a stack allocated 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
+ */
+#define cx_stream_copy(src, dest, rfnc, wfnc) \
+    cx_stream_ncopy(src, dest, rfnc, wfnc, SIZE_MAX)
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // UCX_STREAMS_H
--- a/src/cx/utils.h	Sat Nov 02 13:38:51 2024 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,130 +0,0 @@
-/*
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
- *
- * Copyright 2021 Mike Becker, 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:
- *
- *   1. Redistributions of source code must retain the above copyright
- *      notice, this list of conditions and the following disclaimer.
- *
- *   2. Redistributions in binary form must reproduce the above copyright
- *      notice, this list of conditions and the following disclaimer in the
- *      documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
- */
-
-/**
- * \file utils.h
- *
- * \brief General purpose utility functions.
- *
- * \author Mike Becker
- * \author Olaf Wintermann
- * \copyright 2-Clause BSD License
- */
-
-#ifndef UCX_UTILS_H
-#define UCX_UTILS_H
-
-#include "common.h"
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-/**
- * Reads data from a stream and writes it to another stream.
- *
- * @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 \c NULL if a buffer
- * shall be implicitly created on the heap
- * @param bufsize the size of the copy buffer - if \p buf is \c NULL you can
- * set this to zero to let the implementation decide
- * @param n the maximum number of bytes that shall be copied.
- * If this is larger than \p bufsize, the content is copied over multiple
- * iterations.
- * @return the total number of bytes copied
- */
-__attribute__((__nonnull__(1, 2, 3, 4)))
-size_t cx_stream_bncopy(
-        void *src,
-        void *dest,
-        cx_read_func rfnc,
-        cx_write_func wfnc,
-        char *buf,
-        size_t bufsize,
-        size_t n
-);
-
-/**
- * Reads data from a stream and writes it to another stream.
- *
- * @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 \c NULL if a buffer
- * shall be implicitly created on the heap
- * @param bufsize the size of the copy buffer - if \p buf is \c NULL you can
- * set this to zero to let the implementation decide
- * @return total number of bytes copied
- */
-#define cx_stream_bcopy(src, dest, rfnc, wfnc, buf, bufsize) \
-    cx_stream_bncopy(src, dest, rfnc, wfnc, buf, bufsize, SIZE_MAX)
-
-/**
- * Reads data from a stream and writes it to another stream.
- *
- * The data is temporarily stored in a stack allocated buffer.
- *
- * @param src the source stream
- * @param dest the destination stream
- * @param rfnc the read function
- * @param wfnc the write function
- * @param n the maximum number of bytes that shall be copied.
- * @return total number of bytes copied
- */
-__attribute__((__nonnull__))
-size_t cx_stream_ncopy(
-        void *src,
-        void *dest,
-        cx_read_func rfnc,
-        cx_write_func wfnc,
-        size_t n
-);
-
-/**
- * Reads data from a stream and writes it to another stream.
- *
- * The data is temporarily stored in a stack allocated 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
- */
-#define cx_stream_copy(src, dest, rfnc, wfnc) \
-    cx_stream_ncopy(src, dest, rfnc, wfnc, SIZE_MAX)
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif // UCX_UTILS_H
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/streams.c	Sat Nov 02 13:48:53 2024 +0100
@@ -0,0 +1,95 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2021 Mike Becker, 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:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "cx/streams.h"
+
+#ifndef CX_STREAM_BCOPY_BUF_SIZE
+#define CX_STREAM_BCOPY_BUF_SIZE 8192
+#endif
+
+#ifndef CX_STREAM_COPY_BUF_SIZE
+#define CX_STREAM_COPY_BUF_SIZE 1024
+#endif
+
+size_t cx_stream_bncopy(
+        void *src,
+        void *dest,
+        cx_read_func rfnc,
+        cx_write_func wfnc,
+        char *buf,
+        size_t bufsize,
+        size_t n
+) {
+    if (n == 0) {
+        return 0;
+    }
+
+    char *lbuf;
+    size_t ncp = 0;
+
+    if (buf) {
+        if (bufsize == 0) return 0;
+        lbuf = buf;
+    } else {
+        if (bufsize == 0) bufsize = CX_STREAM_BCOPY_BUF_SIZE;
+        lbuf = malloc(bufsize);
+        if (lbuf == NULL) {
+            return 0;
+        }
+    }
+
+    size_t r;
+    size_t rn = bufsize > n ? n : bufsize;
+    while ((r = rfnc(lbuf, 1, rn, src)) != 0) {
+        r = wfnc(lbuf, 1, r, dest);
+        ncp += r;
+        n -= r;
+        rn = bufsize > n ? n : bufsize;
+        if (r == 0 || n == 0) {
+            break;
+        }
+    }
+
+    if (lbuf != buf) {
+        free(lbuf);
+    }
+
+    return ncp;
+}
+
+size_t cx_stream_ncopy(
+        void *src,
+        void *dest,
+        cx_read_func rfnc,
+        cx_write_func wfnc,
+        size_t n
+) {
+    char buf[CX_STREAM_COPY_BUF_SIZE];
+    return cx_stream_bncopy(src, dest, rfnc, wfnc,
+                            buf, CX_STREAM_COPY_BUF_SIZE, n);
+}
--- a/src/szmul.c	Sat Nov 02 13:38:51 2024 +0100
+++ b/src/szmul.c	Sat Nov 02 13:48:53 2024 +0100
@@ -26,6 +26,9 @@
  * POSSIBILITY OF SUCH DAMAGE.
  */
 
+#include "cx/common.h"
+
+#ifndef CX_SZMUL_BUILTIN
 int cx_szmul_impl(
         size_t a,
         size_t b,
@@ -44,3 +47,4 @@
         return 1;
     }
 }
+#endif // CX_SZMUL_BUILTIN
--- a/src/utils.c	Sat Nov 02 13:38:51 2024 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,99 +0,0 @@
-/*
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
- *
- * Copyright 2021 Mike Becker, 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:
- *
- *   1. Redistributions of source code must retain the above copyright
- *      notice, this list of conditions and the following disclaimer.
- *
- *   2. Redistributions in binary form must reproduce the above copyright
- *      notice, this list of conditions and the following disclaimer in the
- *      documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
- */
-
-#include "cx/utils.h"
-
-#ifndef CX_STREAM_BCOPY_BUF_SIZE
-#define CX_STREAM_BCOPY_BUF_SIZE 8192
-#endif
-
-#ifndef CX_STREAM_COPY_BUF_SIZE
-#define CX_STREAM_COPY_BUF_SIZE 1024
-#endif
-
-size_t cx_stream_bncopy(
-        void *src,
-        void *dest,
-        cx_read_func rfnc,
-        cx_write_func wfnc,
-        char *buf,
-        size_t bufsize,
-        size_t n
-) {
-    if (n == 0) {
-        return 0;
-    }
-
-    char *lbuf;
-    size_t ncp = 0;
-
-    if (buf) {
-        if (bufsize == 0) return 0;
-        lbuf = buf;
-    } else {
-        if (bufsize == 0) bufsize = CX_STREAM_BCOPY_BUF_SIZE;
-        lbuf = malloc(bufsize);
-        if (lbuf == NULL) {
-            return 0;
-        }
-    }
-
-    size_t r;
-    size_t rn = bufsize > n ? n : bufsize;
-    while ((r = rfnc(lbuf, 1, rn, src)) != 0) {
-        r = wfnc(lbuf, 1, r, dest);
-        ncp += r;
-        n -= r;
-        rn = bufsize > n ? n : bufsize;
-        if (r == 0 || n == 0) {
-            break;
-        }
-    }
-
-    if (lbuf != buf) {
-        free(lbuf);
-    }
-
-    return ncp;
-}
-
-size_t cx_stream_ncopy(
-        void *src,
-        void *dest,
-        cx_read_func rfnc,
-        cx_write_func wfnc,
-        size_t n
-) {
-    char buf[CX_STREAM_COPY_BUF_SIZE];
-    return cx_stream_bncopy(src, dest, rfnc, wfnc,
-                            buf, CX_STREAM_COPY_BUF_SIZE, n);
-}
-
-#ifndef CX_SZMUL_BUILTIN
-#include "szmul.c"
-#endif
--- a/tests/Makefile	Sat Nov 02 13:38:51 2024 +0100
+++ b/tests/Makefile	Sat Nov 02 13:48:53 2024 +0100
@@ -28,13 +28,13 @@
 TEST_DIR=$(build_dir)/tests
 
 SRC = util_allocator.c \
-	test_szmul.c test_allocator.c test_utils.c \
+	test_szmul.c test_allocator.c \
 	test_compare.c \
 	test_string.c test_buffer.c \
 	test_hash_key.c test_hash_map.c \
 	test_iterator.c test_list.c test_tree.c \
 	test_properties.c test_json.c \
-	test_printf.c \
+	test_printf.c test_streams.c \
 	test_mempool.c \
 	ucxtest.c
 
@@ -126,6 +126,12 @@
 	@echo "Compiling $<"
 	$(CC) -o $@ $(CFLAGS) -c $<
 
+$(TEST_DIR)/test_streams$(OBJ_EXT): test_streams.c ../src/cx/test.h \
+ ../src/cx/common.h ../src/cx/streams.h ../src/cx/buffer.h \
+ ../src/cx/allocator.h
+	@echo "Compiling $<"
+	$(CC) -o $@ $(CFLAGS) -c $<
+
 $(TEST_DIR)/test_string$(OBJ_EXT): test_string.c ../src/cx/test.h \
  ../src/cx/common.h util_allocator.h ../src/cx/allocator.h \
  ../src/cx/string.h ../src/cx/allocator.h
@@ -133,7 +139,7 @@
 	$(CC) -o $@ $(CFLAGS) -c $<
 
 $(TEST_DIR)/test_szmul$(OBJ_EXT): test_szmul.c ../src/cx/test.h \
- ../src/cx/common.h ../src/szmul.c
+ ../src/cx/common.h ../src/szmul.c ../src/cx/common.h
 	@echo "Compiling $<"
 	$(CC) -o $@ $(CFLAGS) -c $<
 
@@ -144,12 +150,6 @@
 	@echo "Compiling $<"
 	$(CC) -o $@ $(CFLAGS) -c $<
 
-$(TEST_DIR)/test_utils$(OBJ_EXT): test_utils.c ../src/cx/test.h \
- ../src/cx/common.h ../src/cx/utils.h ../src/cx/buffer.h \
- ../src/cx/allocator.h
-	@echo "Compiling $<"
-	$(CC) -o $@ $(CFLAGS) -c $<
-
 $(TEST_DIR)/ucxtest$(OBJ_EXT): ucxtest.c ../src/cx/common.h \
  ../src/cx/test.h ../src/cx/common.h
 	@echo "Compiling $<"
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test_streams.c	Sat Nov 02 13:48:53 2024 +0100
@@ -0,0 +1,105 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2023 Mike Becker, 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:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "cx/test.h"
+
+#include "cx/streams.h"
+#include "cx/buffer.h"
+
+CX_TEST(test_stream_bncopy) {
+    CxBuffer source, target;
+    char sbuf[32], tbuf[32];
+    memset(tbuf, 0, 32);
+    cxBufferInit(&source, sbuf, 32, NULL, 0);
+    cxBufferInit(&target, tbuf, 32, NULL, 0);
+    cxBufferPutString(&source, "This is a stream copy test.");
+    cxBufferSeek(&source, 0, SEEK_SET);
+    char tmp[4];
+
+    CX_TEST_DO {
+        size_t result = cx_stream_bncopy(&source, &target,
+                                         (cx_read_func) cxBufferRead,
+                                         (cx_write_func) cxBufferWrite,
+                                         tmp, 4, 20);
+        CX_TEST_ASSERT(result == 20);
+        CX_TEST_ASSERT(target.size == 20);
+        CX_TEST_ASSERT(strcmp("This is a stream cop\0", tbuf) == 0);
+
+        result = cx_stream_bcopy(&source, &target,
+                                 (cx_read_func) cxBufferRead,
+                                 (cx_write_func) cxBufferWrite,
+                                 NULL, 16);
+
+        CX_TEST_ASSERT(result == 7);
+        CX_TEST_ASSERT(target.size == 27);
+        CX_TEST_ASSERT(strcmp("This is a stream copy test.\0", tbuf) == 0);
+    }
+
+    cxBufferDestroy(&source);
+    cxBufferDestroy(&target);
+}
+
+CX_TEST(test_stream_ncopy) {
+    CxBuffer source, target;
+    char sbuf[32], tbuf[32];
+    memset(tbuf, 0, 32);
+    cxBufferInit(&source, sbuf, 32, NULL, 0);
+    cxBufferInit(&target, tbuf, 32, NULL, 0);
+    cxBufferPutString(&source, "This is a stream copy test.");
+    cxBufferSeek(&source, 0, SEEK_SET);
+
+    CX_TEST_DO {
+        size_t result = cx_stream_ncopy(&source, &target,
+                                        (cx_read_func) cxBufferRead,
+                                        (cx_write_func) cxBufferWrite,
+                                        20);
+        CX_TEST_ASSERT(result == 20);
+        CX_TEST_ASSERT(target.size == 20);
+        CX_TEST_ASSERT(strcmp("This is a stream cop\0", tbuf) == 0);
+
+        result = cx_stream_copy(&source, &target,
+                                (cx_read_func) cxBufferRead,
+                                (cx_write_func) cxBufferWrite);
+
+        CX_TEST_ASSERT(result == 7);
+        CX_TEST_ASSERT(target.size == 27);
+        CX_TEST_ASSERT(strcmp("This is a stream copy test.\0", tbuf) == 0);
+    }
+
+    cxBufferDestroy(&source);
+    cxBufferDestroy(&target);
+}
+
+CxTestSuite *cx_test_suite_streams(void) {
+    CxTestSuite *suite = cx_test_suite_new("streams");
+
+    cx_test_register(suite, test_stream_bncopy);
+    cx_test_register(suite, test_stream_ncopy);
+
+    return suite;
+}
--- a/tests/test_utils.c	Sat Nov 02 13:38:51 2024 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,105 +0,0 @@
-/*
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
- *
- * Copyright 2023 Mike Becker, 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:
- *
- *   1. Redistributions of source code must retain the above copyright
- *      notice, this list of conditions and the following disclaimer.
- *
- *   2. Redistributions in binary form must reproduce the above copyright
- *      notice, this list of conditions and the following disclaimer in the
- *      documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
- */
-
-#include "cx/test.h"
-
-#include "cx/utils.h"
-#include "cx/buffer.h"
-
-CX_TEST(test_stream_bncopy) {
-    CxBuffer source, target;
-    char sbuf[32], tbuf[32];
-    memset(tbuf, 0, 32);
-    cxBufferInit(&source, sbuf, 32, NULL, 0);
-    cxBufferInit(&target, tbuf, 32, NULL, 0);
-    cxBufferPutString(&source, "This is a stream copy test.");
-    cxBufferSeek(&source, 0, SEEK_SET);
-    char tmp[4];
-
-    CX_TEST_DO {
-        size_t result = cx_stream_bncopy(&source, &target,
-                                         (cx_read_func) cxBufferRead,
-                                         (cx_write_func) cxBufferWrite,
-                                         tmp, 4, 20);
-        CX_TEST_ASSERT(result == 20);
-        CX_TEST_ASSERT(target.size == 20);
-        CX_TEST_ASSERT(strcmp("This is a stream cop\0", tbuf) == 0);
-
-        result = cx_stream_bcopy(&source, &target,
-                                 (cx_read_func) cxBufferRead,
-                                 (cx_write_func) cxBufferWrite,
-                                 NULL, 16);
-
-        CX_TEST_ASSERT(result == 7);
-        CX_TEST_ASSERT(target.size == 27);
-        CX_TEST_ASSERT(strcmp("This is a stream copy test.\0", tbuf) == 0);
-    }
-
-    cxBufferDestroy(&source);
-    cxBufferDestroy(&target);
-}
-
-CX_TEST(test_stream_ncopy) {
-    CxBuffer source, target;
-    char sbuf[32], tbuf[32];
-    memset(tbuf, 0, 32);
-    cxBufferInit(&source, sbuf, 32, NULL, 0);
-    cxBufferInit(&target, tbuf, 32, NULL, 0);
-    cxBufferPutString(&source, "This is a stream copy test.");
-    cxBufferSeek(&source, 0, SEEK_SET);
-
-    CX_TEST_DO {
-        size_t result = cx_stream_ncopy(&source, &target,
-                                        (cx_read_func) cxBufferRead,
-                                        (cx_write_func) cxBufferWrite,
-                                        20);
-        CX_TEST_ASSERT(result == 20);
-        CX_TEST_ASSERT(target.size == 20);
-        CX_TEST_ASSERT(strcmp("This is a stream cop\0", tbuf) == 0);
-
-        result = cx_stream_copy(&source, &target,
-                                (cx_read_func) cxBufferRead,
-                                (cx_write_func) cxBufferWrite);
-
-        CX_TEST_ASSERT(result == 7);
-        CX_TEST_ASSERT(target.size == 27);
-        CX_TEST_ASSERT(strcmp("This is a stream copy test.\0", tbuf) == 0);
-    }
-
-    cxBufferDestroy(&source);
-    cxBufferDestroy(&target);
-}
-
-CxTestSuite *cx_test_suite_utils(void) {
-    CxTestSuite *suite = cx_test_suite_new("utils");
-
-    cx_test_register(suite, test_stream_bncopy);
-    cx_test_register(suite, test_stream_ncopy);
-
-    return suite;
-}
--- a/tests/ucxtest.c	Sat Nov 02 13:38:51 2024 +0100
+++ b/tests/ucxtest.c	Sat Nov 02 13:48:53 2024 +0100
@@ -32,7 +32,7 @@
 CxTestSuite *cx_test_suite_testing_allocator(void);
 CxTestSuite *cx_test_suite_szmul(void);
 CxTestSuite *cx_test_suite_allocator(void);
-CxTestSuite *cx_test_suite_utils(void);
+CxTestSuite *cx_test_suite_streams(void);
 CxTestSuite *cx_test_suite_compare(void);
 CxTestSuite *cx_test_suite_string(void);
 CxTestSuite *cx_test_suite_buffer(void);
@@ -63,7 +63,7 @@
             cx_test_suite_testing_allocator(),
             cx_test_suite_szmul(),
             cx_test_suite_allocator(),
-            cx_test_suite_utils(),
+            cx_test_suite_streams(),
             cx_test_suite_compare(),
             cx_test_suite_string(),
             cx_test_suite_buffer(),

mercurial