-
Notifications
You must be signed in to change notification settings - Fork 0
/
CFileArchiveItem.cpp
executable file
·147 lines (116 loc) · 2.78 KB
/
CFileArchiveItem.cpp
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
CPakStream.cpp
Author: Tom Naughton
Description: <describe the CPakStream class here>
*/
#include "CFileArchive.h"
#include "CPakStream.h"
#include "utilities.h"
#include "CPakRatApp.h"
CPakStream::item_array CPakStream::_allocatedItems;
CPakStream::CPakStream(FSSpec inFileSpec)
{
_allocatedItems.AddItem(this);
LFileStream *file = new LFileStream(inFileSpec);
// FIXME - this breaks inFileSpec
_name = p2cstr(inFileSpec.name);
_index = -1;
_archive = nil;
_dataCursor = 0;
file->OpenDataFork(fsRdWrPerm);
_dataHandle = ::NewHandle(file->GetLength());
if (!_dataHandle)
goto fail;
::HLock(_dataHandle);
file->GetBytes(*_dataHandle, file->GetLength());
file->CloseDataFork();
::HUnlock(_dataHandle);
_size = ::GetHandleSize(_dataHandle);
delete file;
dprintf("CPakStream loaded %s\n",pathName().c_str());
return;
fail:
delete file;
dprintf("could not load pak item\n");
return;
}
CPakStream::CPakStream(CFileArchive *inArchive, string inName, long inIndex, Handle inHandle)
{
_allocatedItems.AddItem(this);
_archive = inArchive;
_name = inName;
_index = inIndex;
_dataHandle = inHandle;
_dataCursor = 0;
if (_dataHandle)
_size = ::GetHandleSize(_dataHandle);
dprintf("CPakStream loaded %s\n",pathName().c_str());
}
CPakStream::~CPakStream()
{
if (_allocatedItems.ContainsItem(this)) {
_allocatedItems.Remove(this);
}
::HUnlock(_dataHandle);
::DisposeHandle(_dataHandle);
dprintf("~CPakStream %s\n",pathName().c_str());
}
char *CPakStream::getData(char *tag)
{
resetCursor();
char *p = (char*)CMemoryTracker::safeAlloc(1, getSize(), tag, false);
if(p && !getBytes(getSize(), (char*)p)) {
CMemoryTracker::safeFree(p);
p = nil;
}
return p;
}
string CPakStream::pathName()
{
string path;
if (_archive)
path += _archive->pathName();
path += _name;
return path;
}
StringPtr CPakStream::itemTitle()
{
static char _pTitle[1024];
string title = pathName();
strcpy(_pTitle, title.c_str());
return (StringPtr) c2pstr(_pTitle);
}
string CPakStream::dataType()
{
string package, file, extension;
decomposeEntryName(_name, package, file, extension);
return extension;
}
Boolean CPakStream::getBytes(unsigned long nBytes, void *bytes)
{
char *p = 0;
// never gets unlocked
if (_dataHandle) {
::HLock(_dataHandle);
p = (*_dataHandle);
}
if (!p)
return false;
if (_dataCursor + nBytes > _size)
return false;
memcpy(bytes, p + _dataCursor, nBytes);
_dataCursor += nBytes;
return true;
}
void CPakStream::dumpHeap()
{
dprintf("Allocated loaders:\n");
TArrayIterator<CPakStream*> iterator(
(const_cast<TArray<CPakStream*>&>
(_allocatedItems)));
CPakStream* item = nil;
while (iterator.Next(item) && (item != nil)) {
string viewerPath = item->pathName();
dprintf(" %s\n",viewerPath.c_str());
}
}