forked from OpenXcom/OpenXcom
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCatFile.cpp
More file actions
90 lines (75 loc) · 1.98 KB
/
Copy pathCatFile.cpp
File metadata and controls
90 lines (75 loc) · 1.98 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
/*
* Copyright 2010 OpenXcom Developers.
*
* This file is part of OpenXcom.
*
* OpenXcom 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 3 of the License, or
* (at your option) any later version.
*
* OpenXcom 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 OpenXcom. If not, see <http://www.gnu.org/licenses/>.
*/
#include "CatFile.h"
namespace OpenXcom
{
/**
* Creates a CAT file stream. A CAT file starts with an index of the
* offset and size of every file contained within. Each file consists
* of a filename followed by its contents.
* @param path Full path to CAT file.
*/
CatFile::CatFile(const char *path) :
std::ifstream(path, std::ios::in | std::ios::binary),
_amount(0), _offset(0), _size(0)
{
if (!this)
return;
// Get amount of files
read((char*)&_amount, sizeof(_amount));
_amount /= 2 * sizeof(_amount);
// Get object offsets
seekg(0, std::ios::beg);
_offset = new unsigned int[_amount];
_size = new unsigned int[_amount];
for (unsigned int i = 0; i < _amount; i++)
{
read((char*)&_offset[i], sizeof(*_offset));
read((char*)&_size[i], sizeof(*_size));
}
}
/**
* Frees associated memory.
*/
CatFile::~CatFile()
{
delete[] _offset;
delete[] _size;
close();
}
/**
* Loads an object into memory.
* @param i Object number to load.
* @return Pointer to the loaded object.
*/
char *CatFile::load(unsigned int i)
{
if (i >= _amount)
return 0;
seekg(_offset[i], std::ios::beg);
// Skip filename
char namesize;
read(&namesize, 1);
seekg(namesize, std::ios::cur);
// Read object
char *object = new char[_size[i]];
read(object, _size[i]);
return object;
}
}