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 <sys/stat.h>
19 #include "mednafen.h"
20 #include "FileWrapper.h"
21
22 #include <stdarg.h>
23 #include <string.h>
24
25 #ifdef _WIN32
26 #include <io.h>
27 #else
28 #include <unistd.h>
29 #endif
30
31 // Some really bad preprocessor abuse follows to handle platforms that don't have fseeko and ftello...and of course
32 // for largefile support on Windows:
33
34 #define fseeko fseek
35 #define ftello ftell
36
37 // For special uses, IE in classes that take a path or a FileWrapper & in the constructor, and the FileWrapper non-pointer member
38 // is in the initialization list for the path constructor but not the constructor with FileWrapper&
39
FileWrapper(const char * path,const int mode,const char * purpose)40 FileWrapper::FileWrapper(const char *path, const int mode, const char *purpose) : OpenedMode(mode)
41 {
42 if(mode == MODE_WRITE)
43 fp = fopen(path, "wb");
44 else
45 fp = fopen(path, "rb");
46
47 if(!fp)
48 {
49 ErrnoHolder ene(errno);
50
51 throw(MDFN_Error(ene.Errno(), _("Error opening file %s"), ene.StrError()));
52 }
53 }
54
~FileWrapper()55 FileWrapper::~FileWrapper()
56 {
57 close();
58 }
59
close(void)60 void FileWrapper::close(void)
61 {
62 if(!fp)
63 return;
64
65 FILE *tmp = fp;
66 fp = NULL;
67 fclose(tmp);
68 }
69
read(void * data,uint64 count,bool error_on_eof)70 uint64 FileWrapper::read(void *data, uint64 count, bool error_on_eof)
71 {
72 return fread(data, 1, count, fp);
73 }
74
flush(void)75 void FileWrapper::flush(void)
76 {
77 fflush(fp);
78 }
79
write(const void * data,uint64 count)80 void FileWrapper::write(const void *data, uint64 count)
81 {
82 fwrite(data, 1, count, fp);
83 }
84
put_char(int c)85 void FileWrapper::put_char(int c)
86 {
87 fputc(c, fp);
88 }
89
put_string(const char * str)90 void FileWrapper::put_string(const char *str)
91 {
92 write(str, strlen(str));
93 }
94
95 // We need to decide whether to prohibit NULL characters in output and input strings via std::string.
96 // Yes for correctness, no for potential security issues(though unlikely in context all things considered).
put_string(const std::string & str)97 void FileWrapper::put_string(const std::string &str)
98 {
99 write(str.data(), str.size());
100 }
101
get_line(char * buf_s,int buf_size)102 char *FileWrapper::get_line(char *buf_s, int buf_size)
103 {
104 return ::fgets(buf_s, buf_size, fp);
105 }
106
107
seek(int64 offset,int whence)108 void FileWrapper::seek(int64 offset, int whence)
109 {
110 fseeko(fp, offset, whence);
111 }
112
size(void)113 int64 FileWrapper::size(void)
114 {
115 struct stat buf;
116
117 fstat(fileno(fp), &buf);
118
119 return(buf.st_size);
120 }
121
tell(void)122 int64 FileWrapper::tell(void)
123 {
124 return ftello(fp);
125 }
126