discarded fascism from buffer

Wed, 10 Oct 2012 13:58:51 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 10 Oct 2012 13:58:51 +0200
changeset 63
1d3500806565
parent 62
6814aea8462d
child 64
16590c9c497c

discarded fascism from buffer

test/buffer_tests.c file | annotate | diff | comparison | revisions
ucx/buffer.c file | annotate | diff | comparison | revisions
ucx/buffer.h file | annotate | diff | comparison | revisions
--- a/test/buffer_tests.c	Wed Oct 10 10:46:20 2012 +0200
+++ b/test/buffer_tests.c	Wed Oct 10 13:58:51 2012 +0200
@@ -16,29 +16,29 @@
 
     r = ucx_buffer_seek(b, 5, SEEK_SET);
     UCX_TEST_ASSERT(r == 0, "seek SET+5 failed");
-    UCX_TEST_ASSERT(ucx_buffer_tell(b) == 5, "seek SET+5 set wrong position");
+    UCX_TEST_ASSERT(b->pos == 5, "seek SET+5 set wrong position");
 
     r = ucx_buffer_seek(b, 20, SEEK_SET);
     UCX_TEST_ASSERT(r != 0, "seek beyond bounds shall fail");
-    UCX_TEST_ASSERT(ucx_buffer_tell(b) == 5,
+    UCX_TEST_ASSERT(b->pos == 5,
             "failed seek shall leave pos unchanged");
 
     r = ucx_buffer_seek(b, 5, SEEK_CUR);
     UCX_TEST_ASSERT(r == 0, "seek CUR+5 failed");
-    UCX_TEST_ASSERT(ucx_buffer_tell(b) == 10, "seek CUR+5 set wrong position");
+    UCX_TEST_ASSERT(b->pos == 10, "seek CUR+5 set wrong position");
 
     r = ucx_buffer_seek(b, 10, SEEK_CUR);
     UCX_TEST_ASSERT(r != 0, "seek CUR beyond bounds shall fail");
-    UCX_TEST_ASSERT(ucx_buffer_tell(b) == 10,
+    UCX_TEST_ASSERT(b->pos == 10,
             "failed seek shall leave pos unchanged");
 
     r = ucx_buffer_seek(b, -5, SEEK_END);
     UCX_TEST_ASSERT(r == 0, "seek END-5 failed");
-    UCX_TEST_ASSERT(ucx_buffer_tell(b) == 2, "seek END-5 set wrong position");
+    UCX_TEST_ASSERT(b->pos == 2, "seek END-5 set wrong position");
 
     r = ucx_buffer_seek(b, -10, SEEK_END);
     UCX_TEST_ASSERT(r != 0, "seek END beyond bounds shall fail");
-    UCX_TEST_ASSERT(ucx_buffer_tell(b) == 2,
+    UCX_TEST_ASSERT(b->pos == 2,
             "failed seek shall leave pos unchanged");
 
     UCX_TEST_END
@@ -57,10 +57,10 @@
     UCX_TEST_BEGIN
 
     ucx_buffer_putc(b, 48); ucx_buffer_putc(b, 48); ucx_buffer_putc(b, 48);
-    UCX_TEST_ASSERT(ucx_buffer_tell(b) == 3, "pos wrong after first 3 puts");
+    UCX_TEST_ASSERT(b->pos == 3, "pos wrong after first 3 puts");
     ucx_buffer_seek(b, 10, SEEK_CUR);
     ucx_buffer_putc(b, 48); ucx_buffer_putc(b, 48); ucx_buffer_putc(b, 48);
-    UCX_TEST_ASSERT(ucx_buffer_tell(b) == 16, "pos wrong after last 3 puts");
+    UCX_TEST_ASSERT(b->pos == 16, "pos wrong after last 3 puts");
     UCX_TEST_ASSERT(ucx_buffer_eof(b), "eof not set");
     UCX_TEST_ASSERT(ucx_buffer_putc(b, 48) == EOF,
             "put shall return EOF when buffer is full");
@@ -87,12 +87,12 @@
 
     char rb[16];
     for (int i = 0 ; i < 16 ; i++) {
-        UCX_TEST_ASSERT(ucx_buffer_tell(b) == i, "pos wrong during read loop");
+        UCX_TEST_ASSERT(b->pos == i, "pos wrong during read loop");
         UCX_TEST_ASSERT(!ucx_buffer_eof(b),
                 "EOF shall not be set during read loop");
         rb[i] = ucx_buffer_getc(b);
     }
-    UCX_TEST_ASSERT(ucx_buffer_tell(b) == 16, "pos wrong after read loop");
+    UCX_TEST_ASSERT(b->pos == 16, "pos wrong after read loop");
     UCX_TEST_ASSERT(ucx_buffer_eof(b), "EOF not set");
     UCX_TEST_ASSERT(memcmp(rb, "        01234567", 16) == 0,
             "read data incorrect");
@@ -133,7 +133,7 @@
     ucx_buffer_seek(b, 0, SEEK_SET);
     r = ucx_buffer_write(threebytestring, 3, 6, b);
     UCX_TEST_ASSERT(r == 15, "three byte string not correctly trimed");
-    UCX_TEST_ASSERT(ucx_buffer_tell(b) == 15,
+    UCX_TEST_ASSERT(b->pos == 15,
             "position after write of three byte string incorrect");
     UCX_TEST_ASSERT(!ucx_buffer_eof(b), "eof shall not be set");
     UCX_TEST_ASSERT(memcmp(buffer, "  t  h  r  e  e1", 16) == 0,
@@ -194,9 +194,9 @@
             *dst = ucx_buffer_extract(src, 5, 5, UCX_BUFFER_DEFAULT);
 
     UCX_TEST_BEGIN
-    UCX_TEST_ASSERT(ucx_buffer_testflags(dst, UCX_BUFFER_AUTOFREE),
+    UCX_TEST_ASSERT((dst->flags & UCX_BUFFER_AUTOFREE) == UCX_BUFFER_AUTOFREE,
             "autofree flag shall be enforced");
-    UCX_TEST_ASSERT(ucx_buffer_size(dst) == 5, "wrong size for new buffer");
+    UCX_TEST_ASSERT(dst->size == 5, "wrong size for new buffer");
     char rb[5];
     ucx_buffer_read(rb, 1, 5, dst);
     UCX_TEST_ASSERT(memcmp(rb, "is a ", 5) == 0,
--- a/ucx/buffer.c	Wed Oct 10 10:46:20 2012 +0200
+++ b/ucx/buffer.c	Wed Oct 10 13:58:51 2012 +0200
@@ -3,13 +3,6 @@
 #include <stdlib.h>
 #include <string.h>
 
-struct UcxBuffer {
-    void *space;
-    size_t pos;
-    size_t size;
-    int flags;
-};
-
 UcxBuffer *ucx_buffer_new(void *space, size_t length, int flags) {
     UcxBuffer *buffer = (UcxBuffer*) malloc(sizeof(UcxBuffer));
     if (buffer) {
@@ -34,7 +27,7 @@
 }
 
 void ucx_buffer_free(UcxBuffer *buffer) {
-    if (ucx_buffer_testflags(buffer, UCX_BUFFER_AUTOFREE)) {
+    if ((buffer->flags & UCX_BUFFER_AUTOFREE) == UCX_BUFFER_AUTOFREE) {
         free(buffer->space);
     }
     free(buffer);
@@ -93,18 +86,6 @@
     return buffer->pos >= buffer->size;
 }
 
-size_t ucx_buffer_tell(UcxBuffer *buffer) {
-    return buffer->pos;
-}
-
-size_t ucx_buffer_size(UcxBuffer *buffer) {
-    return buffer->size;
-}
-
-int ucx_buffer_testflags(UcxBuffer *buffer, int flags) {
-    return (buffer->flags & flags) == flags;
-}
-
 size_t ucx_bufio(void* d, size_t s, size_t n, UcxBuffer *b, _Bool read) {
     size_t len;
     if (b->pos + s*n > b->size) {
--- a/ucx/buffer.h	Wed Oct 10 10:46:20 2012 +0200
+++ b/ucx/buffer.h	Wed Oct 10 13:58:51 2012 +0200
@@ -10,9 +10,15 @@
 
 #define UCX_BUFFER_DEFAULT      0x00
 #define UCX_BUFFER_AUTOFREE     0x01
+#define UCX_BUFFER_AUTOEXTEND   0x02
 
-/* the user shall not modify values */
-typedef struct UcxBuffer UcxBuffer;
+/* the user shall not modify values, but can get an up to date space pointer */
+typedef struct {
+    void *space;
+    size_t pos;
+    size_t size;
+    int flags;
+} UcxBuffer;
 
 /* if space is NULL, new space is allocated and the autofree flag is enforced */
 UcxBuffer *ucx_buffer_new(void *space, size_t length, int flags);
@@ -41,10 +47,6 @@
  *
  */
 int ucx_buffer_seek(UcxBuffer *buffer, off_t offset, int whence);
-size_t ucx_buffer_tell(UcxBuffer *buffer);
-size_t ucx_buffer_size(UcxBuffer *buffer);
-/* returns non-zero, if at least the specified flags are set */
-int ucx_buffer_testflags(UcxBuffer *buffer, int flags);
 
 /*
  * returns non-zero, iff the current buffer position has exceeded the last

mercurial