File Coverage

blib/lib/WARC/Record/Block.pm
Criterion Covered Total %
statement 96 96 100.0
branch 41 44 93.1
condition 2 2 100.0
subroutine 16 16 100.0
pod n/a
total 155 158 98.1


line stmt bran cond sub pod time code
1             package WARC::Record::Block; # -*- CPerl -*-
2              
3 28     28   59889 use strict;
  28         67  
  28         865  
4 28     28   139 use warnings;
  28         46  
  28         1128  
5              
6             our @ISA = qw();
7              
8 28     28   498 use WARC; *WARC::Record::Block::VERSION = \$WARC::VERSION;
  28         49  
  28         805  
9              
10 28     28   134 use Carp;
  28         55  
  28         1591  
11 28     28   170 use Fcntl qw/SEEK_SET SEEK_CUR SEEK_END/;
  28         62  
  28         1777  
12              
13             # This implementation uses an array as the underlying object.
14              
15 28     28   171 use constant { BASE => 0, LENGTH => 1, HANDLE => 2, PARENT => 3, AT_EOF => 4 };
  28         50  
  28         2621  
16 28     28   201 use constant OBJECT_INIT => undef, undef, undef, undef, 0;
  28         56  
  28         26347  
17              
18             sub _dbg_dump {
19 26     26   686 my $self = shift;
20              
21 26         63 my $out = 'record block '.$self->[BASE].' +> '.$self->[LENGTH];
22 26         80 $out .= ' @'.((tell $self->[HANDLE]) - $self->[BASE]);
23 26 100       60 $out .= ' [EOF]' if $self->[AT_EOF];
24 26         34 $out .= "\n";
25              
26 26         60 return $out;
27             }
28              
29             sub TIEHANDLE {
30 152     152   6269 my $class = shift;
31 152         201 my $parent = shift;
32              
33 152         327 my $handle = $parent->volume->open;
34              
35 152 100       1334 if (defined $parent->{compression}) {
36 1 50       3 seek $handle, $parent->offset, SEEK_SET or die "seek: $!";
37              
38 1         7 my $z_reader = $parent->{compression};
39 1 50       9 my $zhandle = new $z_reader ($handle, MultiStream => 0, Transparent => 0,
40             AutoClose => 1)
41             or die "$z_reader: ".$parent->_get_compression_error;
42              
43 1         1593 $handle = $zhandle;
44             }
45              
46 152         369 my $ob = [OBJECT_INIT];
47 152         413 @$ob[PARENT, HANDLE] = ($parent, $handle);
48             @$ob[BASE, LENGTH] =
49 152         456 ($parent->{data_offset}, $parent->field('Content-Length'));
50              
51 152         605 bless $ob, $class;
52              
53 152         412 $ob->SEEK(0, SEEK_SET);
54              
55 152         775 return $ob;
56             }
57              
58             sub READLINE {
59 283     283   409 my $self = shift;
60              
61 283 100       491 if (wantarray) {
62 10         23 my @data = ();
63 10         80 while (defined(my $line = $self->READLINE())) { push @data, $line }
  13         37  
64             return @data
65 10         94 }
66              
67 273 100       507 return undef if $self->[AT_EOF];
68              
69 259         3980 my $line = readline $self->[HANDLE];
70 259 100       610 unless (defined $line) { $self->[AT_EOF] = 1; return undef }
  1         3  
  1         3  
71              
72 258         584 my $excess = (tell $self->[HANDLE]) - $self->[BASE] - $self->[LENGTH];
73 258 100       477 $self->[AT_EOF] = 1 unless $excess < 0;
74 258 100       571 $line = substr $line, 0, -$excess if $excess > 0;
75              
76 258         1036 return $line;
77             }
78              
79             # This sub must rely on the aliasing effect of @_.
80             sub READ {
81 34     34   1138 my $self = shift;
82             # args now: 0: buffer 1: length 2: offset into buffer or undef
83 34         41 my $length = $_[1];
84 34   100     90 my $offset = $_[2] || 0;
85              
86 34 100       71 return 0 if $self->[AT_EOF];
87              
88 31         61 my $excess = (($length + tell $self->[HANDLE])
89             - $self->[BASE] - $self->[LENGTH]);
90 31 100       58 $self->[AT_EOF] = 1 unless $excess < 0;
91 31 100       62 $length -= $excess if $excess > 0;
92              
93 31         34 my $buf; my $count = read $self->[HANDLE], $buf, $length;
  31         88  
94 31 50       83 return undef unless defined $count;
95              
96 31 100       55 $_[0] = '' unless defined $_[0];
97 31 100       51 $_[0] .= "\0" x ($offset - length($_[0])) if $offset > length $_[0];
98 31         51 substr $_[0], $offset, (length($_[0]) - $offset), $buf;
99 31         75 return $count;
100             }
101              
102             sub GETC {
103 23     23   391 my $self = shift;
104              
105 23         27 my $ch;
106 23 100       34 return undef unless $self->READ($ch, 1);
107 21         58 return $ch;
108             }
109              
110 24     24   4795 sub EOF { (shift)->[AT_EOF] }
111              
112             sub SEEK {
113 169     169   242 my $self = shift;
114 169         209 my $offset = shift;
115 169         222 my $whence = shift;
116              
117 169         221 my $npos;
118 169         256 $self->[AT_EOF] = 0;
119              
120 169 100       324 if ($whence == SEEK_SET) { $npos = $offset }
  164 100       212  
    100          
121 2         6 elsif ($whence == SEEK_CUR) { $npos = $offset + $self->TELL }
122 2         4 elsif ($whence == SEEK_END) { $npos = $self->[LENGTH] + $offset }
123 1         194 else { croak "unknown WHENCE $whence in call to seek" }
124              
125 168 100       326 return 0 if $npos < 0;
126 167 100       452 if ($npos >= $self->[LENGTH]) { $self->[AT_EOF] = 1; $npos = $self->[LENGTH] }
  2         4  
  2         4  
127              
128 167         1461 seek $self->[HANDLE], $self->[BASE] + $npos, SEEK_SET;
129             }
130              
131             sub TELL {
132 39     39   424 my $self = shift;
133              
134 39 100       118 return $self->[LENGTH] if $self->[AT_EOF];
135              
136 21         71 return ((tell $self->[HANDLE]) - $self->[BASE]);
137             }
138              
139             sub CLOSE {
140 51     51   90 my $self = shift;
141 51         118 @$self[BASE, LENGTH, AT_EOF] = (0, 0, 1);
142 51         683 close $self->[HANDLE];
143             }
144              
145             1;
146             __END__