1 /* Mednafen - Multi-system Emulator
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 */
17
18 #include <stdarg.h>
19 #include <string.h>
20
21 #include <sys/stat.h>
22
23 #ifdef _WIN32
24 #include <io.h>
25 #else
26 #include <unistd.h>
27 #endif
28
29 #include "mednafen.h"
30 #include "FileWrapper.h"
31 #include "Stream.h"
32
33 // For special uses, IE in classes that take a path or a FileWrapper & in the constructor, and the FileWrapper non-pointer member
34 // is in the initialization list for the path constructor but not the constructor with FileWrapper&
35
FileWrapper(const char * path,const int mode,const char * purpose)36 FileWrapper::FileWrapper(const char *path, const int mode, const char *purpose) : OpenedMode(mode)
37 {
38 if(mode == MODE_WRITE)
39 fp = filestream_open(path, RETRO_VFS_FILE_ACCESS_READ_WRITE, RETRO_VFS_FILE_ACCESS_HINT_NONE);
40 else
41 fp = filestream_open(path, RETRO_VFS_FILE_ACCESS_READ, RETRO_VFS_FILE_ACCESS_HINT_NONE);
42
43 if(!fp)
44 {
45 ErrnoHolder ene(errno);
46
47 throw(MDFN_Error(ene.Errno(), _("Error opening file %s"), ene.StrError()));
48 }
49 }
50
~FileWrapper()51 FileWrapper::~FileWrapper()
52 {
53 close();
54 }
55
close(void)56 void FileWrapper::close(void)
57 {
58 if(!fp)
59 return;
60
61 RFILE *tmp = fp;
62 fp = NULL;
63 filestream_close(tmp);
64 }
65
read(void * data,uint64 count,bool error_on_eof)66 uint64 FileWrapper::read(void *data, uint64 count, bool error_on_eof)
67 {
68 return filestream_read(fp, data, count);
69 }
70
flush(void)71 void FileWrapper::flush(void)
72 {
73 filestream_flush(fp);
74 }
75
write(const void * data,uint64 count)76 void FileWrapper::write(const void *data, uint64 count)
77 {
78 filestream_write(fp, data, count);
79 }
80
put_char(int c)81 void FileWrapper::put_char(int c)
82 {
83 filestream_putc(fp, c);
84 }
85
put_string(const char * str)86 void FileWrapper::put_string(const char *str)
87 {
88 write(str, strlen(str));
89 }
90
91 // We need to decide whether to prohibit NULL characters in output and input strings via std::string.
92 // Yes for correctness, no for potential security issues(though unlikely in context all things considered).
put_string(const std::string & str)93 void FileWrapper::put_string(const std::string &str)
94 {
95 write(str.data(), str.size());
96 }
97
get_line(char * buf_s,int buf_size)98 char *FileWrapper::get_line(char *buf_s, int buf_size)
99 {
100 return filestream_gets(fp, buf_s, buf_size);
101 }
102
103
seek(int64 offset,int whence)104 void FileWrapper::seek(int64 offset, int whence)
105 {
106 filestream_seek(fp, offset, whence);
107 }
108
size(void)109 uint64_t FileWrapper::size(void)
110 {
111 ssize_t curPos = filestream_tell(fp);
112 filestream_seek(fp, 0, SEEK_END);
113 ssize_t output = filestream_tell(fp);
114 filestream_seek(fp, curPos, SEEK_SET);
115 return(output);
116 }
117
tell(void)118 uint64_t FileWrapper::tell(void)
119 {
120 return filestream_tell(fp);
121 }
122