source: trunk/gsdl/perllib/cpan/rm/Header/PurePerl.pm@ 10395

Last change on this file since 10395 was 10395, checked in by mdewsnip, 19 years ago

A plugin for RealMedia files. By Xin Gao for the 517 Digital Libraries course.

  • Property svn:keywords set to Author Date Id Revision
File size: 4.3 KB
Line 
1package rm::Header::PurePerl;
2
3use 5.005;
4use strict;
5use warnings;
6
7use Fcntl qw/SEEK_END/;
8
9our $VERSION = '0.07';
10
11sub new
12{
13 my $class = shift;
14 my $file = shift;
15
16 return load($class, $file);
17}
18
19sub load
20{
21 my $class = shift;
22 my $file = shift;
23 my $from_new = shift;
24 my %data;
25 my $self;
26
27 # there must be a better way...
28 if ($class eq 'rm::Header::PurePerl')
29 {
30 $self = bless \%data, $class;
31 }
32 else
33 {
34 $self = $class;
35 }
36
37 if ($self->{'FILE_LOADED'})
38 {
39 return $self;
40 }
41
42 $self->{'FILE_LOADED'} = 1;
43
44 # check that the file exists and is readable
45 unless ( -e $file && -r _ )
46 {
47 warn "File does not exist or cannot be read.";
48 # file does not exist, can't do anything
49 return undef;
50 }
51 # open up the file
52 open FILE, $file;
53 # make sure dos-type systems can handle it...
54 binmode FILE;
55
56 $data{'filename'} = $file;
57 $data{'fileHandle'} = \*FILE;
58
59 _loadInfo(\%data);
60
61 close FILE;
62
63 return $self;
64}
65
66sub info
67{
68 my $self = shift;
69 my $key = shift;
70
71 # if the user did not supply a key, return the entire hash
72 unless ($key)
73 {
74 return $self->{'INFO'};
75 }
76
77 # otherwise, return the value for the given key
78 return $self->{'INFO'}{lc $key};
79}
80
81sub _loadInfo
82{
83 my $data = shift;
84 my $start = 0;
85 my $fh = $data->{'fileHandle'};
86 my $buffer;
87 my $byteCount = $start;
88 my %info;
89
90 # check that the first four bytes are '.RMF'
91 read($fh, $buffer, 4);
92 if ($buffer ne '.RMF')
93 {
94 warn "No RMF header?";
95 return undef;
96 }
97
98$buffer='';
99my $char;
100
101#find the header
102my $bytes = "DATA";
103my @byteList = split //, $bytes;
104my $numBytes = @byteList;
105my $i;
106
107LINE: while (1){
108 INNER: for ($i = 0; $i < $numBytes; $i ++)
109 {
110 unless ( read($fh, $char, 1) ) {last LINE ;}
111 # Find out all of char
112 $buffer= $buffer.$char;
113
114if (ord($char) != ord($byteList[$i]) )
115{last INNER ;}
116 }
117if ($i == $numBytes) {last LINE ;} #jump out the while loop
118 }
119
120#find the tail
121 $bytes = "INDX";
122 @byteList = split //, $bytes;
123 $numBytes = @byteList;
124
125my $isrecord=0;
126LINE: while (read($fh, $char, 1)){
127 if ($isrecord) {
128 # Find out all of char
129 $buffer= $buffer.$char;
130 }else
131 {
132 INNER: for ($i = 0; $i < $numBytes; $i ++)
133 {
134 if (ord($char) != ord($byteList[$i]) ) {last INNER ;}
135 unless ( read($fh, $char, 1) ) {last LINE ;}
136 }
137if ($i == $numBytes) {$isrecord = 1;} #start record
138 }
139 }
140
141my @cliptype = (
142
143#add clip type here
144"Comments",
145"Keywords",
146"Category",
147"MimeType",# title
148"Lyrics",
149"Artist",
150"CD Track #",
151"Album",
152"Extension",
153"Genre",
154"Statistics",
155"PROP",
156"MDPR",
157"Target Audiences",
158"Audio Format",
159"Creation Date",
160"Modification Date",
161"Generated By",
162"Abstract",
163"Content Rating",
164"File ID",
165"CONT",
166"Audio Stream",
167"Video Stream",
168"Title"
169);
170
171for my $j ( 1 .. scalar(@cliptype) ) {
172$info{$cliptype[$j - 1]} = _loadInfor($buffer,$cliptype[$j - 1]);
173}
174
175 $data->{'INFO'} = \%info;
176}
177
178#search for the element name and value
179sub _loadInfor
180{my $data = shift;
181my $item = shift;
182
183my @byteList = split //, $data;
184my $startbyte = 0;
185
186
187my $isrecord;
188my $data2 = "";
189my $char;
190my $item2 = "";
191if ( $item eq "Title") {$item2 = $item; $item = "MimeType";}
192
193
194OUT: while(index($data, $item, $startbyte) != -1){
195$startbyte = index($data, $item, $startbyte);
196$isrecord=0;
197
198$startbyte += length($item);
199
200if(ord($byteList[$startbyte]) == 0 or ord($byteList[$startbyte]) == 0x14){
201 if ( $item eq "Album" or $item eq "Artist" or $item2 eq "Title"){
202 if ( index($data,"Name",$startbyte) != -1) {
203 $startbyte = index($data,"Name",$startbyte);
204 $startbyte += length("Name");
205 }else {next OUT;}
206}
207
208 if ($data2 ne "") {$data2 = $data2."; ";}
209
210LINE: while (1){
211 $char = $byteList[++$startbyte];
212
213 if (ord($char) >= 32 and ord($char) <= 126)
214 {
215 $isrecord=1; #record the string started
216 $data2 = $data2.$char;
217 }else{
218 if ( $isrecord == 1 ) {last LINE ;}# stop at the end of string
219 }
220 }# end LINE: while
221
222 }# end if
223}# end while
224
225return $data2;
226}
227
2281;
Note: See TracBrowser for help on using the repository browser.