aboutsummaryrefslogtreecommitdiff
path: root/engines/gob/variables.cpp
blob: 805aaeb839e45e0f7a607788a74464b053226b63 (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
/* 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 "common/endian.h"

#include "gob/gob.h"
#include "gob/variables.h"

namespace Gob {

Variables::Variables(uint32 size) {
	_size = size;

	_vars = new byte[_size];
	_sizes = new byte[_size];

	clear();
}

Variables::~Variables() {
	delete[] _vars;
	delete[] _sizes;
}

void Variables::clear() {
	memset(_vars, 0, _size);

	// All variables are 32 bit wide per default
	memset(_sizes, 0, _size);
	for (uint32 i = 0; i < _size; i += 4)
		_sizes[i] = kSize32;
}

void Variables::clearSize(uint32 offset) {
	uint32 inVar = offset % 4;
	uint32 varOff = (offset >> 2) << 2;

	// Clearing out the old size
	for (uint32 i = 0; i < 4; i++) {
		if (_sizes[varOff + i] == kSize32)
			_sizes[varOff + i] = kSize8;
		else if ((inVar == (i + 1)) && (_sizes[varOff + i] == kSize16))
			_sizes[varOff + i] = kSize8;
	}
}

void Variables::writeSize(uint32 offset, byte n) {
	clearSize(offset);

	_sizes[offset] = n;
	// Setting following bytes of size to 8 bit, for easy clearing out afterwards
	for (; n > 0; n--)
		_sizes[offset + n] = kSize8;
}

void Variables::writeSizeString(uint32 offset, uint32 length) {
	clearSize(offset);

	memset(_sizes + offset, kSize8, length);
}

void Variables::writeVar8(uint32 var, uint8 value) {
	writeOff8(var * 4, value);
}

void Variables::writeVar16(uint32 var, uint16 value) {
	writeOff16(var * 4, value);
}

void Variables::writeVar32(uint32 var, uint32 value) {
	writeOff32(var * 4, value);
}

void Variables::writeVarString(uint32 var, const char *value) {
	writeOffString(var * 4, value);
}

void Variables::writeOff8(uint32 offset, uint8 value) {
	write8(_vars + offset, value);
	writeSize(offset, kSize8);
}

void Variables::writeOff16(uint32 offset, uint16 value) {
	write16(_vars + offset, value);
	writeSize(offset, kSize16);
}

void Variables::writeOff32(uint32 offset, uint32 value) {
	write32(_vars + offset, value);
	writeSize(offset, kSize32);
}

void Variables::writeOffString(uint32 offset, const char *value) {
	strcpy((char *) (_vars + offset), value);
	writeSizeString(offset, strlen(value));
}

uint8 Variables::readVar8(uint32 var) const {
	return readOff8(var * 4);
}

uint16 Variables::readVar16(uint32 var) const {
	return readOff16(var * 4);
}

uint32 Variables::readVar32(uint32 var) const {
	return readOff32(var * 4);
}

void Variables::readVarString(uint32 var, char *value, uint32 length) {
	readOffString(var * 4, value, length);
}

uint8 Variables::readOff8(uint32 offset) const {
	return read8(_vars + offset);
}

uint16 Variables::readOff16(uint32 offset) const {
	return read16(_vars + offset);
}

uint32 Variables::readOff32(uint32 offset) const {
	return read32(_vars + offset);
}

void Variables::readOffString(uint32 offset, char *value, uint32 length) {
	strncpy0(value, (const char *) (_vars + offset), length - 1);
}

const uint8 *Variables::getAddressVar8(uint32 var) const {
	return getAddressOff8(var * 4);
}

uint8 *Variables::getAddressVar8(uint32 var, uint32 n) {
	return getAddressOff8(var * 4, n);
}

const uint16 *Variables::getAddressVar16(uint32 var) const {
	return getAddressOff16(var * 4);
}

uint16 *Variables::getAddressVar16(uint32 var, uint32 n) {
	return getAddressOff16(var * 4, n);
}

const uint32 *Variables::getAddressVar32(uint32 var) const {
	return getAddressOff32(var * 4);
}

uint32 *Variables::getAddressVar32(uint32 var, uint32 n) {
	return getAddressOff32(var * 4, n);
}

const char *Variables::getAddressVarString(uint32 var) const {
	return getAddressOffString(var * 4);
}

char *Variables::getAddressVarString(uint32 var, uint32 n) {
	return getAddressOffString(var * 4, n);
}

const uint8 *Variables::getAddressOff8(uint32 offset) const {
	return ((const uint8 *) (_vars + offset));
}

uint8 *Variables::getAddressOff8(uint32 offset, uint32 n) {
	for (uint32 i = 0; i < n; i++)
		writeSize(offset + i, kSize8);

	return ((uint8 *) (_vars + offset));
}

const uint16 *Variables::getAddressOff16(uint32 offset) const {
	return ((const uint16 *) (_vars + offset));
}

uint16 *Variables::getAddressOff16(uint32 offset, uint32 n) {
	for (uint32 i = 0; i < n; i++)
		writeSize(offset + i * 2, kSize16);

	return ((uint16 *) (_vars + offset));
}

const uint32 *Variables::getAddressOff32(uint32 offset) const {
	return ((const uint32 *) (_vars + offset));
}

uint32 *Variables::getAddressOff32(uint32 offset, uint32 n) {
	for (uint32 i = 0; i < n; i++)
		writeSize(offset + i * 4, kSize32);

	return ((uint32 *) (_vars + offset));
}

const char *Variables::getAddressOffString(uint32 offset) const {
	return ((const char *) (_vars + offset));
}

char *Variables::getAddressOffString(uint32 offset, uint32 n) {
	writeSizeString(offset, (n == 0xFFFFFFFF) ? strlen((char *) (_vars + offset)) : n);

	return ((char *) (_vars + offset));
}

bool Variables::copyTo(uint32 offset, byte *variables, byte *sizes, uint32 n) const {
	if ((offset + n) > _size)
		return false;

	if (variables)
		memcpy(variables, _vars + offset, n);
	if (sizes)
		memcpy(sizes, _sizes + offset, n);

	return true;
}

bool Variables::copyFrom(uint32 offset, const byte *variables, const byte *sizes, uint32 n) {
	if (((offset + n) > _size) || !variables || !sizes)
		return false;

	memcpy(_vars + offset, variables, n);
	memcpy(_sizes + offset, sizes, n);

	return true;
}


VariablesLE::VariablesLE(uint32 size) : Variables(size) {
}

VariablesLE::~VariablesLE() {
}

void VariablesLE::write8(byte *buf, uint8 data) const {
	*buf = (byte) data;
}

void VariablesLE::write16(byte *buf, uint16 data) const {
	WRITE_LE_UINT16(buf, data);
}

void VariablesLE::write32(byte *buf, uint32 data) const {
	WRITE_LE_UINT32(buf, data);
}

uint8 VariablesLE::read8(const byte *buf) const {
	return (uint8) *buf;
}

uint16 VariablesLE::read16(const byte *buf) const {
	return READ_LE_UINT16(buf);
}

uint32 VariablesLE::read32(const byte *buf) const {
	return READ_LE_UINT32(buf);
}


VariablesBE::VariablesBE(uint32 size) : Variables(size) {
}

VariablesBE::~VariablesBE() {
}

void VariablesBE::write8(byte *buf, uint8 data) const {
	*buf = (byte) data;
}

void VariablesBE::write16(byte *buf, uint16 data) const {
	WRITE_BE_UINT16(buf, data);
}

void VariablesBE::write32(byte *buf, uint32 data) const {
	WRITE_BE_UINT32(buf, data);
}

uint8 VariablesBE::read8(const byte *buf) const {
	return (uint8) *buf;
}

uint16 VariablesBE::read16(const byte *buf) const {
	return READ_BE_UINT16(buf);
}

uint32 VariablesBE::read32(const byte *buf) const {
	return READ_BE_UINT32(buf);
}

VariableReference::VariableReference() {
	_vars = 0;
	_offset = 0;
}

VariableReference::VariableReference(Variables &vars, uint32 offset, Variables::Type type) {
	set(vars, offset, type);
}

VariableReference::~VariableReference() {
}

void VariableReference::set(Variables &vars, uint32 offset, Variables::Type type) {
	_vars = &vars;
	_offset = offset;
	_type = type;
}

VariableReference &VariableReference::operator=(uint32 value) {
	if (_vars) {
		switch (_type) {
			case Variables::kVariableType8:
				_vars->writeOff8(_offset, (uint8) value);
				break;
			case Variables::kVariableType16:
				_vars->writeOff16(_offset, (uint16) value);
				break;
			case Variables::kVariableType32:
				_vars->writeOff32(_offset, value);
				break;
		}
	}
	return *this;
}

VariableReference::operator uint32() {
	if (_vars) {
		switch (_type) {
			case Variables::kVariableType8:
				return (uint32) _vars->readOff8(_offset);
			case Variables::kVariableType16:
				return (uint32) _vars->readOff16(_offset);
			case Variables::kVariableType32:
				return _vars->readOff32(_offset);
		}
	}

	return 0;
}

VariableReference &VariableReference::operator+=(uint32 value) {
	return (*this = (*this + value));
}

VariableReference &VariableReference::operator*=(uint32 value) {
	return (*this = (*this * value));
}

} // End of namespace Gob