-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCFSArchive.cpp
executable file
·175 lines (132 loc) · 3.77 KB
/
CFSArchive.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
170
171
172
/*
CFSArchive.cpp
Author: Tom Naughton
Description: <describe the CFSArchive class here>
*/
#include "CFSArchive.h"
#include "CPakStream.h"
#include "String.h"
extern "C" {
#include "MoreFiles.h"
#include "MoreFilesExtras.h"
}
CFSArchive::CFSArchive(CFileArchive *inParent) : CFileArchive(inParent)
{
}
CFSArchive::CFSArchive(FSSpec inFileSpec) : CFileArchive(inFileSpec)
{
_loaded = false;
}
CFSArchive::~CFSArchive()
{
dprintf("~CFSArchive()\n");
}
Boolean CFSArchive::init()
{
// find the parent directory
FSSpec outSpec;
long directory;
GetParentID(_fileSpec.vRefNum,
_fileSpec.parID,
_fileSpec.name,
&directory);
OSErr anErr = FSMakeFSSpec(_fileSpec.vRefNum, directory, 0, &outSpec);
traverseDirectory(outSpec);
return true;
}
void CFSArchive::traverseDirectory(FSSpec inFileSpec)
{
_loaded = true;
CInfoPBRec folderInfo;
Str31 dirFileName;
LString::CopyPStr( inFileSpec.name, dirFileName, sizeof(Str31) );
long pakIndex = 0;
// find the info for this directory (mostly to get it's dirID)
folderInfo.hFileInfo.ioCompletion = nil;
folderInfo.hFileInfo.ioNamePtr = dirFileName;
folderInfo.hFileInfo.ioVRefNum = inFileSpec.vRefNum;
folderInfo.hFileInfo.ioFDirIndex = 0;
folderInfo.hFileInfo.ioDirID = inFileSpec.parID;
ThrowIfOSErr_(::PBGetCatInfoSync( &folderInfo ));
SInt16 index = 1;
while ( true ) {
// and recurse through this folder to list its contents
CInfoPBRec fileInfo;
Str31 fileName;
fileInfo.hFileInfo.ioCompletion = nil;
fileInfo.hFileInfo.ioNamePtr = fileName;
fileInfo.hFileInfo.ioVRefNum = folderInfo.hFileInfo.ioVRefNum;
fileInfo.hFileInfo.ioFDirIndex = index++;
fileInfo.hFileInfo.ioDirID = folderInfo.hFileInfo.ioDirID;
OSErr err = ::PBGetCatInfoSync( &fileInfo );
if ( err != noErr )
break;
// we ignore invis stuff
if ( fileInfo.hFileInfo.ioFlFndrInfo.fdFlags & kIsInvisible )
continue;
// make an FSSpec for the file
FSSpec theSpec;
ThrowIfOSErr_(::FSMakeFSSpec( folderInfo.hFileInfo.ioVRefNum,
folderInfo.hFileInfo.ioDirID,
fileName,
&theSpec ));
FSSpec tempSpec;
p2cstrcpy((char*)tempSpec.name, (const unsigned char *)theSpec.name);
string entryName = (char*)tempSpec.name;
string package, file, extension;
decomposeEntryName(lowerString(entryName), package, file, extension);
if ( fileInfo.hFileInfo.ioFlAttrib & ioDirMask ) {
// it's a directory
CFSArchive *aPackage = new CFSArchive(theSpec);
aPackage->setName(file.c_str());
aPackage->setParent(this);
addPackageWithName(aPackage, file.c_str());
} else {
// it's a file
addFileWithName(folderInfo.hFileInfo.ioDirID, (file + "." + extension).c_str());
}
}
}
CFileArchive::database_type *CFSArchive::getSubpackages()
{
if (!_loaded) {
traverseDirectory(_fileSpec);
}
return CFileArchive::getSubpackages();
}
CFileArchive::file_database_type *CFSArchive::getFiles()
{
if (!_loaded) {
traverseDirectory(_fileSpec);
}
return CFileArchive::getFiles();
}
CFileArchive *CFSArchive::subPackage(const char*name)
{
if (!_loaded) {
traverseDirectory(_fileSpec);
}
return CFileArchive::subPackage(name);
}
long CFSArchive::SizeOfItemAtIndex(const char *itemName, UInt32 inIndex)
{
FSSpec spec = _fileSpec;
spec.parID = inIndex;
long dataSize, rsrcSize;
c2pstrcpy((unsigned char *)spec.name, (const char *)itemName);
OSErr err = GetFileSize(spec.vRefNum,
spec.parID,
spec.name,
&dataSize,
&rsrcSize);
return dataSize;
}
CPakStream *CFSArchive::LoadItemAtIndex(CFileArchive *inParent, const char *itemName, UInt32 inIndex)
{
FSSpec spec = _fileSpec;
spec.parID = inIndex;
c2pstrcpy((unsigned char*)spec.name, (const char *)itemName);
CPakStream *stream = new CPakStream(spec);
stream->setArchive(inParent);
return stream;
}