1 package Compress::Zlib::Common;
8 use Scalar::Util qw(blessed readonly);
12 our ($VERSION, @ISA, @EXPORT);
14 $VERSION = '2.000_05';
16 @EXPORT = qw( isaFilehandle isaFilename whatIsInput whatIsOutput ckInputParam
17 isaFileGlobString cleanFileGlobString oneTarget
18 setBinModeInput setBinModeOutput
19 ckOutputParam ckInOutParams
26 sub setBinModeInput($)
30 #binmode $handle if $] == 5.008 ;
31 #binmode $handle unless isSTDIN($handle) ;
34 sub setBinModeOutput($)
38 #binmode $handle if $] == 5.008;
39 #binmode $handle unless isSTDOUT($handle) ;
44 # my $handle = shift ;
46 # return 0 unless isaFilehandle($handle);
47 # return fileno $handle == fileno STDIN || fileno $handle == fileno STDOUT;
52 # my $handle = shift ;
54 # return 0 unless isaFilehandle($handle);
55 # return fileno $handle == fileno STDIN;
60 # my $handle = shift ;
62 # return 0 unless isaFilehandle($handle);
63 # return fileno $handle == fileno STDOUT;
68 use utf8; # Pragma needed to keep Perl 5.6.0 happy
69 return (defined $_[0] and
70 (UNIVERSAL::isa($_[0],'GLOB') or UNIVERSAL::isa(\$_[0],'GLOB'))
71 and defined fileno($_[0]) )
76 return (defined $_[0] and
78 UNIVERSAL::isa(\$_[0], 'SCALAR'));
83 return defined $_[0] && $_[0] =~ /^<.*>$/;
86 sub cleanFileGlobString
90 $string =~ s/^\s*<\s*(.*)\s*>\s*$/$1/;
95 use constant WANT_CODE => 1 ;
96 use constant WANT_EXT => 2 ;
97 use constant WANT_UNDEF => 4 ;
98 use constant WANT_HASH => 8 ;
102 my $got = whatIs(@_);
104 if (defined $got && $got eq 'filename' && defined $_[0] && $_[0] eq '-')
109 $_[0] = new IO::File("<-");
115 sub whatIsOutput($;$)
117 my $got = whatIs(@_);
119 if (defined $got && $got eq 'filename' && defined $_[0] && $_[0] eq '-')
123 $_[0] = new IO::File(">-");
131 return 'handle' if isaFilehandle($_[0]);
133 my $wantCode = defined $_[1] && $_[1] & WANT_CODE ;
134 my $extended = defined $_[1] && $_[1] & WANT_EXT ;
135 my $undef = defined $_[1] && $_[1] & WANT_UNDEF ;
136 my $hash = defined $_[1] && $_[1] & WANT_HASH ;
138 return 'undef' if ! defined $_[0] && $undef ;
141 return '' if blessed($_[0]); # is an object
142 #return '' if UNIVERSAL::isa($_[0], 'UNIVERSAL'); # is an object
143 return 'buffer' if UNIVERSAL::isa($_[0], 'SCALAR');
144 return 'array' if UNIVERSAL::isa($_[0], 'ARRAY') && $extended ;
145 return 'hash' if UNIVERSAL::isa($_[0], 'HASH') && $hash ;
146 return 'code' if UNIVERSAL::isa($_[0], 'CODE') && $wantCode ;
150 return 'fileglob' if $extended && isaFileGlobString($_[0]);
156 return $_[0] =~ /^(code|handle|buffer|filename)$/;
159 sub ckInputParam ($$$;$)
162 my $inType = whatIsInput($_[0], $_[2]);
163 local $Carp::CarpLevel = 1;
165 croak "$from: input parameter not a filename, filehandle, array ref or scalar ref"
168 if ($inType eq 'filename' )
170 croak "$from: input filename is undef or null string"
171 if ! defined $_[0] || $_[0] eq '' ;
173 if ($_[0] ne '-' && ! -e $_[0] )
175 ${$_[1]} = "input file '$_[0]' does not exist";
183 sub ckOutputParam ($$$)
186 my $outType = whatIsOutput($_[0]);
187 local $Carp::CarpLevel = 1;
189 croak "$from: output parameter not a filename, filehandle or scalar ref"
192 croak "$from: output filename is undef or null string"
193 if $outType eq 'filename' && (! defined $_[0] || $_[0] eq '') ;
195 croak("$from: output buffer is read-only")
196 if $outType eq 'buffer' && readonly(${ $_[0] });
201 #sub ckInOutParams($$$$)
205 # ckInputParam($from, $_[0], $_[2])
207 # ckOutputParam($from, $_[1], $_[2])
210 # my $inType = whatIs($_[0]);
211 # my $outType = whatIs($_[1]);
213 # # Check that input != output
214 # if ($inType eq $outType && $_[0] eq $_[1])
216 # local $Carp::CarpLevel = 1;
217 # croak("$from: input and output $inType are identical");
230 my $error_ref = shift ;
231 my $reportClass = shift ;
233 my %data = (Class => $Class,
236 reportClass => $reportClass,
239 my $obj = bless \%data, $class ;
241 local $Carp::CarpLevel = 1;
243 my $inType = $data{inType} = whatIsInput($_[0], WANT_EXT|WANT_HASH);
244 my $outType = $data{outType} = whatIsOutput($_[1], WANT_EXT|WANT_HASH);
246 my $oneInput = $data{oneInput} = oneTarget($inType);
247 my $oneOutput = $data{oneOutput} = oneTarget($outType);
251 croak "$reportClass: illegal input parameter" ;
255 if ($inType eq 'hash')
258 $obj->{oneInput} = 1 ;
259 return $obj->validateHash($_[0]);
264 croak "$reportClass: illegal output parameter" ;
269 if ($inType ne 'fileglob' && $outType eq 'fileglob')
271 ${ $data{Error} } = "Need input fileglob for outout fileglob";
275 if ($inType ne 'fileglob' && $outType eq 'hash' && $inType ne 'filename' )
277 ${ $data{Error} } = "input must ne filename or fileglob when output is a hash";
281 if ($inType eq 'fileglob' && $outType eq 'fileglob')
284 $data{inType} = $data{outType} = 'filename';
285 my $mapper = new File::GlobMapper($_[0], $_[1]);
288 ${ $data{Error} } = $File::GlobMapper::Error ;
291 $data{Pairs} = $mapper->getFileMap();
296 croak("$reportClass: input and output $inType are identical")
297 if $inType eq $outType && $_[0] eq $_[1] && $_[0] ne '-' ;
299 if ($inType eq 'fileglob') # && $outType ne 'fileglob'
301 my $glob = cleanFileGlobString($_[0]);
302 my @inputs = glob($glob);
307 die "legal or die???" ;
311 $obj->validateInputFilenames($inputs[0])
314 $data{inType} = 'filename' ;
319 $obj->validateInputFilenames(@inputs)
321 $_[0] = [ @inputs ] ;
322 $data{inType} = 'filenames' ;
325 elsif ($inType eq 'filename')
327 $obj->validateInputFilenames($_[0])
330 elsif ($inType eq 'array')
332 $obj->validateInputArray($_[0])
336 croak("$reportClass: output buffer is read-only")
337 if $outType eq 'buffer' && Compress::Zlib::_readonly_ref($_[1]);
339 if ($outType eq 'filename' )
341 croak "$reportClass: output filename is undef or null string"
342 if ! defined $_[1] || $_[1] eq '' ;
349 sub Validator::validateInputFilenames
353 foreach my $filename (@_)
355 croak "$self->{reportClass}: input filename is undef or null string"
356 if ! defined $filename || $filename eq '' ;
358 next if $filename eq '-';
362 ${ $self->{Error} } = "input file '$filename' does not exist";
368 ${ $self->{Error} } = "cannot open file '$filename': $!";
376 sub Validator::validateInputArray
380 foreach my $element ( @{ $_[0] } )
382 my $inType = whatIsInput($element);
386 ${ $self->{Error} } = "unknown input parameter" ;
394 sub Validator::validateHash
399 while (my($k, $v) = each %$href)
401 my $ktype = whatIsInput($k);
402 my $vtype = whatIsOutput($v, WANT_EXT|WANT_UNDEF) ;
404 if ($ktype ne 'filename')
406 ${ $self->{Error} } = "hash key not filename" ;
410 my %valid = map { $_ => 1 } qw(filename buffer array undef handle) ;
411 if (! $valid{$vtype})
413 ${ $self->{Error} } = "hash value not ok" ;