1 package IO::Compress::Base::Common;
8 use Scalar::Util qw(blessed readonly);
12 our ($VERSION, @ISA, @EXPORT, %EXPORT_TAGS);
14 $VERSION = '2.000_13';
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
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 ;
439 use constant OFF_PARSED => 0 ;
440 use constant OFF_TYPE => 1 ;
441 use constant OFF_DEFAULT => 2 ;
442 use constant OFF_FIXED => 3 ;
443 use constant OFF_FIRST_ONLY => 4 ;
444 use constant OFF_STICKY => 5 ;
450 my $level = shift || 0 ;
452 my $sub = (caller($level + 1))[3] ;
453 local $Carp::CarpLevel = 1 ;
454 my $p = new IO::Compress::Base::Parameters() ;
456 or croak "$sub: $p->{Error}" ;
461 #package IO::Compress::Base::Parameters;
467 sub IO::Compress::Base::Parameters::new
471 my $obj = { Error => '',
475 #return bless $obj, ref($class) || $class || __PACKAGE__ ;
476 return bless $obj, 'IO::Compress::Base::Parameters' ;
479 sub IO::Compress::Base::Parameters::setError
483 my $retval = @_ ? shift : undef ;
485 $self->{Error} = $error ;
492 # return $self->{Error} ;
495 sub IO::Compress::Base::Parameters::parse
499 my $default = shift ;
501 my $got = $self->{Got} ;
502 my $firstTime = keys %{ $got } == 0 ;
507 # Allow the options to be passed as a hash reference or
508 # as the complete hash.
514 return $self->setError("Expected even number of parameters, got 1")
515 if ! defined $href or ! ref $href or ref $href ne "HASH" ;
517 foreach my $key (keys %$href) {
518 push @entered, $key ;
519 push @entered, \$href->{$key} ;
524 return $self->setError("Expected even number of parameters, got $count")
527 for my $i (0.. $count / 2 - 1) {
528 push @entered, $_[2* $i] ;
529 push @entered, \$_[2* $i+1] ;
534 while (my ($key, $v) = each %$default)
536 croak "need 4 params [@$v]"
539 my ($first_only, $sticky, $type, $value) = @$v ;
541 $self->_checkType($key, \$value, $type, 0, \$x)
546 if ($firstTime || ! $sticky) {
547 $got->{$key} = [0, $type, $value, $x, $first_only, $sticky] ;
550 $got->{$key}[OFF_PARSED] = 0 ;
553 for my $i (0.. @entered / 2 - 1) {
554 my $key = $entered[2* $i] ;
555 my $value = $entered[2* $i+1] ;
557 #print "Key [$key] Value [$value]" ;
558 #print defined $$value ? "[$$value]\n" : "[undef]\n";
561 my $canonkey = lc $key;
563 if ($got->{$canonkey} && ($firstTime ||
564 ! $got->{$canonkey}[OFF_FIRST_ONLY] ))
566 my $type = $got->{$canonkey}[OFF_TYPE] ;
568 $self->_checkType($key, $value, $type, 1, \$s)
570 #$value = $$value unless $type & Parse_store_ref ;
572 $got->{$canonkey} = [1, $type, $value, $s] ;
575 { push (@Bad, $key) }
579 my ($bad) = join(", ", @Bad) ;
580 return $self->setError("unknown key value(s) @Bad") ;
586 sub IO::Compress::Base::Parameters::_checkType
593 my $validate = shift ;
596 #local $Carp::CarpLevel = $level ;
597 #print "PARSE $type $key $value $validate $sub\n" ;
598 if ( $type & Parse_store_ref)
601 # if ref ${ $value } ;
609 if ($type & Parse_any)
614 elsif ($type & Parse_unsigned)
616 return $self->setError("Parameter '$key' must be an unsigned int, got 'undef'")
617 if $validate && ! defined $value ;
618 return $self->setError("Parameter '$key' must be an unsigned int, got '$value'")
619 if $validate && $value !~ /^\d+$/;
621 $$output = defined $value ? $value : 0 ;
624 elsif ($type & Parse_signed)
626 return $self->setError("Parameter '$key' must be a signed int, got 'undef'")
627 if $validate && ! defined $value ;
628 return $self->setError("Parameter '$key' must be a signed int, got '$value'")
629 if $validate && $value !~ /^-?\d+$/;
631 $$output = defined $value ? $value : 0 ;
634 elsif ($type & Parse_boolean)
636 return $self->setError("Parameter '$key' must be an int, got '$value'")
637 if $validate && defined $value && $value !~ /^\d*$/;
638 $$output = defined $value ? $value != 0 : 0 ;
641 elsif ($type & Parse_string)
643 $$output = defined $value ? $value : "" ;
653 sub IO::Compress::Base::Parameters::parsed
658 return $self->{Got}{lc $name}[OFF_PARSED] ;
661 sub IO::Compress::Base::Parameters::value
668 $self->{Got}{lc $name}[OFF_PARSED] = 1;
669 $self->{Got}{lc $name}[OFF_DEFAULT] = $_[0] ;
670 $self->{Got}{lc $name}[OFF_FIXED] = $_[0] ;
673 return $self->{Got}{lc $name}[OFF_FIXED] ;
676 sub IO::Compress::Base::Parameters::valueOrDefault
680 my $default = shift ;
682 my $value = $self->{Got}{lc $name}[OFF_DEFAULT] ;
684 return $value if defined $value ;
688 sub IO::Compress::Base::Parameters::wantValue
693 return defined $self->{Got}{lc $name}[OFF_DEFAULT] ;
697 sub IO::Compress::Base::Parameters::clone
703 while (my ($k, $v) = each %{ $self->{Got} }) {
707 $obj->{Error} = $self->{Error};
708 $obj->{Got} = \%got ;
710 return bless $obj, 'IO::Compress::Base::Parameters' ;
715 use constant MAX32 => 0xFFFFFFFF ;
716 use constant LOW => 0 ;
717 use constant HIGH => 1;
734 bless [$low, $high], $class;
741 my ($low, $hi) = unpack "V V", $string ;
742 bless [ $low, $hi ], "U64";
749 my $low = unpack "V", $string ;
750 bless [ $low, 0 ], "U64";
756 $self->[HIGH] = $self->[LOW] = 0;
762 bless [ @$self ], ref $self ;
768 return $self->[HIGH];
788 if (ref $value eq 'U64') {
789 $self->[HIGH] += $value->[HIGH] ;
790 $value = $value->[LOW];
793 my $available = MAX32 - $self->[LOW] ;
795 if ($value > $available) {
797 $self->[LOW] = $value - $available - 1;
800 $self->[LOW] += $value ;
809 return $self->[LOW] == $other->[LOW] &&
810 $self->[HIGH] == $other->[HIGH] ;
817 return pack "V V", @$self ;
824 return pack "V", $self->[LOW] ;
831 return pack "V V", $low, 0;
835 package IO::Compress::Base::Common;