src/utils.c

Fri, 12 Apr 2024 21:48:12 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 12 Apr 2024 21:48:12 +0200
changeset 849
edb9f875b7f9
parent 736
70885c3d15b0
permissions
-rw-r--r--

improves interface of cx_sprintf() variants

universe@483 1 /*
universe@483 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@483 3 *
universe@483 4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
universe@483 5 *
universe@483 6 * Redistribution and use in source and binary forms, with or without
universe@483 7 * modification, are permitted provided that the following conditions are met:
universe@483 8 *
universe@483 9 * 1. Redistributions of source code must retain the above copyright
universe@483 10 * notice, this list of conditions and the following disclaimer.
universe@483 11 *
universe@483 12 * 2. Redistributions in binary form must reproduce the above copyright
universe@483 13 * notice, this list of conditions and the following disclaimer in the
universe@483 14 * documentation and/or other materials provided with the distribution.
universe@483 15 *
universe@483 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@483 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@483 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
universe@483 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
universe@483 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
universe@483 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
universe@483 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
universe@483 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
universe@483 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
universe@483 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
universe@483 26 * POSSIBILITY OF SUCH DAMAGE.
universe@483 27 */
universe@483 28
universe@483 29 #include "cx/utils.h"
universe@483 30
universe@736 31 #ifndef CX_STREAM_BCOPY_BUF_SIZE
universe@674 32 #define CX_STREAM_BCOPY_BUF_SIZE 8192
universe@736 33 #endif
universe@736 34
universe@736 35 #ifndef CX_STREAM_COPY_BUF_SIZE
universe@674 36 #define CX_STREAM_COPY_BUF_SIZE 1024
universe@736 37 #endif
universe@674 38
universe@674 39 size_t cx_stream_bncopy(
universe@674 40 void *src,
universe@674 41 void *dest,
universe@674 42 cx_read_func rfnc,
universe@674 43 cx_write_func wfnc,
universe@674 44 char *buf,
universe@674 45 size_t bufsize,
universe@674 46 size_t n
universe@674 47 ) {
universe@674 48 if (n == 0) {
universe@483 49 return 0;
universe@483 50 }
universe@674 51
universe@674 52 char *lbuf;
universe@674 53 size_t ncp = 0;
universe@674 54
universe@674 55 if (buf) {
universe@674 56 if (bufsize == 0) return 0;
universe@674 57 lbuf = buf;
universe@674 58 } else {
universe@674 59 if (bufsize == 0) bufsize = CX_STREAM_BCOPY_BUF_SIZE;
universe@674 60 lbuf = malloc(bufsize);
universe@674 61 if (lbuf == NULL) {
universe@674 62 return 0;
universe@674 63 }
universe@674 64 }
universe@674 65
universe@674 66 size_t r;
universe@674 67 size_t rn = bufsize > n ? n : bufsize;
universe@674 68 while ((r = rfnc(lbuf, 1, rn, src)) != 0) {
universe@674 69 r = wfnc(lbuf, 1, r, dest);
universe@674 70 ncp += r;
universe@674 71 n -= r;
universe@674 72 rn = bufsize > n ? n : bufsize;
universe@674 73 if (r == 0 || n == 0) {
universe@674 74 break;
universe@674 75 }
universe@674 76 }
universe@674 77
universe@674 78 if (lbuf != buf) {
universe@674 79 free(lbuf);
universe@674 80 }
universe@674 81
universe@674 82 return ncp;
universe@674 83 }
universe@674 84
universe@674 85 size_t cx_stream_ncopy(
universe@674 86 void *src,
universe@674 87 void *dest,
universe@674 88 cx_read_func rfnc,
universe@674 89 cx_write_func wfnc,
universe@674 90 size_t n
universe@674 91 ) {
universe@675 92 char buf[CX_STREAM_COPY_BUF_SIZE];
universe@675 93 return cx_stream_bncopy(src, dest, rfnc, wfnc,
universe@675 94 buf, CX_STREAM_COPY_BUF_SIZE, n);
universe@483 95 }
universe@674 96
universe@674 97 #ifndef CX_SZMUL_BUILTIN
universe@674 98 #include "szmul.c"
universe@483 99 #endif

mercurial