152 lines
3.3 KiB
C++
152 lines
3.3 KiB
C++
/* ScummVM - Scumm Interpreter
|
|
* Copyright (C) 2006 The ScummVM project
|
|
*
|
|
* 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/file.h"
|
|
|
|
#include "parallaction/disk.h"
|
|
|
|
|
|
namespace Parallaction {
|
|
|
|
|
|
void Archive::open(const char *file) {
|
|
debugC(1, kDebugDisk, "open archive '%s'", file);
|
|
|
|
if (_archive.isOpen())
|
|
close();
|
|
|
|
uint32 offset = DIRECTORY_OFFSET_IN_FILE;
|
|
char path[PATH_LEN];
|
|
|
|
strcpy(path, file);
|
|
if (!_archive.open(path))
|
|
errorFileNotFound(path);
|
|
|
|
_archive.skip(22);
|
|
_archive.read(_archiveDir, MAX_ARCHIVE_ENTRIES*32);
|
|
|
|
uint16 i;
|
|
for (i = 0; i < MAX_ARCHIVE_ENTRIES; i++) {
|
|
_archiveOffsets[i] = offset;
|
|
|
|
uint32 len = _archive.readUint32BE();
|
|
// if (len>0) printf("%i) %s - [%i bytes]\n", i, _archiveDir[i], len);
|
|
|
|
_archiveLenghts[i] = len;
|
|
offset += len;
|
|
}
|
|
|
|
// printf("%i entries found\n", i);
|
|
// printf("%i bytes of data\n", offset);
|
|
|
|
return;
|
|
}
|
|
|
|
|
|
void Archive::close() {
|
|
debugC(1, kDebugDisk, "close current archive");
|
|
|
|
if (!_archive.isOpen()) return;
|
|
|
|
_archive.close();
|
|
}
|
|
|
|
|
|
bool Archive::openArchivedFile(const char *name) {
|
|
|
|
uint16 i = 0;
|
|
for ( ; i < MAX_ARCHIVE_ENTRIES; i++) {
|
|
if (!scumm_stricmp(_archiveDir[i], name)) break;
|
|
}
|
|
if (i == MAX_ARCHIVE_ENTRIES) return false;
|
|
|
|
debugC(1, kDebugDisk, "file '%s' found in slot %i", name, i);
|
|
|
|
_file = true;
|
|
|
|
_fileIndex = i;
|
|
_fileOffset = _archiveOffsets[i];
|
|
_fileCursor = _archiveOffsets[i];
|
|
_fileEndOffset = _archiveOffsets[i] + _archiveLenghts[i];
|
|
|
|
_archive.seek(_fileOffset);
|
|
|
|
return true;
|
|
}
|
|
|
|
|
|
void Archive::closeArchivedFile() {
|
|
_file = false;
|
|
_fileIndex = 0;
|
|
_fileCursor = 0;
|
|
_fileOffset = 0;
|
|
_fileEndOffset = 0;
|
|
return;
|
|
}
|
|
|
|
|
|
uint16 Archive::getArchivedFileLength(const char *name) {
|
|
// printf("getArchivedFileLength(%s)\n", name);
|
|
|
|
for (uint16 i = 0; i < MAX_ARCHIVE_ENTRIES; i++) {
|
|
if (!scumm_stricmp(_archiveDir[i], name))
|
|
return _archiveLenghts[i];
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
|
|
int16 Archive::readArchivedFile(void *buffer, uint16 size) {
|
|
// printf("readArchivedFile(%i, %i)\n", file->_cursor, file->_endOffset);
|
|
if (_file == false)
|
|
error("readArchiveFile: no archived file is currently open");
|
|
|
|
if (_fileCursor == _fileEndOffset) return -1;
|
|
|
|
if (_fileEndOffset - _fileCursor < size)
|
|
size = _fileEndOffset - _fileCursor;
|
|
|
|
_archive.seek(_fileCursor);
|
|
int16 read = _archive.read(buffer, size);
|
|
_fileCursor += read;
|
|
|
|
return read;
|
|
}
|
|
|
|
|
|
char *Archive::readArchivedFileText(char *buf, uint16 size) {
|
|
if (_file == false)
|
|
error("readArchiveFileText: no archived file is currently open");
|
|
|
|
char *t = _archive.readLine(buf, size);
|
|
|
|
if (_archive.eof() || t == NULL)
|
|
return NULL;
|
|
|
|
return t;
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace Parallaction
|