blib/lib/PerlIO/via/xz.pm untested

filter: Statementstmt 0.0% 0 / 51 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 19 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 105 CC 4 SCAR 14.8
CC
4
Cov
0%
FILENO
12.0
CLOSE
6.0
import
2.0
CRAP 4.4
1 package PerlIO::via::xz;
2  
3 use 5.012000;
4 use warnings;
5  
6 use PerlIO;
7 use IO::Compress::Xz qw( $XzError );
8 use IO::Uncompress::UnXz qw( $UnXzError );
9 #use Data::Peek;
10 use Carp;
11  
12 our $VERSION = "0.07";
13  
14 sub import {
15 my ($class, %args) = @_;
16  
17 #DDumper { import => \@_ };
18 } # import
19  
20 # $class->PUSHED ([$mode, [$fh]])
21 # Should return an object or the class, or -1 on failure. (Compare
22 # TIEHANDLE.) The arguments are an optional mode string ("r", "w",
23 # "w+", ...) and a filehandle for the PerlIO layer below. Mandatory.
24 #
25 # When the layer is pushed as part of an "open" call, "PUSHED" will be
26 # called before the actual open occurs, whether that be via "OPEN",
27 # "SYSOPEN", "FDOPEN" or by letting a lower layer do the open.
28 sub PUSHED {
29 my ($class, $mode, $fh) = @_;
30  
31 #DDumper { PUSHED => \@_ };
32 $mode =~ m/^[wr]$/ or return 1;
33 my $self = {
34 mode => $mode, # "r" or "w"
35 fh => undef,
36 level => 9, # Not yet settable
37 bsz => 4096, # Not yet settable
38 xz => undef,
39 };
40 return bless $self => $class;
41 } # PUSHED
42  
43 sub FILENO {
44 my ($self, $fh) = @_;
45 #DDumper { FILENO => \@_ };
46 unless (defined $self->{xz}) {
47 $self->{fh} = $fh;
48 $self->{fileno} = fileno $fh;
49 if ($self->{mode} eq "r") {
50 my $in = $fh;
51 $self->{xz} = IO::Uncompress::UnXz->new ($in,
52 BlockSize => $self->{bsz},
53 ) or croak "Something went wrong in new (): $UnXzError";
54 }
55 else {
56 my $out = $fh;
57 $self->{xz} = IO::Compress::Xz->new ($out,
58 AutoClose => 1,
59 Preset => $self->{level},
60 ) or croak "Something went wrong in new (): $XzError";
61 $self->{xz}->autoflush (1);
62 }
63 }
64 #DDumper $self;
65 $self->{fileno};
66 } # FILENO
67  
68 # $obj->POPPED ([$fh])
69 # Optional - called when the layer is about to be removed.
70 #
71 # $obj->UTF8 ($belowFlag, [$fh])
72 # Optional - if present it will be called immediately after PUSHED
73 # has returned. It should return a true value if the layer expects
74 # data to be UTF-8 encoded. If it returns true, the result is as if
75 # the caller had done
76 #
77 # ":via(YourClass):utf8"
78 #
79 # If not present or if it returns false, then the stream is left with
80 # the UTF-8 flag clear. The $belowFlag argument will be true if
81 # there is a layer below and that layer was expecting UTF-8.
82 #
83 # $obj->OPEN ($path, $mode, [$fh])
84 # Optional - if not present a lower layer does the open. If present,
85 # called for normal opens after the layer is pushed. This function
86 # is subject to change as there is no easy way to get a lower layer
87 # to do the open and then regain control.
88 #
89 # $obj->BINMODE ([$fh])
90 # Optional - if not present the layer is popped on binmode ($fh) or
91 # when ":raw" is pushed. If present it should return 0 on success, -1
92 # on error, or undef to pop the layer.
93 #
94 # $obj->FDOPEN ($fd, [$fh])
95 # Optional - if not present a lower layer does the open. If present,
96 # called after the layer is pushed for opens which pass a numeric
97 # file descriptor. This function is subject to change as there is no
98 # easy way to get a lower layer to do the open and then regain control.
99 #
100 # $obj->SYSOPEN ($path, $imode, $perm, [$fh])
101 # Optional - if not present a lower layer does the open. If present,
102 # called after the layer is pushed for sysopen style opens which pass
103 # a numeric mode and permissions. This function is subject to change
104 # as there is no easy way to get a lower layer to do the open and
105 # then regain control.
106 #
107 # $obj->FILENO ($fh)
108 # Returns a numeric value for a Unix-like file descriptor. Returns -1
109 # if there isn't one. Optional. Default is fileno ($fh).
110 #
111 # $obj->READ ($buffer, $len, $fh)
112 # Returns the number of octets placed in $buffer (must be less than
113 # or equal to $len). Optional. Default is to use FILL instead.
114 #
115 # $obj->WRITE ($buffer, $fh)
116 # Returns the number of octets from $buffer that have been
117 # successfully written.
118 sub WRITE {
119 my ($self, $buf, $fh) = @_;
120  
121 #DDumper { WRITE => \@_ };
122 return $self->{xz}->write ($buf);
123 } # WRITE
124  
125 # $obj->FILL ($fh)
126 # Should return a string to be placed in the buffer. Optional. If
127 # not provided, must provide READ or reject handles open for reading
128 # in PUSHED.
129 sub FILL {
130 my ($self, $fh) = @_;
131  
132 #DDumper { FILL => \@_ };
133 my $data = $self->{xz}->getline;
134 #DDumper { data => $data };
135 return $data;
136 } # FILL
137  
138 # $obj->CLOSE ($fh)
139 # Should return 0 on success, -1 on error. Optional.
140 sub CLOSE {
141 my ($self, $fh) = @_;
142 #DDumper { CLOSE => \@_ };
143  
144 ref $self && $self->{xz} or return -1;
145 $self->{xz}->flush;
146 $self->{xz}->close;
147 return ($fh ? $fh->close : 0);
148 } # CLOSE
149  
150 # $obj->SEEK ($posn,$whence,$fh)
151 # Should return 0 on success, -1 on error. Optional. Default is to
152 # fail, but that is likely to be changed in future.
153 #
154 # $obj->TELL ($fh)
155 # Returns file position. Optional. Default to be determined.
156 #
157 # $obj->UNREAD ($buffer, $fh)
158 # Returns the number of octets from $buffer that have been
159 # successfully saved to be returned on future FILL/READ calls.
160 # Optional. Default is to push data into a temporary layer above
161 # this one.
162 #
163 # $obj->FLUSH ($fh)
164 # Flush any buffered write data. May possibly be called on readable
165 # handles too. Should return 0 on success, -1 on error.
166 sub FLUSH {
167 my ($self, $fh) = @_;
168 #DDumper { FLUSH => \@_ };
169  
170 ref $self && $self->{xz} or return 0;
171 $self->{xz}->flush;
172 $self->{mode} eq "w" and $self->{xz}->close;
173 return 0;
174 } # FLUSH
175  
176 # $obj->SETLINEBUF ($fh)
177 # Optional. No return.
178 #
179 # $obj->CLEARERR ($fh)
180 # Optional. No return.
181 #
182 # $obj->ERROR ($fh)
183 # Optional. Returns error state. Default is no error until a
184 # mechanism to signal error (die?) is worked out.
185 #
186 # $obj->EOF ($fh)
187 # Optional. Returns end-of-file state. Default is a function of the
188 # return value of FILL or READ.
189  
190 1;
191 __END__