RetroArch/libretro-common/file/nbio/nbio_windowsmmap.c

266 lines
6.4 KiB
C

/* Copyright (C) 2010-2017 The RetroArch team
*
* ---------------------------------------------------------------------------------------
* The following license statement only applies to this file (nbio_windowsmmap.c).
* ---------------------------------------------------------------------------------------
*
* Permission is hereby granted, free of charge,
* to any person obtaining a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#if defined(_WIN32) && !defined(_XBOX)
#include <stdio.h>
#include <stdlib.h>
#include <file/nbio.h>
#include <encodings/utf.h>
/* Assume W-functions do not work below VC2005 and Xbox platforms */
#if defined(_MSC_VER) && _MSC_VER < 1400 || defined(_XBOX)
#ifndef LEGACY_WIN32
#define LEGACY_WIN32
#endif
#endif
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;
signed char mode;
};
#if !defined(_WIN32) || defined(LEGACY_WIN32)
static const char * modes[]={ "rb", "wb", "r+b", "rb", "wb", "r+b" };
#else
static const wchar_t * modes[]={ L"rb", L"wb", L"r+b", L"rb", L"wb", L"r+b" };
#endif
static struct nbio_t* nbio_mmap_win32_open(const char * filename, unsigned mode)
{
void *buf = NULL;
struct nbio_t* handle = NULL;
size_t len = 0;
#if !defined(_WIN32) || defined(LEGACY_WIN32)
FILE* f = fopen(filename, modes[mode]);
#else
wchar_t *filename_wide = utf8_to_utf16_string_alloc(filename);
FILE* f = _wfopen(filename_wide, modes[mode]);
if (filename_wide)
free(filename_wide);
#endif
if (!f)
return NULL;
handle = (struct nbio_t*)malloc(sizeof(struct nbio_t));
if (!handle)
goto error;
handle->f = f;
switch (mode)
{
case NBIO_WRITE:
case BIO_WRITE:
break;
default:
fseek(handle->f, 0, SEEK_END);
len = ftell(handle->f);
break;
}
handle->mode = mode;
if (len)
buf = malloc(len);
if (len && !buf)
goto error;
handle->data = buf;
handle->len = len;
handle->progress = handle->len;
handle->op = -2;
return handle;
error:
if (handle)
free(handle);
fclose(f);
return NULL;
}
static void nbio_mmap_win32_begin_read(struct nbio_t* handle)
{
if (!handle)
return;
if (handle->op >= 0)
{
puts("ERROR - attempted file read operation while busy");
abort();
}
fseek(handle->f, 0, SEEK_SET);
handle->op = NBIO_READ;
handle->progress = 0;
}
static void nbio_mmap_win32_begin_write(struct nbio_t* handle)
{
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;
}
static bool nbio_mmap_win32_iterate(struct nbio_t* handle)
{
size_t amount = 65536;
if (!handle)
return false;
if (amount > handle->len - handle->progress)
amount = handle->len - handle->progress;
switch (handle->op)
{
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);
break;
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);
break;
}
handle->progress += amount;
if (handle->progress == handle->len)
handle->op = -1;
return (handle->op < 0);
}
static void nbio_mmap_win32_resize(struct nbio_t* handle, size_t len)
{
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;
}
static void *nbio_mmap_win32_get_ptr(struct nbio_t* handle, size_t* len)
{
if (!handle)
return NULL;
if (len)
*len = handle->len;
if (handle->op == -1)
return handle->data;
return NULL;
}
static void nbio_mmap_win32_cancel(struct nbio_t* handle)
{
if (!handle)
return;
handle->op = -1;
handle->progress = handle->len;
}
static void nbio_mmap_win32_free(struct nbio_t* handle)
{
if (!handle)
return;
if (handle->op >= 0)
{
puts("ERROR - attempted free() while busy");
abort();
}
fclose(handle->f);
free(handle->data);
handle->f = NULL;
handle->data = NULL;
free(handle);
}
nbio_intf_t nbio_mmap_win32 = {
nbio_mmap_win32_open,
nbio_mmap_win32_begin_read,
nbio_mmap_win32_begin_write,
nbio_mmap_win32_iterate,
nbio_mmap_win32_resize,
nbio_mmap_win32_get_ptr,
nbio_mmap_win32_cancel,
nbio_mmap_win32_free,
"nbio_mmap_win32",
};
#endif