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->len = 0;
if (mode != NBIO_WRITE)
switch (mode)
{
case NBIO_WRITE:
case BIO_WRITE:
break;
default:
fseek(handle->f, 0, SEEK_END);
handle->len = ftell(handle->f);
break;
}
handle->mode = mode;
@ -107,10 +112,25 @@ bool nbio_iterate(struct nbio_t* handle)
switch (handle->op)
{
case NBIO_READ:
fread((char*)handle->data + handle->progress, 1,amount, handle->f);
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);
break;
case NBIO_WRITE:
fwrite((char*)handle->data + handle->progress, 1,amount, handle->f);
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);
break;
}

View File

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