/***************************************************************************
* RimArchive.cpp
*
* Sun Jan 22 19:26:38 2006
* Copyright 2006 Sikon
* sikon@sourceforge.net
****************************************************************************/
/*
* 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <libkts.h>
using namespace KTS;
RimArchive::RimArchive(const char *name)
: Archive(name), _size(0), _head(0)
{
}
RimArchive::~RimArchive()
{
_fs.close();
}
void RimArchive::open()
{
_fs.open(getName(), std::ios_base::in | std::ios_base::binary);
if(_fs.fail())
throw KTXIO("Cannot open RIM archive");
_fs.seekg(0, std::ios_base::end);
_size = _fs.tellg();
_fs.seekg(0, std::ios_base::beg);
_head = new RimHeader();
KtsRead(_fs, _head, sizeof(RimHeader));
if(!isValidHeader(_head))
throw KTXFileFormat("Invalid RIM header");
RimKey key;
FileInfo info;
_fs.seekg(_head->ofsKeyList, std::ios_base::beg);
// reading key list - name and type
//
for(std::size_t i = 0; i < _head->entryCount; i++)
{
KtsRead(_fs, &key, sizeof(RimKey));
std::memcpy(info.name, key.resRef, sizeof(key.resRef));
info.name[KTS_FILENAME_MAX - 1] = 0;
info.type = key.resType;
info.size = key.size;
info.offset = key.offset;
addFileRecord(info);
}
_fs.seekg(0, std::ios_base::beg);
}
void RimArchive::extract(ktsu32 index, const char *extractTo) const
{
if(index >= getFileCount())
throw KTXOutOfRange("RIM Extract: file index too large");
std::ofstream outFile(extractTo, std::ios_base::out | std::ios_base::binary
| std::ios_base::trunc);
if(outFile.fail())
throw KTXIO("Cannot open output file");
const FileInfo& infoLink = getFileInfo(index);
std::ifstream& inFile = const_cast<std::ifstream&>(_fs);
std::size_t curPos = inFile.tellg();
inFile.seekg(infoLink.offset, std::ios_base::beg);
KtsCopy(inFile, outFile, infoLink.size);
inFile.seekg(curPos, std::ios_base::beg);
}
bool RimArchive::isValidHeader(const RimHeader *head) const
{
if(strncmp(head->fileType, "RIM ", sizeof(head->fileType)))
return false;
if(head->ofsKeyList + head->entryCount * sizeof(RimKey) > _size)
return false;
return true;
}