2012-10-13 13:31:49 +00:00
|
|
|
// 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
|
|
|
|
*/
|
|
|
|
|
2015-10-01 01:11:33 +00:00
|
|
|
|
2012-10-13 13:31:49 +00:00
|
|
|
#include "mednafen.h"
|
|
|
|
#include "Stream.h"
|
|
|
|
#include "FileStream.h"
|
|
|
|
|
2012-11-16 15:33:10 +00:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <string.h>
|
2012-10-13 13:31:49 +00:00
|
|
|
|
2016-08-14 09:54:31 +00:00
|
|
|
FileStream::FileStream(const char *path, const int mode)
|
2012-10-13 13:31:49 +00:00
|
|
|
{
|
2017-08-07 21:32:46 +00:00
|
|
|
OpenedMode = mode;
|
2017-12-16 02:07:16 +00:00
|
|
|
fp = filestream_open(path, (mode == MODE_WRITE) ? RETRO_VFS_FILE_ACCESS_WRITE : RETRO_VFS_FILE_ACCESS_READ, RETRO_VFS_FILE_ACCESS_HINT_NONE);
|
2015-10-01 01:11:33 +00:00
|
|
|
original_path = strdup(path);
|
2012-10-13 13:31:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FileStream::~FileStream()
|
|
|
|
{
|
2015-10-01 01:11:33 +00:00
|
|
|
if (original_path)
|
|
|
|
free(original_path);
|
|
|
|
original_path = NULL;
|
2012-10-13 13:31:49 +00:00
|
|
|
}
|
|
|
|
|
2015-10-01 01:11:33 +00:00
|
|
|
uint64_t FileStream::attributes(void)
|
2012-10-13 13:31:49 +00:00
|
|
|
{
|
2015-10-01 01:11:33 +00:00
|
|
|
uint64_t ret = ATTRIBUTE_SEEKABLE;
|
2014-04-20 16:49:46 +00:00
|
|
|
|
|
|
|
switch(OpenedMode)
|
|
|
|
{
|
2015-10-01 01:11:33 +00:00
|
|
|
case MODE_READ:
|
2014-04-20 16:49:46 +00:00
|
|
|
ret |= ATTRIBUTE_READABLE;
|
|
|
|
break;
|
2015-10-01 01:11:33 +00:00
|
|
|
case MODE_WRITE_SAFE:
|
|
|
|
case MODE_WRITE:
|
2014-04-20 16:49:46 +00:00
|
|
|
ret |= ATTRIBUTE_WRITEABLE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2012-10-13 13:31:49 +00:00
|
|
|
}
|
|
|
|
|
2015-10-01 01:11:33 +00:00
|
|
|
uint64_t FileStream::read(void *data, uint64_t count, bool error_on_eos)
|
2012-10-13 13:31:49 +00:00
|
|
|
{
|
2015-10-01 01:11:33 +00:00
|
|
|
if (!fp)
|
|
|
|
return 0;
|
2016-03-24 05:33:52 +00:00
|
|
|
return filestream_read(fp, data, count);
|
2012-10-13 13:31:49 +00:00
|
|
|
}
|
|
|
|
|
2015-10-01 01:11:33 +00:00
|
|
|
void FileStream::write(const void *data, uint64_t count)
|
2012-10-13 13:31:49 +00:00
|
|
|
{
|
2015-10-01 01:11:33 +00:00
|
|
|
if (!fp)
|
|
|
|
return;
|
2016-03-24 05:33:52 +00:00
|
|
|
filestream_write(fp, data, count);
|
2012-10-13 13:31:49 +00:00
|
|
|
}
|
|
|
|
|
2015-10-01 01:11:33 +00:00
|
|
|
void FileStream::seek(int64_t offset, int whence)
|
2012-10-13 13:31:49 +00:00
|
|
|
{
|
2015-10-01 01:11:33 +00:00
|
|
|
if (!fp)
|
|
|
|
return;
|
2016-03-24 05:33:52 +00:00
|
|
|
filestream_seek(fp, offset, whence);
|
2012-10-13 13:31:49 +00:00
|
|
|
}
|
|
|
|
|
2015-10-01 01:11:33 +00:00
|
|
|
int64_t FileStream::tell(void)
|
2012-10-13 13:31:49 +00:00
|
|
|
{
|
2015-10-01 01:11:33 +00:00
|
|
|
if (!fp)
|
|
|
|
return -1;
|
2016-03-24 05:33:52 +00:00
|
|
|
return filestream_tell(fp);
|
2012-10-13 13:31:49 +00:00
|
|
|
}
|
|
|
|
|
2015-10-01 01:11:33 +00:00
|
|
|
int64_t FileStream::size(void)
|
2012-10-13 13:31:49 +00:00
|
|
|
{
|
2015-10-01 01:11:33 +00:00
|
|
|
if (!original_path)
|
|
|
|
return -1;
|
2012-11-16 15:33:10 +00:00
|
|
|
|
2015-10-01 01:11:33 +00:00
|
|
|
return path_get_size(original_path);
|
2012-10-13 13:31:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FileStream::close(void)
|
|
|
|
{
|
2015-10-01 01:11:33 +00:00
|
|
|
if (!fp)
|
2014-04-20 16:49:46 +00:00
|
|
|
return;
|
2016-03-24 05:33:52 +00:00
|
|
|
filestream_close(fp);
|
2012-10-13 13:31:49 +00:00
|
|
|
}
|