aboutsummaryrefslogtreecommitdiff
path: root/devtools/create_xeen/file.h
blob: 3d1082571147f0575fb0f126f7ddce08690b411f (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
/* 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.
 *
 */

#ifndef __FILE_H__
#define __FILE_H__

#include <stdio.h>
#include <stdlib.h>

#define FORBIDDEN_SYMBOL_ALLOW_ALL

#include "common/scummsys.h"
#include "common/endian.h"
#include "common/util.h"

namespace Common {

enum AccessMode {
	kFileReadMode = 1,
	kFileWriteMode = 2
};

class Stream {
public:
	Stream() {}
	virtual ~Stream() {}

	virtual int seek(int offset, int whence = SEEK_SET) = 0;
	virtual long read(void *buffer, size_t len) = 0;
	virtual void write(const void *buffer, size_t len) = 0;
	virtual uint pos() const = 0;
	virtual uint size() const = 0;
	virtual bool eof() const = 0;

	void skip(int offset) {
		seek(offset, SEEK_CUR);
	}
	void write(Stream &src, size_t len) {
		for (size_t idx = 0; idx < len; ++idx)
			writeByte(src.readByte());
	}
	byte readByte() {
		byte v;
		read(&v, sizeof(byte));
		return v;
	}
	uint16 readWord() {
		uint16 v;
		read(&v, sizeof(uint16));
		return FROM_LE_16(v);
	}
	uint readLong() {
		uint v;
		read(&v, sizeof(uint));
		return FROM_LE_32(v);
	}

	uint readUint16BE() {
		uint16 v;
		read(&v, sizeof(uint16));
		return FROM_BE_16(v);
	}
	uint readUint16LE() {
		uint16 v;
		read(&v, sizeof(uint16));
		return FROM_LE_16(v);
	}
	uint readUint32BE() {
		uint32 v;
		read(&v, sizeof(uint32));
		return FROM_BE_32(v);
	}
	uint readUint32LE() {
		uint32 v;
		read(&v, sizeof(uint32));
		return FROM_LE_32(v);
	}

	void writeByte(byte v) {
		write(&v, sizeof(byte));
	}
	void writeShort(int8 v) {
		write(&v, sizeof(int8));
	}
	void writeByte(byte v, int len) {
		byte *b = new byte[len];
		memset(b, v, len);
		write(b, len);
		delete[] b;
	}
	void writeWord(uint16 v) {
		uint16 vTemp = TO_LE_16(v);
		write(&vTemp, sizeof(uint16));
	}
	void writeLong(uint v) {
		uint vTemp = TO_LE_32(v);
		write(&vTemp, sizeof(uint));
	}
	void writeString(const char *msg) {
		if (!msg) {
			writeByte(0);
		} else {
			do {
				writeByte(*msg);
			} while (*msg++);
		}
	}
};

class File : public Stream {
private:
	::FILE *_f;
public:
	File() : _f(nullptr) {}
	virtual ~File() { close(); }

	bool open(const char *filename, AccessMode mode = kFileReadMode) {
		_f = fopen(filename, (mode == kFileReadMode) ? "rb" : "wb+");
		return (_f != NULL);
	}
	void close() {
		if (_f)
			fclose(_f);
		_f = nullptr;
	}

	virtual int seek(int offset, int whence = SEEK_SET) {
		return fseek(_f, offset, whence);
	}
	virtual long read(void *buffer, size_t len) {
		return fread(buffer, 1, len, _f);
	}
	virtual void write(const void *buffer, size_t len) {
		assert(_f);
		fwrite(buffer, 1, len, _f);
	}
	virtual uint pos() const {
		return ftell(_f);
	}
	virtual uint size() const {
		uint currentPos = pos();
		fseek(_f, 0, SEEK_END);
		uint result = pos();
		fseek(_f, currentPos, SEEK_SET);
		return result;
	}
	virtual bool eof() const {
		return feof(_f) != 0;
	}
};

#define MAX_MEM_SIZE 65536

class MemFile : public Stream {
private:
	byte _data[MAX_MEM_SIZE];
	size_t _size, _offset;
public:
	MemFile() : _size(0), _offset(0) {
		memset(_data, 0, MAX_MEM_SIZE);
	}
	MemFile(const byte *data, size_t size) : _size(size), _offset(0) {
		memcpy(_data, data, size);
	}
	virtual ~MemFile() {}

	bool open() {
		memset(_data, 0, MAX_MEM_SIZE);
		_size = _offset = 0;
		return true;
	}
	void close() {
	}

	virtual int seek(int offset, int whence = SEEK_SET) {
		switch (whence) {
		case SEEK_SET: _offset = whence; break;
		case SEEK_CUR: _offset += whence; break;
		case SEEK_END: _offset = _size + whence; break;
		}
		
		return _offset;
	}
	virtual long read(void *buffer, size_t len) {
		len = MAX(len, _size - _offset);
		memcpy(buffer, &_data[_offset], len);
		return len;
	}
	virtual void write(const void *buffer, size_t len) {
		assert(len <= (MAX_MEM_SIZE - _offset));
		memcpy(&_data[_offset], buffer, len);
		_offset += len;
		_size = MAX(_offset, _size);
	}
	virtual uint pos() const {
		return _offset;
	}
	virtual uint size() const {
		return _size;
	}
	virtual bool eof() const {
		return _offset >= _size;
	}

	const byte *getData() const { return _data; }

	void syncString(const char *str) {
		write(str, strlen(str) + 1);
	}
	void syncStrings(const char *const *str, int count) {
		writeLong(MKTAG(count, 0, 0, 0));
		for (int idx = 0; idx < count; ++idx, ++str)
			writeString(*str);
	}
	void syncStrings2D(const char *const *str, int count1, int count2) {
		writeLong(MKTAG(count1, count2, 0, 0));
		for (int idx = 0; idx < count1 * count2; ++idx, ++str)
			writeString(*str);
	}
	void syncNumber(const int val) {
		writeLong(val);
	}
	void syncNumbers(const int *vals, int count) {
		writeLong(MKTAG(count, 0, 0, 0));
		for (int idx = 0; idx < count; ++idx, ++vals)
			writeLong(*vals);
	}
	void syncNumbers2D(const int *vals, int count1, int count2) {
		writeLong(MKTAG(count1, count2, 0, 0));
		for (int idx = 0; idx < count1 * count2; ++idx, ++vals)
			writeLong(*vals);
	}
	void syncNumbers3D(const int *vals, int count1, int count2, int count3) {
		writeLong(MKTAG(count1, count2, count3, 0));
		for (int idx = 0; idx < count1 * count2 * count3; ++idx, ++vals)
			writeLong(*vals);
	}
	void syncNumbers4D(const int *vals, int count1, int count2, int count3, int count4) {
		writeLong(MKTAG(count1, count2, count3, count4));
		for (int idx = 0; idx < count1 * count2 * count3 * count4; ++idx, ++vals)
			writeLong(*vals);
	}
	void syncBytes2D(const byte *vals, int count1, int count2) {
		writeLong(MKTAG(count1, count2, 0, 0));
		write(vals, count1 * count2);
	}
};

} // End of namespace Common

#endif