File/MicroLIF.pm

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