forked from uliwitness/stackimport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCResourceFile.cpp
227 lines (192 loc) · 3.82 KB
/
CResourceFile.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
* CResourceFile.cpp
* stackimport
*
* Created by Justin Blanchard on 11/11/15.
* License: MIT
*
*/
#include "CResourceFile.h"
CResourceFile::CResourceFile()
{
}
CResource::CResource()
{
}
int16_t CResource::GetID()
{
return mID;
}
const std::string& CResource::GetName()
{
return mName;
}
#if MAC_CODE
CResource::CResource( Handle handle )
: mHandle(handle)
{
ResID theID;
ResType theType;
Str255 theName;
GetResInfo( mHandle, &theID, &theType, theName );
mID = theID;
mName = std::string(&theName[1], theName[0]);
}
CResource::operator bool()
{
return mHandle;
}
const char* CResource::GetBuffer()
{
return *mHandle;
}
Handle CResource::GetHandle()
{
return mHandle;
}
size_t CResource::GetSize()
{
return GetHandleSize(mHandle);
}
OSType CResourceFile::MakeOSType( const std::string& fpath )
{
return (OSType(fpath[0]) << 24) |
(OSType(fpath[1]) << 16) |
(OSType(fpath[2]) << 8) |
OSType(fpath[3]);
}
void CResourceFile::LoadFile( const std::string& fpath )
{
FSRef fileRef;
mResRefNum = -1;
OSStatus resErr = FSPathMakeRef( (const UInt8*) fpath.c_str(), &fileRef, NULL );
if( resErr == noErr )
{
mResRefNum = FSOpenResFile( &fileRef, fsRdPerm );
if( mResRefNum < 0 )
{
fprintf( stderr, "Warning: No Mac resource fork to import.\n" );
resErr = fnfErr;
}
}
else
{
fprintf( stderr, "Error: Error %d locating input file's resource fork.\n", (int)resErr );
mResRefNum = -1;
}
}
void CResourceFile::Close()
{
if( mResRefNum > 0 )
{
CloseResFile( mResRefNum );
}
}
int16_t CResourceFile::Count( const std::string& type )
{
if( mResRefNum > 0 )
{
return Count1Resources(MakeOSType(type));
}
else
{
return 0;
}
}
CResource CResourceFile::GetByID( const std::string& type, int16_t id )
{
if( mResRefNum > 0 )
{
return CResource( Get1Resource( type, id ) );
}
else
{
return CResource( NULL );
}
}
CResource CResourceFile::GetByIndex( const std::string& type, int16_t index )
{
if( mResRefNum > 0 )
{
return CResource( Get1IndResource( type, index ) );
}
else
{
return CResource( NULL );
}
}
#else //MAC_CODE
#include <cstdlib>
#include <dirent.h>
#include <fstream>
#include <sstream>
CResource::CResource( int16_t id, const std::string& fpath )
: mID(id)
{
std::string fname = fpath.substr( fpath.rfind( '/' ) );
std::string::size_type _1 = fname.find( '_' ), _2 = fname.find( '_', _1);
std::ifstream in( fpath );
mName = fname.substr( _2+1 );
if( in )
{
std::stringstream ss;
ss << in.rdbuf();
mBuffer = ss.str();
}
}
CResource::operator bool()
{
return GetSize();
}
const char* CResource::GetBuffer()
{
return mBuffer.c_str();
}
size_t CResource::GetSize()
{
return mBuffer.size();
}
void CResourceFile::LoadFile( const std::string& fpath )
{
std::string::size_type _1, _2, slash, stak = fpath.rfind( ".stak" );
std::string type, name, path = fpath.substr( 0, stak ) + ".rsrc";
DIR* dir = opendir( path.c_str() );
dirent* ent;
int16_t id;
if( dir )
{
while( (ent = readdir(dir)) )
{
name = ent->d_name;
_1 = name.find( '_');
_2 = name.find( '_', _1+1 );
type = name.substr( 0, _1 );
id = atoi( name.substr( _1+1, _2-_1-1 ).c_str() );
mMap[type][id] = path + '/' + name;
}
closedir( dir );
}
}
void CResourceFile::Close()
{
}
int16_t CResourceFile::Count( const std::string& type )
{
return mMap[type].size();
}
CResource CResourceFile::GetByID( const std::string& type, int16_t id )
{
auto it = mMap[type].find(id);
if( it == mMap[type].end() )
return CResource();
return CResource( id, it->second );
}
CResource CResourceFile::GetByIndex( const std::string& type, int16_t index )
{
// FIXME: This is O(N) (so a scan is quadratic) for no reason.
auto it = mMap[type].begin();
while( --index )
++it;
return CResource( it->first, it->second );
}
#endif //MAC_CODE