mirror of
https://github.com/libretro/beetle-lynx-libretro.git
synced 2024-11-23 16:21:03 +00:00
Cleanups
This commit is contained in:
parent
d3e0741392
commit
9f80c2f114
2
Makefile
2
Makefile
@ -276,8 +276,6 @@ MEDNAFEN_SOURCES := $(MEDNAFEN_DIR)/mednafen.cpp \
|
||||
$(MEDNAFEN_DIR)/math_ops.cpp \
|
||||
$(MEDNAFEN_DIR)/settings.cpp \
|
||||
$(MEDNAFEN_DIR)/general.cpp \
|
||||
$(MEDNAFEN_DIR)/FileWrapper.cpp \
|
||||
$(MEDNAFEN_DIR)/FileStream.cpp \
|
||||
$(MEDNAFEN_DIR)/MemoryStream.cpp \
|
||||
$(MEDNAFEN_DIR)/Stream.cpp \
|
||||
$(MEDNAFEN_DIR)/state.cpp \
|
||||
|
@ -89,8 +89,6 @@ MEDNAFEN_SOURCES := $(MEDNAFEN_DIR)/mednafen.cpp \
|
||||
$(MEDNAFEN_DIR)/math_ops.cpp \
|
||||
$(MEDNAFEN_DIR)/settings.cpp \
|
||||
$(MEDNAFEN_DIR)/general.cpp \
|
||||
$(MEDNAFEN_DIR)/FileWrapper.cpp \
|
||||
$(MEDNAFEN_DIR)/FileStream.cpp \
|
||||
$(MEDNAFEN_DIR)/MemoryStream.cpp \
|
||||
$(MEDNAFEN_DIR)/Stream.cpp \
|
||||
$(MEDNAFEN_DIR)/state.cpp \
|
||||
|
@ -1,112 +0,0 @@
|
||||
// TODO/WIP
|
||||
|
||||
/* Mednafen - Multi-system Emulator
|
||||
*
|
||||
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
#include <sys/stat.h>
|
||||
#include "mednafen.h"
|
||||
#include "Stream.h"
|
||||
#include "FileStream.h"
|
||||
|
||||
#include <trio/trio.h>
|
||||
#include <stdarg.h>
|
||||
#include <string.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <io.h>
|
||||
#else
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
#define fseeko fseek
|
||||
#define ftello ftell
|
||||
|
||||
FileStream::FileStream(const char *path, const int mode): OpenedMode(mode)
|
||||
{
|
||||
if(mode == FileStream::MODE_WRITE)
|
||||
fp = fopen(path, "wb");
|
||||
else
|
||||
fp = fopen(path, "rb");
|
||||
|
||||
if(!fp)
|
||||
{
|
||||
ErrnoHolder ene(errno);
|
||||
|
||||
throw(MDFN_Error(ene.Errno(), _("Error opening file %s"), ene.StrError()));
|
||||
}
|
||||
}
|
||||
|
||||
FileStream::~FileStream()
|
||||
{
|
||||
}
|
||||
|
||||
uint64 FileStream::attributes(void)
|
||||
{
|
||||
uint64 ret = ATTRIBUTE_SEEKABLE;
|
||||
|
||||
switch(OpenedMode)
|
||||
{
|
||||
case FileStream::MODE_READ:
|
||||
ret |= ATTRIBUTE_READABLE;
|
||||
break;
|
||||
case FileStream::MODE_WRITE_SAFE:
|
||||
case FileStream::MODE_WRITE:
|
||||
ret |= ATTRIBUTE_WRITEABLE;
|
||||
break;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
uint64 FileStream::read(void *data, uint64 count, bool error_on_eos)
|
||||
{
|
||||
return fread(data, 1, count, fp);
|
||||
}
|
||||
|
||||
void FileStream::write(const void *data, uint64 count)
|
||||
{
|
||||
fwrite(data, 1, count, fp);
|
||||
}
|
||||
|
||||
void FileStream::seek(int64 offset, int whence)
|
||||
{
|
||||
fseeko(fp, offset, whence);
|
||||
}
|
||||
|
||||
int64 FileStream::tell(void)
|
||||
{
|
||||
return ftello(fp);
|
||||
}
|
||||
|
||||
int64 FileStream::size(void)
|
||||
{
|
||||
struct stat buf;
|
||||
|
||||
fstat(fileno(fp), &buf);
|
||||
|
||||
return(buf.st_size);
|
||||
}
|
||||
|
||||
void FileStream::close(void)
|
||||
{
|
||||
if(!fp)
|
||||
return;
|
||||
|
||||
FILE *tmp = fp;
|
||||
fp = NULL;
|
||||
fclose(tmp);
|
||||
}
|
@ -1,56 +0,0 @@
|
||||
/* Mednafen - Multi-system Emulator
|
||||
*
|
||||
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
// TODO/WIP
|
||||
|
||||
#ifndef __MDFN_FILESTREAM_H
|
||||
#define __MDFN_FILESTREAM_H
|
||||
|
||||
#include "Stream.h"
|
||||
#include "FileWrapper.h"
|
||||
|
||||
class FileStream : public Stream
|
||||
{
|
||||
public:
|
||||
|
||||
enum
|
||||
{
|
||||
MODE_READ = FileWrapper::MODE_READ,
|
||||
MODE_WRITE = FileWrapper::MODE_WRITE,
|
||||
MODE_WRITE_SAFE = FileWrapper::MODE_WRITE_SAFE,
|
||||
};
|
||||
|
||||
FileStream(const char *path, const int mode);
|
||||
virtual ~FileStream();
|
||||
|
||||
virtual uint64 attributes(void);
|
||||
|
||||
virtual uint64 read(void *data, uint64 count, bool error_on_eos = true);
|
||||
virtual void write(const void *data, uint64 count);
|
||||
virtual void seek(int64 offset, int whence);
|
||||
virtual int64 tell(void);
|
||||
virtual int64 size(void);
|
||||
virtual void close(void);
|
||||
|
||||
private:
|
||||
FILE *fp;
|
||||
const int OpenedMode;
|
||||
};
|
||||
|
||||
|
||||
|
||||
#endif
|
@ -1,140 +0,0 @@
|
||||
/* Mednafen - Multi-system Emulator
|
||||
*
|
||||
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
#include <sys/stat.h>
|
||||
#include "mednafen.h"
|
||||
#include "FileWrapper.h"
|
||||
|
||||
#include <trio/trio.h>
|
||||
#include <stdarg.h>
|
||||
#include <string.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <io.h>
|
||||
#else
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
// Some really bad preprocessor abuse follows to handle platforms that don't have fseeko and ftello...and of course
|
||||
// for largefile support on Windows:
|
||||
|
||||
#define fseeko fseek
|
||||
#define ftello ftell
|
||||
|
||||
// For special uses, IE in classes that take a path or a FileWrapper & in the constructor, and the FileWrapper non-pointer member
|
||||
// is in the initialization list for the path constructor but not the constructor with FileWrapper&
|
||||
|
||||
FileWrapper::FileWrapper(const char *path, const int mode, const char *purpose) : OpenedMode(mode)
|
||||
{
|
||||
if(mode == MODE_WRITE)
|
||||
fp = fopen(path, "wb");
|
||||
else
|
||||
fp = fopen(path, "rb");
|
||||
|
||||
if(!fp)
|
||||
{
|
||||
ErrnoHolder ene(errno);
|
||||
|
||||
throw(MDFN_Error(ene.Errno(), _("Error opening file %s"), ene.StrError()));
|
||||
}
|
||||
}
|
||||
|
||||
FileWrapper::~FileWrapper()
|
||||
{
|
||||
close();
|
||||
}
|
||||
|
||||
void FileWrapper::close(void)
|
||||
{
|
||||
if(!fp)
|
||||
return;
|
||||
|
||||
FILE *tmp = fp;
|
||||
fp = NULL;
|
||||
fclose(tmp);
|
||||
}
|
||||
|
||||
uint64 FileWrapper::read(void *data, uint64 count, bool error_on_eof)
|
||||
{
|
||||
return fread(data, 1, count, fp);
|
||||
}
|
||||
|
||||
void FileWrapper::flush(void)
|
||||
{
|
||||
fflush(fp);
|
||||
}
|
||||
|
||||
void FileWrapper::write(const void *data, uint64 count)
|
||||
{
|
||||
fwrite(data, 1, count, fp);
|
||||
}
|
||||
|
||||
int FileWrapper::scanf(const char *format, ...)
|
||||
{
|
||||
va_list ap;
|
||||
int ret;
|
||||
|
||||
va_start(ap, format);
|
||||
|
||||
ret = trio_vfscanf(fp, format, ap);
|
||||
|
||||
va_end(ap);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void FileWrapper::put_char(int c)
|
||||
{
|
||||
fputc(c, fp);
|
||||
}
|
||||
|
||||
void FileWrapper::put_string(const char *str)
|
||||
{
|
||||
write(str, strlen(str));
|
||||
}
|
||||
|
||||
// We need to decide whether to prohibit NULL characters in output and input strings via std::string.
|
||||
// Yes for correctness, no for potential security issues(though unlikely in context all things considered).
|
||||
void FileWrapper::put_string(const std::string &str)
|
||||
{
|
||||
write(str.data(), str.size());
|
||||
}
|
||||
|
||||
char *FileWrapper::get_line(char *buf_s, int buf_size)
|
||||
{
|
||||
return ::fgets(buf_s, buf_size, fp);
|
||||
}
|
||||
|
||||
|
||||
void FileWrapper::seek(int64 offset, int whence)
|
||||
{
|
||||
fseeko(fp, offset, whence);
|
||||
}
|
||||
|
||||
int64 FileWrapper::size(void)
|
||||
{
|
||||
struct stat buf;
|
||||
|
||||
fstat(fileno(fp), &buf);
|
||||
|
||||
return(buf.st_size);
|
||||
}
|
||||
|
||||
int64 FileWrapper::tell(void)
|
||||
{
|
||||
return ftello(fp);
|
||||
}
|
@ -1,57 +0,0 @@
|
||||
#ifndef __MDFN_FILEWRAPPER_H
|
||||
#define __MDFN_FILEWRAPPER_H
|
||||
|
||||
// A stdio FILE wrapper(with some BSD and POSIXisms, and a little dash of Win32, thrown in for special behaviors)
|
||||
class FileWrapper
|
||||
{
|
||||
public:
|
||||
|
||||
enum
|
||||
{
|
||||
MODE_READ = 0,
|
||||
MODE_WRITE,
|
||||
MODE_WRITE_SAFE // Will throw an exception instead of overwriting an existing file.
|
||||
};
|
||||
|
||||
FileWrapper(const char *path, const int mode, const char *purpose = NULL);
|
||||
~FileWrapper();
|
||||
|
||||
uint64 read(void *data, uint64 count, bool error_on_eof = true);
|
||||
|
||||
void write(const void *data, uint64 count);
|
||||
|
||||
int scanf(const char *format, ...) MDFN_FORMATSTR(scanf, 2, 3);
|
||||
|
||||
void put_char(int c);
|
||||
|
||||
void put_string(const char *str);
|
||||
void put_string(const std::string &str);
|
||||
|
||||
char *get_line(char *s, int size); // Same semantics as fgets(), for now
|
||||
|
||||
void seek(int64 offset, int whence);
|
||||
|
||||
int64 tell(void);
|
||||
|
||||
int64 size(void);
|
||||
|
||||
void flush(void);
|
||||
|
||||
void close(void); // Flushes and closes the underlying OS/C lib file. Calling any other method of this class after a call to
|
||||
// this method is illegal(except for the implicit call to the destructor).
|
||||
//
|
||||
// This is necessary since there can be errors when closing a file, and we can't safely throw an
|
||||
// exception from the destructor.
|
||||
//
|
||||
// Manually calling this method isn't strictly necessary, it'll be called from the destructor
|
||||
// automatically, but calling is strongly recommended when the file is opened for writing.
|
||||
private:
|
||||
|
||||
FileWrapper & operator=(const FileWrapper &); // Assignment operator
|
||||
FileWrapper(const FileWrapper &); // Copy constructor
|
||||
|
||||
FILE *fp;
|
||||
const int OpenedMode;
|
||||
};
|
||||
|
||||
#endif
|
@ -29,7 +29,6 @@
|
||||
#include "state.h"
|
||||
#include "video.h"
|
||||
#include "file.h"
|
||||
#include "FileWrapper.h"
|
||||
|
||||
#include "mempatcher.h"
|
||||
#include "md5.h"
|
||||
|
Loading…
Reference in New Issue
Block a user