1 // TODO/WIP
2 
3 /* Mednafen - Multi-system Emulator
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19 
20 #include "mednafen.h"
21 #include "error.h"
22 #include "Stream.h"
23 #include "FileStream.h"
24 
25 #include <stdarg.h>
26 #include <string.h>
27 
FileStream(const char * path,const int mode)28 FileStream::FileStream(const char *path, const int mode)
29 {
30    fp = filestream_open(path, (mode == MODE_WRITE || mode == MODE_WRITE_INPLACE) ? RETRO_VFS_FILE_ACCESS_WRITE : RETRO_VFS_FILE_ACCESS_READ, RETRO_VFS_FILE_ACCESS_HINT_NONE);
31 
32    if (!fp)
33    {
34       ErrnoHolder ene(errno);
35 
36       MDFN_Error(ene.Errno(), "Error opening file:\n%s\n%s", path, ene.StrError());
37    }
38 }
39 
~FileStream()40 FileStream::~FileStream()
41 {
42 }
43 
read(void * data,uint64_t count,bool error_on_eos)44 uint64_t FileStream::read(void *data, uint64_t count, bool error_on_eos)
45 {
46    if (!fp)
47       return 0;
48    return filestream_read(fp, data, count);
49 }
50 
write(const void * data,uint64_t count)51 void FileStream::write(const void *data, uint64_t count)
52 {
53    if (!fp)
54       return;
55    filestream_write(fp, data, count);
56 }
57 
seek(int64_t offset,int whence)58 void FileStream::seek(int64_t offset, int whence)
59 {
60    int seek_position = -1;
61    if (!fp)
62       return;
63    switch (whence)
64    {
65       case SEEK_SET:
66          seek_position = RETRO_VFS_SEEK_POSITION_START;
67          break;
68       case SEEK_CUR:
69          seek_position = RETRO_VFS_SEEK_POSITION_CURRENT;
70          break;
71       case SEEK_END:
72          seek_position = RETRO_VFS_SEEK_POSITION_END;
73          break;
74    }
75    filestream_seek(fp, offset, seek_position);
76 }
77 
tell(void)78 uint64_t FileStream::tell(void)
79 {
80    if (!fp)
81       return -1;
82    return filestream_tell(fp);
83 }
84 
size(void)85 uint64_t FileStream::size(void)
86 {
87    if (!fp)
88       return -1;
89    return filestream_get_size(fp);
90 }
91 
close(void)92 void FileStream::close(void)
93 {
94    if (!fp)
95       return;
96    filestream_close(fp);
97 }
98