1 package IO::Compress::Base::Common;
8 use Scalar::Util qw(blessed readonly);
12 our ($VERSION, @ISA, @EXPORT, %EXPORT_TAGS, $HAS_ENCODE);
16 @EXPORT = qw( isaFilehandle isaFilename whatIsInput whatIsOutput
17 isaFileGlobString cleanFileGlobString oneTarget
18 setBinModeInput setBinModeOutput
34 %EXPORT_TAGS = ( Status => [qw( STATUS_OK
41 use constant STATUS_OK => 0;
42 use constant STATUS_ENDSTREAM => 1;
43 use constant STATUS_EOF => 2;
44 use constant STATUS_ERROR => -1;
48 if (! defined $HAS_ENCODE) {
55 $HAS_ENCODE = $@ ? 0 : 1 ;
65 my $want_encoding = shift ;
67 $obj->croakError("$class: Encode module needed to use -Encode")
70 my $encoding = Encode::find_encoding($want_encoding);
72 $obj->croakError("$class: Encoding '$want_encoding' is not available")
79 $needBinmode = ($^O eq 'MSWin32' ||
80 ($] >= 5.006 && eval ' ${^UNICODE} || ${^UTF8LOCALE} '))
83 sub setBinModeInput($)
91 sub setBinModeOutput($)
101 use utf8; # Pragma needed to keep Perl 5.6.0 happy
102 return (defined $_[0] and
103 (UNIVERSAL::isa($_[0],'GLOB') or
104 UNIVERSAL::isa($_[0],'IO::Handle') or
105 UNIVERSAL::isa(\$_[0],'GLOB'))
111 return (defined $_[0] and
113 UNIVERSAL::isa(\$_[0], 'SCALAR'));
116 sub isaFileGlobString
118 return defined $_[0] && $_[0] =~ /^<.*>$/;
121 sub cleanFileGlobString
125 $string =~ s/^\s*<\s*(.*)\s*>\s*$/$1/;
130 use constant WANT_CODE => 1 ;
131 use constant WANT_EXT => 2 ;
132 use constant WANT_UNDEF => 4 ;
133 #use constant WANT_HASH => 8 ;
134 use constant WANT_HASH => 0 ;
138 my $got = whatIs(@_);
140 if (defined $got && $got eq 'filename' && defined $_[0] && $_[0] eq '-')
145 #$_[0] = new IO::File("<-");
151 sub whatIsOutput($;$)
153 my $got = whatIs(@_);
155 if (defined $got && $got eq 'filename' && defined $_[0] && $_[0] eq '-')
159 #$_[0] = new IO::File(">-");
167 return 'handle' if isaFilehandle($_[0]);
169 my $wantCode = defined $_[1] && $_[1] & WANT_CODE ;
170 my $extended = defined $_[1] && $_[1] & WANT_EXT ;
171 my $undef = defined $_[1] && $_[1] & WANT_UNDEF ;
172 my $hash = defined $_[1] && $_[1] & WANT_HASH ;
174 return 'undef' if ! defined $_[0] && $undef ;
177 return '' if blessed($_[0]); # is an object
178 #return '' if UNIVERSAL::isa($_[0], 'UNIVERSAL'); # is an object
179 return 'buffer' if UNIVERSAL::isa($_[0], 'SCALAR');
180 return 'array' if UNIVERSAL::isa($_[0], 'ARRAY') && $extended ;
181 return 'hash' if UNIVERSAL::isa($_[0], 'HASH') && $hash ;
182 return 'code' if UNIVERSAL::isa($_[0], 'CODE') && $wantCode ;
186 return 'fileglob' if $extended && isaFileGlobString($_[0]);
192 return $_[0] =~ /^(code|handle|buffer|filename)$/;
195 sub IO::Compress::Base::Validator::new
200 my $error_ref = shift ;
201 my $reportClass = shift ;
203 my %data = (Class => $Class,
205 reportClass => $reportClass,
208 my $obj = bless \%data, $class ;
210 local $Carp::CarpLevel = 1;
212 my $inType = $data{inType} = whatIsInput($_[0], WANT_EXT|WANT_HASH);
213 my $outType = $data{outType} = whatIsOutput($_[1], WANT_EXT|WANT_HASH);
215 my $oneInput = $data{oneInput} = oneTarget($inType);
216 my $oneOutput = $data{oneOutput} = oneTarget($outType);
220 $obj->croakError("$reportClass: illegal input parameter") ;
224 # if ($inType eq 'hash')
227 # $obj->{oneInput} = 1 ;
228 # return $obj->validateHash($_[0]);
233 $obj->croakError("$reportClass: illegal output parameter") ;
238 if ($inType ne 'fileglob' && $outType eq 'fileglob')
240 $obj->croakError("Need input fileglob for outout fileglob");
243 # if ($inType ne 'fileglob' && $outType eq 'hash' && $inType ne 'filename' )
245 # $obj->croakError("input must ne filename or fileglob when output is a hash");
248 if ($inType eq 'fileglob' && $outType eq 'fileglob')
251 $data{inType} = $data{outType} = 'filename';
252 my $mapper = new File::GlobMapper($_[0], $_[1]);
255 return $obj->saveErrorString($File::GlobMapper::Error) ;
257 $data{Pairs} = $mapper->getFileMap();
262 $obj->croakError("$reportClass: input and output $inType are identical")
263 if $inType eq $outType && $_[0] eq $_[1] && $_[0] ne '-' ;
265 if ($inType eq 'fileglob') # && $outType ne 'fileglob'
267 my $glob = cleanFileGlobString($_[0]);
268 my @inputs = glob($glob);
272 # TODO -- legal or die?
273 die "globmap matched zero file -- legal or die???" ;
277 $obj->validateInputFilenames($inputs[0])
280 $data{inType} = 'filename' ;
285 $obj->validateInputFilenames(@inputs)
287 $_[0] = [ @inputs ] ;
288 $data{inType} = 'filenames' ;
291 elsif ($inType eq 'filename')
293 $obj->validateInputFilenames($_[0])
296 elsif ($inType eq 'array')
298 $data{inType} = 'filenames' ;
299 $obj->validateInputArray($_[0])
303 return $obj->saveErrorString("$reportClass: output buffer is read-only")
304 if $outType eq 'buffer' && readonly(${ $_[1] });
306 if ($outType eq 'filename' )
308 $obj->croakError("$reportClass: output filename is undef or null string")
309 if ! defined $_[1] || $_[1] eq '' ;
315 return $obj->saveErrorString("output file '$_[1]' is a directory");
323 sub IO::Compress::Base::Validator::saveErrorString
326 ${ $self->{Error} } = shift ;
331 sub IO::Compress::Base::Validator::croakError
334 $self->saveErrorString($_[0]);
340 sub IO::Compress::Base::Validator::validateInputFilenames
344 foreach my $filename (@_)
346 $self->croakError("$self->{reportClass}: input filename is undef or null string")
347 if ! defined $filename || $filename eq '' ;
349 next if $filename eq '-';
353 return $self->saveErrorString("input file '$filename' does not exist");
358 return $self->saveErrorString("input file '$filename' is a directory");
363 return $self->saveErrorString("cannot open file '$filename': $!");
370 sub IO::Compress::Base::Validator::validateInputArray
374 if ( @{ $_[0] } == 0 )
376 return $self->saveErrorString("empty array reference") ;
379 foreach my $element ( @{ $_[0] } )
381 my $inType = whatIsInput($element);
385 $self->croakError("unknown input parameter") ;
387 elsif($inType eq 'filename')
389 $self->validateInputFilenames($element)
394 $self->croakError("not a filename") ;
401 #sub IO::Compress::Base::Validator::validateHash
406 # while (my($k, $v) = each %$href)
408 # my $ktype = whatIsInput($k);
409 # my $vtype = whatIsOutput($v, WANT_EXT|WANT_UNDEF) ;
411 # if ($ktype ne 'filename')
413 # return $self->saveErrorString("hash key not filename") ;
416 # my %valid = map { $_ => 1 } qw(filename buffer array undef handle) ;
417 # if (! $valid{$vtype})
419 # return $self->saveErrorString("hash value not ok") ;
426 sub createSelfTiedObject
428 my $class = shift || (caller)[0] ;
429 my $error_ref = shift ;
431 my $obj = bless Symbol::gensym(), ref($class) || $class;
432 tie *$obj, $obj if $] >= 5.005;
433 *$obj->{Closed} = 1 ;
435 *$obj->{Error} = $error_ref ;
437 *$obj->{ErrorNo} = \$errno ;
444 #package Parse::Parameters ;
448 #our ($VERSION, @ISA, @EXPORT);
449 #$VERSION = '2.000_08';
450 #@ISA = qw(Exporter);
452 $EXPORT_TAGS{Parse} = [qw( ParseParameters
453 Parse_any Parse_unsigned Parse_signed
454 Parse_boolean Parse_custom Parse_string
455 Parse_multiple Parse_writable_scalar
459 push @EXPORT, @{ $EXPORT_TAGS{Parse} } ;
461 use constant Parse_any => 0x01;
462 use constant Parse_unsigned => 0x02;
463 use constant Parse_signed => 0x04;
464 use constant Parse_boolean => 0x08;
465 use constant Parse_string => 0x10;
466 use constant Parse_custom => 0x12;
468 #use constant Parse_store_ref => 0x100 ;
469 use constant Parse_multiple => 0x100 ;
470 use constant Parse_writable => 0x200 ;
471 use constant Parse_writable_scalar => 0x400 | Parse_writable ;
473 use constant OFF_PARSED => 0 ;
474 use constant OFF_TYPE => 1 ;
475 use constant OFF_DEFAULT => 2 ;
476 use constant OFF_FIXED => 3 ;
477 use constant OFF_FIRST_ONLY => 4 ;
478 use constant OFF_STICKY => 5 ;
484 my $level = shift || 0 ;
486 my $sub = (caller($level + 1))[3] ;
487 local $Carp::CarpLevel = 1 ;
490 if @_ == 2 && defined $_[1] && UNIVERSAL::isa($_[1], "IO::Compress::Base::Parameters");
492 my $p = new IO::Compress::Base::Parameters() ;
494 or croak "$sub: $p->{Error}" ;
499 #package IO::Compress::Base::Parameters;
505 sub IO::Compress::Base::Parameters::new
509 my $obj = { Error => '',
513 #return bless $obj, ref($class) || $class || __PACKAGE__ ;
514 return bless $obj, 'IO::Compress::Base::Parameters' ;
517 sub IO::Compress::Base::Parameters::setError
521 my $retval = @_ ? shift : undef ;
523 $self->{Error} = $error ;
530 # return $self->{Error} ;
533 sub IO::Compress::Base::Parameters::parse
537 my $default = shift ;
539 my $got = $self->{Got} ;
540 my $firstTime = keys %{ $got } == 0 ;
546 # Allow the options to be passed as a hash reference or
547 # as the complete hash.
554 return $self->setError("Expected even number of parameters, got 1")
555 if ! defined $href or ! ref $href or ref $href ne "HASH" ;
557 foreach my $key (keys %$href) {
558 push @entered, $key ;
559 push @entered, \$href->{$key} ;
564 return $self->setError("Expected even number of parameters, got $count")
567 for my $i (0.. $count / 2 - 1) {
568 if ($_[2 * $i] eq '__xxx__') {
569 $other = $_[2 * $i + 1] ;
572 push @entered, $_[2 * $i] ;
573 push @entered, \$_[2 * $i + 1] ;
579 while (my ($key, $v) = each %$default)
581 croak "need 4 params [@$v]"
584 my ($first_only, $sticky, $type, $value) = @$v ;
586 $self->_checkType($key, \$value, $type, 0, \$x)
591 if ($firstTime || ! $sticky) {
593 if $type & Parse_multiple;
595 $got->{$key} = [0, $type, $value, $x, $first_only, $sticky] ;
598 $got->{$key}[OFF_PARSED] = 0 ;
605 for my $key (keys %$default)
607 my $canonkey = lc $key;
608 if ($other->parsed($canonkey))
610 my $value = $other->value($canonkey);
611 #print "SET '$canonkey' to $value [$$value]\n";
612 ++ $parsed{$canonkey};
613 $got->{$canonkey}[OFF_PARSED] = 1;
614 $got->{$canonkey}[OFF_DEFAULT] = $value;
615 $got->{$canonkey}[OFF_FIXED] = $value;
620 for my $i (0.. @entered / 2 - 1) {
621 my $key = $entered[2* $i] ;
622 my $value = $entered[2* $i+1] ;
624 #print "Key [$key] Value [$value]" ;
625 #print defined $$value ? "[$$value]\n" : "[undef]\n";
628 my $canonkey = lc $key;
630 if ($got->{$canonkey} && ($firstTime ||
631 ! $got->{$canonkey}[OFF_FIRST_ONLY] ))
633 my $type = $got->{$canonkey}[OFF_TYPE] ;
634 my $parsed = $parsed{$canonkey};
635 ++ $parsed{$canonkey};
637 return $self->setError("Muliple instances of '$key' found")
638 if $parsed && $type & Parse_multiple == 0 ;
641 $self->_checkType($key, $value, $type, 1, \$s)
645 if ($type & Parse_multiple) {
646 $got->{$canonkey}[OFF_PARSED] = 1;
647 push @{ $got->{$canonkey}[OFF_FIXED] }, $s ;
650 $got->{$canonkey} = [1, $type, $value, $s] ;
654 { push (@Bad, $key) }
658 my ($bad) = join(", ", @Bad) ;
659 return $self->setError("unknown key value(s) $bad") ;
665 sub IO::Compress::Base::Parameters::_checkType
672 my $validate = shift ;
675 #local $Carp::CarpLevel = $level ;
676 #print "PARSE $type $key $value $validate $sub\n" ;
678 if ($type & Parse_writable_scalar)
680 return $self->setError("Parameter '$key' not writable")
681 if $validate && readonly $$value ;
685 return $self->setError("Parameter '$key' not a scalar reference")
686 if $validate && ref $$value ne 'SCALAR' ;
692 return $self->setError("Parameter '$key' not a scalar")
693 if $validate && ref $value ne 'SCALAR' ;
701 # if ($type & Parse_store_ref)
704 # # if ref ${ $value } ;
706 # $$output = $value ;
712 if ($type & Parse_any)
717 elsif ($type & Parse_unsigned)
719 return $self->setError("Parameter '$key' must be an unsigned int, got 'undef'")
720 if $validate && ! defined $value ;
721 return $self->setError("Parameter '$key' must be an unsigned int, got '$value'")
722 if $validate && $value !~ /^\d+$/;
724 $$output = defined $value ? $value : 0 ;
727 elsif ($type & Parse_signed)
729 return $self->setError("Parameter '$key' must be a signed int, got 'undef'")
730 if $validate && ! defined $value ;
731 return $self->setError("Parameter '$key' must be a signed int, got '$value'")
732 if $validate && $value !~ /^-?\d+$/;
734 $$output = defined $value ? $value : 0 ;
737 elsif ($type & Parse_boolean)
739 return $self->setError("Parameter '$key' must be an int, got '$value'")
740 if $validate && defined $value && $value !~ /^\d*$/;
741 $$output = defined $value ? $value != 0 : 0 ;
744 elsif ($type & Parse_string)
746 $$output = defined $value ? $value : "" ;
756 sub IO::Compress::Base::Parameters::parsed
761 return $self->{Got}{lc $name}[OFF_PARSED] ;
764 sub IO::Compress::Base::Parameters::value
771 $self->{Got}{lc $name}[OFF_PARSED] = 1;
772 $self->{Got}{lc $name}[OFF_DEFAULT] = $_[0] ;
773 $self->{Got}{lc $name}[OFF_FIXED] = $_[0] ;
776 return $self->{Got}{lc $name}[OFF_FIXED] ;
779 sub IO::Compress::Base::Parameters::valueOrDefault
783 my $default = shift ;
785 my $value = $self->{Got}{lc $name}[OFF_DEFAULT] ;
787 return $value if defined $value ;
791 sub IO::Compress::Base::Parameters::wantValue
796 return defined $self->{Got}{lc $name}[OFF_DEFAULT] ;
800 sub IO::Compress::Base::Parameters::clone
806 while (my ($k, $v) = each %{ $self->{Got} }) {
810 $obj->{Error} = $self->{Error};
811 $obj->{Got} = \%got ;
813 return bless $obj, 'IO::Compress::Base::Parameters' ;
818 use constant MAX32 => 0xFFFFFFFF ;
819 use constant HI_1 => MAX32 + 1 ;
820 use constant LOW => 0 ;
821 use constant HIGH => 1;
838 bless [$low, $high], $class;
845 my ($low, $hi) = unpack "V V", $string ;
846 bless [ $low, $hi ], "U64";
853 my $low = unpack "V", $string ;
854 bless [ $low, 0 ], "U64";
860 $self->[HIGH] = $self->[LOW] = 0;
866 bless [ @$self ], ref $self ;
872 return $self->[HIGH];
890 # Not using << here because the result will still be
891 # a 32-bit value on systems where int size is 32-bits
892 return $self->[HIGH] * HI_1 + $self->[LOW];
900 if (ref $value eq 'U64') {
901 $self->[HIGH] += $value->[HIGH] ;
902 $value = $value->[LOW];
905 my $available = MAX32 - $self->[LOW] ;
907 if ($value > $available) {
909 $self->[LOW] = $value - $available - 1;
912 $self->[LOW] += $value ;
922 return $self->[LOW] == $other->[LOW] &&
923 $self->[HIGH] == $other->[HIGH] ;
929 return $self->[HIGH] > 0 ;
936 return pack "V V", @$self ;
943 return pack "V", $self->[LOW] ;
950 return pack "V V", $low, 0;
954 package IO::Compress::Base::Common;