| 1 | #$Id: gzip.pm 517 2009-10-23 15:52:21Z maj $ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | package PerlIO::via::gzip; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use PerlIO; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use IO::Compress::Gzip qw(:constants); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use IO::Uncompress::Gunzip; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 1 | our $VERSION = '0.03'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 1 | our $COMPRESSION_LEVEL = Z_DEFAULT_COMPRESSION; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 1 | our $COMPRESSION_STRATEGY = Z_DEFAULT_STRATEGY; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 1 | our $BLOCK_SIZE = 4096; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 1 | our $INSTANCE = 128; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 2 | ▶ | sub PUSHED { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
PUSHED: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 1 | ▶ | no strict qw(refs); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 2 | my ($class, $mode) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 2 | my $stat; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 2 | my $self = { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | instance => $INSTANCE++ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 2 | $mode =~ s/\+//; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 2 | $self->{mode} = $mode; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 2 | bless $self, $_[0]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | # open hook | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 2 | ▶ | sub FILENO { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
FILENO: called CC=8 SCAR=21.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 2 | my ($self, $fh) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 2 | ▶ | if ( !defined $self->{inited} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 2 | my $via = grep (/via/, PerlIO::get_layers($fh)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | my $compress = ($self->{mode} =~ /w|a/ and !$via) || | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 2 | ▶ | ($self->{mode} =~ /r/ and $via); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition55%5/9
$self->{'mode'} =~ /w|a/ && !$via || $self->{'mode'} =~ /r/ && $via
$self->{"mode"} =~ /w|a/ and not $via
$self->{"mode"} =~ /r/ and $via
MC/DC25%1/4
$self->{'mode'} =~ /w|a/ && !$via || $self->{'mode'} =~ /r/ && $via
$self->{"mode"} =~ /w|a/
not $via
$self->{"mode"} =~ /r/
$via
Truth table28%2/7
A and not(B) || C and D
A= $self->{"mode"} =~ /w|a/B= not $viaC= $self->{"mode"} =~ /r/D= $via
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 2 | $self->{fileno} = fileno($fh); # nec. to kick fileno hooks | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 2 | $self->{inited} = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 2 | ▶ | if ($compress) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 1 | $self->{gzip} = IO::Compress::Gzip->new( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | $fh, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | AutoClose => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | Level => $COMPRESSION_LEVEL, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | Strategy => $COMPRESSION_STRATEGY, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 1 | ▶ | croak "via(gzip) [OPEN]: Couldn't create compression stream" unless ($self->{gzip}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 1 | $self->{gzip}->autoflush(1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 1 | $self->{gunzip} = IO::Uncompress::Gunzip->new( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | $fh, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | BlockSize => $BLOCK_SIZE | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 1 | ▶ | croak "via(gzip) [OPEN]: Couldn't create decompression stream" unless ($self->{gunzip}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 2 | $self->{fileno}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 2 | ▶ | sub FILL { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
FILL: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 2 | my ($self, $fh) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 2 | return $self->Readline($fh); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 2 | ▶ | sub Readline { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
Readline: called CC=3 SCAR=13.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 2 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 2 | ▶ | if ($self->{gzip}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | return $self->{gzip}->getline; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | elsif ($self->{gunzip}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 2 | return $self->{gunzip}->getline; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | croak "via(gzip) [FILL]: handle not initialized"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 2006 | ▶ | sub WRITE { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
WRITE: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 2006 | my ($self, $buf, $fh) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 2006 | return $self->Write($fh, $buf); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 2006 | ▶ | sub Write { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
Write: called CC=3 SCAR=12.7
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 2006 | my ($self, $fh, $buf) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 2006 | my $ret; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 2006 | ▶ | if ($self->{gunzip}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | return $self->{gunzip}->write($buf); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | elsif ($self->{gzip}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 2006 | return $self->{gzip}->print($buf); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | croak "via(gzip) [WRITE]: handle not initialized"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 2 | ▶ | sub FLUSH { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
FLUSH: called CC=4 SCAR=14.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 2 | my ($self, $fh) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 2 | ▶ | return -1 unless $self->{inited} == 1; # not open yet | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 2 | ▶ | $fh && $fh->flush; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 2 | ▶ | if ($self->{gzip}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 1 | $self->{gzip}->flush; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | # to get a valid gzip file, the Gzip handle must | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | # be closed before the source handle. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | # if FLUSH is called on via handle close, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | # the source handle is closed before we | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | # can get to it in via::gzip::CLOSE. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | # So we are closing the Gzip handle here. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 1 | $self->{gzip}->close; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 2 | return 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 2 | ▶ | sub CLOSE { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
CLOSE: called CC=5 SCAR=16.6
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | 2 | my ($self, $fh) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 2 | ▶ | return -1 unless $self->{inited}; # not open yet | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 2 | ▶ | if ($self->{gzip}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | # the $self->{gzip} handle was already flushed and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | # closed by FLUSH | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 1 | ▶ | return $fh ? $fh->close : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 1 | $self->{gunzip}->close; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 1 | ▶ | return $fh->close if $fh; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||