aboutsummaryrefslogtreecommitdiff
path: root/engines/gob/dbase.cpp
blob: 41bd5308eca1403c218ebafd91993a2b06e7a145 (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
/* 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 "gob/dbase.h"

namespace Gob {

dBase::dBase() : _recordData(0) {
	clear();
}

dBase::~dBase() {
	clear();
}

bool dBase::load(Common::SeekableReadStream &stream) {
	clear();

	uint32 startPos = stream.pos();

	_version = stream.readByte();
	if ((_version != 0x03) && (_version != 0x83))
		// Unsupported version
		return false;

	// TODO: Add support for memo files. A memo file is an external data file
	//       .DBT, segmented into "blocks". Each memo field in a record is an
	//       index this file.
	_hasMemo = (_version & 0x80) != 0;

	_lastUpdate.tm_year = stream.readByte();
	_lastUpdate.tm_mon  = stream.readByte() - 1;
	_lastUpdate.tm_mday = stream.readByte();
	_lastUpdate.tm_hour = 0;
	_lastUpdate.tm_min  = 0;
	_lastUpdate.tm_sec  = 0;

	uint32 recordCount = stream.readUint32LE();
	uint32 headerSize  = stream.readUint16LE();
	uint32 recordSize  = stream.readUint16LE();

	stream.skip(20); // Reserved

	// Read all field descriptions, 0x0D is the end marker
	uint32 fieldsLength = 0;
	while (!stream.eos() && !stream.err() && (stream.readByte() != 0x0D)) {
		Field field;

		stream.seek(-1, SEEK_CUR);

		field.name = readString(stream, 11);
		field.type = (Type) stream.readByte();

		stream.skip(4); // Field data address

		field.size     = stream.readByte();
		field.decimals = stream.readByte();

		fieldsLength += field.size;

		stream.skip(14); // Reserved and/or useless for us

		_fields.push_back(field);
	}

	if (stream.eos() || stream.err())
		return false;

	if ((stream.pos() - startPos) != headerSize)
		// Corrupted file / unknown format
		return false;

	if (recordSize != (fieldsLength + 1))
		// Corrupted file / unknown format
		return false;

	_recordData = new byte[recordSize * recordCount];
	if (stream.read(_recordData, recordSize * recordCount) != (recordSize * recordCount))
		return false;

	if (stream.readByte() != 0x1A)
		// Missing end marker
		return false;

	uint32 fieldCount = _fields.size();

	// Create the records array
	_records.resize(recordCount);
	for (uint32 i = 0; i < recordCount; i++) {
		Record &record = _records[i];
		const byte *data = _recordData + i * recordSize;

		char status = *data++;
		if ((status != ' ') && (status != '*'))
			// Corrupted file / unknown format
			return false;

		record.deleted = status == '*';

		record.fields.resize(fieldCount);
		for (uint32 j = 0; j < fieldCount; j++) {
			record.fields[j] = data;
			data += _fields[j].size;
		}
	}

	return true;
}

void dBase::clear() {
	memset(&_lastUpdate, 0, sizeof(_lastUpdate));

	_version = 0;
	_hasMemo = false;

	_fields.clear();
	_records.clear();

	delete[] _recordData;
	_recordData = 0;
}

byte dBase::getVersion() const {
	return _version;
}

TimeDate dBase::getLastUpdate() const {
	return _lastUpdate;
}

const Common::Array<dBase::Field> &dBase::getFields() const {
	return _fields;
}

const Common::Array<dBase::Record> &dBase::getRecords() const {
	return _records;
}

Common::String dBase::getString(const Record &record, int field) const {
	assert(_fields[field].type == kTypeString);

	uint32 fieldLength = stringLength(record.fields[field], _fields[field].size);
	return Common::String((const char *) record.fields[field], fieldLength);
}

// String fields are padded with spaces. This finds the real length.
inline uint32 dBase::stringLength(const byte *data, uint32 max) {
	while (max-- > 0)
		if ((data[max] != 0x20) && (data[max] != 0x00))
			return max + 1;

	return 0;
}

// Read a constant-length string out of a stream.
inline Common::String dBase::readString(Common::SeekableReadStream &stream, int n) {
	Common::String str;

	char c;
	while (n-- > 0) {
		if ((c = stream.readByte()) == '\0')
			break;

		str += c;
	}

	if (n > 0)
		stream.skip(n);

	return str;
}

} // End of namespace Gob