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
27 $needBinmode = ($^O eq 'MSWin32' ||
28 ($] >= 5.006 && eval ' ${^UNICODE} || ${^UTF8LOCALE} '))
31 sub setBinModeInput($)
39 sub setBinModeOutput($)
49 use utf8; # Pragma needed to keep Perl 5.6.0 happy
50 return (defined $_[0] and
51 (UNIVERSAL::isa($_[0],'GLOB') or UNIVERSAL::isa(\$_[0],'GLOB'))
52 and defined fileno($_[0]) )
57 return (defined $_[0] and
59 UNIVERSAL::isa(\$_[0], 'SCALAR'));
64 return defined $_[0] && $_[0] =~ /^<.*>$/;
67 sub cleanFileGlobString
71 $string =~ s/^\s*<\s*(.*)\s*>\s*$/$1/;
76 use constant WANT_CODE => 1 ;
77 use constant WANT_EXT => 2 ;
78 use constant WANT_UNDEF => 4 ;
79 use constant WANT_HASH => 8 ;
85 if (defined $got && $got eq 'filename' && defined $_[0] && $_[0] eq '-')
90 $_[0] = new IO::File("<-");
100 if (defined $got && $got eq 'filename' && defined $_[0] && $_[0] eq '-')
104 $_[0] = new IO::File(">-");
112 return 'handle' if isaFilehandle($_[0]);
114 my $wantCode = defined $_[1] && $_[1] & WANT_CODE ;
115 my $extended = defined $_[1] && $_[1] & WANT_EXT ;
116 my $undef = defined $_[1] && $_[1] & WANT_UNDEF ;
117 my $hash = defined $_[1] && $_[1] & WANT_HASH ;
119 return 'undef' if ! defined $_[0] && $undef ;
122 return '' if blessed($_[0]); # is an object
123 #return '' if UNIVERSAL::isa($_[0], 'UNIVERSAL'); # is an object
124 return 'buffer' if UNIVERSAL::isa($_[0], 'SCALAR');
125 return 'array' if UNIVERSAL::isa($_[0], 'ARRAY') && $extended ;
126 return 'hash' if UNIVERSAL::isa($_[0], 'HASH') && $hash ;
127 return 'code' if UNIVERSAL::isa($_[0], 'CODE') && $wantCode ;
131 return 'fileglob' if $extended && isaFileGlobString($_[0]);
137 return $_[0] =~ /^(code|handle|buffer|filename)$/;
140 sub ckInputParam ($$$;$)
143 my $inType = whatIsInput($_[0], $_[2]);
144 local $Carp::CarpLevel = 1;
146 croak "$from: input parameter not a filename, filehandle, array ref or scalar ref"
149 if ($inType eq 'filename' )
151 croak "$from: input filename is undef or null string"
152 if ! defined $_[0] || $_[0] eq '' ;
154 if ($_[0] ne '-' && ! -e $_[0] )
156 ${$_[1]} = "input file '$_[0]' does not exist";
164 sub ckOutputParam ($$$)
167 my $outType = whatIsOutput($_[0]);
168 local $Carp::CarpLevel = 1;
170 croak "$from: output parameter not a filename, filehandle or scalar ref"
173 croak "$from: output filename is undef or null string"
174 if $outType eq 'filename' && (! defined $_[0] || $_[0] eq '') ;
176 croak("$from: output buffer is read-only")
177 if $outType eq 'buffer' && readonly(${ $_[0] });
188 my $error_ref = shift ;
189 my $reportClass = shift ;
191 my %data = (Class => $Class,
194 reportClass => $reportClass,
197 my $obj = bless \%data, $class ;
199 local $Carp::CarpLevel = 1;
201 my $inType = $data{inType} = whatIsInput($_[0], WANT_EXT|WANT_HASH);
202 my $outType = $data{outType} = whatIsOutput($_[1], WANT_EXT|WANT_HASH);
204 my $oneInput = $data{oneInput} = oneTarget($inType);
205 my $oneOutput = $data{oneOutput} = oneTarget($outType);
209 croak "$reportClass: illegal input parameter" ;
213 if ($inType eq 'hash')
216 $obj->{oneInput} = 1 ;
217 return $obj->validateHash($_[0]);
222 croak "$reportClass: illegal output parameter" ;
227 if ($inType ne 'fileglob' && $outType eq 'fileglob')
229 ${ $data{Error} } = "Need input fileglob for outout fileglob";
233 if ($inType ne 'fileglob' && $outType eq 'hash' && $inType ne 'filename' )
235 ${ $data{Error} } = "input must ne filename or fileglob when output is a hash";
239 if ($inType eq 'fileglob' && $outType eq 'fileglob')
242 $data{inType} = $data{outType} = 'filename';
243 my $mapper = new File::GlobMapper($_[0], $_[1]);
246 ${ $data{Error} } = $File::GlobMapper::Error ;
249 $data{Pairs} = $mapper->getFileMap();
254 croak("$reportClass: input and output $inType are identical")
255 if $inType eq $outType && $_[0] eq $_[1] && $_[0] ne '-' ;
257 if ($inType eq 'fileglob') # && $outType ne 'fileglob'
259 my $glob = cleanFileGlobString($_[0]);
260 my @inputs = glob($glob);
265 die "legal or die???" ;
269 $obj->validateInputFilenames($inputs[0])
272 $data{inType} = 'filename' ;
277 $obj->validateInputFilenames(@inputs)
279 $_[0] = [ @inputs ] ;
280 $data{inType} = 'filenames' ;
283 elsif ($inType eq 'filename')
285 $obj->validateInputFilenames($_[0])
288 elsif ($inType eq 'array')
290 $obj->validateInputArray($_[0])
294 croak("$reportClass: output buffer is read-only")
295 if $outType eq 'buffer' && Compress::Zlib::_readonly_ref($_[1]);
297 if ($outType eq 'filename' )
299 croak "$reportClass: output filename is undef or null string"
300 if ! defined $_[1] || $_[1] eq '' ;
307 sub Validator::validateInputFilenames
311 foreach my $filename (@_)
313 croak "$self->{reportClass}: input filename is undef or null string"
314 if ! defined $filename || $filename eq '' ;
316 next if $filename eq '-';
320 ${ $self->{Error} } = "input file '$filename' does not exist";
326 ${ $self->{Error} } = "cannot open file '$filename': $!";
334 sub Validator::validateInputArray
338 foreach my $element ( @{ $_[0] } )
340 my $inType = whatIsInput($element);
344 ${ $self->{Error} } = "unknown input parameter" ;
352 sub Validator::validateHash
357 while (my($k, $v) = each %$href)
359 my $ktype = whatIsInput($k);
360 my $vtype = whatIsOutput($v, WANT_EXT|WANT_UNDEF) ;
362 if ($ktype ne 'filename')
364 ${ $self->{Error} } = "hash key not filename" ;
368 my %valid = map { $_ => 1 } qw(filename buffer array undef handle) ;
369 if (! $valid{$vtype})
371 ${ $self->{Error} } = "hash value not ok" ;