| 1 | package WARC::Record::Block; # -*- CPerl -*- | ||||||||||||||||||
| 2 | |||||||||||||||||||
| 3 | 28 | ▶ | use strict; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 4 | 28 | ▶ | use warnings; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 5 | |||||||||||||||||||
| 6 | 28 | our @ISA = qw(); | |||||||||||||||||
| 7 | |||||||||||||||||||
| 8 | 28 | ▶ | use WARC; *WARC::Record::Block::VERSION = \$WARC::VERSION; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 9 | |||||||||||||||||||
| 10 | 28 | ▶ | use Carp; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 11 | 28 | ▶ | 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 | 28 | ▶ | use constant { BASE => 0, LENGTH => 1, HANDLE => 2, PARENT => 3, AT_EOF => 4 }; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 16 | 28 | ▶ | use constant OBJECT_INIT => undef, undef, undef, undef, 0; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 17 | |||||||||||||||||||
| 18 | sub _dbg_dump { | ||||||||||||||||||
| 19 | 26 | ▶ | my $self = shift; | ||||||||||||||||
|
Subroutine
_dbg_dump: called CC=2 SCAR=6.9
| |||||||||||||||||||
| 20 | |||||||||||||||||||
| 21 | 26 | my $out = 'record block '.$self->[BASE].' +> '.$self->[LENGTH]; | |||||||||||||||||
| 22 | 26 | $out .= ' @'.((tell $self->[HANDLE]) - $self->[BASE]); | |||||||||||||||||
| 23 | 26 | ▶ | $out .= ' [EOF]' if $self->[AT_EOF]; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 24 | 26 | $out .= "\n"; | |||||||||||||||||
| 25 | |||||||||||||||||||
| 26 | 26 | return $out; | |||||||||||||||||
| 27 | } | ||||||||||||||||||
| 28 | |||||||||||||||||||
| 29 | sub TIEHANDLE { | ||||||||||||||||||
| 30 | 152 | ▶ | my $class = shift; | ||||||||||||||||
|
Subroutine
TIEHANDLE: called CC=4 SCAR=13.9
| |||||||||||||||||||
| 31 | 152 | my $parent = shift; | |||||||||||||||||
| 32 | |||||||||||||||||||
| 33 | 152 | my $handle = $parent->volume->open; | |||||||||||||||||
| 34 | |||||||||||||||||||
| 35 | 152 | ▶ | if (defined $parent->{compression}) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 36 | 1 | ▶ | seek $handle, $parent->offset, SEEK_SET or die "seek: $!"; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 37 | |||||||||||||||||||
| 38 | 1 | my $z_reader = $parent->{compression}; | |||||||||||||||||
| 39 | 1 | ▶ | my $zhandle = new $z_reader ($handle, MultiStream => 0, Transparent => 0, | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 40 | AutoClose => 1) | ||||||||||||||||||
| 41 | or die "$z_reader: ".$parent->_get_compression_error; | ||||||||||||||||||
| 42 | |||||||||||||||||||
| 43 | 1 | $handle = $zhandle; | |||||||||||||||||
| 44 | } | ||||||||||||||||||
| 45 | |||||||||||||||||||
| 46 | 152 | my $ob = [OBJECT_INIT]; | |||||||||||||||||
| 47 | 152 | @$ob[PARENT, HANDLE] = ($parent, $handle); | |||||||||||||||||
| 48 | @$ob[BASE, LENGTH] = | ||||||||||||||||||
| 49 | 152 | ($parent->{data_offset}, $parent->field('Content-Length')); | |||||||||||||||||
| 50 | |||||||||||||||||||
| 51 | 152 | bless $ob, $class; | |||||||||||||||||
| 52 | |||||||||||||||||||
| 53 | 152 | $ob->SEEK(0, SEEK_SET); | |||||||||||||||||
| 54 | |||||||||||||||||||
| 55 | 152 | return $ob; | |||||||||||||||||
| 56 | } | ||||||||||||||||||
| 57 | |||||||||||||||||||
| 58 | sub READLINE { | ||||||||||||||||||
| 59 | 283 | ▶ | my $self = shift; | ||||||||||||||||
|
Subroutine
READLINE: called CC=7 SCAR=19.5
| |||||||||||||||||||
| 60 | |||||||||||||||||||
| 61 | 283 | ▶ | if (wantarray) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 62 | 10 | my @data = (); | |||||||||||||||||
| 63 | 10 | ▶ | while (defined(my $line = $self->READLINE())) { push @data, $line } | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 64 | return @data | ||||||||||||||||||
| 65 | 10 | } | |||||||||||||||||
| 66 | |||||||||||||||||||
| 67 | 273 | ▶ | return undef if $self->[AT_EOF]; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 68 | |||||||||||||||||||
| 69 | 259 | my $line = readline $self->[HANDLE]; | |||||||||||||||||
| 70 | 259 | ▶ | unless (defined $line) { $self->[AT_EOF] = 1; return undef } | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 71 | |||||||||||||||||||
| 72 | 258 | my $excess = (tell $self->[HANDLE]) - $self->[BASE] - $self->[LENGTH]; | |||||||||||||||||
| 73 | 258 | ▶ | $self->[AT_EOF] = 1 unless $excess < 0; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 74 | 258 | ▶ | $line = substr $line, 0, -$excess if $excess > 0; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 75 | |||||||||||||||||||
| 76 | 258 | return $line; | |||||||||||||||||
| 77 | } | ||||||||||||||||||
| 78 | |||||||||||||||||||
| 79 | # This sub must rely on the aliasing effect of @_. | ||||||||||||||||||
| 80 | sub READ { | ||||||||||||||||||
| 81 | 34 | ▶ | my $self = shift; | ||||||||||||||||
|
Subroutine
READ: called CC=8 SCAR=20.8
| |||||||||||||||||||
| 82 | # args now: 0: buffer 1: length 2: offset into buffer or undef | ||||||||||||||||||
| 83 | 34 | my $length = $_[1]; | |||||||||||||||||
| 84 | 34 | ▶ | my $offset = $_[2] || 0; | ||||||||||||||||
|
Condition100%2/2
$_[2] || 0
MC/DC100%1/1
$_[2] || 0
$_[2]
Truth table100%2/2
A
A= $_[2]
| |||||||||||||||||||
| 85 | |||||||||||||||||||
| 86 | 34 | ▶ | return 0 if $self->[AT_EOF]; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 87 | |||||||||||||||||||
| 88 | 31 | my $excess = (($length + tell $self->[HANDLE]) | |||||||||||||||||
| 89 | - $self->[BASE] - $self->[LENGTH]); | ||||||||||||||||||
| 90 | 31 | ▶ | $self->[AT_EOF] = 1 unless $excess < 0; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 91 | 31 | ▶ | $length -= $excess if $excess > 0; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 92 | |||||||||||||||||||
| 93 | 31 | my $buf; my $count = read $self->[HANDLE], $buf, $length; | |||||||||||||||||
| 94 | 31 | ▶ | return undef unless defined $count; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 95 | |||||||||||||||||||
| 96 | 31 | ▶ | $_[0] = '' unless defined $_[0]; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 97 | 31 | ▶ | $_[0] .= "\0" x ($offset - length($_[0])) if $offset > length $_[0]; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 98 | 31 | substr $_[0], $offset, (length($_[0]) - $offset), $buf; | |||||||||||||||||
| 99 | 31 | return $count; | |||||||||||||||||
| 100 | } | ||||||||||||||||||
| 101 | |||||||||||||||||||
| 102 | sub GETC { | ||||||||||||||||||
| 103 | 23 | ▶ | my $self = shift; | ||||||||||||||||
|
Subroutine
GETC: called CC=2 SCAR=6.9
| |||||||||||||||||||
| 104 | |||||||||||||||||||
| 105 | 23 | my $ch; | |||||||||||||||||
| 106 | 23 | ▶ | return undef unless $self->READ($ch, 1); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 107 | 21 | return $ch; | |||||||||||||||||
| 108 | } | ||||||||||||||||||
| 109 | |||||||||||||||||||
| 110 | 24 | ▶ | sub EOF { (shift)->[AT_EOF] } | ||||||||||||||||
|
Subroutine
EOF: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 111 | |||||||||||||||||||
| 112 | sub SEEK { | ||||||||||||||||||
| 113 | 169 | ▶ | my $self = shift; | ||||||||||||||||
|
Subroutine
SEEK: called CC=6 SCAR=17.9
| |||||||||||||||||||
| 114 | 169 | my $offset = shift; | |||||||||||||||||
| 115 | 169 | my $whence = shift; | |||||||||||||||||
| 116 | |||||||||||||||||||
| 117 | 169 | my $npos; | |||||||||||||||||
| 118 | 169 | $self->[AT_EOF] = 0; | |||||||||||||||||
| 119 | |||||||||||||||||||
| 120 | 169 | ▶ | if ($whence == SEEK_SET) { $npos = $offset } | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 121 | 2 | elsif ($whence == SEEK_CUR) { $npos = $offset + $self->TELL } | |||||||||||||||||
| 122 | 2 | elsif ($whence == SEEK_END) { $npos = $self->[LENGTH] + $offset } | |||||||||||||||||
| 123 | 1 | else { croak "unknown WHENCE $whence in call to seek" } | |||||||||||||||||
| 124 | |||||||||||||||||||
| 125 | 168 | ▶ | return 0 if $npos < 0; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 126 | 167 | ▶ | if ($npos >= $self->[LENGTH]) { $self->[AT_EOF] = 1; $npos = $self->[LENGTH] } | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 127 | |||||||||||||||||||
| 128 | 167 | seek $self->[HANDLE], $self->[BASE] + $npos, SEEK_SET; | |||||||||||||||||
| 129 | } | ||||||||||||||||||
| 130 | |||||||||||||||||||
| 131 | sub TELL { | ||||||||||||||||||
| 132 | 39 | ▶ | my $self = shift; | ||||||||||||||||
|
Subroutine
TELL: called CC=2 SCAR=6.9
| |||||||||||||||||||
| 133 | |||||||||||||||||||
| 134 | 39 | ▶ | return $self->[LENGTH] if $self->[AT_EOF]; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 135 | |||||||||||||||||||
| 136 | 21 | return ((tell $self->[HANDLE]) - $self->[BASE]); | |||||||||||||||||
| 137 | } | ||||||||||||||||||
| 138 | |||||||||||||||||||
| 139 | sub CLOSE { | ||||||||||||||||||
| 140 | 51 | ▶ | my $self = shift; | ||||||||||||||||
|
Subroutine
CLOSE: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 141 | 51 | @$self[BASE, LENGTH, AT_EOF] = (0, 0, 1); | |||||||||||||||||
| 142 | 51 | close $self->[HANDLE]; | |||||||||||||||||
| 143 | } | ||||||||||||||||||
| 144 | |||||||||||||||||||
| 145 | 28 | 1; | |||||||||||||||||
| 146 | __END__ | ||||||||||||||||||