[MacPerl-Toolbox] Listing resources from the resource fork
[email protected] (Bart Lateur) Thu, 21 Jun 2001 18:29:11 +0200
| Newsgroups | perl.macperl.toolbox |
|---|---|
| Organization | MediaMind |
| Message-ID | <[email protected]> |
I am attempting to come to grips with reading resource fork in MacPerl.
What I'm really trying to do, is to convert FOND resources from a font
suitcase to an AFM file. But don't let me bother you with that.
So here's my basic testing script. Save as a droplet, drop a file with a
resource fork (for example a font suitcase) on it, and it will list all
resource types, the count of resources for each, and then list them one
by one, with a hex dump of the first 32 bytes.
It kinda works.
#! perl -w
use Mac::Resources;
use Mac::Memory;
while(@ARGV) {
my $file =3D shift;
if (defined(my $RFD =3D OpenResFile($file))) {
my $types =3D Count1Types;
print "$file: $types types\n";
for my $i (1 .. Count1Types) {
my $restype =3D Get1IndType($i);
print " Resource type: $restype\n";
my $rescount =3D Count1Resources($restype);
print " $rescount resources\n";
for my $j (1 .. $rescount) {
my $reshandle =3D Get1IndResource($restype, $j);
my @resinfo =3D GetResInfo $reshandle;=20
print " -> @resinfo\n";
my $resdata =3D $reshandle->get(0,32);
print " @{[map { sprintf '%02X', $_ } unpack 'C*',
$resdata]}\n";
# $reshandle->dispose; ????
}
}
CloseResFile $RFD;
} else {
print "Cannot open resource fork for file $file: $^E\n";
}
}
My problem is with disposing of the handle. I found that
"RunTimeBuilder.dp" uses $sourceCodeRes->dispose, so that's what I tried
first. My problem is that when I do that, I get errors in my dump. I
probably get errors without it, too...
According to ResEdit, I have two resource types: "FOND", with 4 named
resources (1 for each font), the resource name is the font name, Id's
8798 to 8801; and "NFNT", which contains 20 resources, 20 bitmap fonts,
with no name, and ID's between 17663 and 17686 (yes, there are a few
missing).
When run as above, I get the proper ID from GetResInfo for "NFNT", but
without the string "NFNT", which I would expect in analogy to its
behaviour under "FOND". I just get the ID.
But, with dispose, I get a repetition of the info for the last "FOND"
resource, which got listed above, 20 times.
So, something surely is wrong, either way.
* is dispose the proper method? I've seen that Mac::Resources lists
ReleaseResource and DetachResource as well, but their purpose is far
from clear to me.
* Shouldn't Mac::Memory dispose of the memory all by itself, when the
variable containing the handle gets DESTROY-ed?
--=20
Bart.