Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alexbevi
GitHub Repository: alexbevi/BizHawk
Path: blob/master/psx/mednadisc/FileStream.cpp
2 views
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 "emuware/emuware.h"
19
#include "FileStream.h"
20
21
#include <stdarg.h>
22
#include <sys/types.h>
23
#include <sys/stat.h>
24
#include <fcntl.h>
25
//#include <unistd.h>
26
#include <string.h>
27
#include <stdlib.h>
28
29
//work around gettext
30
#define _(X) X
31
32
#ifdef _MSC_VER
33
#include <sys/stat.h>
34
#include <io.h>
35
// These are not defined in MSVC version of stat.h
36
#define S_IWUSR S_IWRITE
37
#define S_IRUSR S_IREAD
38
#endif
39
40
#ifdef HAVE_MMAP
41
#include <sys/mman.h>
42
#include <sys/types.h>
43
#include <sys/stat.h>
44
#include <fcntl.h>
45
#endif
46
47
// Some really bad preprocessor abuse follows to handle platforms that don't have fseeko and ftello...and of course
48
// for largefile support on Windows:
49
50
#ifndef HAVE_FSEEKO
51
#define fseeko fseek
52
#endif
53
54
#ifndef HAVE_FTELLO
55
#define ftello ftell
56
#endif
57
58
#define STRUCT_STAT struct stat
59
60
#if SIZEOF_OFF_T == 4
61
62
#ifdef HAVE_FOPEN64
63
#define fopen fopen64
64
#endif
65
66
#ifdef HAVE_FTELLO64
67
#undef ftello
68
#define ftello ftello64
69
#endif
70
71
#ifdef HAVE_FSEEKO64
72
#undef fseeko
73
#define fseeko fseeko64
74
#endif
75
76
#ifdef HAVE_FSTAT64
77
#define fstat fstat64
78
#define stat stat64
79
#undef STRUCT_STAT
80
#define STRUCT_STAT struct stat64
81
#endif
82
83
#ifdef HAVE_FTRUNCATE64
84
#define ftruncate ftruncate64
85
#endif
86
#endif
87
88
FileStream::FileStream(const std::string& path, const int mode) : OpenedMode(mode), mapping(NULL), mapping_size(0)
89
{
90
path_save = path;
91
92
if(mode == MODE_READ)
93
fp = fopen(path.c_str(), "rb");
94
else if(mode == MODE_WRITE)
95
fp = fopen(path.c_str(), "wb");
96
else if(mode == MODE_WRITE_SAFE || mode == MODE_WRITE_INPLACE) // SO ANNOYING
97
{
98
int open_flags = O_WRONLY | O_CREAT;
99
100
if(mode == MODE_WRITE_SAFE)
101
open_flags |= O_EXCL;
102
103
#ifdef O_BINARY
104
open_flags |= O_BINARY;
105
#elif defined(_O_BINARY)
106
open_flags |= _O_BINARY;
107
#endif
108
109
#if defined(S_IRGRP) && defined(S_IROTH)
110
int tmpfd = open(path.c_str(), open_flags, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
111
#else
112
int tmpfd = open(path.c_str(), open_flags, S_IRUSR | S_IWUSR);
113
#endif
114
if(tmpfd == -1)
115
{
116
ErrnoHolder ene(errno);
117
118
throw(MDFN_Error(ene.Errno(), _("Error opening file \"%s\": %s"), path_save.c_str(), ene.StrError()));
119
}
120
fp = fdopen(tmpfd, "wb");
121
}
122
else
123
abort();
124
125
if(!fp)
126
{
127
ErrnoHolder ene(errno);
128
129
throw(MDFN_Error(ene.Errno(), _("Error opening file \"%s\": %s"), path_save.c_str(), ene.StrError()));
130
}
131
}
132
133
FileStream::~FileStream()
134
{
135
try
136
{
137
close();
138
}
139
catch(std::exception &e)
140
{
141
printf(e.what());
142
}
143
}
144
145
uint64 FileStream::attributes(void)
146
{
147
uint64 ret = ATTRIBUTE_SEEKABLE;
148
149
switch(OpenedMode)
150
{
151
case MODE_READ:
152
ret |= ATTRIBUTE_READABLE;
153
break;
154
155
case MODE_WRITE_INPLACE:
156
case MODE_WRITE_SAFE:
157
case MODE_WRITE:
158
ret |= ATTRIBUTE_WRITEABLE;
159
break;
160
}
161
162
return ret;
163
}
164
165
uint8 *FileStream::map(void) noexcept
166
{
167
if(!mapping)
168
{
169
#ifdef HAVE_MMAP
170
uint64 length = size();
171
int prot = 0;
172
int flags = 0;
173
void* tptr;
174
175
if(OpenedMode == MODE_READ)
176
{
177
prot |= PROT_READ; // | PROT_EXEC;
178
flags |= MAP_PRIVATE;
179
}
180
else
181
{
182
prot |= PROT_WRITE;
183
prot |= MAP_SHARED;
184
}
185
186
if(length > SIZE_MAX)
187
return(NULL);
188
189
tptr = mmap(NULL, length, prot, flags, fileno(fp), 0);
190
if(tptr != (void*)-1)
191
{
192
mapping = tptr;
193
mapping_size = length;
194
195
#ifdef HAVE_MADVISE
196
// Should probably make this controllable via flag or somesuch.
197
madvise(mapping, mapping_size, MADV_SEQUENTIAL | MADV_WILLNEED);
198
#endif
199
}
200
#endif
201
}
202
203
return((uint8*)mapping);
204
}
205
206
uint64 FileStream::map_size(void) noexcept
207
{
208
return mapping_size;
209
}
210
211
void FileStream::unmap(void) noexcept
212
{
213
if(mapping)
214
{
215
#ifdef HAVE_MMAP
216
munmap(mapping, mapping_size);
217
#endif
218
mapping = NULL;
219
mapping_size = 0;
220
}
221
}
222
223
224
uint64 FileStream::read(void *data, uint64 count, bool error_on_eos)
225
{
226
uint64 read_count;
227
228
clearerr(fp);
229
230
read_count = fread(data, 1, count, fp);
231
232
if(read_count != count)
233
{
234
ErrnoHolder ene(errno);
235
236
if(ferror(fp))
237
throw(MDFN_Error(ene.Errno(), _("Error reading from opened file \"%s\": %s"), path_save.c_str(), ene.StrError()));
238
239
if(error_on_eos)
240
throw(MDFN_Error(0, _("Error reading from opened file \"%s\": %s"), path_save.c_str(), _("Unexpected EOF")));
241
}
242
243
return(read_count);
244
}
245
246
void FileStream::write(const void *data, uint64 count)
247
{
248
if(fwrite(data, 1, count, fp) != count)
249
{
250
ErrnoHolder ene(errno);
251
252
throw(MDFN_Error(ene.Errno(), _("Error writing to opened file \"%s\": %s"), path_save.c_str(), ene.StrError()));
253
}
254
}
255
256
void FileStream::truncate(uint64 length)
257
{
258
//not needed by mednadisc
259
//if(fflush(fp) == EOF || ftruncate(fileno(fp), length) != 0)
260
{
261
ErrnoHolder ene(errno);
262
263
throw(MDFN_Error(ene.Errno(), _("Error truncating opened file \"%s\": %s"), path_save.c_str(), ene.StrError()));
264
}
265
}
266
267
void FileStream::seek(int64 offset, int whence)
268
{
269
if(fseeko(fp, offset, whence) == -1)
270
{
271
ErrnoHolder ene(errno);
272
273
throw(MDFN_Error(ene.Errno(), _("Error seeking in opened file \"%s\": %s"), path_save.c_str(), ene.StrError()));
274
}
275
}
276
277
void FileStream::flush(void)
278
{
279
if(fflush(fp) == EOF)
280
{
281
ErrnoHolder ene(errno);
282
283
throw(MDFN_Error(ene.Errno(), _("Error flushing to opened file \"%s\": %s"), path_save.c_str(), ene.StrError()));
284
}
285
}
286
287
uint64 FileStream::tell(void)
288
{
289
auto offset = ftello(fp);
290
291
if(offset == -1)
292
{
293
ErrnoHolder ene(errno);
294
295
throw(MDFN_Error(ene.Errno(), _("Error getting position in opened file \"%s\": %s"), path_save.c_str(), ene.StrError()));
296
}
297
298
return (std::make_unsigned<decltype(offset)>::type)offset;
299
}
300
301
uint64 FileStream::size(void)
302
{
303
STRUCT_STAT buf;
304
305
if((OpenedMode != MODE_READ && fflush(fp) == EOF) || fstat(fileno(fp), &buf) == -1)
306
{
307
ErrnoHolder ene(errno);
308
309
throw(MDFN_Error(ene.Errno(), _("Error getting the size of opened file \"%s\": %s"), path_save.c_str(), ene.StrError()));
310
}
311
312
return (std::make_unsigned<decltype(buf.st_size)>::type)buf.st_size;
313
}
314
315
void FileStream::close(void)
316
{
317
if(fp)
318
{
319
FILE *tmp = fp;
320
321
unmap();
322
fp = NULL;
323
324
if(fclose(tmp) == EOF)
325
{
326
ErrnoHolder ene(errno);
327
328
throw(MDFN_Error(ene.Errno(), _("Error closing opened file \"%s\": %s"), path_save.c_str(), ene.StrError()));
329
}
330
}
331
}
332
333
int FileStream::get_line(std::string &str)
334
{
335
int c;
336
337
str.clear();
338
339
while((c = get_char()) >= 0)
340
{
341
if(c == '\r' || c == '\n' || c == 0)
342
return(c);
343
344
str.push_back(c);
345
}
346
347
return(str.length() ? 256 : -1);
348
}
349
350
351