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($)
31 unless $^O eq 'MSWin32' && ! ( ${^UNICODE} || ${^UTF8LOCALE} );
34 sub setBinModeOutput($)
39 unless $^O eq 'MSWin32' && ! ( ${^UNICODE} || ${^UTF8LOCALE} );
44 use utf8; # Pragma needed to keep Perl 5.6.0 happy
45 return (defined $_[0] and
46 (UNIVERSAL::isa($_[0],'GLOB') or UNIVERSAL::isa(\$_[0],'GLOB'))
47 and defined fileno($_[0]) )
52 return (defined $_[0] and
54 UNIVERSAL::isa(\$_[0], 'SCALAR'));
59 return defined $_[0] && $_[0] =~ /^<.*>$/;
62 sub cleanFileGlobString
66 $string =~ s/^\s*<\s*(.*)\s*>\s*$/$1/;
71 use constant WANT_CODE => 1 ;
72 use constant WANT_EXT => 2 ;
73 use constant WANT_UNDEF => 4 ;
74 use constant WANT_HASH => 8 ;
80 if (defined $got && $got eq 'filename' && defined $_[0] && $_[0] eq '-')
85 $_[0] = new IO::File("<-");
95 if (defined $got && $got eq 'filename' && defined $_[0] && $_[0] eq '-')
99 $_[0] = new IO::File(">-");
107 return 'handle' if isaFilehandle($_[0]);
109 my $wantCode = defined $_[1] && $_[1] & WANT_CODE ;
110 my $extended = defined $_[1] && $_[1] & WANT_EXT ;
111 my $undef = defined $_[1] && $_[1] & WANT_UNDEF ;
112 my $hash = defined $_[1] && $_[1] & WANT_HASH ;
114 return 'undef' if ! defined $_[0] && $undef ;
117 return '' if blessed($_[0]); # is an object
118 #return '' if UNIVERSAL::isa($_[0], 'UNIVERSAL'); # is an object
119 return 'buffer' if UNIVERSAL::isa($_[0], 'SCALAR');
120 return 'array' if UNIVERSAL::isa($_[0], 'ARRAY') && $extended ;
121 return 'hash' if UNIVERSAL::isa($_[0], 'HASH') && $hash ;
122 return 'code' if UNIVERSAL::isa($_[0], 'CODE') && $wantCode ;
126 return 'fileglob' if $extended && isaFileGlobString($_[0]);
132 return $_[0] =~ /^(code|handle|buffer|filename)$/;
135 sub ckInputParam ($$$;$)
138 my $inType = whatIsInput($_[0], $_[2]);
139 local $Carp::CarpLevel = 1;
141 croak "$from: input parameter not a filename, filehandle, array ref or scalar ref"
144 if ($inType eq 'filename' )
146 croak "$from: input filename is undef or null string"
147 if ! defined $_[0] || $_[0] eq '' ;
149 if ($_[0] ne '-' && ! -e $_[0] )
151 ${$_[1]} = "input file '$_[0]' does not exist";
159 sub ckOutputParam ($$$)
162 my $outType = whatIsOutput($_[0]);
163 local $Carp::CarpLevel = 1;
165 croak "$from: output parameter not a filename, filehandle or scalar ref"
168 croak "$from: output filename is undef or null string"
169 if $outType eq 'filename' && (! defined $_[0] || $_[0] eq '') ;
171 croak("$from: output buffer is read-only")
172 if $outType eq 'buffer' && readonly(${ $_[0] });
183 my $error_ref = shift ;
184 my $reportClass = shift ;
186 my %data = (Class => $Class,
189 reportClass => $reportClass,
192 my $obj = bless \%data, $class ;
194 local $Carp::CarpLevel = 1;
196 my $inType = $data{inType} = whatIsInput($_[0], WANT_EXT|WANT_HASH);
197 my $outType = $data{outType} = whatIsOutput($_[1], WANT_EXT|WANT_HASH);
199 my $oneInput = $data{oneInput} = oneTarget($inType);
200 my $oneOutput = $data{oneOutput} = oneTarget($outType);
204 croak "$reportClass: illegal input parameter" ;
208 if ($inType eq 'hash')
211 $obj->{oneInput} = 1 ;
212 return $obj->validateHash($_[0]);
217 croak "$reportClass: illegal output parameter" ;
222 if ($inType ne 'fileglob' && $outType eq 'fileglob')
224 ${ $data{Error} } = "Need input fileglob for outout fileglob";
228 if ($inType ne 'fileglob' && $outType eq 'hash' && $inType ne 'filename' )
230 ${ $data{Error} } = "input must ne filename or fileglob when output is a hash";
234 if ($inType eq 'fileglob' && $outType eq 'fileglob')
237 $data{inType} = $data{outType} = 'filename';
238 my $mapper = new File::GlobMapper($_[0], $_[1]);
241 ${ $data{Error} } = $File::GlobMapper::Error ;
244 $data{Pairs} = $mapper->getFileMap();
249 croak("$reportClass: input and output $inType are identical")
250 if $inType eq $outType && $_[0] eq $_[1] && $_[0] ne '-' ;
252 if ($inType eq 'fileglob') # && $outType ne 'fileglob'
254 my $glob = cleanFileGlobString($_[0]);
255 my @inputs = glob($glob);
260 die "legal or die???" ;
264 $obj->validateInputFilenames($inputs[0])
267 $data{inType} = 'filename' ;
272 $obj->validateInputFilenames(@inputs)
274 $_[0] = [ @inputs ] ;
275 $data{inType} = 'filenames' ;
278 elsif ($inType eq 'filename')
280 $obj->validateInputFilenames($_[0])
283 elsif ($inType eq 'array')
285 $obj->validateInputArray($_[0])
289 croak("$reportClass: output buffer is read-only")
290 if $outType eq 'buffer' && Compress::Zlib::_readonly_ref($_[1]);
292 if ($outType eq 'filename' )
294 croak "$reportClass: output filename is undef or null string"
295 if ! defined $_[1] || $_[1] eq '' ;
302 sub Validator::validateInputFilenames
306 foreach my $filename (@_)
308 croak "$self->{reportClass}: input filename is undef or null string"
309 if ! defined $filename || $filename eq '' ;
311 next if $filename eq '-';
315 ${ $self->{Error} } = "input file '$filename' does not exist";
321 ${ $self->{Error} } = "cannot open file '$filename': $!";
329 sub Validator::validateInputArray
333 foreach my $element ( @{ $_[0] } )
335 my $inType = whatIsInput($element);
339 ${ $self->{Error} } = "unknown input parameter" ;
347 sub Validator::validateHash
352 while (my($k, $v) = each %$href)
354 my $ktype = whatIsInput($k);
355 my $vtype = whatIsOutput($v, WANT_EXT|WANT_UNDEF) ;
357 if ($ktype ne 'filename')
359 ${ $self->{Error} } = "hash key not filename" ;
363 my %valid = map { $_ => 1 } qw(filename buffer array undef handle) ;
364 if (! $valid{$vtype})
366 ${ $self->{Error} } = "hash value not ok" ;