aboutsummaryrefslogtreecommitdiff
path: root/engines/adl/disk.cpp
blob: e2c3da3d5c565c70e1422efefc4ad4029c5da605 (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
/* 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/stream.h"
#include "common/substream.h"

#include "adl/disk.h"

namespace Adl {

DiskImage::DiskImage() :
		_tracks(0),
		_sectorsPerTrack(0),
		_bytesPerSector(0) {
	_f = new Common::File();
}

DiskImage::~DiskImage() {
	delete _f;
}

DiskImageDataBlock::DiskImageDataBlock(const DiskImage *disk, uint track, uint sector, uint offset, uint size) :
		_track(track),
		_sector(sector),
		_offset(offset),
		_size(size),
		_disk(disk) { }

bool DiskImageDataBlock::isValid() const {
	return _track != 0 || _sector != 0 || _offset != 0 || _size != 0;
}

Common::SeekableReadStream *DiskImageDataBlock::createReadStream() const {
	return _disk->createReadStream(_track, _sector, _offset, _size);
}

// .DSK disk image - 35 tracks, 16 sectors per track, 256 bytes per sector, raw sector layout
const DataBlockPtr DiskImage_DSK::getDataBlock(uint track, uint sector, uint offset, uint size) const {
	return Common::SharedPtr<DiskImageDataBlock>(new DiskImageDataBlock(this, track, sector, offset, size));
}

Common::SeekableReadStream *DiskImage_DSK::createReadStream(uint track, uint sector, uint offset, uint size) const {
	_f->seek((track * _sectorsPerTrack + sector) * _bytesPerSector + offset);
	Common::SeekableReadStream *stream = _f->readStream(size * _bytesPerSector + _bytesPerSector - offset);

	if (_f->eos() || _f->err())
		error("Error reading disk image");

	return stream;
}

bool DiskImage_DSK::open(const Common::String &filename) {
	assert(!_f->isOpen());

	if (!_f->open(filename))
		return false;

	uint filesize = _f->size();
	switch (filesize) {
	case 143360:
		_tracks = 35;
		_sectorsPerTrack = 16;
		_bytesPerSector = 256;
		break;
	default:
		warning("Unrecognized disk image '%s' of size %d bytes", filename.c_str(), filesize);
	}

	return true;
}

Common::SeekableReadStream *FilesDataBlock::createReadStream() const {
	return _files->createReadStream(_filename, _offset);
}

const DataBlockPtr PlainFiles::getDataBlock(const Common::String &filename, uint offset) const {
	return Common::SharedPtr<FilesDataBlock>(new FilesDataBlock(this, filename, offset));
}

Common::SeekableReadStream *PlainFiles::createReadStream(const Common::String &filename, uint offset) const {
	Common::File *f(new Common::File());

	if (!f->open(filename))
		error("Failed to open '%s'", filename.c_str());

	if (offset == 0)
		return f;
	else
		return new Common::SeekableSubReadStream(f, offset, f->size(), DisposeAfterUse::YES);
}

} // End of namespace Adl