Add BIO_READ/BIO_WRITE to nbio_stdio.c

This commit is contained in:
twinaphex 2015-03-18 03:59:44 +01:00
parent fd338160bc
commit c6d0e4ec51
2 changed files with 41 additions and 5 deletions

View File

@ -36,10 +36,15 @@ struct nbio_t* nbio_open(const char * filename, unsigned mode)
handle->f = f; handle->f = f;
handle->len = 0; handle->len = 0;
if (mode != NBIO_WRITE) switch (mode)
{ {
case NBIO_WRITE:
case BIO_WRITE:
break;
default:
fseek(handle->f, 0, SEEK_END); fseek(handle->f, 0, SEEK_END);
handle->len = ftell(handle->f); handle->len = ftell(handle->f);
break;
} }
handle->mode = mode; handle->mode = mode;
@ -107,9 +112,24 @@ bool nbio_iterate(struct nbio_t* handle)
switch (handle->op) switch (handle->op)
{ {
case NBIO_READ: case NBIO_READ:
if (handle->mode == BIO_READ)
{
amount = handle->len;
fread((char*)handle->data, 1, amount, handle->f);
}
else
fread((char*)handle->data + handle->progress, 1, amount, handle->f); fread((char*)handle->data + handle->progress, 1, amount, handle->f);
break; break;
case NBIO_WRITE: case NBIO_WRITE:
if (handle->mode == BIO_WRITE)
{
size_t written = 0;
amount = handle->len;
written = fwrite((char*)handle->data, 1, amount, handle->f);
if (written != amount)
return false;
}
else
fwrite((char*)handle->data + handle->progress, 1, amount, handle->f); fwrite((char*)handle->data + handle->progress, 1, amount, handle->f);
break; break;
} }

View File

@ -26,9 +26,25 @@
#include <stddef.h> #include <stddef.h>
#include <boolean.h> #include <boolean.h>
#ifndef NBIO_READ
#define NBIO_READ 0 #define NBIO_READ 0
#endif
#ifndef NBIO_WRITE
#define NBIO_WRITE 1 #define NBIO_WRITE 1
#endif
#ifndef NBIO_UPDATE
#define NBIO_UPDATE 2 #define NBIO_UPDATE 2
#endif
#ifndef BIO_READ
#define BIO_READ 3
#endif
#ifndef BIO_WRITE
#define BIO_WRITE 4
#endif
struct nbio_t; struct nbio_t;