Menu

[r29]: / ktscommon / RimArchive.cpp  Maximize  Restore  History

Download this file

110 lines (84 with data), 2.9 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
/***************************************************************************
* 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;
}
MongoDB Logo MongoDB