scummvm/engine/lab.cpp

128 lines
3.5 KiB
C++
Raw Normal View History

2006-04-02 14:20:45 +00:00
/* Residual - Virtual machine to run LucasArts' 3D adventure games
* Copyright (C) 2003-2006 The ScummVM-Residual Team (www.scummvm.org)
*
* 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 "stdafx.h"
#include "bits.h"
2005-01-01 12:27:57 +00:00
#include "lab.h"
2003-08-15 18:00:22 +00:00
#include <algorithm>
#include <cstdlib>
#include <cctype>
bool Lab::open(const char *filename) {
2005-01-01 09:57:33 +00:00
_labFileName = filename;
close();
2004-12-09 23:55:43 +00:00
_f = std::fopen(filename, "rb");
if (!isOpen())
return false;
char header[16];
2004-12-09 23:55:43 +00:00
if (std::fread(header, 1, sizeof(header), _f) < sizeof(header)) {
close();
return false;
}
if (std::memcmp(header, "LABN", 4) != 0) {
close();
return false;
}
int num_entries = READ_LE_UINT32(header + 8);
int string_table_size = READ_LE_UINT32(header + 12);
char *string_table = new char[string_table_size];
2004-12-09 23:55:43 +00:00
std::fseek(_f, 16 * (num_entries + 1), SEEK_SET);
std::fread(string_table, 1, string_table_size, _f);
2004-12-09 23:55:43 +00:00
std::fseek(_f, 16, SEEK_SET);
char binary_entry[16];
for (int i = 0; i < num_entries; i++) {
2004-12-09 23:55:43 +00:00
std::fread(binary_entry, 1, 16, _f);
int fname_offset = READ_LE_UINT32(binary_entry);
int start = READ_LE_UINT32(binary_entry + 4);
int size = READ_LE_UINT32(binary_entry + 8);
std::string fname = string_table + fname_offset;
std::transform(fname.begin(), fname.end(), fname.begin(), tolower);
2004-12-10 07:26:03 +00:00
_fileMap.insert(std::make_pair(fname, LabEntry(start, size)));
_fileMap.size();
}
delete [] string_table;
return true;
2003-08-15 18:00:22 +00:00
}
bool Lab::fileExists(const char *filename) const {
2004-12-10 07:26:03 +00:00
return findFilename(filename) != _fileMap.end();
2003-08-15 18:00:22 +00:00
}
Block *Lab::getFileBlock(const char *filename) const {
2004-12-10 07:26:03 +00:00
FileMapType::const_iterator i = findFilename(filename);
if (i == _fileMap.end())
return NULL;
2004-12-09 23:55:43 +00:00
std::fseek(_f, i->second.offset, SEEK_SET);
// The sound decoder reads up to two bytes past the end of data
// (but shouldn't actually use those bytes). So allocate two extra bytes
// to be safe against crashes.
char *data = new char[i->second.len + 2];
2004-12-09 23:55:43 +00:00
std::fread(data, 1, i->second.len, _f);
data[i->second.len] = '\0'; // For valgrind cleanness
data[i->second.len + 1] = '\0';
return new Block(data, i->second.len);
}
2003-08-15 18:00:22 +00:00
std::FILE *Lab::openNewStream(const char *filename) const {
2004-12-10 07:26:03 +00:00
FileMapType::const_iterator i = findFilename(filename);
if (i == _fileMap.end())
return NULL;
2005-01-01 09:57:33 +00:00
FILE *file = std::fopen(_labFileName.c_str(), "rb");
assert(file);
std::fseek(file, i->second.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 {
2004-12-10 07:26:03 +00:00
FileMapType::const_iterator i = findFilename(filename);
if (i == _fileMap.end())
return -1;
2003-08-15 18:00:22 +00:00
return i->second.len;
2003-08-15 18:00:22 +00:00
}
2004-12-10 07:26:03 +00:00
Lab::FileMapType::const_iterator Lab::findFilename(const char *filename) const {
std::string s = filename;
std::transform(s.begin(), s.end(), s.begin(), tolower);
2004-12-10 07:26:03 +00:00
return _fileMap.find(s);
2003-08-15 18:00:22 +00:00
}
void Lab::close() {
2004-12-09 23:55:43 +00:00
if (_f != NULL)
std::fclose(_f);
_f = NULL;
2004-12-10 07:26:03 +00:00
_fileMap.clear();
2003-08-15 18:00:22 +00:00
}