ucx/memstream.c

changeset 60
abae4669fba7
parent 59
c80c910fe191
child 61
fb07a0ab9a17
--- a/ucx/memstream.c	Wed Oct 10 09:34:13 2012 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,118 +0,0 @@
-#include "memstream.h"
-#include <stdarg.h>
-#include <stdlib.h>
-#include <string.h>
-
-struct UcxMemstream {
-    void *space;
-    off_t pos;
-    size_t length;
-    _Bool autofree;
-};
-
-UcxMemstream *ucx_memopen(void *space, size_t length) {
-    UcxMemstream *stream = (UcxMemstream*) malloc(sizeof(UcxMemstream));
-    if (stream) {
-        if (!space) {
-            stream->space = malloc(length);
-            if (!stream->space) {
-                free(stream);
-                return NULL;
-            }
-            memset(stream->space, 0, length);
-            stream->autofree = 1;
-        } else {
-            stream->space = space;
-            stream->autofree = 0;
-        }
-        stream->length = length;
-
-        stream->pos = 0;
-    }
-
-    return stream;
-}
-
-void ucx_memclose(UcxMemstream *stream) {
-    if (stream->autofree) {
-        free(stream->space);
-    }
-    free(stream);
-}
-
-int ucx_memseek(UcxMemstream *stream, off_t offset, int whence) {
-    off_t npos;
-    switch (whence) {
-    case SEEK_SET:
-        npos = 0;
-        break;
-    case SEEK_CUR:
-        npos = stream->pos;
-        break;
-    case SEEK_END:
-        npos = strlen(stream->space);
-        break;
-    }
-
-    npos += offset;
-
-    if (npos < 0 || npos > stream->length) {
-        return -1;
-    } else {
-        stream->pos = npos;
-        return 0;
-    }
-
-}
-
-int ucx_memeof(UcxMemstream *stream) {
-    return stream->pos >= stream->length;
-}
-
-size_t ucx_memtell(UcxMemstream *stream) {
-    return stream->pos;
-}
-
-size_t ucx_memio(void* d, size_t s, size_t n, UcxMemstream *m, _Bool read) {
-    size_t len;
-    if (m->pos + s*n > m->length) {
-        len = m->length - m->pos;
-        if (s > 1) len -= len%s;
-    } else {
-        len = s*n;
-    }
-
-    if (len == 0) {
-        return 0;
-    }
-
-    if (read) {
-        memcpy(d, (char*)m->space+m->pos, len);
-    } else {
-        memcpy((char*)m->space+m->pos, d, len);
-    }
-    m->pos += len;
-
-    return len;
-}
-
-int ucx_memputc(UcxMemstream *stream, int c) {
-    if (ucx_memeof(stream)) {
-        return EOF;
-    } else {
-        c &= 0xFF;
-        ((char*)(stream->space))[stream->pos] = (char) c;
-        stream->pos++;
-        return c;
-    }
-}
-
-int ucx_memgetc(UcxMemstream *stream) {
-    if (ucx_memeof(stream)) {
-        return EOF;
-    } else {
-        int c = ((char*)(stream->space))[stream->pos];
-        stream->pos++;
-        return c;
-    }
-}

mercurial