From b1558d651ecc4fb49ad12508fd5e87153a229cc7 Mon Sep 17 00:00:00 2001 From: twinaphex Date: Sun, 15 Feb 2015 23:29:13 +0100 Subject: [PATCH] style nits and other cleanups --- nbio/nbio_stdio.c | 86 ++++++++++++++++++++++++++++++----------------- 1 file changed, 55 insertions(+), 31 deletions(-) diff --git a/nbio/nbio_stdio.c b/nbio/nbio_stdio.c index f0aaba8312..dbc779a4d7 100644 --- a/nbio/nbio_stdio.c +++ b/nbio/nbio_stdio.c @@ -2,38 +2,54 @@ #include #include -struct nbio_t { - FILE* f; - void* data; - size_t progress; - size_t len; - /* - * possible values: - * NBIO_READ, NBIO_WRITE - obvious - * -1 - currently doing nothing - * -2 - the pointer was reallocated since the last operation - */ - signed char op; +struct nbio_t +{ + FILE* f; + void* data; + size_t progress; + size_t len; + /* + * possible values: + * NBIO_READ, NBIO_WRITE - obvious + * -1 - currently doing nothing + * -2 - the pointer was reallocated since the last operation + */ + signed char op; }; static const char * modes[]={ "rb", "wb", "r+b" }; + struct nbio_t* nbio_open(const char * filename, enum nbio_mode_t mode) { struct nbio_t* handle; FILE* f=fopen(filename, modes[mode]); - if (!f) return NULL; + if (!f) + return NULL; handle=(struct nbio_t*)malloc(sizeof(struct nbio_t)); - handle->f = f; + + if (!handle) + return NULL; + + handle->f = f; + handle->len = 0; + if (mode != NBIO_WRITE) { fseek(handle->f, 0, SEEK_END); handle->len = ftell(handle->f); } - else handle->len = 0; + handle->data = malloc(handle->len); + + if (!handle->data) + { + free(handle); + return NULL; + } + handle->progress = handle->len; - handle->op = -2; + handle->op = -2; return handle; } @@ -45,8 +61,10 @@ void nbio_begin_read(struct nbio_t* handle) puts("ERROR - attempted file read operation while busy"); abort(); } + fseek(handle->f, 0, SEEK_SET); - handle->op = NBIO_READ; + + handle->op = NBIO_READ; handle->progress = 0; } @@ -57,6 +75,7 @@ void nbio_begin_write(struct nbio_t* handle) puts("ERROR - attempted file write operation while busy"); abort(); } + fseek(handle->f, 0, SEEK_SET); handle->op = NBIO_WRITE; handle->progress = 0; @@ -65,22 +84,24 @@ void nbio_begin_write(struct nbio_t* handle) bool nbio_iterate(struct nbio_t* handle, size_t* progress, size_t* len) { size_t amount = 65536; + if (amount > handle->len - handle->progress) - { amount = handle->len - handle->progress; - } + if (handle->op == NBIO_READ) - { fread((char*)handle->data + handle->progress, 1,amount, handle->f); - } if (handle->op == NBIO_WRITE) - { fwrite((char*)handle->data + handle->progress, 1,amount, handle->f); - } + handle->progress += amount; - if (progress) *progress = handle->progress; - if (len) *len = handle->len; - if (handle->progress == handle->len) handle->op = -1; + + if (progress) + *progress = handle->progress; + if (len) + *len = handle->len; + + if (handle->progress == handle->len) + handle->op = -1; return (handle->op < 0); } @@ -96,16 +117,19 @@ void nbio_resize(struct nbio_t* handle, size_t len) puts("ERROR - attempted file shrink operation, not implemented"); abort(); } - handle->len = len; + + handle->len = len; handle->data = realloc(handle->data, handle->len); - handle->op = -1; + handle->op = -1; } void* nbio_get_ptr(struct nbio_t* handle, size_t* len) { - if (len) *len = handle->len; - if (handle->op == -1) return handle->data; - else return NULL; + if (len) + *len = handle->len; + if (handle->op == -1) + return handle->data; + return NULL; } void nbio_free(struct nbio_t* handle)