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