scummvm/lab.cpp

118 lines
3.4 KiB
C++
Raw Normal View History

2003-08-15 18:00:22 +00:00
// Residual - Virtual machine to run LucasArts' 3D adventure games
// Copyright (C) 2003-2004 The ScummVM-Residual Team (www.scummvm.org)
2003-08-15 18:00:22 +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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#include "stdafx.h"
#include "lab.h"
#include "bits.h"
2003-08-15 18:00:22 +00:00
#include <algorithm>
#include <cstdlib>
#include <cctype>
bool Lab::open(const char *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-09 23:55:43 +00:00
_file_map.insert(std::make_pair(fname, LabEntry(start, size)));
_file_map.size();
}
delete [] string_table;
return true;
2003-08-15 18:00:22 +00:00
}
bool Lab::fileExists(const char *filename) const {
2004-12-09 23:55:43 +00:00
return find_filename(filename) != _file_map.end();
2003-08-15 18:00:22 +00:00
}
Block *Lab::getFileBlock(const char *filename) const {
file_map_type::const_iterator i = find_filename(filename);
2004-12-09 23:55:43 +00:00
if (i == _file_map.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 {
file_map_type::const_iterator i = find_filename(filename);
2004-12-09 23:55:43 +00:00
if (i == _file_map.end())
return NULL;
2004-12-09 23:55:43 +00:00
std::fseek(_f, i->second.offset, SEEK_SET);
2004-12-09 23:55:43 +00:00
return _f;
}
2003-08-15 18:00:22 +00:00
int Lab::fileLength(const char *filename) const {
file_map_type::const_iterator i = find_filename(filename);
2004-12-09 23:55:43 +00:00
if (i == _file_map.end())
return -1;
2003-08-15 18:00:22 +00:00
return i->second.len;
2003-08-15 18:00:22 +00:00
}
Lab::file_map_type::const_iterator Lab::find_filename(const char *filename) const {
std::string s = filename;
std::transform(s.begin(), s.end(), s.begin(), tolower);
2004-12-09 23:55:43 +00:00
return _file_map.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;
_file_map.clear();
2003-08-15 18:00:22 +00:00
}