From b74160fa4e4af5604bb7a3c5278c2b3982094b23 Mon Sep 17 00:00:00 2001 From: DTB Date: Wed, 3 Jul 2024 19:04:01 -0600 Subject: [PATCH] dj(1): remove Io_bufxfer --- src/dj.c | 32 ++++++++++++-------------------- 1 file changed, 12 insertions(+), 20 deletions(-) diff --git a/src/dj.c b/src/dj.c index ac52883..22c46a7 100644 --- a/src/dj.c +++ b/src/dj.c @@ -75,21 +75,6 @@ Io_bufrpad(struct Io *io, int padding){ return io; } -/* Copies from the buffer in src to the buffer in dest no more than n units, - * removing the copied units from src and permuting the remaining units in the - * src buffer to the start of the buffer, modifying both the src and dest - * bufuse and returning dest. */ -static struct Io* -Io_bufxfer(struct Io *dest, struct Io *src, int n){ - - assert(dest != NULL && src != NULL); - - memcpy(dest->buf, src->buf, (dest->bufuse = n)); - memmove(src->buf, src->buf + n, (src->bufuse -= n)); - - return dest; -} - /* Opens io->fn and saves the file descriptor into io->fd. Returns io->fd, * which will be -1 if an error occured. */ static int @@ -318,10 +303,18 @@ int main(int argc, char *argv[]){ do{ int t; - if(io[1].bs > io[0].bs){ + if(io[0].bs <= io[1].bs){ int n; - /* copy from ibuf as much as possible to the obuf */ + /* saturate obuf */ + memcpy(io[1].buf, io[0].buf, + (io[1].bufuse = (n = MIN(io[0].bufuse, io[1].bs)))); + /* permute the copied units out of ibuf */ + memmove(io[0].buf, io[0].buf + n, (io[0].bufuse -= n)); + }else /* if(io[0].bs < io[1].bs) */ { + int n; + + /* drain what we can from ibuf */ memcpy(io[1].buf + io[1].bufuse, io[0].buf, (n = MIN(io[0].bufuse, io[1].bs - io[1].bufuse))); io[1].bufuse += n; @@ -330,9 +323,8 @@ int main(int argc, char *argv[]){ io[0].bufuse -= n; if(io[0].bs + io[1].bufuse <= io[1].bs && count != 1) - continue; /* we could write more */ - }else - Io_bufxfer(&io[1], &io[0], MIN(io[0].bufuse, io[1].bs)); + continue; /* obuf not saturated - we could write more */ + } t = io[1].bufuse; Io_write(&io[1]);