Move read and write code from buffer_read and buffer_write to new
functions for each buffer type.

Signed-off-by: Jonathan Nieder <[email protected]>
---
 lib/dpkg/buffer.c |  140 +++++++++++++++++++++++++++++++++++++++++------------
 1 files changed, 108 insertions(+), 32 deletions(-)

diff --git a/lib/dpkg/buffer.c b/lib/dpkg/buffer.c
index d348c6e..def6c58 100644
--- a/lib/dpkg/buffer.c
+++ b/lib/dpkg/buffer.c
@@ -37,11 +37,82 @@
 #include <dpkg/md5.h>
 #include <dpkg/buffer.h>
 
+/*
+ * Fixed-length memory area.
+ */
+
+static off_t
+write_buf(struct buffer_data *data, const void *buf, off_t length)
+{
+       memcpy(data->arg.ptr, buf, length);
+       data->arg.ptr += length;
+       return length;
+}
+
+/*
+ * Variable-length memory area.
+ */
+
+static off_t
+write_vbuf(struct buffer_data *data, const void *buf, off_t length)
+{
+       varbufaddbuf((struct varbuf *)data->arg.ptr, buf, length);
+       return length;
+}
+
+/*
+ * Unix file output.
+ */
+
+static off_t
+write_fd(struct buffer_data *data, const void *buf, off_t length)
+{
+       return write(data->arg.i, buf, length);
+}
+
+/*
+ * Data sink.
+ */
+
+static off_t
+write_null(struct buffer_data *data, const void *buf, off_t length)
+{
+       return length;
+}
+
+/*
+ * Standard C output.
+ */
+
+static off_t
+write_stream(struct buffer_data *data, const void *buf, off_t length)
+{
+       off_t ret = fwrite(buf, 1, length, (FILE *)data->arg.ptr);
+
+       if (feof((FILE *)data->arg.ptr))
+               return -1;
+       if (ferror((FILE *)data->arg.ptr))
+               return -1;
+       return ret;
+}
+
+/*
+ * MD5 hash accumulator.
+ */
+
 struct buffer_write_md5ctx {
        struct MD5Context ctx;
        char *hash;
 };
 
+static off_t
+write_md5(struct buffer_data *data, const void *buf, off_t length)
+{
+       MD5Update(&(((struct buffer_write_md5ctx *)data->arg.ptr)->ctx),
+                 buf, length);
+       return length;
+}
+
 static void
 buffer_md5_init(struct buffer_data *data)
 {
@@ -72,6 +143,35 @@ buffer_md5_done(struct buffer_data *data)
        free(ctx);
 }
 
+/*
+ * Unix file input.
+ */
+
+static off_t
+read_fd(struct buffer_data *data, void *buf, off_t length)
+{
+       return read(data->arg.i, buf, length);
+}
+
+/*
+ * Standard C input.
+ */
+
+static off_t
+read_stream(struct buffer_data *data, void *buf, off_t length)
+{
+       off_t ret = fread(buf, 1, length, (FILE *)data->arg.ptr);
+
+       if (feof((FILE *)data->arg.ptr))
+               return ret;
+       if (ferror((FILE *)data->arg.ptr))
+               return -1;
+}
+
+/*
+ * Generic input and output.
+ */
+
 off_t
 buffer_init(struct buffer_data *read_data, struct buffer_data *write_data)
 {
@@ -97,61 +197,37 @@ buffer_done(struct buffer_data *read_data, struct 
buffer_data *write_data)
 off_t
 buffer_write(struct buffer_data *data, const void *buf, off_t length)
 {
-       off_t ret = length;
-
        switch (data->type) {
        case BUFFER_WRITE_BUF:
-               memcpy(data->arg.ptr, buf, length);
-               data->arg.ptr += length;
-               break;
+               return write_buf(data, buf, length);
        case BUFFER_WRITE_VBUF:
-               varbufaddbuf((struct varbuf *)data->arg.ptr, buf, length);
-               break;
+               return write_vbuf(data, buf, length);
        case BUFFER_WRITE_FD:
-               ret = write(data->arg.i, buf, length);
-               break;
+               return write_fd(data, buf, length);
        case BUFFER_WRITE_NULL:
-               break;
+               return write_null(data, buf, length);
        case BUFFER_WRITE_STREAM:
-               ret = fwrite(buf, 1, length, (FILE *)data->arg.ptr);
-               if (feof((FILE *)data->arg.ptr))
-                       return -1;
-               if (ferror((FILE *)data->arg.ptr))
-                       return -1;
-               break;
+               return write_stream(data, buf, length);
        case BUFFER_WRITE_MD5:
-               MD5Update(&(((struct buffer_write_md5ctx 
*)data->arg.ptr)->ctx), buf, length);
-               break;
+               return write_md5(data, buf, length);
        default:
                internerr("unknown data type '%i' in buffer_write",
                          data->type);
        }
-
-       return ret;
 }
 
 off_t
 buffer_read(struct buffer_data *data, void *buf, off_t length)
 {
-       off_t ret;
-
        switch (data->type) {
        case BUFFER_READ_FD:
-               ret = read(data->arg.i, buf, length);
-               break;
+               return read_fd(data, buf, length);
        case BUFFER_READ_STREAM:
-               ret = fread(buf, 1, length, (FILE *)data->arg.ptr);
-               if (feof((FILE *)data->arg.ptr))
-                       return ret;
-               if (ferror((FILE *)data->arg.ptr))
-                       return -1;
-               break;
+               return read_stream(data, buf, length);
        default:
                internerr("unknown data type '%i' in buffer_read\n",
                          data->type);
        }
-
-       return ret;
 }
 
 #define buffer_copy_TYPE(name, type1, name1, type2, name2) \
-- 
1.7.0.3


-- 
To UNSUBSCRIBE, email to [email protected]
with a subject of "unsubscribe". Trouble? Contact [email protected]
Archive: http://lists.debian.org/[email protected]

Reply via email to