fceugx/source/fceultra/emufile.h
2010-08-29 21:15:42 +00:00

429 lines
9.1 KiB
C++

/* Copyright (C) 2009 DeSmuME team
*
* This file is part of DeSmuME
*
* DeSmuME 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.
*
* DeSmuME 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 DeSmuME; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef EMUFILE_H
#define EMUFILE_H
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <algorithm>
#include <string>
#include <stdarg.h>
#include <malloc.h>
//should be changed to #ifdef FCEUX but too much work
#ifndef DESMUME
#ifndef __GCTYPES_H__
typedef unsigned char u8;
typedef unsigned short u16;
typedef unsigned int u32;
typedef char s8;
typedef short s16;
typedef int s32;
#endif
#endif
#ifdef _XBOX
#undef min;
#undef max;
#endif
class EMUFILE {
protected:
bool failbit;
public:
EMUFILE()
: failbit(false)
{}
//takes control of the provided EMUFILE and returns a new EMUFILE which is guranteed to be in memory
static EMUFILE* memwrap(EMUFILE* fp);
virtual ~EMUFILE() {}
static bool readAllBytes(std::vector<u8>* buf, const std::string& fname);
bool fail() { return failbit; }
bool eof() { return size()==ftell(); }
size_t fread(const void *ptr, size_t bytes){
return _fread(ptr,bytes);
}
void unget() { fseek(-1,SEEK_CUR); }
//virtuals
public:
virtual FILE *get_fp() = 0;
virtual int fprintf(const char *format, ...) = 0;
virtual int fgetc() = 0;
virtual int fputc(int c) = 0;
virtual size_t _fread(const void *ptr, size_t bytes) = 0;
//removing these return values for now so we can find any code that might be using them and make sure
//they handle the return values correctly
virtual void fwrite(const void *ptr, size_t bytes) = 0;
virtual int fseek(int offset, int origin) = 0;
virtual int ftell() = 0;
virtual int size() = 0;
};
//todo - handle read-only specially?
class EMUFILE_MEMORY : public EMUFILE {
protected:
std::vector<u8> *vec;
bool ownvec;
s32 pos, len;
void reserve(u32 amt) {
if(vec->size() < amt)
vec->resize(amt);
}
public:
EMUFILE_MEMORY(std::vector<u8> *underlying) : vec(underlying), ownvec(false), pos(0), len(underlying->size()) { }
EMUFILE_MEMORY(u32 preallocate) : vec(new std::vector<u8>()), ownvec(true), pos(0), len(0) {
vec->resize(preallocate);
len = preallocate;
}
EMUFILE_MEMORY() : vec(new std::vector<u8>()), ownvec(true), pos(0), len(0) { vec->reserve(1024); }
EMUFILE_MEMORY(void* buf, s32 size) : vec(new std::vector<u8>()), ownvec(true), pos(0), len(size) {
vec->resize(size);
if(size != 0)
memcpy(&vec[0],buf,size);
}
~EMUFILE_MEMORY() {
if(ownvec) delete vec;
}
u8* buf() { return &(*vec)[0]; }
std::vector<u8>* get_vec() { return vec; };
virtual FILE *get_fp() { return NULL; }
virtual int fprintf(const char *format, ...) {
va_list argptr;
va_start(argptr, format);
//we dont generate straight into the buffer because it will null terminate (one more byte than we want)
int amt = vsnprintf(0,0,format,argptr);
char* tempbuf = new char[amt+1];
vsprintf(tempbuf,format,argptr);
fwrite(tempbuf,amt);
delete[] tempbuf;
va_end(argptr);
return amt;
};
virtual int fgetc() {
u8 temp;
//need an optimized codepath
//if(_fread(&temp,1) != 1)
// return EOF;
//else return temp;
u32 remain = len-pos;
if(remain<1) {
failbit = true;
return -1;
}
temp = buf()[pos];
pos++;
return temp;
}
virtual int fputc(int c) {
u8 temp = (u8)c;
//TODO
//if(fwrite(&temp,1)!=1) return EOF;
fwrite(&temp,1);
return 0;
}
virtual size_t _fread(const void *ptr, size_t bytes){
u32 remain = len-pos;
u32 todo = std::min<u32>(remain,(u32)bytes);
memcpy((void*)ptr,buf()+pos,todo);
pos += todo;
if(todo<bytes)
failbit = true;
return todo;
}
//removing these return values for now so we can find any code that might be using them and make sure
//they handle the return values correctly
virtual void fwrite(const void *ptr, size_t bytes){
reserve(pos+bytes);
memcpy(buf()+pos,ptr,bytes);
pos += bytes;
len = std::max(pos,len);
}
virtual int fseek(int offset, int origin){
//work differently for read-only...?
switch(origin) {
case SEEK_SET:
pos = offset;
break;
case SEEK_CUR:
pos += offset;
break;
case SEEK_END:
pos = size()+offset;
break;
default:
assert(false);
}
reserve(pos);
return 0;
}
virtual int ftell() {
return pos;
}
void trim()
{
vec->resize(len);
}
virtual int size() { return (int)len; }
};
#ifdef GEKKO
class EMUFILE_MEMFILE : public EMUFILE {
protected:
bool own_buffer;
char *buffer;
s32 buffersize, pos, len;
public:
EMUFILE_MEMFILE(u32 preallocate) {
buffer = (char *)memalign(32, preallocate);
own_buffer = true;
buffersize = 0;
len = 0;
pos = 0;
if(!buffer)
return;
buffersize = preallocate;
}
EMUFILE_MEMFILE(void* src, s32 src_size) {
buffer = (char *)src;
own_buffer = false;
buffersize = src_size;
len = src_size;
pos = 0;
}
~EMUFILE_MEMFILE() {
if(own_buffer && buffer) free(buffer);
}
char* buf() { return buffer; }
virtual FILE *get_fp() { return NULL; }
virtual int fprintf(const char *format, ...) {
va_list argptr;
va_start(argptr, format);
//we dont generate straight into the buffer because it will null terminate (one more byte than we want)
int amt = vsnprintf(0,0,format,argptr);
char* tempbuf = new char[amt+1];
vsprintf(tempbuf,format,argptr);
fwrite(tempbuf,amt);
delete[] tempbuf;
va_end(argptr);
return amt;
};
virtual int fgetc() {
if(pos >= len) {
failbit = true;
return -1;
}
return (int)buffer[pos++];
}
virtual int fputc(int c) {
if(pos >= buffersize) {
failbit = true;
return -1;
}
buffer[pos] = (char)c;
pos++;
len++;
return 0;
}
virtual size_t _fread(const void *ptr, size_t bytes){
u32 remain = len-pos;
if(remain == 0)
return 0;
u32 todo = std::min<u32>(remain,(u32)bytes);
memcpy((void*)ptr,buffer+pos,todo);
pos += todo;
return todo;
}
virtual void fwrite(const void *ptr, size_t bytes){
if(pos+bytes > (u32)buffersize) return;
memcpy(buffer+pos,ptr,bytes);
pos += bytes;
len = std::max(pos,len);
}
virtual int fseek(int offset, int origin){
switch(origin) {
case SEEK_SET:
if(offset >= 0 && offset <= len)
pos = offset;
else
return 1; // failure
break;
case SEEK_CUR:
if((pos + offset) >= 0 && (pos + offset) <= len)
pos += offset;
else
return 1; // failure
break;
case SEEK_END:
if((pos + offset) >= 0 && (pos + offset) <= len)
pos = len + offset;
else
return 1; // failure
break;
default:
return 1; // failure
}
return 0;
}
virtual int ftell() {
return pos;
}
void trim()
{
}
virtual int size() { return (int)len; }
};
#endif
class EMUFILE_FILE : public EMUFILE {
protected:
FILE* fp;
private:
void open(const char* fname, const char* mode)
{
fp = fopen(fname,mode);
if(!fp)
failbit = true;
}
public:
EMUFILE_FILE(const std::string& fname, const char* mode) { open(fname.c_str(),mode); }
EMUFILE_FILE(const char* fname, const char* mode) { open(fname,mode); }
virtual ~EMUFILE_FILE() {
if(NULL != fp)
fclose(fp);
}
virtual FILE *get_fp() {
return fp;
}
bool is_open() { return fp != NULL; }
virtual int fprintf(const char *format, ...) {
va_list argptr;
va_start(argptr, format);
int ret = ::vfprintf(fp, format, argptr);
va_end(argptr);
return ret;
};
virtual int fgetc() {
return ::fgetc(fp);
}
virtual int fputc(int c) {
return ::fputc(c, fp);
}
virtual size_t _fread(const void *ptr, size_t bytes){
size_t ret = ::fread((void*)ptr, 1, bytes, fp);
if(ret < bytes)
failbit = true;
return ret;
}
//removing these return values for now so we can find any code that might be using them and make sure
//they handle the return values correctly
virtual void fwrite(const void *ptr, size_t bytes){
size_t ret = ::fwrite((void*)ptr, 1, bytes, fp);
if(ret < bytes)
failbit = true;
}
virtual int fseek(int offset, int origin){
return ::fseek(fp, offset, origin);
}
virtual int ftell() {
return (u32)::ftell(fp);
}
virtual int size() {
int oldpos = ftell();
fseek(0,SEEK_END);
int len = ftell();
fseek(oldpos,SEEK_SET);
return len;
}
};
#endif