snes9x/stream.h

136 lines
3.5 KiB
C
Raw Permalink Normal View History

/*****************************************************************************\
Snes9x - Portable Super Nintendo Entertainment System (TM) emulator.
This file is licensed under the Snes9x License.
For further information, consult the LICENSE file in the root directory.
\*****************************************************************************/
2010-09-25 17:46:12 +02:00
#ifndef _STREAM_H_
#define _STREAM_H_
2010-09-25 17:46:12 +02:00
#include <string>
class Stream
2010-09-25 17:46:12 +02:00
{
public:
Stream (void);
virtual ~Stream (void);
2010-09-25 17:46:12 +02:00
virtual int get_char (void) = 0;
virtual char * gets (char *, size_t) = 0;
virtual char * getline (void); // free() when done
virtual std::string getline (bool &);
virtual size_t read (void *, size_t) = 0;
virtual size_t write (void *, size_t) = 0;
virtual size_t pos (void) = 0;
virtual size_t size (void) = 0;
virtual int revert (uint8 origin, int32 offset) = 0;
virtual void closeStream() = 0;
protected:
size_t pos_from_origin_offset(uint8 origin, int32 offset);
2010-09-25 17:46:12 +02:00
};
class fStream : public Stream
2010-09-25 17:46:12 +02:00
{
public:
fStream (FSTREAM);
virtual ~fStream (void);
2010-09-25 17:46:12 +02:00
virtual int get_char (void);
virtual char * gets (char *, size_t);
virtual size_t read (void *, size_t);
virtual size_t write (void *, size_t);
virtual size_t pos (void);
virtual size_t size (void);
virtual int revert (uint8 origin, int32 offset);
virtual void closeStream();
2010-09-25 17:46:12 +02:00
private:
FSTREAM fp;
2010-09-25 17:46:12 +02:00
};
#ifdef UNZIP_SUPPORT
# ifdef SYSTEM_ZIP
# include <minizip/unzip.h>
# else
# include "unzip.h"
# endif
2010-09-25 17:46:12 +02:00
#define unz_BUFFSIZ 1024
class unzStream : public Stream
2010-09-25 17:46:12 +02:00
{
public:
unzStream (unzFile &);
virtual ~unzStream (void);
2010-09-25 17:46:12 +02:00
virtual int get_char (void);
virtual char * gets (char *, size_t);
virtual size_t read (void *, size_t);
virtual size_t write (void *, size_t);
virtual size_t pos (void);
virtual size_t size (void);
virtual int revert (uint8 origin, int32 offset);
virtual void closeStream();
2010-09-25 17:46:12 +02:00
private:
2016-12-03 17:09:52 +01:00
void fill_buffer();
size_t buffer_remaining();
2010-09-25 17:46:12 +02:00
unzFile file;
char buffer[unz_BUFFSIZ];
2016-12-03 17:09:52 +01:00
size_t pos_in_buf;
size_t buf_pos_in_unzipped;
size_t bytes_in_buf;
unz_file_pos unz_file_start_pos;
2010-09-25 17:46:12 +02:00
};
#endif
class memStream : public Stream
{
public:
memStream (uint8 *,size_t);
memStream (const uint8 *,size_t);
virtual ~memStream (void);
virtual int get_char (void);
virtual char * gets (char *, size_t);
virtual size_t read (void *, size_t);
virtual size_t write (void *, size_t);
virtual size_t pos (void);
virtual size_t size (void);
virtual int revert (uint8 origin, int32 offset);
virtual void closeStream();
private:
uint8 *mem;
size_t msize;
size_t remaining;
uint8 *head;
bool readonly;
};
/* dummy stream that always reads 0 and writes nowhere
but counts bytes written
*/
class nulStream : public Stream
{
public:
nulStream (void);
virtual ~nulStream (void);
virtual int get_char (void);
virtual char * gets (char *, size_t);
virtual size_t read (void *, size_t);
virtual size_t write (void *, size_t);
virtual size_t pos (void);
virtual size_t size (void);
virtual int revert (uint8 origin, int32 offset);
virtual void closeStream();
private:
size_t bytes_written;
};
Stream *openStreamFromFSTREAM(const char* filename, const char* mode);
Stream *reopenStreamFromFd(int fd, const char* mode);
2010-09-25 17:46:12 +02:00
#endif