Files
ci-libretro-fceumm/src/file.c

204 lines
4.2 KiB
C
Raw Normal View History

/* FCE Ultra - NES/Famicom Emulator
*
* Copyright notice for this file:
* Copyright (C) 2002 Xodnizel
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdlib.h>
#include <string.h>
2014-04-15 01:59:54 -07:00
#ifdef _WIN32
#include <direct.h>
#else
#include <unistd.h>
2014-04-15 01:59:54 -07:00
#endif
#include <string/stdstring.h>
#include <file/file_path.h>
#include <streams/file_stream.h>
#include "fceu-types.h"
#include "file.h"
#include "fceu-endian.h"
2014-03-30 22:29:30 +02:00
#include "fceu-memory.h"
#include "driver.h"
#include "general.h"
static MEMWRAP *MakeMemWrap(RFILE *tz)
2015-08-06 13:00:18 +02:00
{
MEMWRAP *tmp = NULL;
2015-08-06 13:00:18 +02:00
if (!(tmp = (MEMWRAP*)FCEU_malloc(sizeof(MEMWRAP))))
goto doret;
tmp->location = 0;
filestream_seek(tz, 0, RETRO_VFS_SEEK_POSITION_END);
tmp->size = filestream_tell(tz);
filestream_seek(tz, 0, RETRO_VFS_SEEK_POSITION_START);
if (!(tmp->data_int = (uint8*)FCEU_malloc(tmp->size)))
2015-08-06 13:00:18 +02:00
{
free(tmp);
tmp = NULL;
2015-08-06 13:00:18 +02:00
goto doret;
}
filestream_read(tz, tmp->data_int, tmp->size);
tmp->data = tmp->data_int;
2015-08-06 13:00:18 +02:00
doret:
return tmp;
}
static MEMWRAP *MakeMemWrapBuffer(const uint8 *buffer, size_t bufsize)
2015-08-06 13:00:18 +02:00
{
MEMWRAP *tmp = (MEMWRAP*)FCEU_malloc(sizeof(MEMWRAP));
if (!tmp)
return NULL;
tmp->location = 0;
tmp->size = bufsize;
tmp->data_int = NULL;
tmp->data = buffer;
2015-08-06 13:00:18 +02:00
return tmp;
}
FCEUFILE * FCEU_fopen(const char *path, const uint8 *buffer, size_t bufsize)
2015-08-06 13:00:18 +02:00
{
FCEUFILE *fceufp = (FCEUFILE*)malloc(sizeof(FCEUFILE));
if (buffer)
fceufp->fp = MakeMemWrapBuffer(buffer, bufsize);
2015-08-06 13:00:18 +02:00
else
2017-04-02 02:12:44 +08:00
{
RFILE *t = NULL;
if (!string_is_empty(path) && path_is_valid(path))
t = filestream_open(path,
RETRO_VFS_FILE_ACCESS_READ,
RETRO_VFS_FILE_ACCESS_HINT_NONE);
2017-04-02 02:12:44 +08:00
if (!t)
{
free(fceufp);
return NULL;
2017-04-02 02:12:44 +08:00
}
fceufp->fp = MakeMemWrap(t);
filestream_close(t);
2017-04-02 02:12:44 +08:00
}
2015-08-06 13:00:18 +02:00
return fceufp;
}
2015-08-06 13:00:18 +02:00
int FCEU_fclose(FCEUFILE *fp)
{
if (!fp)
return 0;
2015-08-06 13:00:18 +02:00
if (fp->fp)
{
if (fp->fp->data_int)
free(fp->fp->data_int);
fp->fp->data_int = NULL;
2015-08-06 13:00:18 +02:00
free(fp->fp);
}
2015-08-06 13:00:18 +02:00
fp->fp = NULL;
free(fp);
fp = NULL;
return 1;
}
2015-08-06 13:00:18 +02:00
uint64 FCEU_fread(void *ptr, size_t element_size, size_t nmemb, FCEUFILE *fp)
{
uint32_t total = nmemb * element_size;
2015-08-06 13:00:18 +02:00
if (fp->fp->location >= fp->fp->size)
return 0;
2015-08-06 11:30:34 +02:00
2015-08-06 13:00:18 +02:00
if((fp->fp->location + total) > fp->fp->size)
{
int64_t ak = fp->fp->size - fp->fp->location;
2015-08-06 11:30:34 +02:00
2015-08-06 13:00:18 +02:00
memcpy((uint8_t*)ptr, fp->fp->data + fp->fp->location, ak);
2015-08-06 11:30:34 +02:00
2015-08-06 13:00:18 +02:00
fp->fp->location = fp->fp->size;
return (ak / element_size);
}
2017-04-02 02:12:44 +08:00
2015-08-06 13:00:18 +02:00
memcpy((uint8_t*)ptr, fp->fp->data + fp->fp->location, total);
fp->fp->location += total;
return nmemb;
}
2015-08-06 13:00:18 +02:00
int FCEU_fseek(FCEUFILE *fp, long offset, int whence)
{
switch (whence)
{
case SEEK_SET:
if (offset >= fp->fp->size)
return -1;
fp->fp->location = offset;
break;
case SEEK_CUR:
if ((offset + fp->fp->location) > fp->fp->size)
return -1;
fp->fp->location += offset;
break;
}
return 0;
}
2015-07-23 22:18:46 +02:00
int FCEU_read32le(uint32 *Bufo, FCEUFILE *fp)
{
2015-08-06 13:00:18 +02:00
if ((fp->fp->location + 4) > fp->fp->size)
return 0;
*Bufo = FCEU_de32lsb(fp->fp->data + fp->fp->location);
fp->fp->location += 4;
return 1;
}
2015-08-06 13:00:18 +02:00
int FCEU_fgetc(FCEUFILE *fp)
{
if (fp->fp->location < fp->fp->size)
return fp->fp->data[fp->fp->location++];
return EOF;
}
2015-08-06 13:00:18 +02:00
uint64 FCEU_ftell(FCEUFILE *fp)
{
return fp->fp->location;
}
2015-08-06 13:00:18 +02:00
uint64 FCEU_fgetsize(FCEUFILE *fp)
{
return fp->fp->size;
}