Mercurial > libjeffpc
changeset 828:3fa2ebce953c
buffer: remove support for stdio-backed buffers
While this was a fun demonstration, it was a bad idea. It prevented the
consumers from assuming that buffered data is always seekable (with the
exception of the sink buffer) and could be re-read. This caused a lot of
unnecessary error handling code to sprout up.
Signed-off-by: Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
author | Josef 'Jeff' Sipek <jeffpc@josefsipek.net> |
---|---|
date | Wed, 30 Dec 2020 17:28:41 -0500 |
parents | 0ccca7ddbaec |
children | 2abc6dc12302 |
files | CMakeLists.txt buffer.c buffer_impl.h buffer_stdio.c include/jeffpc/buffer.h mapfile-vers |
diffstat | 6 files changed, 1 insertions(+), 87 deletions(-) [+] |
line wrap: on
line diff
--- a/CMakeLists.txt Fri Jan 01 12:51:20 2021 -0500 +++ b/CMakeLists.txt Wed Dec 30 17:28:41 2020 -0500 @@ -96,7 +96,6 @@ buffer_heap.c buffer_sink.c buffer_static.c - buffer_stdio.c cstr.c error.c file_cache.c
--- a/buffer.c Fri Jan 01 12:51:20 2021 -0500 +++ b/buffer.c Wed Dec 30 17:28:41 2020 -0500 @@ -96,17 +96,6 @@ buffer->heap = false; } -void buffer_init_stdio(struct buffer *buffer, FILE *f) -{ - buffer->data = NULL; - buffer->off = 0; - buffer->size = 0; - buffer->allocsize = SIZE_MAX; - buffer->ops = &stdio_buffer; - buffer->private = f; - buffer->heap = false; -} - static int resize(struct buffer *buffer, size_t newsize) { void *tmp;
--- a/buffer_impl.h Fri Jan 01 12:51:20 2021 -0500 +++ b/buffer_impl.h Wed Dec 30 17:28:41 2020 -0500 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2018 Josef 'Jeff' Sipek <jeffpc@josefsipek.net> + * Copyright (c) 2017-2020 Josef 'Jeff' Sipek <jeffpc@josefsipek.net> * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -29,7 +29,6 @@ extern const struct buffer_ops sink_buffer; extern const struct buffer_ops static_buffer_ro; extern const struct buffer_ops static_buffer_rw; -extern const struct buffer_ops stdio_buffer; /* clear implementations */ extern void generic_buffer_clear_memset(struct buffer *buffer, size_t off,
--- a/buffer_stdio.c Fri Jan 01 12:51:20 2021 -0500 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,69 +0,0 @@ -/* - * Copyright (c) 2018-2019 Josef 'Jeff' Sipek <jeffpc@josefsipek.net> - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -#include "buffer_impl.h" - -static int stdio_buffer_check_truncate(struct buffer *buffer, size_t size) -{ - return -ESPIPE; -} - -static ssize_t stdio_buffer_check_seek(struct buffer *buffer, off_t offset, - int whence, size_t newoff) -{ - /* allow no-op seeks */ - if (newoff == buffer->size) - return 0; - - return -ENOTSUP; -} - -static void stdio_buffer_copyin(struct buffer *buffer, size_t off, - const void *newdata, size_t newdatalen) -{ - if (fwrite(newdata, newdatalen, 1, buffer->private) != 1) - panic("%s: failed to write data to buffer: %s", __func__, - strerror(errno)); -} - -static void stdio_buffer_copyout(struct buffer *buffer, size_t off, - void *data, size_t datalen) -{ - if (fread(data, datalen, 1, buffer->private) != 1) - panic("%s: failed to read data from buffer: %s", __func__, - strerror(errno)); -} - -const struct buffer_ops stdio_buffer = { - .check_truncate = stdio_buffer_check_truncate, - .check_seek = stdio_buffer_check_seek, - - /* - * no need for: - * - realloc since we use SIZE_MAX alloc size - * - free since there is nothing to free - */ - - .clear = generic_buffer_clear_panic, - .copyin = stdio_buffer_copyin, - .copyout = stdio_buffer_copyout, -};
--- a/include/jeffpc/buffer.h Fri Jan 01 12:51:20 2021 -0500 +++ b/include/jeffpc/buffer.h Wed Dec 30 17:28:41 2020 -0500 @@ -23,7 +23,6 @@ #ifndef __JEFFPC_BUFFER_H #define __JEFFPC_BUFFER_H -#include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include <fcntl.h> @@ -76,8 +75,6 @@ */ extern void buffer_init_static(struct buffer *buffer, const void *data, size_t size, size_t allocsize, bool writable); -/* a buffer that writes to a FILE * */ -extern void buffer_init_stdio(struct buffer *buffer, FILE *f); extern int buffer_append(struct buffer *buffer, const void *data, size_t size); /* append specified number of bytes from src to dst */