blib/lib/IO/Zlib.pm

filter: Statementstmt 92.5% 174 / 188 Branchbran 73.1% 60 / 82 Conditioncond 52.6% 10 / 19 MC/DCmcdc 27.2% 3 / 11 Subroutinesub 88.8% 32 / 36 Podpod 62.5% 10 / 16 totaltotal 82.1% 289 / 352 SCAR 40.6
CC
49
Cov
84%
gzopen_external
14.0
_import
10.1
can_gunzip
6.0
CRAP 58.1
↑ Summary
1 # IO::Zlib.pm
2 #
3 # Copyright (c) 1998-2004 Tom Hughes <tom@compton.nu>.
4 # All rights reserved. This program is free software; you can redistribute
5 # it and/or modify it under the same terms as Perl itself.
6  
7 package IO::Zlib;
8  
9 =head1 NAME
10  
11 IO::Zlib - IO:: style interface to L<Compress::Zlib>
12  
13 =head1 SYNOPSIS
14  
15 With any version of Perl 5 you can use the basic OO interface:
16  
17 use IO::Zlib;
18  
19 $fh = new IO::Zlib;
20 if ($fh->open("file.gz", "rb")) {
21 print <$fh>;
22 $fh->close;
23 }
24  
25 $fh = IO::Zlib->new("file.gz", "wb9");
26 if (defined $fh) {
27 print $fh "bar\n";
28 $fh->close;
29 }
30  
31 $fh = IO::Zlib->new("file.gz", "rb");
32 if (defined $fh) {
33 print <$fh>;
34 undef $fh; # automatically closes the file
35 }
36  
37 With Perl 5.004 you can also use the TIEHANDLE interface to access
38 compressed files just like ordinary files:
39  
40 use IO::Zlib;
41  
42 tie *FILE, 'IO::Zlib', "file.gz", "wb";
43 print FILE "line 1\nline2\n";
44  
45 tie *FILE, 'IO::Zlib', "file.gz", "rb";
46 while (<FILE>) { print "LINE: ", $_ };
47  
48 =head1 DESCRIPTION
49  
50 C<IO::Zlib> provides an IO:: style interface to L<Compress::Zlib> and
51 hence to gzip/zlib compressed files. It provides many of the same methods
52 as the L<IO::Handle> interface.
53  
54 Starting from IO::Zlib version 1.02, IO::Zlib can also use an
55 external F<gzip> command. The default behaviour is to try to use
56 an external F<gzip> if no C<Compress::Zlib> can be loaded, unless
57 explicitly disabled by
58  
59 use IO::Zlib qw(:gzip_external 0);
60  
61 If explicitly enabled by
62  
63 use IO::Zlib qw(:gzip_external 1);
64  
65 then the external F<gzip> is used B<instead> of C<Compress::Zlib>.
66  
67 =head1 CONSTRUCTOR
68  
69 =over 4
70  
71 =item new ( [ARGS] )
72  
73 Creates an C<IO::Zlib> object. If it receives any parameters, they are
74 passed to the method C<open>; if the open fails, the object is destroyed.
75 Otherwise, it is returned to the caller.
76  
77 =back
78  
79 =head1 OBJECT METHODS
80  
81 =over 4
82  
83 =item open ( FILENAME, MODE )
84  
85 C<open> takes two arguments. The first is the name of the file to open
86 and the second is the open mode. The mode can be anything acceptable to
87 L<Compress::Zlib> and by extension anything acceptable to I<zlib> (that
88 basically means POSIX fopen() style mode strings plus an optional number
89 to indicate the compression level).
90  
91 =item opened
92  
93 Returns true if the object currently refers to a opened file.
94  
95 =item close
96  
97 Close the file associated with the object and disassociate
98 the file from the handle.
99 Done automatically on destroy.
100  
101 =item getc
102  
103 Return the next character from the file, or undef if none remain.
104  
105 =item getline
106  
107 Return the next line from the file, or undef on end of string.
108 Can safely be called in an array context.
109 Currently ignores $/ ($INPUT_RECORD_SEPARATOR or $RS when L<English>
110 is in use) and treats lines as delimited by "\n".
111  
112 =item getlines
113  
114 Get all remaining lines from the file.
115 It will croak() if accidentally called in a scalar context.
116  
117 =item print ( ARGS... )
118  
119 Print ARGS to the file.
120  
121 =item read ( BUF, NBYTES, [OFFSET] )
122  
123 Read some bytes from the file.
124 Returns the number of bytes actually read, 0 on end-of-file, undef on error.
125  
126 =item eof
127  
128 Returns true if the handle is currently positioned at end of file?
129  
130 =item seek ( OFFSET, WHENCE )
131  
132 Seek to a given position in the stream.
133 Not yet supported.
134  
135 =item tell
136  
137 Return the current position in the stream, as a numeric offset.
138 Not yet supported.
139  
140 =item setpos ( POS )
141  
142 Set the current position, using the opaque value returned by C<getpos()>.
143 Not yet supported.
144  
145 =item getpos ( POS )
146  
147 Return the current position in the string, as an opaque object.
148 Not yet supported.
149  
150 =back
151  
152 =head1 USING THE EXTERNAL GZIP
153  
154 If the external F<gzip> is used, the following C<open>s are used:
155  
156 open(FH, "gzip -dc $filename |") # for read opens
157 open(FH, " | gzip > $filename") # for write opens
158  
159 You can modify the 'commands' for example to hardwire
160 an absolute path by e.g.
161  
162 use IO::Zlib ':gzip_read_open' => '/some/where/gunzip -c %s |';
163 use IO::Zlib ':gzip_write_open' => '| /some/where/gzip.exe > %s';
164  
165 The C<%s> is expanded to be the filename (C<sprintf> is used, so be
166 careful to escape any other C<%> signs). The 'commands' are checked
167 for sanity - they must contain the C<%s>, and the read open must end
168 with the pipe sign, and the write open must begin with the pipe sign.
169  
170 =head1 CLASS METHODS
171  
172 =over 4
173  
174 =item has_Compress_Zlib
175  
176 Returns true if C<Compress::Zlib> is available. Note that this does
177 not mean that C<Compress::Zlib> is being used: see L</gzip_external>
178 and L<gzip_used>.
179  
180 =item gzip_external
181  
182 Undef if an external F<gzip> B<can> be used if C<Compress::Zlib> is
183 not available (see L</has_Compress_Zlib>), true if an external F<gzip>
184 is explicitly used, false if an external F<gzip> must not be used.
185 See L</gzip_used>.
186  
187 =item gzip_used
188  
189 True if an external F<gzip> is being used, false if not.
190  
191 =item gzip_read_open
192  
193 Return the 'command' being used for opening a file for reading using an
194 external F<gzip>.
195  
196 =item gzip_write_open
197  
198 Return the 'command' being used for opening a file for writing using an
199 external F<gzip>.
200  
201 =back
202  
203 =head1 DIAGNOSTICS
204  
205 =over 4
206  
207 =item IO::Zlib::getlines: must be called in list context
208  
209 If you want read lines, you must read in list context.
210  
211 =item IO::Zlib::gzopen_external: mode '...' is illegal
212  
213 Use only modes 'rb' or 'wb' or /wb[1-9]/.
214  
215 =item IO::Zlib::import: '...' is illegal
216  
217 The known import symbols are the C<:gzip_external>, C<:gzip_read_open>,
218 and C<:gzip_write_open>. Anything else is not recognized.
219  
220 =item IO::Zlib::import: ':gzip_external' requires an argument
221  
222 The C<:gzip_external> requires one boolean argument.
223  
224 =item IO::Zlib::import: 'gzip_read_open' requires an argument
225  
226 The C<:gzip_external> requires one string argument.
227  
228 =item IO::Zlib::import: 'gzip_read' '...' is illegal
229  
230 The C<:gzip_read_open> argument must end with the pipe sign (|)
231 and have the C<%s> for the filename. See L</"USING THE EXTERNAL GZIP">.
232  
233 =item IO::Zlib::import: 'gzip_write_open' requires an argument
234  
235 The C<:gzip_external> requires one string argument.
236  
237 =item IO::Zlib::import: 'gzip_write_open' '...' is illegal
238  
239 The C<:gzip_write_open> argument must begin with the pipe sign (|)
240 and have the C<%s> for the filename. An output redirect (>) is also
241 often a good idea, depending on your operating system shell syntax.
242 See L</"USING THE EXTERNAL GZIP">.
243  
244 =item IO::Zlib::import: no Compress::Zlib and no external gzip
245  
246 Given that we failed to load C<Compress::Zlib> and that the use of
247 an external F<gzip> was disabled, IO::Zlib has not much chance of working.
248  
249 =item IO::Zlib::open: needs a filename
250  
251 No filename, no open.
252  
253 =item IO::Zlib::READ: NBYTES must be specified
254  
255 We must know how much to read.
256  
257 =item IO::Zlib::WRITE: too long LENGTH
258  
259 The LENGTH must be less than or equal to the buffer size.
260  
261 =back
262  
263 =head1 SEE ALSO
264  
265 L<perlfunc>,
266 L<perlop/"I/O Operators">,
267 L<IO::Handle>,
268 L<Compress::Zlib>
269  
270 =head1 HISTORY
271  
272 Created by Tom Hughes E<lt>F<tom@compton.nu>E<gt>.
273  
274 Support for external gzip added by Jarkko Hietaniemi E<lt>F<jhi@iki.fi>E<gt>.
275  
276 =head1 COPYRIGHT
277  
278 Copyright (c) 1998-2004 Tom Hughes E<lt>F<tom@compton.nu>E<gt>.
279 All rights reserved. This program is free software; you can redistribute
280 it and/or modify it under the same terms as Perl itself.
281  
282 =cut
283  
284 9 require 5.006;
285  
286 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
287 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
288  
289 9 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
290 9 ▶use Fcntl qw(SEEK_SET);
Subroutine BEGIN: called CC=1 SCAR=0.0
291 9 ▶use Symbol;
Subroutine BEGIN: called CC=1 SCAR=0.0
292 9 ▶use Tie::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
293  
294 9 our $VERSION = "1.14";
295 9 our $AUTOLOAD;
296 9 our @ISA = qw(Tie::Handle);
297  
298 9 my $has_Compress_Zlib;
299 my $gzip_external;
300 9 my $gzip_used;
301 9 my $gzip_read_open = "gzip -dc %s |";
302 9 my $gzip_write_open = "| gzip > %s";
303 9 my $aliased;
304  
305 BEGIN {
306 9 ▶ eval { require Compress::Zlib };
Subroutine BEGIN: called CC=3 SCAR=12.5
307 9 ▶ $has_Compress_Zlib = $@ || $Compress::Zlib::VERSION < 2.000 ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$@ || $Compress::Zlib::VERSION < 2.0 ? : 0 9 9
Condition33%1/3
$@ or $Compress::Zlib::VERSION < 2
l!l&&r!l&&!r
009
MC/DC0%0/2
$@ or $Compress::Zlib::VERSION < 2
$@ $Compress::Zlib::VERSION < 2
Truth table33%1/3
A or B
A= $@B= $Compress::Zlib::VERSION < 2
ABresult
1X1
011
000
308 }
309  
310 sub has_Compress_Zlib
311 {
312 1 ▶ $has_Compress_Zlib;
Subroutine has_Compress_Zlib: called CC=1 SCAR=0.0
Pod documented
313 }
314  
315 sub gzip_external
316 {
317 3 ▶ $gzip_external;
Subroutine gzip_external: called CC=1 SCAR=0.0
Pod documented
318 }
319  
320 sub gzip_used
321 {
322 1 ▶ $gzip_used;
Subroutine gzip_used: called CC=1 SCAR=0.0
Pod documented
323 }
324  
325 sub gzip_read_open
326 {
327 2 ▶ $gzip_read_open;
Subroutine gzip_read_open: called CC=1 SCAR=0.0
Pod documented
328 }
329  
330 sub gzip_write_open
331 {
332 2 ▶ $gzip_write_open;
Subroutine gzip_write_open: called CC=1 SCAR=0.0
Pod documented
333 }
334  
335 sub can_gunzip
336 {
337 0 ▶ $has_Compress_Zlib || $gzip_external;
Subroutine can_gunzip: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$has_Compress_Zlib or $gzip_external 0 0 0
338 }
339  
340 sub _import
341 {
342 8 ▶ my $import = shift;
Subroutine _import: called CC=10 SCAR=23.2
343  
344 8 ▶ while (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 8 4 12
345 {
346 8 ▶ if ($_[0] eq ':gzip_external')
Branch: 3 branches
BranchTrueFalseTotal
if ($_[0] eq ':gzip_external') { } 3 5 8
elsif ($_[0] eq ':gzip_read_open') { } 2 3 5
elsif ($_[0] eq ':gzip_write_open') { } 2 1 3
347 {
348 3 shift;
349  
350 3 ▶ if (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 2 1 3
351 {
352 2 $gzip_external = shift;
353 }
354 else
355 {
356 1 croak "$import: ':gzip_external' requires an argument";
357 }
358 }
359 elsif ($_[0] eq ':gzip_read_open')
360 {
361 2 shift;
362  
363 2 ▶ if (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 2 0 2
364 {
365 2 $gzip_read_open = shift;
366  
367 2 ▶ croak "$import: ':gzip_read_open' '$gzip_read_open' is illegal"
Branch: 1 branch
BranchTrueFalseTotal
unless $gzip_read_open =~ /^.+%s.+\|\s*$/ 1 1 2
368 unless $gzip_read_open =~ /^.+%s.+\|\s*$/;
369 }
370 else
371 {
372 0 croak "$import: ':gzip_read_open' requires an argument";
373 }
374 }
375 elsif ($_[0] eq ':gzip_write_open')
376 {
377 2 shift;
378  
379 2 ▶ if (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 2 0 2
380 {
381 2 $gzip_write_open = shift;
382  
383 2 ▶ croak "$import: ':gzip_write_open' '$gzip_read_open' is illegal"
Branch: 1 branch
BranchTrueFalseTotal
unless $gzip_write_open =~ /^\s*\|.+%s.*$/ 1 1 2
384 unless $gzip_write_open =~ /^\s*\|.+%s.*$/;
385 }
386 else
387 {
388 0 croak "$import: ':gzip_write_open' requires an argument";
389 }
390 }
391 else
392 {
393 1 last;
394 }
395 }
396  
397 5 return @_;
398 }
399  
400 sub _alias
401 {
402 13 ▶ my $import = shift;
Subroutine _alias: called CC=5 SCAR=16.4
403  
404 13 ▶ if ($gzip_external || (!$has_Compress_Zlib && !defined($gzip_external)))
Branch: 2 branches
BranchTrueFalseTotal
if ($gzip_external or not $has_Compress_Zlib || defined $gzip_external) { } 3 10 13
elsif ($has_Compress_Zlib) { } 10 0 10
Condition50%3/6
$gzip_external or not $has_Compress_Zlib || defined $gzip_external
l!l&&r!l&&!r
3010
$has_Compress_Zlib || defined $gzip_external
l!l&&r!l&&!r
1000
MC/DC33%1/3
$gzip_external or not $has_Compress_Zlib || defined $gzip_external
$gzip_external $has_Compress_Zlib defined $gzip_external
Truth table50%2/4
A or not(B || C)
A= $gzip_externalB= $has_Compress_ZlibC= defined $gzip_external
ABCresult
1XX1
0001
01X0
0010
405 {
406 3 require IO::Handle;
407  
408 3 undef *gzopen;
409 3 *gzopen = \&gzopen_external;
410  
411 3 *IO::Handle::gzread = \&gzread_external;
412 3 *IO::Handle::gzwrite = \&gzwrite_external;
413 3 *IO::Handle::gzreadline = \&gzreadline_external;
414 3 *IO::Handle::gzeof = \&gzeof_external;
415 3 *IO::Handle::gzclose = \&gzclose_external;
416  
417 3 $gzip_used = 1;
418 }
419 elsif ($has_Compress_Zlib)
420 {
421 10 *gzopen = \&Compress::Zlib::gzopen;
422 10 *gzread = \&Compress::Zlib::gzread;
423 10 *gzwrite = \&Compress::Zlib::gzwrite;
424 10 *gzreadline = \&Compress::Zlib::gzreadline;
425 10 *gzeof = \&Compress::Zlib::gzeof;
426 }
427 else
428 {
429 0 croak "$import: no Compress::Zlib and no external gzip";
430 }
431  
432 13 $aliased = 1;
433 }
434  
435 sub import
436 {
437 16 ▶ my $class = shift;
Subroutine import: called CC=3 SCAR=11.0
438 16 my $import = "IO::Zlib::import";
439  
440 16 ▶ if (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 8 8 16
441 {
442 8 ▶ if (_import($import, @_))
Branch: 1 branch
BranchTrueFalseTotal
if (_import($import, @_)) 1 4 5
443 {
444 1 croak "$import: '@_' is illegal";
445 }
446 }
447  
448 12 _alias($import);
449 }
450  
451 sub TIEHANDLE
452 {
453 22 ▶ my $class = shift;
Subroutine TIEHANDLE: called CC=2 SCAR=6.9
454 22 my @args = @_;
455  
456 22 my $self = bless {}, $class;
457  
458 22 ▶ return @args ? $self->OPEN(@args) : $self;
Branch: 1 branch
BranchTrueFalseTotal
@args ? : 18 4 22
459 }
460  
461 sub DESTROY
462 ▶{
Subroutine DESTROY: not called CC=1 SCAR=0.0
463 }
464  
465 sub OPEN
466 {
467 22 ▶ my $self = shift;
Subroutine OPEN: called CC=3 SCAR=11.0
468 22 my $filename = shift;
469 22 my $mode = shift;
470  
471 22 ▶ croak "IO::Zlib::open: needs a filename" unless defined($filename);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $filename 0 22 22
472  
473 22 $self->{'file'} = gzopen($filename,$mode);
474  
475 21 ▶ return defined($self->{'file'}) ? $self : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'file'} ? : 17 4 21
476 }
477  
478 sub CLOSE
479 {
480 15 ▶ my $self = shift;
Subroutine CLOSE: called CC=3 SCAR=11.3
481  
482 15 ▶ return undef unless defined($self->{'file'});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"file"} 0 15 15
483  
484 15 my $status = $self->{'file'}->gzclose();
485  
486 15 delete $self->{'file'};
487  
488 15 ▶ return ($status == 0) ? 1 : undef;
Branch: 1 branch
BranchTrueFalseTotal
$status == 0 ? : 15 0 15
489 }
490  
491 sub READ
492 {
493 13 ▶ my $self = shift;
Subroutine READ: called CC=5 SCAR=16.2
494 13 my $bufref = \$_[0];
495 13 my $nbytes = $_[1];
496 13 ▶ my $offset = $_[2] || 0;
Condition100%2/2
$_[2] || 0
l!l
211
MC/DC100%1/1
$_[2] || 0
$_[2]
Truth table100%2/2
A
A= $_[2]
Aresult
11
00
497  
498 13 ▶ croak "IO::Zlib::READ: NBYTES must be specified" unless defined($nbytes);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $nbytes 0 13 13
499  
500 13 ▶ $$bufref = "" unless defined($$bufref);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$bufref 11 2 13
501  
502 13 my $bytesread = $self->{'file'}->gzread(substr($$bufref,$offset),$nbytes);
503  
504 13 ▶ return undef if $bytesread < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $bytesread < 0 0 13 13
505  
506 13 return $bytesread;
507 }
508  
509 sub READLINE
510 {
511 7 ▶ my $self = shift;
Subroutine READLINE: called CC=4 SCAR=13.9
512  
513 7 my $line;
514  
515 7 ▶ return () if $self->{'file'}->gzreadline($line) <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"file"}->gzreadline($line) <= 0 1 6 7
516  
517 6 ▶ return $line unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 5 1 6
518  
519 1 my @lines = $line;
520  
521 1 ▶ while ($self->{'file'}->gzreadline($line) > 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"file"}->gzreadline($line) > 0) 3 1 4
522 {
523 3 push @lines, $line;
524 }
525  
526 1 return @lines;
527 }
528  
529 sub WRITE
530 {
531 7 ▶ my $self = shift;
Subroutine WRITE: called CC=2 SCAR=7.0
532 7 my $buf = shift;
533 7 my $length = shift;
534 7 my $offset = shift;
535  
536 7 ▶ croak "IO::Zlib::WRITE: too long LENGTH" unless $offset + $length <= length($buf);
Branch: 1 branch
BranchTrueFalseTotal
unless $offset + $length <= length $buf 0 7 7
537  
538 7 return $self->{'file'}->gzwrite(substr($buf,$offset,$length));
539 }
540  
541 sub EOF
542 {
543 14 ▶ my $self = shift;
Subroutine EOF: called CC=1 SCAR=0.0
544  
545 14 return $self->{'file'}->gzeof();
546 }
547  
548 sub FILENO
549 {
550 0 ▶ return undef;
Subroutine FILENO: not called CC=1 SCAR=6.9
551 }
552  
553 sub new
554 {
555 20 ▶ my $class = shift;
Subroutine new: called CC=3 SCAR=11.0
Pod documented
556 20 my @args = @_;
557  
558 20 ▶ _alias("new", @_) unless $aliased; # Some call new IO::Zlib directly...
Branch: 1 branch
BranchTrueFalseTotal
unless $aliased 1 19 20
559  
560 20 my $self = gensym();
561  
562 20 tie *{$self}, $class, @args;
563  
564 19 ▶ return tied(${$self}) ? bless $self, $class : undef;
Branch: 1 branch
BranchTrueFalseTotal
tied ${$self;} ? : 15 4 19
565 }
566  
567 sub getline
568 {
569 5 ▶ my $self = shift;
Subroutine getline: called CC=1 SCAR=0.0
Pod documented
570  
571 5 return scalar tied(*{$self})->READLINE();
572 }
573  
574 sub getlines
575 {
576 2 ▶ my $self = shift;
Subroutine getlines: called CC=2 SCAR=6.9
Pod documented
577  
578 2 ▶ croak "IO::Zlib::getlines: must be called in list context"
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 1 1 2
579 unless wantarray;
580  
581 1 return tied(*{$self})->READLINE();
582 }
583  
584 sub opened
585 {
586 12 ▶ my $self = shift;
Subroutine opened: called CC=1 SCAR=0.0
Pod documented
587  
588 12 return defined tied(*{$self})->{'file'};
589 }
590  
591 sub AUTOLOAD
592 {
593 48 ▶ my $self = shift;
Subroutine AUTOLOAD: called CC=1 SCAR=0.0
594  
595 48 $AUTOLOAD =~ s/.*:://;
596 48 $AUTOLOAD =~ tr/a-z/A-Z/;
597  
598 48 return tied(*{$self})->$AUTOLOAD(@_);
599 }
600  
601 sub gzopen_external
602 {
603 5 ▶ my $filename = shift;
Subroutine gzopen_external: called CC=11 SCAR=26.4
Pod documented
604 5 my $mode = shift;
605 5 my $fh = IO::Handle->new();
606  
607 5 ▶ if ($mode =~ /r/)
Branch: 2 branches
BranchTrueFalseTotal
if ($mode =~ /r/) { } 3 2 5
elsif ($mode =~ /w/) { } 1 1 2
608 {
609 # Because someone will try to read ungzipped files
610 # with this we peek and verify the signature. Yes,
611 # this means that we open the file twice (if it is
612 # gzipped).
613 # Plenty of race conditions exist in this code, but
614 # the alternative would be to capture the stderr of
615 # gzip and parse it, which would be a portability nightmare.
616 3 ▶ if (-e $filename && open($fh, $filename))
Branch: 1 branch
BranchTrueFalseTotal
if (-e $filename and open $fh, $filename) { } 2 1 3
Condition66%2/3
-e $filename and open $fh, $filename
!ll&&!rl&&r
102
MC/DC50%1/2
-e $filename and open $fh, $filename
-e $filename open $fh, $filename
Truth table66%2/3
A and B
A= -e $filenameB= open $fh, $filename
ABresult
0X0
100
111
617 {
618 2 binmode $fh;
619  
620 2 my $sig;
621 2 my $rdb = read($fh, $sig, 2);
622  
623 2 ▶ if ($rdb == 2 && $sig eq "\x1F\x8B")
Branch: 1 branch
BranchTrueFalseTotal
if ($rdb == 2 and $sig eq "\37\213") 2 0 2
Condition33%1/3
$rdb == 2 and $sig eq "\37\213"
!ll&&!rl&&r
002
MC/DC0%0/2
$rdb == 2 and $sig eq "\37\213"
$rdb == 2 $sig eq "\37\213"
Truth table33%1/3
A and B
A= $rdb == 2B= $sig eq "\37\213"
ABresult
0X0
100
111
624 {
625 2 my $ropen = sprintf($gzip_read_open, $filename);
626  
627 2 ▶ if (open($fh, $ropen))
Branch: 1 branch
BranchTrueFalseTotal
if (open $fh, $ropen) { } 2 0 2
628 {
629 2 binmode $fh;
630  
631 2 return $fh;
632 }
633 else
634 {
635 0 return undef;
636 }
637 }
638  
639 0 ▶ seek($fh, 0, SEEK_SET) or
Branch: 1 branch
BranchTrueFalseTotal
seek $fh, 0, 0 or die "IO::Zlib: open('${filename}', 'r'): seek: $!" 0 0 0
640 die "IO::Zlib: open('$filename', 'r'): seek: $!";
641  
642 0 return $fh;
643 }
644 else
645 {
646 1 return undef;
647 }
648 }
649 elsif ($mode =~ /w/)
650 {
651 1 ▶ my $level = $mode =~ /([1-9])/ ? "-$1" : "";
Branch: 1 branch
BranchTrueFalseTotal
$mode =~ /([1-9])/ ? : 0 1 1
652  
653 # To maximize portability we would need to open
654 # two filehandles here, one for "| gzip $level"
655 # and another for "> $filename", and then when
656 # writing copy bytes from the first to the second.
657 # We are using IO::Handle objects for now, however,
658 # and they can only contain one stream at a time.
659 1 my $wopen = sprintf($gzip_write_open, $filename);
660  
661 1 ▶ if (open($fh, $wopen))
Branch: 1 branch
BranchTrueFalseTotal
if (open $fh, $wopen) { } 1 0 1
662 {
663 1 $fh->autoflush(1);
664 1 binmode $fh;
665  
666 1 return $fh;
667 }
668 else
669 {
670 0 return undef;
671 }
672 }
673 else
674 {
675 1 croak "IO::Zlib::gzopen_external: mode '$mode' is illegal";
676 }
677  
678 0 return undef;
679 }
680  
681 sub gzread_external
682 {
683 2 ▶ my $file = shift;
Subroutine gzread_external: called CC=3 SCAR=11.3
Pod undocumented
684 2 my $bufref = \$_[0];
685 2 ▶ my $nbytes = $_[1] || 4096;
Condition50%1/2
$_[1] || 4096
l!l
20
MC/DC0%0/1
$_[1] || 4096
$_[1]
Truth table50%1/2
A
A= $_[1]
Aresult
11
00
686  
687 # Use read() instead of sysread() because people may
688 # mix reads and readlines, and we don't want to mess
689 # the stdio buffering. See also gzreadline_external()
690 # and gzwrite_external().
691 2 my $nread = read($file, $$bufref, $nbytes);
692  
693 2 ▶ return defined $nread ? $nread : -1;
Branch: 1 branch
BranchTrueFalseTotal
defined $nread ? : 2 0 2
694 }
695  
696 sub gzwrite_external
697 {
698 1 ▶ my $file = shift;
Subroutine gzwrite_external: called CC=2 SCAR=7.0
Pod undocumented
699 1 my $buf = shift;
700  
701 # Using syswrite() is okay (cf. gzread_external())
702 # since the bytes leave this process and buffering
703 # is therefore not an issue.
704 1 my $nwrote = syswrite($file, $buf);
705  
706 1 ▶ return defined $nwrote ? $nwrote : -1;
Branch: 1 branch
BranchTrueFalseTotal
defined $nwrote ? : 1 0 1
707 }
708  
709 sub gzreadline_external
710 {
711 0 ▶ my $file = shift;
Subroutine gzreadline_external: not called CC=2 SCAR=17.9
Pod undocumented
712 0 my $bufref = \$_[0];
713  
714 # See the comment in gzread_external().
715 0 $$bufref = readline($file);
716  
717 0 ▶ return defined $$bufref ? length($$bufref) : -1;
Branch: 1 branch
BranchTrueFalseTotal
defined $$bufref ? : 0 0 0
718 }
719  
720 sub gzeof_external
721 {
722 2 ▶ my $file = shift;
Subroutine gzeof_external: called CC=1 SCAR=0.0
Pod undocumented
723  
724 2 return eof($file);
725 }
726  
727 sub gzclose_external
728 {
729 3 ▶ my $file = shift;
Subroutine gzclose_external: called CC=1 SCAR=0.0
Pod undocumented
730  
731 3 close($file);
732  
733 # I am not entirely certain why this is needed but it seems
734 # the above close() always fails (as if the stream would have
735 # been already closed - something to do with using external
736 # processes via pipes?)
737 3 return 0;
738 }
739  
740 9 1;
↑ Summary