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 <sys/stat.h>
21 #include "mednafen.h"
22 #include "Stream.h"
23 #include "FileStream.h"
24
25 #include <stdarg.h>
26 #include <string.h>
27
28 #ifdef _WIN32
29 #include <io.h>
30 #else
31 #include <unistd.h>
32 #endif
33
34 #define fseeko fseek
35 #define ftello ftell
36
FileStream(const char * path,const int mode)37 FileStream::FileStream(const char *path, const int mode)
38 {
39 if(mode == FileStream::MODE_WRITE)
40 fp = fopen(path, "wb");
41 else
42 fp = fopen(path, "rb");
43
44 if(!fp)
45 {
46 ErrnoHolder ene(errno);
47
48 throw(MDFN_Error(ene.Errno(), _("Error opening file %s"), ene.StrError()));
49 }
50 }
51
~FileStream()52 FileStream::~FileStream()
53 {
54 close();
55 }
56
read(void * data,uint64 count,bool error_on_eos)57 uint64 FileStream::read(void *data, uint64 count, bool error_on_eos)
58 {
59 return fread(data, 1, count, fp);
60 }
61
write(const void * data,uint64 count)62 void FileStream::write(const void *data, uint64 count)
63 {
64 fwrite(data, 1, count, fp);
65 }
66
seek(int64 offset,int whence)67 void FileStream::seek(int64 offset, int whence)
68 {
69 fseeko(fp, offset, whence);
70 }
71
tell(void)72 int64 FileStream::tell(void)
73 {
74 return ftello(fp);
75 }
76
size(void)77 int64 FileStream::size(void)
78 {
79 struct stat buf;
80
81 fstat(fileno(fp), &buf);
82
83 return(buf.st_size);
84 }
85
close(void)86 void FileStream::close(void)
87 {
88 if(!fp)
89 return;
90
91 FILE *tmp = fp;
92 fp = NULL;
93 fclose(tmp);
94 }
95