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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/*
*
* file.cpp
*
* 3rd February 2009: Created file.cpp from parts of util.cpp
*
* Part of the OpenJazz project
*
*
* Copyright (c) 2005-2010 Alister Thomson
*
* OpenJazz is distributed under the terms of
* the GNU General Public License, version 2.0
*
* 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 St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include "file.h"
#include "io/gfx/video.h"
File::File (const char * name, bool write) {
Path *path;
// Try opening the file from all the available directories
path = firstPath;
while (path) {
if (open(path->path, name, write)) return;
path = path->next;
}
log("Could not open file", name);
throw E_FILE;
}
File::~File () {
fclose(f);
LOG("Closed file", filePath);
delete[] filePath;
return;
}
bool File::open (const char * path, const char * name, bool write) {
// Create the file path for the given directory
filePath = createString(path, name);
// Open the file from the path
f = fopen(filePath, write ? "wb": "rb");
if (f) {
LOG("Opened file", filePath);
return true;
}
delete[] filePath;
return false;
}
int File::getSize () {
int pos, size;
pos = ftell(f);
fseek(f, 0, SEEK_END);
size = ftell(f);
fseek(f, pos, SEEK_SET);
return size;
}
int File::tell () {
return ftell(f);
}
void File::seek (int offset, bool reset) {
fseek(f, offset, reset ? SEEK_SET: SEEK_CUR);
return;
}
unsigned char File::loadChar () {
return fgetc(f);
}
void File::storeChar (unsigned char val) {
fputc(val, f);
return;
}
unsigned short int File::loadShort () {
unsigned short int val;
val = fgetc(f);
val += fgetc(f) << 8;
return val;
}
void File::storeShort (unsigned short int val) {
fputc(val & 255, f);
fputc(val >> 8, f);
return;
}
signed long int File::loadInt () {
unsigned long int val;
val = fgetc(f);
val += fgetc(f) << 8;
val += fgetc(f) << 16;
val += fgetc(f) << 24;
return *((signed long int *)&val);
}
void File::storeInt (signed long int val) {
unsigned long int uval;
uval = *((unsigned long int *)&val);
fputc(uval & 255, f);
fputc((uval >> 8) & 255, f);
fputc((uval >> 16) & 255, f);
fputc(uval >> 24, f);
return;
}
unsigned char * File::loadBlock (int length) {
unsigned char *buffer;
buffer = new unsigned char[length];
fread(buffer, 1, length, f);
return buffer;
}
unsigned char * File::loadRLE (int length, bool dontseek) {
unsigned char *buffer;
int rle, pos, byte, count, next;
// Determine the offset that follows the block
next = fgetc(f);
next += fgetc(f) << 8;
next += ftell(f);
buffer = new unsigned char[length];
pos = 0;
while (pos < length) {
rle = fgetc(f);
if (rle > 127) {
byte = fgetc(f);
for (count = 0; count < (rle & 127); count++) {
buffer[pos++] = byte;
if (pos >= length) break;
}
} else if (rle > 0) {
for (count = 0; count < rle; count++) {
buffer[pos++] = fgetc(f);
if (pos >= length) break;
}
} else buffer[pos++] = fgetc(f);
}
if(!dontseek) {
fseek(f, next, SEEK_SET);
}
return buffer;
}
void File::skipRLE () {
int next;
next = fgetc(f);
next += fgetc(f) << 8;
fseek(f, next, SEEK_CUR);
return;
}
char * File::loadString () {
char *string;
int length, count;
length = fgetc(f);
count = 0;
if (length) {
string = new char[length + 1];
for (; count < length; count++) string[count] = fgetc(f);
} else {
// If the length is not given, assume it is an 8.3 file name
string = new char[13];
for (; count < 9; count++) {
string[count] = fgetc(f);
if (string[count] == '.') {
string[++count] = fgetc(f);
string[++count] = fgetc(f);
string[++count] = fgetc(f);
count++;
break;
}
}
}
string[count] = 0;
return string;
}
SDL_Surface * File::loadSurface (int width, int height, bool dontseek) {
return createSurface(loadRLE(width * height, dontseek), width, height);
}
void File::loadPalette (SDL_Color *palette) {
unsigned char *buffer;
int count;
buffer = loadRLE(768);
for (count = 0; count < 256; count++) {
// Palette entries are 6-bit
// Shift them upwards to 8-bit, and fill in the lower 2 bits
palette[count].r = (buffer[count * 3] << 2) + (buffer[count * 3] >> 4);
palette[count].g = (buffer[(count * 3) + 1] << 2) +
(buffer[(count * 3) + 1] >> 4);
palette[count].b = (buffer[(count * 3) + 2] << 2) +
(buffer[(count * 3) + 2] >> 4);
}
delete[] buffer;
return;
}
Path::Path (Path *newNext, char *newPath) {
next = newNext;
path = newPath;
return;
}
Path::~Path () {
if (next) delete next;
delete[] path;
return;
}