1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* $URL$
* $Id$
*
*/
#include "backends/platform/ps2/rawsavefile.h"
#include "backends/platform/ps2/savefilemgr.h"
RawReadFile::RawReadFile(McAccess *mcAccess) {
_mcAccess = mcAccess;
_size = -1;
_pos = 0;
_buf = NULL;
_eof = false;
}
RawReadFile::~RawReadFile(void) {
if (_buf)
free(_buf);
}
bool RawReadFile::bufOpen(const char *path) {
int handle = _mcAccess->open(path, O_RDONLY);
printf("RawReadFile Open: \"%s\" => %d\n", path, handle);
if (handle >= 0) {
_size = _mcAccess->size(handle);
_buf = (uint8 *)memalign(64, _size);
int res = _mcAccess->read(handle, _buf, _size);
if (res != _size) {
free(_buf);
_size = -1;
_buf = NULL;
}
_mcAccess->close(handle);
} else {
_size = -1;
_buf = NULL;
}
_pos = 0;
return (_buf != NULL);
}
int RawReadFile::bufTell(void) const {
return _pos;
}
int RawReadFile::bufSeek(int ofs, int whence) {
switch (whence) {
case SEEK_SET:
_pos = ofs;
break;
case SEEK_CUR:
_pos += ofs;
break;
case SEEK_END:
_pos = _size + ofs;
break;
}
if (_pos < 0)
_pos = 0;
else if (_pos > _size)
_pos = _size;
_eof = false;
return _pos;
}
int RawReadFile::bufRead(void *dest, int size) {
if (_pos + size > _size) {
size = _size - _pos;
_eof = true;
}
memcpy(dest, _buf + _pos, size);
_pos += size;
return size;
}
int RawReadFile::bufSize(void) const {
return _size;
}
bool RawReadFile::bufEof(void) const {
return _eof;
}
void RawReadFile::bufClearErr(void) const {
_eof = false;
}
RawWriteFile::RawWriteFile(McAccess *mcAccess) {
_mcAccess = mcAccess;
_size = 64 * 1024;
_buf = (uint8 *)memalign(64, _size);
_pos = 0;
_handle = -1;
}
RawWriteFile::~RawWriteFile() {
if (_pos != 0) {
printf("RawWriteFile d'tor: file wasn't flushed!\n");
bufFlush();
}
free(_buf);
if (_handle >= 0)
_mcAccess->close(_handle);
}
bool RawWriteFile::bufOpen(const char *path) {
_handle = _mcAccess->open(path, O_WRONLY | O_CREAT);
strcpy(_filename, path);
return (_handle >= 0);
}
void RawWriteFile::bufWrite(const void *buf, int len) {
while (_pos + len > _size) {
_size = _size * 2;
_buf = (uint8 *)realloc(_buf, _size);
}
memcpy(_buf + _pos, buf, len);
_pos += len;
}
bool RawWriteFile::bufFlush(void) {
int result = _mcAccess->write(_handle, _buf, _pos);
if (_pos != result) {
if (result > 0) {
memmove(_buf, _buf + result, _pos - result);
_pos -= result;
}
return false;
}
_pos = 0;
return true;
}
int RawWriteFile::bufTell(void) const {
return _pos;
}
void RawWriteFile::removeFile(void) {
if (_handle >= 0)
_mcAccess->close(_handle);
_handle = -1;
_pos = 0;
_mcAccess->remove(_filename);
}
|