Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alexbevi
GitHub Repository: alexbevi/BizHawk
Path: blob/master/psx/octoshock/FileStream.cpp
2 views
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 <trio/trio.h>
26
#include <stdarg.h>
27
#include <string.h>
28
29
#ifdef _WIN32
30
#include <io.h>
31
#else
32
#include <unistd.h>
33
#endif
34
35
#define fseeko fseek
36
#define ftello ftell
37
38
FileStream::FileStream(const char *path, const int mode): OpenedMode(mode)
39
{
40
if(!(fp = fopen(path, (mode == FileStream::MODE_WRITE) ? "wb" : "rb")))
41
{
42
ErrnoHolder ene(errno);
43
44
throw(MDFN_Error(ene.Errno(), _("Error opening file %s"), ene.StrError()));
45
}
46
}
47
48
FileStream::~FileStream()
49
{
50
}
51
52
uint64 FileStream::attributes(void)
53
{
54
uint64 ret = ATTRIBUTE_SEEKABLE;
55
56
switch(OpenedMode)
57
{
58
case FileStream::MODE_READ:
59
ret |= ATTRIBUTE_READABLE;
60
break;
61
case FileStream::MODE_WRITE_SAFE:
62
case FileStream::MODE_WRITE:
63
ret |= ATTRIBUTE_WRITEABLE;
64
break;
65
}
66
67
return ret;
68
}
69
70
uint64 FileStream::read(void *data, uint64 count, bool error_on_eos)
71
{
72
return fread(data, 1, count, fp);
73
}
74
75
void FileStream::write(const void *data, uint64 count)
76
{
77
fwrite(data, 1, count, fp);
78
}
79
80
void FileStream::seek(int64 offset, int whence)
81
{
82
fseeko(fp, offset, whence);
83
}
84
85
int64 FileStream::tell(void)
86
{
87
return ftello(fp);
88
}
89
90
int64 FileStream::size(void) {
91
struct stat buf;
92
93
fstat(fileno(fp), &buf);
94
95
return(buf.st_size);
96
}
97
98
void FileStream::close(void)
99
{
100
if(!fp)
101
return;
102
103
FILE *tmp = fp;
104
fp = NULL;
105
fclose(tmp);
106
}
107
108