207 lines
4.0 KiB
C
Raw Normal View History

2015-02-15 22:43:59 +01:00
#include <stdio.h>
#include <stdlib.h>
#include <file/nbio.h>
2015-02-15 23:29:13 +01:00
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;
2015-03-18 03:51:58 +01:00
signed char mode;
2015-02-15 22:43:59 +01:00
};
2015-03-18 04:40:10 +01:00
static const char * modes[]={ "rb", "wb", "r+b", "rb", "wb", "r+b" };
2015-02-15 23:29:13 +01:00
2015-02-20 03:18:06 +01:00
struct nbio_t* nbio_open(const char * filename, unsigned mode)
2015-02-15 22:43:59 +01:00
{
struct nbio_t* handle = NULL;
2015-03-18 03:51:58 +01:00
FILE* f = fopen(filename, modes[mode]);
if (!f)
2015-02-15 23:29:13 +01:00
return NULL;
2015-03-18 03:51:58 +01:00
handle = (struct nbio_t*)malloc(sizeof(struct nbio_t));
2015-02-15 23:29:13 +01:00
if (!handle)
2015-03-18 03:51:58 +01:00
goto error;
2015-02-15 23:29:13 +01:00
2015-03-18 03:51:58 +01:00
handle->f = f;
handle->len = 0;
2015-02-15 23:29:13 +01:00
2015-03-18 03:59:44 +01:00
switch (mode)
{
2015-03-18 03:59:44 +01:00
case NBIO_WRITE:
case BIO_WRITE:
break;
default:
fseek(handle->f, 0, SEEK_END);
handle->len = ftell(handle->f);
break;
}
2015-02-15 23:29:13 +01:00
2015-03-18 03:51:58 +01:00
handle->mode = mode;
handle->data = malloc(handle->len);
2015-02-15 23:29:13 +01:00
2015-02-15 23:30:56 +01:00
if (handle->len && !handle->data)
2015-03-18 03:51:58 +01:00
goto error;
2015-02-15 23:29:13 +01:00
2015-03-18 03:51:58 +01:00
handle->progress = handle->len;
handle->op = -2;
return handle;
2015-03-18 03:51:58 +01:00
error:
if (handle)
{
if (handle->data)
free(handle->data);
handle->data = NULL;
2015-03-18 03:51:58 +01:00
free(handle);
}
2015-03-18 03:51:58 +01:00
handle = NULL;
fclose(f);
return NULL;
2015-02-15 22:43:59 +01:00
}
void nbio_begin_read(struct nbio_t* handle)
{
2015-02-20 02:34:17 +01:00
if (!handle)
return;
if (handle->op >= 0)
{
puts("ERROR - attempted file read operation while busy");
abort();
}
2015-02-15 23:29:13 +01:00
fseek(handle->f, 0, SEEK_SET);
2015-02-15 23:29:13 +01:00
handle->op = NBIO_READ;
handle->progress = 0;
2015-02-15 22:43:59 +01:00
}
void nbio_begin_write(struct nbio_t* handle)
{
2015-03-18 03:51:58 +01:00
if (!handle)
return;
if (handle->op >= 0)
{
puts("ERROR - attempted file write operation while busy");
abort();
}
fseek(handle->f, 0, SEEK_SET);
handle->op = NBIO_WRITE;
handle->progress = 0;
2015-02-15 22:43:59 +01:00
}
bool nbio_iterate(struct nbio_t* handle)
2015-02-15 22:43:59 +01:00
{
size_t amount = 65536;
2015-02-15 23:29:13 +01:00
2015-02-20 02:34:17 +01:00
if (!handle)
return false;
if (amount > handle->len - handle->progress)
amount = handle->len - handle->progress;
2015-02-15 23:29:13 +01:00
2015-03-18 03:51:58 +01:00
switch (handle->op)
{
case NBIO_READ:
2015-03-18 03:59:44 +01:00
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);
2015-03-18 03:51:58 +01:00
break;
case NBIO_WRITE:
2015-03-18 03:59:44 +01:00
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);
2015-03-18 03:51:58 +01:00
break;
}
2015-02-15 23:29:13 +01:00
handle->progress += amount;
2015-02-15 23:29:13 +01:00
if (handle->progress == handle->len)
2015-02-15 23:29:13 +01:00
handle->op = -1;
return (handle->op < 0);
2015-02-15 22:43:59 +01:00
}
void nbio_resize(struct nbio_t* handle, size_t len)
{
2015-03-18 03:51:58 +01:00
if (!handle)
return;
if (handle->op >= 0)
{
puts("ERROR - attempted file resize operation while busy");
abort();
}
if (len < handle->len)
{
puts("ERROR - attempted file shrink operation, not implemented");
abort();
}
handle->len = len;
handle->data = realloc(handle->data, handle->len);
handle->op = -1;
handle->progress = handle->len;
2015-02-15 22:43:59 +01:00
}
void* nbio_get_ptr(struct nbio_t* handle, size_t* len)
{
2015-02-20 02:34:17 +01:00
if (!handle)
return NULL;
if (len)
2015-02-15 23:29:13 +01:00
*len = handle->len;
if (handle->op == -1)
2015-02-15 23:29:13 +01:00
return handle->data;
return NULL;
2015-02-15 22:43:59 +01:00
}
void nbio_cancel(struct nbio_t* handle)
{
2015-03-18 03:51:58 +01:00
if (!handle)
return;
2015-02-17 00:30:23 +01:00
handle->op = -1;
handle->progress = handle->len;
}
2015-02-15 22:43:59 +01:00
void nbio_free(struct nbio_t* handle)
{
2015-02-17 00:30:23 +01:00
if (!handle)
return;
if (handle->op >= 0)
{
puts("ERROR - attempted free() while busy");
abort();
}
fclose(handle->f);
free(handle->data);
2015-06-12 17:57:19 -03:00
handle->f = NULL;
handle->data = NULL;
free(handle);
2015-02-15 22:43:59 +01:00
}