changeset 442:0d09c7592828

buffer: introduce a stdio buffer type Instead of buffering the appended data, the data is written to a FILE * using fwrite(3). Signed-off-by: Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
author Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
date Sun, 01 Apr 2018 14:24:56 -0400
parents 86cfab08dffd
children 696e64fafaef
files CMakeLists.txt buffer.c buffer_impl.h buffer_stdio.c include/jeffpc/buffer.h
diffstat 5 files changed, 75 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/CMakeLists.txt	Sun Apr 01 14:23:57 2018 -0400
+++ b/CMakeLists.txt	Sun Apr 01 14:24:56 2018 -0400
@@ -88,6 +88,7 @@
 	buffer_const.c
 	buffer_heap.c
 	buffer_sink.c
+	buffer_stdio.c
 	cstr.c
 	error.c
 	fmt_cbor.c
--- a/buffer.c	Sun Apr 01 14:23:57 2018 -0400
+++ b/buffer.c	Sun Apr 01 14:24:56 2018 -0400
@@ -77,6 +77,16 @@
 	buffer->ops = &const_buffer;
 }
 
+void buffer_init_stdio(struct buffer *buffer, FILE *f)
+{
+	buffer->data = NULL;
+	buffer->off = 0;
+	buffer->used = 0;
+	buffer->allocsize = SIZE_MAX;
+	buffer->ops = &stdio_buffer;
+	buffer->private = f;
+}
+
 static int resize(struct buffer *buffer, size_t newsize)
 {
 	void *tmp;
--- a/buffer_impl.h	Sun Apr 01 14:23:57 2018 -0400
+++ b/buffer_impl.h	Sun Apr 01 14:24:56 2018 -0400
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
+ * Copyright (c) 2017-2018 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
@@ -28,6 +28,7 @@
 extern const struct buffer_ops heap_buffer;
 extern const struct buffer_ops sink_buffer;
 extern const struct buffer_ops const_buffer;
+extern const struct buffer_ops stdio_buffer;
 
 /* clear implementations */
 extern void generic_buffer_clear_memset(struct buffer *buffer, size_t off,
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/buffer_stdio.c	Sun Apr 01 14:24:56 2018 -0400
@@ -0,0 +1,59 @@
+/*
+ * Copyright (c) 2018 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->used)
+		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", __func__);
+}
+
+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,
+};
--- a/include/jeffpc/buffer.h	Sun Apr 01 14:23:57 2018 -0400
+++ b/include/jeffpc/buffer.h	Sun Apr 01 14:24:56 2018 -0400
@@ -23,6 +23,7 @@
 #ifndef __JEFFPC_BUFFER_H
 #define __JEFFPC_BUFFER_H
 
+#include <stdio.h>
 #include <stdlib.h>
 #include <stdbool.h>
 #include <fcntl.h>
@@ -62,6 +63,8 @@
 /* a buffer that wraps a const data pointer */
 extern void buffer_init_const(struct buffer *buffer, const void *data,
 			      size_t size);
+/* a buffer that writes to a FILE * */
+extern void buffer_init_stdio(struct buffer *buffer, FILE *f);
 
 /* returns number of bytes appended */
 extern int buffer_append(struct buffer *buffer, const void *data, size_t size);