-
Notifications
You must be signed in to change notification settings - Fork 0
/
CPakStream.cpp
executable file
·169 lines (133 loc) · 2.98 KB
/
CPakStream.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
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_set CPakStream::_itemSet;
CPakStream::CPakStream(FSSpec inFileSpec)
{
_index = -1;
_archive = nil;
_dataCursor = 0;
_itemSet.insert(this);
LFileStream *file = new LFileStream(inFileSpec);
char name[255];
p2cstrcpy(name, inFileSpec.name);
_name = name;
file->OpenDataFork(fsWrPerm);
_dataHandle = ::NewHandle(file->GetLength());
if (!_dataHandle)
goto fail;
::HLock(_dataHandle);
SInt32 length = file->GetLength();
file->GetBytes(*_dataHandle, length);
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)
{
_itemSet.insert(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()
{
_itemSet.erase(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;
}
OSErr CPakStream::copyDataToFile(FSSpec *spec)
{
LFile outFile(*spec);
if (_dataHandle) {
::HLock(_dataHandle);
} else {
return fnfErr;
}
try {
outFile.OpenDataFork(fsWrPerm);
outFile.WriteDataFork(*_dataHandle, getSize());
outFile.CloseDataFork();
} catch (...) {
// oh well
dprintf("exception generated writing data!\n");
}
return noErr;
}
string CPakStream::pathName()
{
string path;
if (_archive)
path += _archive->pathName();
path += _name;
return path;
}
string CPakStream::dataType()
{
string package, file, extension;
decomposeEntryName(_name, package, file, extension);
return extension;
}
char *CPakStream::address()
{
char *p = 0;
// never gets unlocked
if (_dataHandle) {
::HLock(_dataHandle);
p = (*_dataHandle);
}
return p;
}
Boolean CPakStream::getBytes(unsigned long nBytes, void *bytes)
{
char *p = address();
ThrowIf_(!p); // sanity check
ThrowIf_(_dataCursor + nBytes > _size); // sanity check
memcpy(bytes, p + _dataCursor, nBytes);
_dataCursor += nBytes;
return true;
}
void CPakStream::seek(UInt32 location)
{
_dataCursor = location;
}
void CPakStream::dumpHeap()
{
dprintf("Allocated loaders:\n");
item_set_iterator e = _itemSet.begin();
while (e != _itemSet.end()) {
CPakStream *item = *e;
string viewerPath = item->pathName();
dprintf(" %s\n",viewerPath.c_str());
e++;
}
}