root/Mac-Macbinary/trunk/Macbinary.pm

Revision 13 (checked in by miyagawa, 19 years ago)

modified document.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 package Mac::Macbinary;
2
3 use strict;
4 use vars qw($VERSION $AUTOLOAD);
5 $VERSION = '0.01';
6
7 use Carp;
8
9 use constant HEADER_SIZE        => 128;
10
11 sub new {
12     my ($class, $thingy) = @_;
13     my $self = bless { }, $class;
14
15     my $fh = _make_handle($thingy);
16     $self->_parse_handle($fh);
17     return $self;
18 }
19
20 sub _parse_handle {
21     my $self = shift;
22     my ($fh) = @_;
23
24     read $fh, my ($header), HEADER_SIZE;
25     $self->{header} = Mac::Macbinary::Header->new($header);
26     read $fh, $self->{data}, $self->header->dflen;
27     read $fh, $self->{resource}, $self->header->rflen;
28
29     return $self;
30 }
31
32 sub _make_handle($) {
33     my $thingy = shift;
34    
35     if (-f $thingy && ! ref($thingy)) {
36         require FileHandle;
37         my $fh = FileHandle->new($thingy) or Carp::croak "$thingy: $!";
38         return $fh;
39     } else {
40         return $thingy;
41     }
42 }       
43
44 sub AUTOLOAD {
45     my $self = shift;
46     $AUTOLOAD =~ s/.*://o;
47     return $self->{$AUTOLOAD};
48 }
49
50
51 package Mac::Macbinary::Header;
52
53 use vars qw($AUTOLOAD);
54
55 sub new {
56     my ($class, $h) = @_;
57     my $self = bless { }, $class;
58     $self->_parse_header($h);
59     return $self;
60 }
61
62 sub _parse_header {
63     my $self = shift;
64     my ($h) = @_;
65
66     $self->{name}       = unpack("A*", substr($h, 2, 63));
67     $self->{type}       = unpack("A*", substr($h, 65, 4));
68     $self->{creator}    = unpack("A*", substr($h, 69, 4));
69     $self->{flags}      = unpack("C", substr($h, 73, 1));
70     $self->{location}   = unpack("C", substr($h, 80, 6));
71     $self->{dflen}      = unpack("N", substr($h, 83, 4));
72     $self->{rflen}      = unpack("N", substr($h, 87, 4));
73     $self->{cdate}      = unpack("N", substr($h, 91, 4));
74     $self->{mdate}      = unpack("N", substr($h, 95, 4));
75
76     return $self;
77 }
78
79
80 sub AUTOLOAD {
81     my $self = shift;
82     $AUTOLOAD =~ s/.*://o;
83     return $self->{$AUTOLOAD};
84 }
85
86 1;
87 __END__
88
89 =head1 NAME
90
91 Mac::Macbinary - Macbinary Handler
92
93 =head1 SYNOPSIS
94
95   use Mac::Macbinary;
96
97   $mb = new Mac::Macbinary(\*FH);       # filehandle
98   $mb = new Mac::Macbinary($fh);        # IO::* instance
99   $mb = new Mac::Macbinary("/path/to/file");
100
101   $header = $mb->header;                # Mac::Macbinary::Header instance
102   $name = $header->name;
103  
104
105 =head1 DESCRIPTION
106
107 This module provides an object-oriented way to extract various kinds
108 of information from Macintosh Macbinary files.
109
110 =head1 METHODS
111
112 Following methods are available.
113
114 =head2 Class method
115
116 =over 4
117
118 =item new(I<THINGY>)
119
120 Constructor of Mac::Macbinary. Accepts filhandle GLOB reference,
121 FileHandle instance, IO::* instance, or whatever objects that can do
122 C<read> methods.
123
124 If the argument belongs none of those above, C<new()> treats it as a
125 path to file. Any of following examples are valid constructors.
126
127   open FH, "path/to/file";
128   $mb = new Mac::MacBinary(\*FH);
129
130   $fh = new FileHandle "path/to/file";
131   $mb = new Mac::MacBinary($fh);
132
133   $io = new IO::File "path/to/file";
134   $mb = new Mac::MacBinary($io);
135
136   $mb = new Mac::MacBinary "path/to/file";
137
138 =back
139
140 =head2 Instance Method
141
142 =over 4
143
144 =item data
145
146 returns the data range of original file.
147
148 =item header
149
150 returns the header object (instance of Mac::MacBinary::Header).
151
152 =back
153
154 Following accessors are available via Mac::Macbinary instance.
155
156 =over 4
157
158 =item name, type, creator, flags, location, dflen, rflen, cdate, mdate
159
160 returns the original entry in the header of Macbinary file.
161 Below is a structure of the info file, taken from MacBin.C
162
163   char zero1;
164   char nlen;
165   char name[63];
166   char type[4];           65      0101
167   char creator[4];        69
168   char flags;             73
169   char zero2;             74      0112
170   char location[6];       80
171   char protected;         81      0121
172   char zero3;             82      0122
173   char dflen[4];
174   char rflen[4];
175   char cdate[4];
176   char mdate[4];
177
178 =back
179
180 =head1 AUTHOR
181
182 Tatsuhiko Miyagawa <miyagawa@edge.co.jp>
183 Originally written by Dan Kogai <dankogai@dan.co.jp>
184
185 =head1 SEE ALSO
186
187 perl(1).
188
189 =cut
Note: See TracBrowser for help on using the browser.