src/utils.c

Sun, 21 May 2023 16:22:09 +0200

author
Mike Becker <universe@uap-core.de>
date
Sun, 21 May 2023 16:22:09 +0200
changeset 710
2dd409ed056f
parent 675
765cf785b7fa
child 736
70885c3d15b0
permissions
-rw-r--r--

fix const-ness of non-mutating iterator creation for maps

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@674 31 #define CX_STREAM_BCOPY_BUF_SIZE 8192
universe@674 32 #define CX_STREAM_COPY_BUF_SIZE 1024
universe@674 33
universe@674 34 size_t cx_stream_bncopy(
universe@674 35 void *src,
universe@674 36 void *dest,
universe@674 37 cx_read_func rfnc,
universe@674 38 cx_write_func wfnc,
universe@674 39 char *buf,
universe@674 40 size_t bufsize,
universe@674 41 size_t n
universe@674 42 ) {
universe@674 43 if (n == 0) {
universe@483 44 return 0;
universe@483 45 }
universe@674 46
universe@674 47 char *lbuf;
universe@674 48 size_t ncp = 0;
universe@674 49
universe@674 50 if (buf) {
universe@674 51 if (bufsize == 0) return 0;
universe@674 52 lbuf = buf;
universe@674 53 } else {
universe@674 54 if (bufsize == 0) bufsize = CX_STREAM_BCOPY_BUF_SIZE;
universe@674 55 lbuf = malloc(bufsize);
universe@674 56 if (lbuf == NULL) {
universe@674 57 return 0;
universe@674 58 }
universe@674 59 }
universe@674 60
universe@674 61 size_t r;
universe@674 62 size_t rn = bufsize > n ? n : bufsize;
universe@674 63 while ((r = rfnc(lbuf, 1, rn, src)) != 0) {
universe@674 64 r = wfnc(lbuf, 1, r, dest);
universe@674 65 ncp += r;
universe@674 66 n -= r;
universe@674 67 rn = bufsize > n ? n : bufsize;
universe@674 68 if (r == 0 || n == 0) {
universe@674 69 break;
universe@674 70 }
universe@674 71 }
universe@674 72
universe@674 73 if (lbuf != buf) {
universe@674 74 free(lbuf);
universe@674 75 }
universe@674 76
universe@674 77 return ncp;
universe@674 78 }
universe@674 79
universe@674 80 size_t cx_stream_ncopy(
universe@674 81 void *src,
universe@674 82 void *dest,
universe@674 83 cx_read_func rfnc,
universe@674 84 cx_write_func wfnc,
universe@674 85 size_t n
universe@674 86 ) {
universe@675 87 char buf[CX_STREAM_COPY_BUF_SIZE];
universe@675 88 return cx_stream_bncopy(src, dest, rfnc, wfnc,
universe@675 89 buf, CX_STREAM_COPY_BUF_SIZE, n);
universe@483 90 }
universe@674 91
universe@674 92 #ifndef CX_SZMUL_BUILTIN
universe@674 93 #include "szmul.c"
universe@483 94 #endif

mercurial