File/MicroLIF.pm

filter: Statementstmt 97.3% 108 / 111 Branchbran 84.7% 39 / 46 Conditioncond 82.3% 14 / 17 MC/DCmcdc 63.6% 7 / 11 Subroutinesub 100% 15 / 15 Podpod 100% 3 / 3 totaltotal 91.6% 186 / 203 CC 32 SCAR 20.9
CC
32
Cov
93%
_get_chunk
13.1
decode
12.0
_fill_buffer
5.3
CRAP 8.1
« File/USMARC.pm ↑ Summary File.pm »
1 package MARC::File::MicroLIF;
2  
3 =head1 NAME
4  
5 MARC::File::MicroLIF - MicroLIF-specific file handling
6  
7 =cut
8  
9 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 8 ▶use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 8 ▶use vars qw( $ERROR );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 8 ▶use MARC::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 8 ▶use vars qw( @ISA ); @ISA = qw( MARC::File );
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 8 ▶use MARC::Record qw( LEADER_LEN );
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 =head1 SYNOPSIS
20  
21 use MARC::File::MicroLIF;
22  
23 my $file = MARC::File::MicroLIF->in( $filename );
24  
25 while ( my $marc = $file->next() ) {
26 # Do something
27 }
28 $file->close();
29 undef $file;
30  
31 =head1 EXPORT
32  
33 None.
34  
35 =cut
36  
37  
38 =for internal
39  
40 The buffer must be large enough to handle any valid record because
41 we don't check for cases like a CR/LF pair or an end-of-record/CR/LF
42 trio being only partially in the buffer.
43  
44 The max valid record is the max MARC record size (99999) plus one
45 or two characters per tag (CR, LF, or CR/LF). It's hard to say
46 what the max number of tags is, so here we use 6000. (6000 tags
47 can be squeezed into a MARC record only if every tag has only one
48 subfield containing a maximum of one character, or if data from
49 multiple tags overlaps in the MARC record body. We're pretty safe.)
50  
51 =cut
52  
53 8 ▶use constant BUFFER_MIN => (99999 + 6000 * 2);
Subroutine BEGIN: called CC=1 SCAR=0.0
54  
55 =head1 METHODS
56  
57 =head2 in()
58  
59 Opens a MicroLIF file for reading.
60  
61 =cut
62  
63 14 ▶sub in {
Subroutine in: called CC=4 SCAR=14.0
Pod documented
64 14 my $class = shift;
65 14 my $self = $class->SUPER::in( @_ );
66  
67 14 ▶ if ( $self ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self) 14 0 14
68 14 bless $self, $class;
69  
70 14 $self->{exhaustedfh} = 0;
71 14 $self->{inputbuf} = '';
72 14 $self->{header} = undef;
73  
74 # get the MicroLIF header, but handle the case in
75 # which it's missing.
76 14 my $header = $self->_get_chunk( 1 );
77 14 ▶ if ( defined $header ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $header) { } 14 0 14
78 14 ▶ if ( $header =~ /^LDR/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($header =~ /^LDR/) { } 2 12 14
79 # header missing, put this back
80 2 $self->_unget_chunk( $header . "\n" );
81  
82 # XXX should we warn of a missing header?
83 }
84 else {
85 12 $self->{header} = $header;
86 }
87 }
88 else {
89 # can't read from the file
90 0 undef $self;
91 }
92 }
93  
94 14 return $self;
95 } # new
96  
97  
98 # fill the buffer if we need to
99 247 ▶sub _fill_buffer {
Subroutine _fill_buffer: called CC=5 SCAR=16.6
100 247 my $self = shift;
101 247 my $ok = 1;
102  
103 247 ▶ if ( !$self->{exhaustedfh} && length( $self->{inputbuf} ) < BUFFER_MIN ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"exhaustedfh"} and length $self->{"inputbuf"} < 111999) 26 221 247
Condition66%2/3
not $self->{"exhaustedfh"} and length $self->{"inputbuf"} < 111999
!ll&&!rl&&r
221026
MC/DC50%1/2
not $self->{"exhaustedfh"} and length $self->{"inputbuf"} < 111999
not $self->{"exhaustedfh"} length $self->{"inputbuf"} < 111999
Truth table66%2/3
not(A) and B
A= not $self->{"exhaustedfh"}B= length $self->{"inputbuf"} < 111999
ABresult
0X0
100
111
104 # append the next chunk of bytes to the buffer
105 26 my $read = read $self->{fh}, $self->{inputbuf}, BUFFER_MIN, length($self->{inputbuf});
106 26 ▶ if ( !defined $read ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $read) { } 0 26 26
elsif ($read < 1) { } 12 14 26
107 # error!
108 0 $ok = undef;
109 0 $MARC::File::ERROR = "error reading from file " . $self->{filename};
110 }
111 elsif ( $read < 1 ) {
112 12 $self->{exhaustedfh} = 1;
113 }
114 }
115  
116 247 return $ok;
117 }
118  
119  
120 =for internal
121  
122 Gets the next chunk of data. If C<$want_line> is true then you get
123 the next chunk ending with any combination of \r and \n of any length.
124 If it is false or not passed then you get the next chunk ending with
125 \x60 followed by any combination of \r and \n of any length.
126  
127 All trailing \r and \n are stripped.
128  
129 =cut
130  
131 247 ▶sub _get_chunk {
Subroutine _get_chunk: called CC=13 SCAR=25.7
132 247 my $self = shift;
133 247 ▶ my $want_line = shift || 0;
Condition100%2/2
shift() || 0
l!l
14233
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
134  
135 247 my $chunk = undef;
136  
137 247 ▶ if ( $self->_fill_buffer() && length($self->{inputbuf}) > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_fill_buffer and length $self->{"inputbuf"} > 0) 239 8 247
Condition66%2/3
$self->_fill_buffer and length $self->{"inputbuf"} > 0
!ll&&!rl&&r
08239
MC/DC50%1/2
$self->_fill_buffer and length $self->{"inputbuf"} > 0
$self->_fill_buffer length $self->{"inputbuf"} > 0
Truth table66%2/3
A and B
A= $self->_fill_bufferB= length $self->{"inputbuf"} > 0
ABresult
0X0
100
111
138  
139 # the buffer always has at least one full line in it, so we're
140 # guaranteed that if there are no line endings then we're
141 # on the last line.
142  
143 239 ▶ if ( $want_line ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($want_line) { } 14 225 239
144 14 ▶ if ( $self->{inputbuf} =~ /^([^\x0d\x0a]*)([\x0d\x0a]+)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"inputbuf"} =~ /^([^\x0d\x0a]*)([\x0d\x0a]+)/) 14 0 14
145 14 $chunk = $1;
146 14 $self->{inputbuf} = substr( $self->{inputbuf}, length($1)+length($2) );
147 }
148 }
149 else {
150 # couldn't figure out how to make this work as a regex
151 225 my $pos = -1;
152 225 ▶ while ( !$chunk ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($chunk) 225 224 449
153 225 $pos = index( $self->{inputbuf}, '`', $pos+1 );
154 225 ▶ last if $pos < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $pos < 0 1 224 225
155 224 ▶ if ( substr($self->{inputbuf}, $pos+1, 1) eq "\x0d" or substr($self->{inputbuf}, $pos+1, 1) eq "\x0a" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($self->{"inputbuf"}, $pos + 1, 1) eq "\r" or substr($self->{"inputbuf"}, $pos + 1, 1) eq "\n") 224 0 224
Condition66%2/3
substr($self->{"inputbuf"}, $pos + 1, 1) eq "\r" or substr($self->{"inputbuf"}, $pos + 1, 1) eq "\n"
l!l&&r!l&&!r
22220
MC/DC0%0/2
substr($self->{"inputbuf"}, $pos + 1, 1) eq "\r" or substr($self->{"inputbuf"}, $pos + 1, 1) eq "\n"
substr($self->{"inputbuf"}, $pos + 1, 1) eq "\r" substr($self->{"inputbuf"}, $pos + 1, 1) eq "\n"
Truth table66%2/3
A or B
A= substr($self->{"inputbuf"}, $pos + 1, 1) eq "\r"B= substr($self->{"inputbuf"}, $pos + 1, 1) eq "\n"
ABresult
1X1
011
000
156 224 $chunk = substr( $self->{inputbuf}, 0, $pos+1 ); # include the '`' but not the newlines
157 224 ▶ while ( substr($self->{inputbuf}, $pos+1, 1) eq "\x0d" or substr($self->{inputbuf}, $pos+1, 1) eq "\x0a" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($self->{"inputbuf"}, $pos + 1, 1) eq "\r" or substr($self->{"inputbuf"}, $pos + 1, 1) eq "\n") 225 224 449
Condition100%3/3
substr($self->{"inputbuf"}, $pos + 1, 1) eq "\r" or substr($self->{"inputbuf"}, $pos + 1, 1) eq "\n"
l!l&&r!l&&!r
2223224
MC/DC100%2/2
substr($self->{"inputbuf"}, $pos + 1, 1) eq "\r" or substr($self->{"inputbuf"}, $pos + 1, 1) eq "\n"
substr($self->{"inputbuf"}, $pos + 1, 1) eq "\r" substr($self->{"inputbuf"}, $pos + 1, 1) eq "\n"
Truth table100%3/3
A or B
A= substr($self->{"inputbuf"}, $pos + 1, 1) eq "\r"B= substr($self->{"inputbuf"}, $pos + 1, 1) eq "\n"
ABresult
1X1
011
000
158 225 ++$pos;
159 }
160 # $pos now pointing at last newline char
161 224 $self->{inputbuf} = substr( $self->{inputbuf}, $pos+1 );
162 }
163 }
164 }
165  
166 239 ▶ if ( !$chunk ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($chunk) 1 238 239
167 1 $chunk = $self->{inputbuf};
168 1 $self->{inputbuf} = '';
169 1 $self->{exhaustedfh} = 1;
170 }
171 }
172  
173 247 return $chunk;
174 }
175  
176  
177 # $chunk is put at the beginning of the buffer exactly as
178 # passed in. No line endings are added.
179 2 ▶sub _unget_chunk {
Subroutine _unget_chunk: called CC=1 SCAR=0.0
180 2 my $self = shift;
181 2 my $chunk = shift;
182 2 $self->{inputbuf} = $chunk . $self->{inputbuf};
183 2 return;
184 }
185  
186  
187 233 ▶sub _next {
Subroutine _next: called CC=2 SCAR=6.9
188 233 my $self = shift;
189  
190 233 my $lifrec = $self->_get_chunk();
191  
192 # for ease, make the newlines match this platform
193 233 ▶ $lifrec =~ s/[\x0a\x0d]+/\n/g if defined $lifrec;
Branch: 1 branch
BranchTrueFalseTotal
if defined $lifrec 225 8 233
194  
195 233 return $lifrec;
196 }
197  
198  
199 =head2 header()
200  
201 If the MicroLIF file has a file header then the header is returned.
202 If the file has no header or the file has not yet been opened then
203 C<undef> is returned.
204  
205 =cut
206  
207 2 ▶sub header {
Subroutine header: called CC=1 SCAR=0.0
Pod documented
208 2 my $self = shift;
209 2 return $self->{header};
210 }
211  
212 =head2 decode()
213  
214 Decodes a MicroLIF record and returns a USMARC record.
215  
216 Can be called in one of three different ways:
217  
218 $object->decode( $lif )
219 MARC::File::MicroLIF->decode( $lif )
220 MARC::File::MicroLIF::decode( $lif )
221  
222 =cut
223  
224 232 ▶sub decode {
Subroutine decode: called CC=12 SCAR=24.9
Pod documented
225 232 my $self = shift;
226 232 my $location = '';
227 232 my $text = '';
228  
229 ## decode can be called in a variety of ways
230 ## this bit of code covers all three
231  
232 232 ▶ if ( ref($self) =~ /^MARC::File/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref($self) =~ /^MARC::File/) { } 225 7 232
233 225 $location = 'in record '.$self->{recnum};
234 225 $text = shift;
235 } else {
236 7 $location = 'in record 1';
237 7 ▶ $text = $self=~/MARC::File/ ? shift : $self;
Branch: 1 branch
BranchTrueFalseTotal
$self =~ /MARC::File/ ? : 3 4 7
238 }
239  
240 232 my $marc = MARC::Record->new();
241  
242 # for ease, make the newlines match this platform
243 232 ▶ $text =~ s/[\x0a\x0d]+/\n/g if defined $text;
Branch: 1 branch
BranchTrueFalseTotal
if defined $text 232 0 232
244  
245 232 my @lines = split( /\n/, $text );
246 232 for my $line ( @lines ) {
247  
248 2335 ▶ ($line =~ s/^([0-9A-Za-z]{3})//) or
Branch: 1 branch
BranchTrueFalseTotal
$line =~ s/^([0-9A-Za-z]{3})// or $marc->_warn('Invalid tag number: ' . substr($line, 0, 3) . " $location") 2335 0 2335
249 $marc->_warn( "Invalid tag number: ".substr( $line, 0, 3 )." $location" );
250 2335 my $tagno = $1;
251  
252 2335 ▶ ($line =~ s/\^`?$//)
Branch: 1 branch
BranchTrueFalseTotal
$line =~ s/\^`?$// or $marc->_warn("Tag $tagno $location is missing a trailing caret.") 2325 10 2335
253 or $marc->_warn( "Tag $tagno $location is missing a trailing caret." );
254  
255 2335 ▶ if ( $tagno eq "LDR" ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($tagno eq 'LDR') { } 232 2103 2335
elsif ($tagno =~ /^\d+$/ and $tagno < 10) { } 289 1814 2103
256 232 $marc->leader( substr( $line, 0, LEADER_LEN ) );
257 ▶ } elsif ( $tagno =~ /^\d+$/ and $tagno < 10 ) {
Condition100%3/3
$tagno =~ /^\d+$/ and $tagno < 10
!ll&&!rl&&r
71807289
MC/DC100%2/2
$tagno =~ /^\d+$/ and $tagno < 10
$tagno =~ /^\d+$/ $tagno < 10
Truth table100%3/3
A and B
A= $tagno =~ /^\d+$/B= $tagno < 10
ABresult
0X0
100
111
258 289 $marc->add_fields( $tagno, $line );
259 } else {
260 1814 $line =~ s/^(.)(.)//;
261 1814 my ($ind1,$ind2) = ($1,$2);
262 1814 my @subfields;
263 1814 my @subfield_data_pairs = split( /_(?=[a-z0-9])/, $line );
264 1814 ▶ if ( scalar @subfield_data_pairs < 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @subfield_data_pairs < 2) { } 8 1806 1814
265 8 $marc->_warn( "Tag $tagno $location has no subfields--discarded." );
266 }
267 else {
268 1806 shift @subfield_data_pairs; # Leading _ makes an empty pair
269 1806 for my $pair ( @subfield_data_pairs ) {
270 3020 my ($subfield,$data) = (substr( $pair, 0, 1 ), substr( $pair, 1 ));
271 3020 push( @subfields, $subfield, $data );
272 }
273 1806 $marc->add_fields( $tagno, $ind1, $ind2, @subfields );
274 }
275 }
276 } # for
277  
278 232 return $marc;
279 }
280  
281 8 1;
282  
283 __END__
« File/USMARC.pm ↑ Summary File.pm »