source: branches/gsdl-2_70-distribution-branch/gsdl/perllib/cpan/rm/Header/PurePerl.pm@ 11817

Last change on this file since 11817 was 11817, checked in by kjdon, 18 years ago

copyright info added to branch version

  • Property svn:keywords set to Author Date Id Revision
File size: 5.5 KB
Line 
1###########################################################################
2#
3# Code by Xin Gao based on the Ogg::Vorbis::Header::PurePerl module by
4# Andrew Molloy (GNU General Public Licensed)
5#
6# A component of the Greenstone digital library software
7# from the New Zealand Digital Library Project at the
8# University of Waikato, New Zealand.
9#
10# Copyright (C) 2005 New Zealand Digital Library Project
11#
12# This program is free software; you can redistribute it and/or modify
13# it under the terms of the GNU General Public License as published by
14# the Free Software Foundation; either version 2 of the License, or
15# (at your option) any later version.
16#
17# This program is distributed in the hope that it will be useful,
18# but WITHOUT ANY WARRANTY; without even the implied warranty of
19# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20# GNU General Public License for more details.
21#
22# You should have received a copy of the GNU General Public License
23# along with this program; if not, write to the Free Software
24# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25#
26###########################################################################
27
28package rm::Header::PurePerl;
29
30use 5.005;
31use strict;
32use warnings;
33
34use Fcntl qw/SEEK_END/;
35
36our $VERSION = '0.07';
37
38sub new
39{
40 my $class = shift;
41 my $file = shift;
42
43 return load($class, $file);
44}
45
46sub load
47{
48 my $class = shift;
49 my $file = shift;
50 my $from_new = shift;
51 my %data;
52 my $self;
53
54 # there must be a better way...
55 if ($class eq 'rm::Header::PurePerl')
56 {
57 $self = bless \%data, $class;
58 }
59 else
60 {
61 $self = $class;
62 }
63
64 if ($self->{'FILE_LOADED'})
65 {
66 return $self;
67 }
68
69 $self->{'FILE_LOADED'} = 1;
70
71 # check that the file exists and is readable
72 unless ( -e $file && -r _ )
73 {
74 warn "File does not exist or cannot be read.";
75 # file does not exist, can't do anything
76 return undef;
77 }
78 # open up the file
79 open FILE, $file;
80 # make sure dos-type systems can handle it...
81 binmode FILE;
82
83 $data{'filename'} = $file;
84 $data{'fileHandle'} = \*FILE;
85
86 _loadInfo(\%data);
87
88 close FILE;
89
90 return $self;
91}
92
93sub info
94{
95 my $self = shift;
96 my $key = shift;
97
98 # if the user did not supply a key, return the entire hash
99 unless ($key)
100 {
101 return $self->{'INFO'};
102 }
103
104 # otherwise, return the value for the given key
105 return $self->{'INFO'}{lc $key};
106}
107
108sub _loadInfo
109{
110 my $data = shift;
111 my $start = 0;
112 my $fh = $data->{'fileHandle'};
113 my $buffer;
114 my $byteCount = $start;
115 my %info;
116
117 # check that the first four bytes are '.RMF'
118 read($fh, $buffer, 4);
119 if ($buffer ne '.RMF')
120 {
121 warn "No RMF header?";
122 return undef;
123 }
124
125$buffer='';
126my $char;
127
128#find the header
129my $bytes = "DATA";
130my @byteList = split //, $bytes;
131my $numBytes = @byteList;
132my $i;
133
134LINE: while (1){
135 INNER: for ($i = 0; $i < $numBytes; $i ++)
136 {
137 unless ( read($fh, $char, 1) ) {last LINE ;}
138 # Find out all of char
139 $buffer= $buffer.$char;
140
141if (ord($char) != ord($byteList[$i]) )
142{last INNER ;}
143 }
144if ($i == $numBytes) {last LINE ;} #jump out the while loop
145 }
146
147#find the tail
148 $bytes = "INDX";
149 @byteList = split //, $bytes;
150 $numBytes = @byteList;
151
152my $isrecord=0;
153LINE: while (read($fh, $char, 1)){
154 if ($isrecord) {
155 # Find out all of char
156 $buffer= $buffer.$char;
157 }else
158 {
159 INNER: for ($i = 0; $i < $numBytes; $i ++)
160 {
161 if (ord($char) != ord($byteList[$i]) ) {last INNER ;}
162 unless ( read($fh, $char, 1) ) {last LINE ;}
163 }
164if ($i == $numBytes) {$isrecord = 1;} #start record
165 }
166 }
167
168my @cliptype = (
169
170#add clip type here
171"Comments",
172"Keywords",
173"Category",
174"MimeType",# title
175"Lyrics",
176"Artist",
177"CD Track #",
178"Album",
179"Extension",
180"Genre",
181"Statistics",
182"PROP",
183"MDPR",
184"Target Audiences",
185"Audio Format",
186"Creation Date",
187"Modification Date",
188"Generated By",
189"Abstract",
190"Content Rating",
191"File ID",
192"CONT",
193"Audio Stream",
194"Video Stream",
195"Title"
196);
197
198for my $j ( 1 .. scalar(@cliptype) ) {
199$info{$cliptype[$j - 1]} = _loadInfor($buffer,$cliptype[$j - 1]);
200}
201
202 $data->{'INFO'} = \%info;
203}
204
205#search for the element name and value
206sub _loadInfor
207{my $data = shift;
208my $item = shift;
209
210my @byteList = split //, $data;
211my $startbyte = 0;
212
213
214my $isrecord;
215my $data2 = "";
216my $char;
217my $item2 = "";
218if ( $item eq "Title") {$item2 = $item; $item = "MimeType";}
219
220
221OUT: while(index($data, $item, $startbyte) != -1){
222$startbyte = index($data, $item, $startbyte);
223$isrecord=0;
224
225$startbyte += length($item);
226
227if(ord($byteList[$startbyte]) == 0 or ord($byteList[$startbyte]) == 0x14){
228 if ( $item eq "Album" or $item eq "Artist" or $item2 eq "Title"){
229 if ( index($data,"Name",$startbyte) != -1) {
230 $startbyte = index($data,"Name",$startbyte);
231 $startbyte += length("Name");
232 }else {next OUT;}
233}
234
235 if ($data2 ne "") {$data2 = $data2."; ";}
236
237LINE: while (1){
238 $char = $byteList[++$startbyte];
239
240 if (ord($char) >= 32 and ord($char) <= 126)
241 {
242 $isrecord=1; #record the string started
243 $data2 = $data2.$char;
244 }else{
245 if ( $isrecord == 1 ) {last LINE ;}# stop at the end of string
246 }
247 }# end LINE: while
248
249 }# end if
250}# end while
251
252return $data2;
253}
254
2551;
Note: See TracBrowser for help on using the repository browser.