scummvm/engines/grim/lab.cpp

159 lines
3.9 KiB
C++
Raw Normal View History

2006-04-02 14:20:45 +00:00
/* Residual - Virtual machine to run LucasArts' 3D adventure games
*
* Residual is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the AUTHORS
* file distributed with this source distribution.
2006-04-02 14:20:45 +00:00
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library 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
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*
* $URL$
* $Id$
*
*/
2003-08-15 18:00:22 +00:00
#include "common/endian.h"
#include "common/file.h"
2008-07-26 09:18:46 +00:00
#include "engines/grim/lab.h"
#include "engines/grim/lua/lua.h"
2005-01-01 12:27:57 +00:00
static int sortCallback(const void *entry1, const void *entry2) {
return strcasecmp(((Lab::LabEntry *)entry1)->filename, ((Lab::LabEntry *)entry2)->filename);
}
2003-08-15 18:00:22 +00:00
bool Lab::open(const char *filename) {
2005-01-01 09:57:33 +00:00
_labFileName = filename;
close();
2008-08-01 18:27:11 +00:00
_f = new Common::File();
_f->open(filename);
if (!_f->isOpen())
return false;
char header[16];
_f->read(header, sizeof(header));
if (READ_BE_UINT32(header) != MKID_BE('LABN')) {
close();
return false;
}
_numEntries = READ_LE_UINT32(header + 8);
int string_table_size = READ_LE_UINT32(header + 12);
char *string_table = new char[string_table_size];
_f->seek(16 * (_numEntries + 1), SEEK_SET);
2008-08-01 18:27:11 +00:00
_f->read(string_table, string_table_size);
_entries = new LabEntry[_numEntries];
2008-08-01 18:27:11 +00:00
_f->seek(16, SEEK_SET);
char binary_entry[16];
for (int i = 0; i < _numEntries; i++) {
2008-08-01 18:27:11 +00:00
_f->read(binary_entry, 16);
int fname_offset = READ_LE_UINT32(binary_entry);
int start = READ_LE_UINT32(binary_entry + 4);
int size = READ_LE_UINT32(binary_entry + 8);
Common::String fname = string_table + fname_offset;
fname.toLowercase();
_entries[i].offset = start;
_entries[i].len = size;
_entries[i].filename = new char[fname.size() + 1];
strcpy(_entries[i].filename, fname.c_str());
}
qsort(_entries, _numEntries, sizeof(LabEntry), sortCallback);
2009-05-10 09:23:11 +00:00
delete[] string_table;
return true;
2003-08-15 18:00:22 +00:00
}
bool Lab::fileExists(const char *filename) const {
return findFilename(filename) != NULL;
}
bool Lab::isOpen() const {
return _f->isOpen();
2003-08-15 18:00:22 +00:00
}
Block *Lab::getFileBlock(const char *filename) const {
LabEntry *i = findFilename(filename);
if (!i)
return NULL;
_f->seek(i->offset, SEEK_SET);
char *data = new char[i->len];
_f->read(data, i->len);
return new Block(data, i->len);
}
2003-08-15 18:00:22 +00:00
LuaFile *Lab::openNewStreamLua(const char *filename) const {
LabEntry *i = findFilename(filename);
if (!i)
return NULL;
Common::File *file = new Common::File();
file->open(_labFileName);
file->seek(i->offset, SEEK_SET);
LuaFile *filehandle = new LuaFile();
filehandle->_in = file;
return filehandle;
}
Common::File *Lab::openNewStreamFile(const char *filename) const {
2008-07-26 09:18:46 +00:00
Common::File *file;
LabEntry *i = findFilename(filename);
if (!i)
return NULL;
2008-07-26 09:18:46 +00:00
file = new Common::File();
file->open(_labFileName.c_str());
file->seek(i->offset, SEEK_SET);
2005-01-01 09:57:33 +00:00
return file;
}
2003-08-15 18:00:22 +00:00
int Lab::fileLength(const char *filename) const {
LabEntry *i = findFilename(filename);
if (!i)
return -1;
2003-08-15 18:00:22 +00:00
return i->len;
2003-08-15 18:00:22 +00:00
}
Lab::LabEntry *Lab::findFilename(const char *filename) const {
LabEntry key;
Common::String s = filename;
s.toLowercase();
key.filename = (char *)s.c_str();
return (Lab::LabEntry *)bsearch(&key, _entries, _numEntries, sizeof(LabEntry), sortCallback);
2003-08-15 18:00:22 +00:00
}
void Lab::close() {
delete _f;
2004-12-09 23:55:43 +00:00
_f = NULL;
2009-05-10 18:15:26 +00:00
if (_entries)
for (int i = 0; i < _numEntries; i++)
delete[] _entries[i].filename;
2009-05-10 09:28:32 +00:00
delete[] _entries;
2009-05-10 18:16:46 +00:00
_entries = NULL;
_numEntries = 0;
2003-08-15 18:00:22 +00:00
}