WARC/Record/Logical/Block.pm

filter: Statementstmt 99.4% 168 / 169 Branchbran 97.6% 84 / 86 Conditioncond 91.4% 32 / 35 MC/DCmcdc 72.2% 13 / 18 Subroutinesub 100% 21 / 21 Podpod n/a totaltotal 96.6% 318 / 329 CC 57 SCAR 27.8
CC
57
Cov
98%
READLINE
32.0
READ
9.0
SEEK
9.0
CRAP 16.1
« WARC/Record/Logical.pm ↑ Summary WARC/Record/Logical/Heuristics.pm »
1 package WARC::Record::Logical::Block; # -*- CPerl -*-
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 our @ISA = qw();
7  
8 2 ▶use WARC; *WARC::Record::Logical::Block::VERSION = \$WARC::VERSION;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Fcntl qw/SEEK_SET SEEK_CUR SEEK_END/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 # This implementation uses an array as the underlying object.
14  
15 2 ▶use constant { PARENT => 0, SEGMENT => 1, HANDLE => 2 };
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use constant OBJECT_INIT => undef, 0, undef;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 # Invariant: HANDLE is always valid if SEGMENT is within range.
19  
20 2 ▶BEGIN { require WARC::Record::Logical; }
Subroutine BEGIN: called CC=1 SCAR=0.0
21 BEGIN { $WARC::Record::Logical::Block::{$_} = $WARC::Record::Logical::{$_}
22 2 ▶ for WARC::Record::Logical::SEGMENT_INDEX; }
Subroutine BEGIN: called CC=2 SCAR=6.9
23  
24 sub _dbg_dump {
25 62 ▶ my $self = shift;
Subroutine _dbg_dump: called CC=4 SCAR=14.0
26  
27 62 my $out = 'logical record block';
28 $out .= ' @['.($self->[SEGMENT]).' / '
29 62 .($#{$self->[PARENT]{segments}}).']';
30 62 ▶ $out .= ' [EOF]' if $self->[SEGMENT] > $#{$self->[PARENT]{segments}};
Branch: 1 branch
BranchTrueFalseTotal
if $self->[1] > $#{$$self[0]{"segments"};} 14 48 62
31 62 $out .= "\n";
32 0 $out .= ' '.((tied *{$self->[HANDLE]})->_dbg_dump)
33 62 ▶ if $self->[HANDLE] && UNIVERSAL::can(tied *{$self->[HANDLE]}, '_dbg_dump');
Branch: 1 branch
BranchTrueFalseTotal
if $self->[2] and UNIVERSAL::can(tied *{$self->[2];}, "_dbg_dump") 0 62 62
Condition66%2/3
$self->[2] and UNIVERSAL::can(tied *{$self->[2];}, "_dbg_dump")
!ll&&!rl&&r
5570
MC/DC0%0/2
$self->[2] and UNIVERSAL::can(tied *{$self->[2];}, "_dbg_dump")
$self->[2] UNIVERSAL::can(tied *{$self->[2];}, "_dbg_dump")
Truth table66%2/3
A and B
A= $self->[2]B= UNIVERSAL::can(tied *{$self->[2];}, "_dbg_dump")
ABresult
0X0
100
111
34  
35 62 return $out;
36 }
37  
38 sub TIEHANDLE {
39 22 ▶ my $class = shift;
Subroutine TIEHANDLE: called CC=1 SCAR=0.0
40 22 my $parent = shift;
41  
42 22 my $ob = [OBJECT_INIT];
43 22 $ob->[PARENT] = $parent;
44 22 $ob->[HANDLE] = $parent->{segments}[0][SEG_REC]->open_block;
45  
46 22 bless $ob, $class;
47 }
48  
49 # advance to next segment; return false if already at last segment
50 sub _next_segment {
51 79 ▶ my $self = shift;
Subroutine _next_segment: called CC=2 SCAR=6.9
52  
53 79 ▶ unless ($self->[PARENT]{segments}[1+$self->[SEGMENT]])
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->[0]{"segments"}[1 + $self->[1]]) 16 63 79
54 16 { $self->[SEGMENT]++; return 0 }
55  
56 63 close $self->[HANDLE];
57 $self->[HANDLE] =
58 63 $self->[PARENT]{segments}[++$self->[SEGMENT]][SEG_REC]->open_block;
59 63 return $self->[HANDLE];
60 }
61  
62 sub READLINE {
63 63 ▶ my $self = shift;
Subroutine READLINE: called CC=32 SCAR=34.7
64  
65 63 ▶ if (wantarray) { # data slurp; we might run out of memory...
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 1 62 63
66 1 my @data = ();
67 1 ▶ while (defined(my $line = $self->READLINE())) { push @data, $line }
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = $self->READLINE)) 4 1 5
68 return @data
69 1 }
70  
71 62 ▶ return undef if $self->[SEGMENT] > $#{$self->[PARENT]{segments}};
Branch: 1 branch
BranchTrueFalseTotal
if $self->[1] > $#{$$self[0]{"segments"};} 5 57 62
72  
73 57 ▶ if (not defined $/) { # file slurp; we might run out of memory...
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $/) { } 4 53 57
elsif (ref $/) { } 10 43 53
elsif ($/ eq '') { } 11 32 43
74 4 my $data = readline $self->[HANDLE];
75 4 ▶ $data = '' unless defined $data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 2 2 4
76 4 ▶ $data .= readline $self->[HANDLE] while $self->_next_segment;
Branch: 1 branch
BranchTrueFalseTotal
$self->_next_segment and do { $data .= readline $self->[2] } 3 4 7
77 4 ▶ return (length($data) ? $data : undef);
Branch: 1 branch
BranchTrueFalseTotal
length $data ? : 3 1 4
78 } elsif (ref $/) { # record read; fill a block...
79 2 ▶ use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
80 10 my $rec_len = 0+${$/};
81 10 my $rec = readline $self->[HANDLE];
82 10 ▶ $rec = '' unless defined $rec;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rec 3 7 10
83 10 ▶ while (length $rec < $rec_len) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $rec < $rec_len) 9 9 18
84 9 local $/ = \(do {$rec_len - length $rec});
85 9 ▶ last unless $self->_next_segment;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_next_segment 1 8 9
86 8 $rec .= readline $self->[HANDLE];
87 }
88 10 ▶ return (length ($rec) ? $rec : undef);
Branch: 1 branch
BranchTrueFalseTotal
length $rec ? : 9 1 10
89 } elsif ($/ eq '') { # paragraph read; delimiter is empty line...
90 11 my $para = '';
91 11 my $input = readline $self->[HANDLE];
92  
93 11 ▶ while (((defined $input and $para .= $input) or $self->_next_segment)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $input && ($para .= $input) || $self->_next_segment and "\n\n" ne substr($para, -2)) 13 11 24
Condition88%8/9
defined $input && ($para .= $input) || $self->_next_segment and "\n\n" ne substr($para, -2)
!ll&&!rl&&r
2913
defined $input and $para .= $input or $self->_next_segment
l!l&&r!l&&!r
1662
defined $input and $para .= $input
!ll&&!rl&&r
8016
MC/DC75%3/4
defined $input && ($para .= $input) || $self->_next_segment and "\n\n" ne substr($para, -2)
defined $input $para .= $input $self->_next_segment "\n\n" ne substr($para, -2)
Truth table50%4/8
A and B or C and D
A= defined $inputB= $para .= $inputC= $self->_next_segmentD= "\n\n" ne substr($para, -2)
ABCDresult
0X0X0
100X0
11X00
0X100
10100
11X11
0X111
10111
94 and ("\n\n" ne substr $para, -2))
95 # segment boundary was in the middle of a line, continuing read is safe
96 13 { $input = readline $self->[HANDLE] }
97 # paragraph delimiter may or may not span segments...
98 11 ▶ if (eof $self->[HANDLE]) {
Branch: 1 branch
BranchTrueFalseTotal
if (eof $self->[2]) 6 5 11
99 # next segment may begin with newlines, if so, they must be read
100 6 my $ch; my $end_pos = $self->TELL;
101 6 ▶ $end_pos = $self->TELL, $para .= "\n"
Branch: 1 branch
BranchTrueFalseTotal
defined($ch = $self->GETC) and $ch eq "\n" and do { $end_pos = $self->TELL, $para .= "\n" } 2 6 8
Condition100%3/3
defined($ch = $self->GETC) and $ch eq "\n"
!ll&&!rl&&r
332
MC/DC100%2/2
defined($ch = $self->GETC) and $ch eq "\n"
defined($ch = $self->GETC) $ch eq "\n"
Truth table100%3/3
A and B
A= defined($ch = $self->GETC)B= $ch eq "\n"
ABresult
0X0
100
111
102 while (defined ($ch = $self->GETC) and $ch eq "\n");
103 6 $self->SEEK($end_pos, SEEK_SET);
104 6 ▶ $input = defined($ch) ? '' : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $ch ? : 3 3 6
105 }
106 11 ▶ return (length ($para) ? $para : undef);
Branch: 1 branch
BranchTrueFalseTotal
length $para ? : 10 1 11
107 } else { # ordinary line read...
108 2 ▶ use bytes; # a line delimiter may be split on any octet boundary
Subroutine BEGIN: called CC=1 SCAR=0.0
109 32 my $line = '';
110 32 my $input = readline $self->[HANDLE];
111  
112 # read more data until we have a complete line or reach EOF
113 32 ▶ while (((defined $input and $line .= $input) or $self->_next_segment)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $input && ($line .= $input) || $self->_next_segment and $/ ne substr($line, -length($/))) 51 27 78
Condition88%8/9
defined $input && ($line .= $input) || $self->_next_segment and $/ ne substr($line, -length($/))
!ll&&!rl&&r
52251
defined $input and $line .= $input or $self->_next_segment
l!l&&r!l&&!r
50235
defined $input and $line .= $input
!ll&&!rl&&r
28050
MC/DC75%3/4
defined $input && ($line .= $input) || $self->_next_segment and $/ ne substr($line, -length($/))
defined $input $line .= $input $self->_next_segment $/ ne substr($line, -length($/))
Truth table50%4/8
A and B or C and D
A= defined $inputB= $line .= $inputC= $self->_next_segmentD= $/ ne substr($line, -length($/))
ABCDresult
0X0X0
100X0
11X00
0X100
10100
11X11
0X111
10111
114 and ($/ ne substr $line, -length $/)) {
115 51 ▶ if (length $/ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $/ > 1) 22 29 51
116 # each number N in this array indicates that a length-N prefix of
117 # $/ currently matches at the end of the line buffer
118 my @prefixes =
119 22 grep {substr($/, 0, $_) eq substr($line, -$_)} 1 .. length $/;
120 22 ▶ while (@prefixes) {
Branch: 1 branch
BranchTrueFalseTotal
if (@prefixes) 39 17 56
121 39 my $count = $self->READ($input, 1);
122 39 ▶ return $line unless $count;
Branch: 1 branch
BranchTrueFalseTotal
unless $count 1 38 39
123 38 $line .= $input;
124 38 ▶ return $line if $/ eq substr $line, -length $/;
Branch: 1 branch
BranchTrueFalseTotal
if $/ eq substr($line, -length($/)) 4 34 38
125 34 unshift @prefixes, 0; @prefixes = # sieve prefixes
126 34 grep {++$_; substr($/, 0, $_) eq substr($line, -$_)} @prefixes;
127 } # loop iterates until no prefix of $/ matches the tail of $line
128 }
129  
130 46 $input = readline $self->[HANDLE];
131 }
132 27 ▶ return $input unless length $line;
Branch: 1 branch
BranchTrueFalseTotal
unless length $line 2 25 27
133 25 return $line;
134 }
135 }
136  
137 # This sub must rely on the aliasing effect of @_.
138 sub READ {
139 70 ▶ my $self = shift;
Subroutine READ: called CC=9 SCAR=22.0
140 # args now: 0: buffer 1: length 2: offset into buffer or undef
141 70 my $length = $_[1];
142 70 ▶ my $offset = $_[2] || 0;
Condition100%2/2
$_[2] || 0
l!l
169
MC/DC100%1/1
$_[2] || 0
$_[2]
Truth table100%2/2
A
A= $_[2]
Aresult
11
00
143  
144 70 ▶ return 0 if $self->[SEGMENT] > $#{$self->[PARENT]{segments}};
Branch: 1 branch
BranchTrueFalseTotal
if $self->[1] > $#{$$self[0]{"segments"};} 2 68 70
145  
146 68 my $buf = ''; my $count = 1; my $bpos = 0;
147 68 ▶ while ($length && ($count || $self->_next_segment)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length and $count || $self->_next_segment) 99 68 167
Condition100%6/6
$length and $count || $self->_next_segment
!ll&&!rl&&r
641984
$count or $self->_next_segment
l!l&&r!l&&!r
76234
MC/DC66%2/3
$length and $count || $self->_next_segment
$length $count $self->_next_segment
Truth table75%3/4
A and B or C
A= $lengthB= $countC= $self->_next_segment
ABCresult
0XX0
1000
11X1
1011
148 99 $count = read $self->[HANDLE], $buf, $length, $bpos;
149 99 ▶ return undef unless defined $count;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $count 0 99 99
150 99 $length -= $count; $bpos += $count;
151 }
152  
153 68 ▶ $_[0] = '' unless defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 21 47 68
154 68 ▶ $_[0] .= "\0" x ($offset - length($_[0])) if $offset > length $_[0];
Branch: 1 branch
BranchTrueFalseTotal
if $offset > length $_[0] 1 67 68
155 68 substr $_[0], $offset, (length($_[0]) - $offset), $buf;
156 68 return $bpos;
157 }
158  
159 sub GETC {
160 22 ▶ my $self = shift;
Subroutine GETC: called CC=2 SCAR=6.9
161  
162 22 my $ch;
163 22 ▶ return undef unless $self->READ($ch, 1);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->READ($ch, 1) 4 18 22
164 18 return $ch;
165 }
166  
167 sub EOF {
168 16 ▶ my $self = shift;
Subroutine EOF: called CC=3 SCAR=11.0
169  
170 16 ▶ return 1 if $self->[SEGMENT] > $#{$self->[PARENT]{segments}};
Branch: 1 branch
BranchTrueFalseTotal
if $self->[1] > $#{$$self[0]{"segments"};} 14 2 16
171 2 ▶ return 0 if $self->[SEGMENT] < $#{$self->[PARENT]{segments}};
Branch: 1 branch
BranchTrueFalseTotal
if $self->[1] < $#{$$self[0]{"segments"};} 1 1 2
172 1 return eof $self->[HANDLE];
173 }
174  
175 sub SEEK {
176 12 ▶ my $self = shift;
Subroutine SEEK: called CC=9 SCAR=22.0
177 12 my $offset = shift;
178 12 my $whence = shift;
179  
180 12 my $segments = $self->[PARENT]{segments};
181  
182 12 my $npos;
183  
184 12 ▶ if ($whence == SEEK_SET) { $npos = $offset }
Branch: 3 branches
BranchTrueFalseTotal
if ($whence == 0) { } 8 4 12
elsif ($whence == 1) { } 1 3 4
elsif ($whence == 2) { } 2 1 3
185 1 elsif ($whence == SEEK_CUR) { $npos = $self->TELL + $offset }
186 elsif ($whence == SEEK_END)
187 2 { $npos = ($segments->[-1][SEG_BASE]
188 + $segments->[-1][SEG_LENGTH] + $offset) }
189 1 else { croak "unknown WHENCE $whence in call to seek" }
190  
191 # This function must be able to seek backwards, even if the underlying
192 # handles cannot, to support paragraph reads. Seeking generally requires
193 # finding the new segment, switching to that segment, and seeking
194 # forwards to the desired offset within the new segment.
195  
196 # Special handling for seek to or past end-of-file
197 11 ▶ if ($npos >= ($segments->[-1][SEG_BASE] + $segments->[-1][SEG_LENGTH])) {
Branch: 1 branch
BranchTrueFalseTotal
if ($npos >= $segments->[-1][2] + $segments->[-1][1]) 4 7 11
198 4 $self->[SEGMENT] = @$segments;
199 4 close $self->[HANDLE]; $self->[HANDLE] = undef;
200 4 return 1;
201 }
202  
203 7 my $new_segment_index = @$segments;
204 7 ▶ for (my $i = 0; $i < $new_segment_index; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $new_segment_index) 23 7 30
205 23 ▶ $new_segment_index = $i # which also exits this loop
Branch: 1 branch
BranchTrueFalseTotal
if $npos >= $segments->[$i][2] and $npos < $segments->[$i][2] + $segments->[$i][1] 6 17 23
Condition100%3/3
$npos >= $segments->[$i][2] and $npos < $segments->[$i][2] + $segments->[$i][1]
!ll&&!rl&&r
2156
MC/DC100%2/2
$npos >= $segments->[$i][2] and $npos < $segments->[$i][2] + $segments->[$i][1]
$npos >= $segments->[$i][2] $npos < $segments->[$i][2] + $segments->[$i][1]
Truth table100%3/3
A and B
A= $npos >= $segments->[$i][2]B= $npos < $segments->[$i][2] + $segments->[$i][1]
ABresult
0X0
100
111
206 if $npos >= $segments->[$i][SEG_BASE]
207 && $npos < ($segments->[$i][SEG_BASE] + $segments->[$i][SEG_LENGTH]);
208 }
209  
210 7 my $new_segment = $segments->[$new_segment_index];
211 7 ▶ return 0 unless $new_segment;
Branch: 1 branch
BranchTrueFalseTotal
unless $new_segment 1 6 7
212  
213 6 $self->[SEGMENT] = $new_segment_index;
214 6 close $self->[HANDLE];
215 6 $self->[HANDLE] = $new_segment->[SEG_REC]->open_block;
216 6 seek $self->[HANDLE], $npos - $new_segment->[SEG_BASE], SEEK_SET;
217 }
218  
219 sub TELL {
220 15 ▶ my $self = shift;
Subroutine TELL: called CC=2 SCAR=6.9
221  
222 15 my $segments = $self->[PARENT]{segments};
223  
224 return ($segments->[$self->[SEGMENT]][SEG_BASE] + (tell $self->[HANDLE]))
225 15 ▶ if $self->[SEGMENT] <= $#{$segments};
Branch: 1 branch
BranchTrueFalseTotal
if $self->[1] <= $#{$segments;} 12 3 15
226 3 return ($segments->[-1][SEG_BASE] + $segments->[-1][SEG_LENGTH]);
227 }
228  
229 sub CLOSE {
230 1 ▶ my $self = shift;
Subroutine CLOSE: called CC=1 SCAR=0.0
231 1 $self->[SEGMENT] = 1+$#{$self->[PARENT]{segments}};
232 1 close $self->[HANDLE]; $self->[HANDLE] = undef;
233 }
234  
235 2 1;
236 __END__
« WARC/Record/Logical.pm ↑ Summary WARC/Record/Logical/Heuristics.pm »