mirror of
https://github.com/libretro/RetroArch
synced 2025-01-30 21:32:45 +00:00
(nbio_stdio.c) Some changes
This commit is contained in:
parent
0d563f1b28
commit
fd338160bc
@ -16,6 +16,7 @@ struct nbio_t
|
|||||||
* -2 - the pointer was reallocated since the last operation
|
* -2 - the pointer was reallocated since the last operation
|
||||||
*/
|
*/
|
||||||
signed char op;
|
signed char op;
|
||||||
|
signed char mode;
|
||||||
};
|
};
|
||||||
|
|
||||||
static const char * modes[]={ "rb", "wb", "r+b" };
|
static const char * modes[]={ "rb", "wb", "r+b" };
|
||||||
@ -23,20 +24,17 @@ static const char * modes[]={ "rb", "wb", "r+b" };
|
|||||||
struct nbio_t* nbio_open(const char * filename, unsigned mode)
|
struct nbio_t* nbio_open(const char * filename, unsigned mode)
|
||||||
{
|
{
|
||||||
struct nbio_t* handle = NULL;
|
struct nbio_t* handle = NULL;
|
||||||
FILE* f=fopen(filename, modes[mode]);
|
FILE* f = fopen(filename, modes[mode]);
|
||||||
if (!f)
|
if (!f)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
handle=(struct nbio_t*)malloc(sizeof(struct nbio_t));
|
handle = (struct nbio_t*)malloc(sizeof(struct nbio_t));
|
||||||
|
|
||||||
if (!handle)
|
if (!handle)
|
||||||
{
|
goto error;
|
||||||
fclose(f);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
handle->f = f;
|
handle->f = f;
|
||||||
handle->len = 0;
|
handle->len = 0;
|
||||||
|
|
||||||
if (mode != NBIO_WRITE)
|
if (mode != NBIO_WRITE)
|
||||||
{
|
{
|
||||||
@ -44,19 +42,23 @@ struct nbio_t* nbio_open(const char * filename, unsigned mode)
|
|||||||
handle->len = ftell(handle->f);
|
handle->len = ftell(handle->f);
|
||||||
}
|
}
|
||||||
|
|
||||||
handle->data = malloc(handle->len);
|
handle->mode = mode;
|
||||||
|
handle->data = malloc(handle->len);
|
||||||
|
|
||||||
if (handle->len && !handle->data)
|
if (handle->len && !handle->data)
|
||||||
{
|
goto error;
|
||||||
free(handle);
|
|
||||||
fclose(f);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
handle->progress = handle->len;
|
handle->progress = handle->len;
|
||||||
handle->op = -2;
|
handle->op = -2;
|
||||||
|
|
||||||
return handle;
|
return handle;
|
||||||
|
|
||||||
|
error:
|
||||||
|
if (handle)
|
||||||
|
free(handle);
|
||||||
|
handle = NULL;
|
||||||
|
fclose(f);
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void nbio_begin_read(struct nbio_t* handle)
|
void nbio_begin_read(struct nbio_t* handle)
|
||||||
@ -78,6 +80,9 @@ void nbio_begin_read(struct nbio_t* handle)
|
|||||||
|
|
||||||
void nbio_begin_write(struct nbio_t* handle)
|
void nbio_begin_write(struct nbio_t* handle)
|
||||||
{
|
{
|
||||||
|
if (!handle)
|
||||||
|
return;
|
||||||
|
|
||||||
if (handle->op >= 0)
|
if (handle->op >= 0)
|
||||||
{
|
{
|
||||||
puts("ERROR - attempted file write operation while busy");
|
puts("ERROR - attempted file write operation while busy");
|
||||||
@ -99,10 +104,15 @@ bool nbio_iterate(struct nbio_t* handle)
|
|||||||
if (amount > handle->len - handle->progress)
|
if (amount > handle->len - handle->progress)
|
||||||
amount = handle->len - handle->progress;
|
amount = handle->len - handle->progress;
|
||||||
|
|
||||||
if (handle->op == NBIO_READ)
|
switch (handle->op)
|
||||||
fread((char*)handle->data + handle->progress, 1,amount, handle->f);
|
{
|
||||||
if (handle->op == NBIO_WRITE)
|
case NBIO_READ:
|
||||||
fwrite((char*)handle->data + handle->progress, 1,amount, handle->f);
|
fread((char*)handle->data + handle->progress, 1,amount, handle->f);
|
||||||
|
break;
|
||||||
|
case NBIO_WRITE:
|
||||||
|
fwrite((char*)handle->data + handle->progress, 1,amount, handle->f);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
handle->progress += amount;
|
handle->progress += amount;
|
||||||
|
|
||||||
@ -113,6 +123,9 @@ bool nbio_iterate(struct nbio_t* handle)
|
|||||||
|
|
||||||
void nbio_resize(struct nbio_t* handle, size_t len)
|
void nbio_resize(struct nbio_t* handle, size_t len)
|
||||||
{
|
{
|
||||||
|
if (!handle)
|
||||||
|
return;
|
||||||
|
|
||||||
if (handle->op >= 0)
|
if (handle->op >= 0)
|
||||||
{
|
{
|
||||||
puts("ERROR - attempted file resize operation while busy");
|
puts("ERROR - attempted file resize operation while busy");
|
||||||
@ -143,6 +156,9 @@ void* nbio_get_ptr(struct nbio_t* handle, size_t* len)
|
|||||||
|
|
||||||
void nbio_cancel(struct nbio_t* handle)
|
void nbio_cancel(struct nbio_t* handle)
|
||||||
{
|
{
|
||||||
|
if (!handle)
|
||||||
|
return;
|
||||||
|
|
||||||
handle->op = -1;
|
handle->op = -1;
|
||||||
handle->progress = handle->len;
|
handle->progress = handle->len;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user