aboutsummaryrefslogtreecommitdiff
path: root/engines/titanic/support/simple_file.cpp
blob: 65d2c8527317d7a874ddeac11f3e8bda030f88b1 (plain)
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
/* 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.
 *
 */

#include "common/util.h"
#include "titanic/support/simple_file.h"

namespace Titanic {

CString readStringFromStream(Common::SeekableReadStream *s) {
	CString result;
	char c;
	while ((c = s->readByte()) != '\0')
		result += c;

	return result;
}

/*------------------------------------------------------------------------*/

bool File::open(const Common::String &filename) {
	if (!Common::File::open(filename))
		error("Could not open file - %s", filename.c_str());
	return true;
}

/*------------------------------------------------------------------------*/

SimpleFile::SimpleFile(): _inStream(nullptr), _outStream(nullptr), _lineCount(1) {
}

SimpleFile::~SimpleFile() {
	close();
}

void SimpleFile::open(Common::SeekableReadStream *stream) {
	close();
	_inStream = stream;
}

void SimpleFile::open(Common::OutSaveFile *stream) {
	close();
	_outStream = stream;
}

void SimpleFile::close() {
	if (_outStream) {
		_outStream->finalize();
		delete _outStream;
		_outStream = nullptr;
	}

	if (_inStream) {
		delete _inStream;
		_inStream = nullptr;
	}
}

void SimpleFile::safeRead(void *dst, size_t count) {
	if (unsafeRead(dst, count) != count)
		error("Could not read %d bytes", (int)count);
}

size_t SimpleFile::unsafeRead(void *dst, size_t count) {
	assert(_inStream);
	return _inStream->read(dst, count);
}

size_t SimpleFile::write(const void *src, size_t count) const {
	assert(_outStream);
	return _outStream->write(src, count);
}

void SimpleFile::seek(int offset, int origin) {
	assert(_inStream);
	_inStream->seek(offset, origin);
}

byte SimpleFile::readByte() {
	byte b;
	safeRead(&b, 1);
	return b;
}

uint SimpleFile::readUint16LE() {
	uint val;
	safeRead(&val, 2);
	return READ_LE_UINT16(&val);
}

uint SimpleFile::readUint32LE() {
	uint val;
	safeRead(&val, 4);
	return READ_LE_UINT32(&val);
}

CString SimpleFile::readString() {
	char c;
	CString result;
	bool backslashFlag = false;

	// First skip any spaces
	do {
		safeRead(&c, 1);
	} while (Common::isSpace(c));

	// Ensure we've found a starting quote for the string
	if (c != '"')
		error("Could not find starting quote");

	bool endFlag = false;
	while (!endFlag) {
		// Read the next character
		safeRead(&c, 1);

		if (backslashFlag) {
			backslashFlag = false;
			switch (c) {
			case 'n':
				result += '\n';
				break;
			case 'r':
				result += '\r';
				break;
			case '\t':
				result += '\t';
				break;
			default:
				result += c;
				break;
			}
		} else {
			switch (c) {
			case '"':
				endFlag = true;
				break;
			case '\\':
				backslashFlag = true;
				break;
			default:
				result += c;
				break;
			}
		}
	}

	// Return the string
	return result;
}

int SimpleFile::readNumber() {
	char c;
	int result = 0;
	bool minusFlag = false;

	// First skip any spaces
	do {
		safeRead(&c, 1);
	} while (Common::isSpace(c));

	// Check for prefix sign
	if (c == '+' || c == '-') {
		minusFlag = c == '-';
		safeRead(&c, 1);
	}

	// Read in the number
	if (!Common::isDigit(c))
		error("Invalid number");

	while (Common::isDigit(c)) {
		result = result * 10 + (c - '0');
		safeRead(&c, 1);
	}

	// Finally, if it's a minus value, then negate it
	if (minusFlag)
		result = -result;

	return result;
}

double SimpleFile::readFloat() {
	char c;
	Common::String result;

	// First skip any spaces
	do {
		safeRead(&c, 1);
	} while (Common::isSpace(c));

	// Check for prefix sign
	if (c == '+' || c == '-') {
		result += c;
		safeRead(&c, 1);
	}

	// Read in the number
	if (!Common::isDigit(c))
		error("Invalid number");

	while (Common::isDigit(c) || c == '.') {
		result += c;
		safeRead(&c, 1);
	}

	// Convert to a float and return it
	float floatValue;
	sscanf(result.c_str(), "%f", &floatValue);

	return floatValue;
}

Point SimpleFile::readPoint() {
	Point pt;
	pt.x = readNumber();
	pt.y = readNumber();

	return pt;
}

Rect SimpleFile::readRect() {
	Rect r;
	r.left = readNumber();
	r.top = readNumber();
	r.right = readNumber();
	r.bottom = readNumber();

	return r;
}

Rect SimpleFile::readBounds() {
	Rect r;
	r.left = readNumber();
	r.top = readNumber();
	r.setWidth(readNumber());
	r.setHeight(readNumber());

	return r;
}

void SimpleFile::readBuffer(char *buffer, size_t count) {
	CString tempString = readString();
	if (buffer) {
		strncpy(buffer, tempString.c_str(), count);
		buffer[count - 1] = '\0';
	}
}

void SimpleFile::writeUint16LE(uint val) {
	byte lo = val & 0xff;
	byte hi = (val >> 8) & 0xff;
	write(&lo, 1);
	write(&hi, 1);
}

void SimpleFile::writeUint32LE(uint val) {
	uint16 lo = val & 0xffff;
	uint16 hi = (val >> 16) & 0xff;
	writeUint16LE(lo);
	writeUint16LE(hi);
}

void SimpleFile::writeLine(const CString &str) const {
	write(str.c_str(), str.size());
	write("\r\n", 2);
}

void SimpleFile::writeString(const CString &str) const {
	if (str.empty())
		return;

	const char *msgP = str.c_str();
	char c;

	while ((c = *msgP++) != '\0') {
		switch (c) {
		case '\r':
			write("\\r", 2);
			break;
		case '\n':
			write("\\n", 2);
			break;
		case '\t':
			write("\\t", 2);
			break;
		case '\"':
			write("\\\"", 2);
			break;
		case '\\':
			write("\\\\", 2);
			break;
		case '{':
			write("\\{", 2);
			break;
		case '}':
			write("\\}", 2);
			break;
		default:
			write(&c, 1);
			break;
		}
	}
}

void SimpleFile::writeQuotedString(const CString &str) const {
	write("\"", 1);
	writeString(str);
	write("\" ", 2);
}

void SimpleFile::writeQuotedLine(const CString &str, int indent) const {
	writeIndent(indent);
	writeQuotedString(str);
	write("\n", 1);
}

void SimpleFile::writeNumber(int val) const {
	CString str = CString::format("%d ", val);
	write(str.c_str(), str.size());
}

void SimpleFile::writeNumberLine(int val, int indent) const {
	writeIndent(indent);
	writeNumber(val);
	write("\n", 1);
}

void SimpleFile::writeFloat(double val) const {
	Common::String valStr = Common::String::format("%f ", val);
	write(valStr.c_str(), valStr.size());
}

void SimpleFile::writeFloatLine(double val, int indent) const {
	writeIndent(indent);
	writeFloat(val);
	write("\n", 1);
}

void SimpleFile::writePoint(const Point &pt, int indent) const {
	writeIndent(indent);
	writeNumber(pt.x);
	writeNumber(pt.y);
	write("\n", 1);
}

void SimpleFile::writeRect(const Rect &r, int indent) const {
	writePoint(Point(r.left, r.top), indent);
	writePoint(Point(r.right, r.bottom), indent);
}

void SimpleFile::writeBounds(const Rect &r, int indent) const {
	writePoint(Point(r.left, r.top), indent);
	writePoint(Point(r.width(), r.height()), indent);
}

void SimpleFile::writeFormat(const char *format, ...) const {
	// Convert the format specifier and params to a string
	va_list va;
	va_start(va, format);
	CString line = CString::vformat(format, va);
	va_end(va);

	// Write out the string
	write(format, strlen(format));
}

void SimpleFile::writeIndent(uint indent) const {
	for (uint idx = 0; idx < indent; ++idx)
		write("\t", 1);
}

bool SimpleFile::IsClassStart() {
	char c;

	do {
		safeRead(&c, 1);
	} while (Common::isSpace(c));

	assert(c == '{' || c == '}');
	return c == '{';
}

void SimpleFile::writeClassStart(const CString &classStr, int indent) {
	write("\n", 1);
	writeIndent(indent);
	write("{\n", 2);
	writeIndent(indent + 1);
	writeQuotedString(classStr);
	write("\n", 1);
}

void SimpleFile::writeClassEnd(int indent) {
	writeIndent(indent);
	write("}\n", 2);
}

bool SimpleFile::scanf(const char *format, ...) {
	va_list va;
	va_start(va, format);
	char c;

	CString formatStr(format);
	while (!formatStr.empty()) {
		if (formatStr.hasPrefix(" ")) {
			formatStr.deleteChar(0);

			safeRead(&c, 1);
			if (!Common::isSpace(c))
				return false;

			// Skip over whitespaces
			skipSpaces();
		} else if (formatStr.hasPrefix("%d")) {
			// Read in a number
			formatStr = CString(formatStr.c_str() + 2);
			int *param = (int *)va_arg(va, int *);
			*param = readNumber();

			if (!eos())
				_inStream->seek(-1, SEEK_CUR);
		} else if (formatStr.hasPrefix("%s")) {
			// Read in text until the next space
			formatStr = CString(formatStr.c_str() + 2);
			CString *str = (CString *)va_arg(va, CString *);
			str->clear();
			while (!eos() && !Common::isSpace(c = readByte()))
				*str += c;

			if (!eos())
				_inStream->seek(-1, SEEK_CUR);
		}
	}

	skipSpaces();
	va_end(va);
	return true;
}

void SimpleFile::skipSpaces() {
	char c = ' ';
	while (!eos() && Common::isSpace(c))
		safeRead(&c, 1);

	if (!eos())
		_inStream->seek(-1, SEEK_CUR);
}

/*------------------------------------------------------------------------*/

bool StdCWadFile::open(const Common::String &filename) {
	Common::File f;
	CString name = filename;

	// Check for whether it is indeed a file/resource pair
	int idx = name.indexOf('#');

	if (idx < 0) {
		// Nope, so open up file for standard reading
		assert(!name.empty());
		if (!f.open(name))
			return false;

		SimpleFile::open(f.readStream(f.size()));
		f.close();
		return true;
	}

	// Split up the name and resource, and get the resource index
	CString fname = name.left(idx) + ".st";
	int extPos = name.lastIndexOf('.');
	CString resStr = name.mid(idx + 1, extPos - idx - 1);
	int resIndex = resStr.readInt();

	// Open up the index for access
	if (!f.open(fname))
		return false;
	int indexSize = f.readUint32LE() / 4;
	assert(resIndex < indexSize);

	// Get the specific resource's offset, and size by also
	// getting the offset of the following resource
	f.seek(resIndex * 4);
	uint resOffset = f.readUint32LE();
	uint nextOffset = (resIndex == (indexSize - 1)) ? f.size() :
		f.readUint32LE();

	// Read in the resource
	f.seek(resOffset);
	Common::SeekableReadStream *stream = f.readStream(nextOffset - resOffset);
	SimpleFile::open(stream);

	f.close();
	return true;
}

} // End of namespace Titanic