1 package IO::Compress::Base::Common;
8 use Scalar::Util qw(blessed readonly);
12 our ($VERSION, @ISA, @EXPORT, %EXPORT_TAGS);
14 $VERSION = '2.000_14';
16 @EXPORT = qw( isaFilehandle isaFilename whatIsInput whatIsOutput
17 isaFileGlobString cleanFileGlobString oneTarget
18 setBinModeInput setBinModeOutput
33 %EXPORT_TAGS = ( Status => [qw( STATUS_OK
40 use constant STATUS_OK => 0;
41 use constant STATUS_ENDSTREAM => 1;
42 use constant STATUS_EOF => 2;
43 use constant STATUS_ERROR => -1;
44 #use constant STATUS_OK => 0;
45 #use constant STATUS_ENDSTREAM => 1;
46 #use constant STATUS_ERROR => 2;
47 #use constant STATUS_EOF => 3;
50 $needBinmode = ($^O eq 'MSWin32' ||
51 ($] >= 5.006 && eval ' ${^UNICODE} || ${^UTF8LOCALE} '))
54 sub setBinModeInput($)
62 sub setBinModeOutput($)
72 use utf8; # Pragma needed to keep Perl 5.6.0 happy
73 return (defined $_[0] and
74 (UNIVERSAL::isa($_[0],'GLOB') or UNIVERSAL::isa(\$_[0],'GLOB'))
75 and defined fileno($_[0]) )
80 return (defined $_[0] and
82 UNIVERSAL::isa(\$_[0], 'SCALAR'));
87 return defined $_[0] && $_[0] =~ /^<.*>$/;
90 sub cleanFileGlobString
94 $string =~ s/^\s*<\s*(.*)\s*>\s*$/$1/;
99 use constant WANT_CODE => 1 ;
100 use constant WANT_EXT => 2 ;
101 use constant WANT_UNDEF => 4 ;
102 #use constant WANT_HASH => 8 ;
103 use constant WANT_HASH => 0 ;
107 my $got = whatIs(@_);
109 if (defined $got && $got eq 'filename' && defined $_[0] && $_[0] eq '-')
114 #$_[0] = new IO::File("<-");
120 sub whatIsOutput($;$)
122 my $got = whatIs(@_);
124 if (defined $got && $got eq 'filename' && defined $_[0] && $_[0] eq '-')
128 #$_[0] = new IO::File(">-");
136 return 'handle' if isaFilehandle($_[0]);
138 my $wantCode = defined $_[1] && $_[1] & WANT_CODE ;
139 my $extended = defined $_[1] && $_[1] & WANT_EXT ;
140 my $undef = defined $_[1] && $_[1] & WANT_UNDEF ;
141 my $hash = defined $_[1] && $_[1] & WANT_HASH ;
143 return 'undef' if ! defined $_[0] && $undef ;
146 return '' if blessed($_[0]); # is an object
147 #return '' if UNIVERSAL::isa($_[0], 'UNIVERSAL'); # is an object
148 return 'buffer' if UNIVERSAL::isa($_[0], 'SCALAR');
149 return 'array' if UNIVERSAL::isa($_[0], 'ARRAY') && $extended ;
150 return 'hash' if UNIVERSAL::isa($_[0], 'HASH') && $hash ;
151 return 'code' if UNIVERSAL::isa($_[0], 'CODE') && $wantCode ;
155 return 'fileglob' if $extended && isaFileGlobString($_[0]);
161 return $_[0] =~ /^(code|handle|buffer|filename)$/;
169 my $error_ref = shift ;
170 my $reportClass = shift ;
172 my %data = (Class => $Class,
174 reportClass => $reportClass,
177 my $obj = bless \%data, $class ;
179 local $Carp::CarpLevel = 1;
181 my $inType = $data{inType} = whatIsInput($_[0], WANT_EXT|WANT_HASH);
182 my $outType = $data{outType} = whatIsOutput($_[1], WANT_EXT|WANT_HASH);
184 my $oneInput = $data{oneInput} = oneTarget($inType);
185 my $oneOutput = $data{oneOutput} = oneTarget($outType);
189 $obj->croakError("$reportClass: illegal input parameter") ;
193 # if ($inType eq 'hash')
196 # $obj->{oneInput} = 1 ;
197 # return $obj->validateHash($_[0]);
202 $obj->croakError("$reportClass: illegal output parameter") ;
207 if ($inType ne 'fileglob' && $outType eq 'fileglob')
209 $obj->croakError("Need input fileglob for outout fileglob");
212 # if ($inType ne 'fileglob' && $outType eq 'hash' && $inType ne 'filename' )
214 # $obj->croakError("input must ne filename or fileglob when output is a hash");
217 if ($inType eq 'fileglob' && $outType eq 'fileglob')
220 $data{inType} = $data{outType} = 'filename';
221 my $mapper = new File::GlobMapper($_[0], $_[1]);
224 return $obj->saveErrorString($File::GlobMapper::Error) ;
226 $data{Pairs} = $mapper->getFileMap();
231 $obj->croakError("$reportClass: input and output $inType are identical")
232 if $inType eq $outType && $_[0] eq $_[1] && $_[0] ne '-' ;
234 if ($inType eq 'fileglob') # && $outType ne 'fileglob'
236 my $glob = cleanFileGlobString($_[0]);
237 my @inputs = glob($glob);
241 # TODO -- legal or die?
242 die "globmap matched zero file -- legal or die???" ;
246 $obj->validateInputFilenames($inputs[0])
249 $data{inType} = 'filename' ;
254 $obj->validateInputFilenames(@inputs)
256 $_[0] = [ @inputs ] ;
257 $data{inType} = 'filenames' ;
260 elsif ($inType eq 'filename')
262 $obj->validateInputFilenames($_[0])
265 elsif ($inType eq 'array')
267 $data{inType} = 'filenames' ;
268 $obj->validateInputArray($_[0])
272 return $obj->saveErrorString("$reportClass: output buffer is read-only")
273 if $outType eq 'buffer' && readonly(${ $_[1] });
275 if ($outType eq 'filename' )
277 $obj->croakError("$reportClass: output filename is undef or null string")
278 if ! defined $_[1] || $_[1] eq '' ;
284 return $obj->saveErrorString("output file '$_[1]' is a directory");
292 sub Validator::saveErrorString
295 ${ $self->{Error} } = shift ;
300 sub Validator::croakError
303 $self->saveErrorString($_[0]);
309 sub Validator::validateInputFilenames
313 foreach my $filename (@_)
315 $self->croakError("$self->{reportClass}: input filename is undef or null string")
316 if ! defined $filename || $filename eq '' ;
318 next if $filename eq '-';
322 return $self->saveErrorString("input file '$filename' does not exist");
327 return $self->saveErrorString("input file '$filename' is a directory");
332 return $self->saveErrorString("cannot open file '$filename': $!");
339 sub Validator::validateInputArray
343 if ( @{ $_[0] } == 0 )
345 return $self->saveErrorString("empty array reference") ;
348 foreach my $element ( @{ $_[0] } )
350 my $inType = whatIsInput($element);
354 $self->croakError("unknown input parameter") ;
356 elsif($inType eq 'filename')
358 $self->validateInputFilenames($element)
363 $self->croakError("not a filename") ;
370 #sub Validator::validateHash
375 # while (my($k, $v) = each %$href)
377 # my $ktype = whatIsInput($k);
378 # my $vtype = whatIsOutput($v, WANT_EXT|WANT_UNDEF) ;
380 # if ($ktype ne 'filename')
382 # return $self->saveErrorString("hash key not filename") ;
385 # my %valid = map { $_ => 1 } qw(filename buffer array undef handle) ;
386 # if (! $valid{$vtype})
388 # return $self->saveErrorString("hash value not ok") ;
395 sub createSelfTiedObject
397 my $class = shift || (caller)[0] ;
398 my $error_ref = shift ;
400 my $obj = bless Symbol::gensym(), ref($class) || $class;
401 tie *$obj, $obj if $] >= 5.005;
402 *$obj->{Closed} = 1 ;
404 *$obj->{Error} = $error_ref ;
406 *$obj->{ErrorNo} = \$errno ;
413 #package Parse::Parameters ;
417 #our ($VERSION, @ISA, @EXPORT);
418 #$VERSION = '2.000_08';
419 #@ISA = qw(Exporter);
421 $EXPORT_TAGS{Parse} = [qw( ParseParameters
422 Parse_any Parse_unsigned Parse_signed
423 Parse_boolean Parse_custom Parse_string
424 Parse_multiple Parse_writable_scalar
428 push @EXPORT, @{ $EXPORT_TAGS{Parse} } ;
430 use constant Parse_any => 0x01;
431 use constant Parse_unsigned => 0x02;
432 use constant Parse_signed => 0x04;
433 use constant Parse_boolean => 0x08;
434 use constant Parse_string => 0x10;
435 use constant Parse_custom => 0x12;
437 #use constant Parse_store_ref => 0x100 ;
438 use constant Parse_multiple => 0x100 ;
439 use constant Parse_writable => 0x200 ;
440 use constant Parse_writable_scalar => 0x400 | Parse_writable ;
442 use constant OFF_PARSED => 0 ;
443 use constant OFF_TYPE => 1 ;
444 use constant OFF_DEFAULT => 2 ;
445 use constant OFF_FIXED => 3 ;
446 use constant OFF_FIRST_ONLY => 4 ;
447 use constant OFF_STICKY => 5 ;
453 my $level = shift || 0 ;
455 my $sub = (caller($level + 1))[3] ;
456 local $Carp::CarpLevel = 1 ;
457 my $p = new IO::Compress::Base::Parameters() ;
459 or croak "$sub: $p->{Error}" ;
464 #package IO::Compress::Base::Parameters;
470 sub IO::Compress::Base::Parameters::new
474 my $obj = { Error => '',
478 #return bless $obj, ref($class) || $class || __PACKAGE__ ;
479 return bless $obj, 'IO::Compress::Base::Parameters' ;
482 sub IO::Compress::Base::Parameters::setError
486 my $retval = @_ ? shift : undef ;
488 $self->{Error} = $error ;
495 # return $self->{Error} ;
498 sub IO::Compress::Base::Parameters::parse
502 my $default = shift ;
504 my $got = $self->{Got} ;
505 my $firstTime = keys %{ $got } == 0 ;
510 # Allow the options to be passed as a hash reference or
511 # as the complete hash.
517 return $self->setError("Expected even number of parameters, got 1")
518 if ! defined $href or ! ref $href or ref $href ne "HASH" ;
520 foreach my $key (keys %$href) {
521 push @entered, $key ;
522 push @entered, \$href->{$key} ;
527 return $self->setError("Expected even number of parameters, got $count")
530 for my $i (0.. $count / 2 - 1) {
531 push @entered, $_[2* $i] ;
532 push @entered, \$_[2* $i+1] ;
537 while (my ($key, $v) = each %$default)
539 croak "need 4 params [@$v]"
542 my ($first_only, $sticky, $type, $value) = @$v ;
544 $self->_checkType($key, \$value, $type, 0, \$x)
549 if ($firstTime || ! $sticky) {
551 if $type & Parse_multiple;
553 $got->{$key} = [0, $type, $value, $x, $first_only, $sticky] ;
556 $got->{$key}[OFF_PARSED] = 0 ;
560 for my $i (0.. @entered / 2 - 1) {
561 my $key = $entered[2* $i] ;
562 my $value = $entered[2* $i+1] ;
564 #print "Key [$key] Value [$value]" ;
565 #print defined $$value ? "[$$value]\n" : "[undef]\n";
568 my $canonkey = lc $key;
570 if ($got->{$canonkey} && ($firstTime ||
571 ! $got->{$canonkey}[OFF_FIRST_ONLY] ))
573 my $type = $got->{$canonkey}[OFF_TYPE] ;
574 my $parsed = $parsed{$canonkey};
575 ++ $parsed{$canonkey};
577 return $self->setError("Muliple instances of '$key' found")
578 if $parsed && $type & Parse_multiple == 0 ;
581 $self->_checkType($key, $value, $type, 1, \$s)
585 if ($type & Parse_multiple) {
586 $got->{$canonkey}[OFF_PARSED] = 1;
587 push @{ $got->{$canonkey}[OFF_FIXED] }, $s ;
590 $got->{$canonkey} = [1, $type, $value, $s] ;
594 { push (@Bad, $key) }
598 my ($bad) = join(", ", @Bad) ;
599 return $self->setError("unknown key value(s) @Bad") ;
605 sub IO::Compress::Base::Parameters::_checkType
612 my $validate = shift ;
615 #local $Carp::CarpLevel = $level ;
616 #print "PARSE $type $key $value $validate $sub\n" ;
618 if ($type & Parse_writable_scalar)
620 return $self->setError("Parameter '$key' not writable")
621 if $validate && readonly $$value ;
625 return $self->setError("Parameter '$key' not a scalar reference")
626 if $validate && ref $$value ne 'SCALAR' ;
632 return $self->setError("Parameter '$key' not a scalar")
633 if $validate && ref $value ne 'SCALAR' ;
641 # if ($type & Parse_store_ref)
644 # # if ref ${ $value } ;
646 # $$output = $value ;
652 if ($type & Parse_any)
657 elsif ($type & Parse_unsigned)
659 return $self->setError("Parameter '$key' must be an unsigned int, got 'undef'")
660 if $validate && ! defined $value ;
661 return $self->setError("Parameter '$key' must be an unsigned int, got '$value'")
662 if $validate && $value !~ /^\d+$/;
664 $$output = defined $value ? $value : 0 ;
667 elsif ($type & Parse_signed)
669 return $self->setError("Parameter '$key' must be a signed int, got 'undef'")
670 if $validate && ! defined $value ;
671 return $self->setError("Parameter '$key' must be a signed int, got '$value'")
672 if $validate && $value !~ /^-?\d+$/;
674 $$output = defined $value ? $value : 0 ;
677 elsif ($type & Parse_boolean)
679 return $self->setError("Parameter '$key' must be an int, got '$value'")
680 if $validate && defined $value && $value !~ /^\d*$/;
681 $$output = defined $value ? $value != 0 : 0 ;
684 elsif ($type & Parse_string)
686 $$output = defined $value ? $value : "" ;
696 sub IO::Compress::Base::Parameters::parsed
701 return $self->{Got}{lc $name}[OFF_PARSED] ;
704 sub IO::Compress::Base::Parameters::value
711 $self->{Got}{lc $name}[OFF_PARSED] = 1;
712 $self->{Got}{lc $name}[OFF_DEFAULT] = $_[0] ;
713 $self->{Got}{lc $name}[OFF_FIXED] = $_[0] ;
716 return $self->{Got}{lc $name}[OFF_FIXED] ;
719 sub IO::Compress::Base::Parameters::valueOrDefault
723 my $default = shift ;
725 my $value = $self->{Got}{lc $name}[OFF_DEFAULT] ;
727 return $value if defined $value ;
731 sub IO::Compress::Base::Parameters::wantValue
736 return defined $self->{Got}{lc $name}[OFF_DEFAULT] ;
740 sub IO::Compress::Base::Parameters::clone
746 while (my ($k, $v) = each %{ $self->{Got} }) {
750 $obj->{Error} = $self->{Error};
751 $obj->{Got} = \%got ;
753 return bless $obj, 'IO::Compress::Base::Parameters' ;
758 use constant MAX32 => 0xFFFFFFFF ;
759 use constant LOW => 0 ;
760 use constant HIGH => 1;
777 bless [$low, $high], $class;
784 my ($low, $hi) = unpack "V V", $string ;
785 bless [ $low, $hi ], "U64";
792 my $low = unpack "V", $string ;
793 bless [ $low, 0 ], "U64";
799 $self->[HIGH] = $self->[LOW] = 0;
805 bless [ @$self ], ref $self ;
811 return $self->[HIGH];
831 if (ref $value eq 'U64') {
832 $self->[HIGH] += $value->[HIGH] ;
833 $value = $value->[LOW];
836 my $available = MAX32 - $self->[LOW] ;
838 if ($value > $available) {
840 $self->[LOW] = $value - $available - 1;
843 $self->[LOW] += $value ;
852 return $self->[LOW] == $other->[LOW] &&
853 $self->[HIGH] == $other->[HIGH] ;
860 return pack "V V", @$self ;
867 return pack "V", $self->[LOW] ;
874 return pack "V V", $low, 0;
878 package IO::Compress::Base::Common;