scummvm/engines/cge/vol.cpp

141 lines
3.2 KiB
C++
Raw Normal View History

/* 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.
*
*/
/*
* This code is based on original Soltys source code
* Copyright (c) 1994-1995 Janus B. Wisniewski and L.K. Avalon
*/
#include "cge/vol.h"
2011-06-11 07:33:17 +00:00
#include "common/system.h"
#include "common/str.h"
2011-07-18 16:05:57 +00:00
#include "cge/cge.h"
#include "common/debug.h"
#include "common/debug-channels.h"
2011-06-10 20:57:09 +00:00
namespace CGE {
2011-07-03 14:22:26 +00:00
Dat *VFile::_dat = NULL;
BtFile *VFile::_cat = NULL;
VFile *VFile::_recent = NULL;
/*-----------------------------------------------------------------------*/
2011-07-03 14:22:26 +00:00
Dat::Dat():
#ifdef VOL_UPD
2011-07-03 14:22:26 +00:00
_file(DAT_NAME, UPD, CRP)
#else
2011-07-23 12:31:39 +00:00
_file(DAT_NAME, kModeRead, CRP)
2011-06-13 09:57:24 +00:00
#endif
{
2011-07-20 12:22:56 +00:00
debugC(1, kCGEDebugFile, "Dat::Dat()");
}
2011-06-13 09:57:24 +00:00
/*-----------------------------------------------------------------------*/
2011-07-03 14:22:26 +00:00
void VFile::init() {
2011-07-20 12:22:56 +00:00
debugC(1, kCGEDebugFile, "VFile::init()");
2011-07-18 16:05:57 +00:00
2011-07-03 14:22:26 +00:00
_dat = new Dat();
#ifdef VOL_UPD
2011-06-30 06:30:23 +00:00
_cat = new BtFile(CAT_NAME, UPD, CRP);
#else
2011-07-23 12:31:39 +00:00
_cat = new BtFile(CAT_NAME, kModeRead, CRP);
#endif
2011-06-30 06:30:23 +00:00
_recent = NULL;
}
2011-07-03 14:22:26 +00:00
void VFile::deinit() {
2011-06-30 06:30:23 +00:00
delete _dat;
delete _cat;
}
2011-06-13 09:57:24 +00:00
2011-07-23 12:31:39 +00:00
VFile::VFile(const char *name, IOMode mode)
: IoBuf(mode, NULL) {
2011-07-20 12:22:56 +00:00
debugC(3, kCGEDebugFile, "VFile::VFile(%s, %d)", name, mode);
2011-07-18 16:05:57 +00:00
2011-07-23 12:31:39 +00:00
if (mode == kModeRead) {
2011-07-03 14:22:26 +00:00
if (_dat->_file._error || _cat->_error)
2011-06-13 09:57:24 +00:00
error("Bad volume data");
2011-06-30 06:30:23 +00:00
BtKeypack *kp = _cat->find(name);
if (scumm_stricmp(kp->_key, name) != 0)
2011-07-01 06:37:40 +00:00
_error = 1;
2011-06-30 06:30:23 +00:00
_endMark = (_bufMark = _begMark = kp->_mark) + kp->_size;
2011-06-13 09:57:24 +00:00
}
#ifdef VOL_UPD
2011-06-13 11:07:45 +00:00
else
2011-06-13 09:57:24 +00:00
Make(name);
#endif
}
2011-07-03 14:22:26 +00:00
VFile::~VFile() {
2011-06-30 06:30:23 +00:00
if (_recent == this)
_recent = NULL;
}
2011-07-03 14:22:26 +00:00
bool VFile::exist(const char *name) {
2011-07-20 12:22:56 +00:00
debugC(1, kCGEDebugFile, "VFile::exist(%s)", name);
2011-07-18 16:05:57 +00:00
2011-06-30 06:30:23 +00:00
return scumm_stricmp(_cat->find(name)->_key, name) == 0;
}
2011-07-18 16:05:57 +00:00
void VFile::readBuf() {
2011-07-20 12:22:56 +00:00
debugC(3, kCGEDebugFile, "VFile::readBuf()");
2011-07-18 16:05:57 +00:00
2011-06-30 06:30:23 +00:00
if (_recent != this) {
2011-07-03 14:22:26 +00:00
_dat->_file.seek(_bufMark + _lim);
2011-06-30 06:30:23 +00:00
_recent = this;
2011-06-13 09:57:24 +00:00
}
2011-07-03 14:22:26 +00:00
_bufMark = _dat->_file.mark();
2011-06-30 06:30:23 +00:00
long n = _endMark - _bufMark;
if (n > kBufferSize)
n = kBufferSize;
2011-07-03 14:22:26 +00:00
_lim = _dat->_file.read(_buff, (uint16) n);
2011-06-30 06:30:23 +00:00
_ptr = 0;
}
2011-06-10 20:57:09 +00:00
long VFile::mark() {
2011-07-20 12:22:56 +00:00
debugC(5, kCGEDebugFile, "VFile::mark()");
2011-07-18 16:05:57 +00:00
return (_bufMark + _ptr) - _begMark;
}
long VFile::size() {
2011-07-20 12:22:56 +00:00
debugC(1, kCGEDebugFile, "VFile::size()");
2011-07-18 16:05:57 +00:00
return _endMark - _begMark;
}
long VFile::seek(long pos) {
2011-07-20 12:22:56 +00:00
debugC(1, kCGEDebugFile, "VFile::seel(%ld)", pos);
2011-07-18 16:05:57 +00:00
_recent = NULL;
_lim = 0;
return (_bufMark = _begMark + pos);
}
2011-06-10 20:57:09 +00:00
} // End of namespace CGE