1 package IO::Compress::Base::Common;
8 use Scalar::Util qw(blessed readonly);
12 our ($VERSION, @ISA, @EXPORT, %EXPORT_TAGS);
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
75 UNIVERSAL::isa($_[0],'IO::Handle') or
76 UNIVERSAL::isa(\$_[0],'GLOB'))
82 return (defined $_[0] and
84 UNIVERSAL::isa(\$_[0], 'SCALAR'));
89 return defined $_[0] && $_[0] =~ /^<.*>$/;
92 sub cleanFileGlobString
96 $string =~ s/^\s*<\s*(.*)\s*>\s*$/$1/;
101 use constant WANT_CODE => 1 ;
102 use constant WANT_EXT => 2 ;
103 use constant WANT_UNDEF => 4 ;
104 #use constant WANT_HASH => 8 ;
105 use constant WANT_HASH => 0 ;
109 my $got = whatIs(@_);
111 if (defined $got && $got eq 'filename' && defined $_[0] && $_[0] eq '-')
116 #$_[0] = new IO::File("<-");
122 sub whatIsOutput($;$)
124 my $got = whatIs(@_);
126 if (defined $got && $got eq 'filename' && defined $_[0] && $_[0] eq '-')
130 #$_[0] = new IO::File(">-");
138 return 'handle' if isaFilehandle($_[0]);
140 my $wantCode = defined $_[1] && $_[1] & WANT_CODE ;
141 my $extended = defined $_[1] && $_[1] & WANT_EXT ;
142 my $undef = defined $_[1] && $_[1] & WANT_UNDEF ;
143 my $hash = defined $_[1] && $_[1] & WANT_HASH ;
145 return 'undef' if ! defined $_[0] && $undef ;
148 return '' if blessed($_[0]); # is an object
149 #return '' if UNIVERSAL::isa($_[0], 'UNIVERSAL'); # is an object
150 return 'buffer' if UNIVERSAL::isa($_[0], 'SCALAR');
151 return 'array' if UNIVERSAL::isa($_[0], 'ARRAY') && $extended ;
152 return 'hash' if UNIVERSAL::isa($_[0], 'HASH') && $hash ;
153 return 'code' if UNIVERSAL::isa($_[0], 'CODE') && $wantCode ;
157 return 'fileglob' if $extended && isaFileGlobString($_[0]);
163 return $_[0] =~ /^(code|handle|buffer|filename)$/;
171 my $error_ref = shift ;
172 my $reportClass = shift ;
174 my %data = (Class => $Class,
176 reportClass => $reportClass,
179 my $obj = bless \%data, $class ;
181 local $Carp::CarpLevel = 1;
183 my $inType = $data{inType} = whatIsInput($_[0], WANT_EXT|WANT_HASH);
184 my $outType = $data{outType} = whatIsOutput($_[1], WANT_EXT|WANT_HASH);
186 my $oneInput = $data{oneInput} = oneTarget($inType);
187 my $oneOutput = $data{oneOutput} = oneTarget($outType);
191 $obj->croakError("$reportClass: illegal input parameter") ;
195 # if ($inType eq 'hash')
198 # $obj->{oneInput} = 1 ;
199 # return $obj->validateHash($_[0]);
204 $obj->croakError("$reportClass: illegal output parameter") ;
209 if ($inType ne 'fileglob' && $outType eq 'fileglob')
211 $obj->croakError("Need input fileglob for outout fileglob");
214 # if ($inType ne 'fileglob' && $outType eq 'hash' && $inType ne 'filename' )
216 # $obj->croakError("input must ne filename or fileglob when output is a hash");
219 if ($inType eq 'fileglob' && $outType eq 'fileglob')
222 $data{inType} = $data{outType} = 'filename';
223 my $mapper = new File::GlobMapper($_[0], $_[1]);
226 return $obj->saveErrorString($File::GlobMapper::Error) ;
228 $data{Pairs} = $mapper->getFileMap();
233 $obj->croakError("$reportClass: input and output $inType are identical")
234 if $inType eq $outType && $_[0] eq $_[1] && $_[0] ne '-' ;
236 if ($inType eq 'fileglob') # && $outType ne 'fileglob'
238 my $glob = cleanFileGlobString($_[0]);
239 my @inputs = glob($glob);
243 # TODO -- legal or die?
244 die "globmap matched zero file -- legal or die???" ;
248 $obj->validateInputFilenames($inputs[0])
251 $data{inType} = 'filename' ;
256 $obj->validateInputFilenames(@inputs)
258 $_[0] = [ @inputs ] ;
259 $data{inType} = 'filenames' ;
262 elsif ($inType eq 'filename')
264 $obj->validateInputFilenames($_[0])
267 elsif ($inType eq 'array')
269 $data{inType} = 'filenames' ;
270 $obj->validateInputArray($_[0])
274 return $obj->saveErrorString("$reportClass: output buffer is read-only")
275 if $outType eq 'buffer' && readonly(${ $_[1] });
277 if ($outType eq 'filename' )
279 $obj->croakError("$reportClass: output filename is undef or null string")
280 if ! defined $_[1] || $_[1] eq '' ;
286 return $obj->saveErrorString("output file '$_[1]' is a directory");
294 sub Validator::saveErrorString
297 ${ $self->{Error} } = shift ;
302 sub Validator::croakError
305 $self->saveErrorString($_[0]);
311 sub Validator::validateInputFilenames
315 foreach my $filename (@_)
317 $self->croakError("$self->{reportClass}: input filename is undef or null string")
318 if ! defined $filename || $filename eq '' ;
320 next if $filename eq '-';
324 return $self->saveErrorString("input file '$filename' does not exist");
329 return $self->saveErrorString("input file '$filename' is a directory");
334 return $self->saveErrorString("cannot open file '$filename': $!");
341 sub Validator::validateInputArray
345 if ( @{ $_[0] } == 0 )
347 return $self->saveErrorString("empty array reference") ;
350 foreach my $element ( @{ $_[0] } )
352 my $inType = whatIsInput($element);
356 $self->croakError("unknown input parameter") ;
358 elsif($inType eq 'filename')
360 $self->validateInputFilenames($element)
365 $self->croakError("not a filename") ;
372 #sub Validator::validateHash
377 # while (my($k, $v) = each %$href)
379 # my $ktype = whatIsInput($k);
380 # my $vtype = whatIsOutput($v, WANT_EXT|WANT_UNDEF) ;
382 # if ($ktype ne 'filename')
384 # return $self->saveErrorString("hash key not filename") ;
387 # my %valid = map { $_ => 1 } qw(filename buffer array undef handle) ;
388 # if (! $valid{$vtype})
390 # return $self->saveErrorString("hash value not ok") ;
397 sub createSelfTiedObject
399 my $class = shift || (caller)[0] ;
400 my $error_ref = shift ;
402 my $obj = bless Symbol::gensym(), ref($class) || $class;
403 tie *$obj, $obj if $] >= 5.005;
404 *$obj->{Closed} = 1 ;
406 *$obj->{Error} = $error_ref ;
408 *$obj->{ErrorNo} = \$errno ;
415 #package Parse::Parameters ;
419 #our ($VERSION, @ISA, @EXPORT);
420 #$VERSION = '2.000_08';
421 #@ISA = qw(Exporter);
423 $EXPORT_TAGS{Parse} = [qw( ParseParameters
424 Parse_any Parse_unsigned Parse_signed
425 Parse_boolean Parse_custom Parse_string
426 Parse_multiple Parse_writable_scalar
430 push @EXPORT, @{ $EXPORT_TAGS{Parse} } ;
432 use constant Parse_any => 0x01;
433 use constant Parse_unsigned => 0x02;
434 use constant Parse_signed => 0x04;
435 use constant Parse_boolean => 0x08;
436 use constant Parse_string => 0x10;
437 use constant Parse_custom => 0x12;
439 #use constant Parse_store_ref => 0x100 ;
440 use constant Parse_multiple => 0x100 ;
441 use constant Parse_writable => 0x200 ;
442 use constant Parse_writable_scalar => 0x400 | Parse_writable ;
444 use constant OFF_PARSED => 0 ;
445 use constant OFF_TYPE => 1 ;
446 use constant OFF_DEFAULT => 2 ;
447 use constant OFF_FIXED => 3 ;
448 use constant OFF_FIRST_ONLY => 4 ;
449 use constant OFF_STICKY => 5 ;
455 my $level = shift || 0 ;
457 my $sub = (caller($level + 1))[3] ;
458 local $Carp::CarpLevel = 1 ;
459 my $p = new IO::Compress::Base::Parameters() ;
461 or croak "$sub: $p->{Error}" ;
466 #package IO::Compress::Base::Parameters;
472 sub IO::Compress::Base::Parameters::new
476 my $obj = { Error => '',
480 #return bless $obj, ref($class) || $class || __PACKAGE__ ;
481 return bless $obj, 'IO::Compress::Base::Parameters' ;
484 sub IO::Compress::Base::Parameters::setError
488 my $retval = @_ ? shift : undef ;
490 $self->{Error} = $error ;
497 # return $self->{Error} ;
500 sub IO::Compress::Base::Parameters::parse
504 my $default = shift ;
506 my $got = $self->{Got} ;
507 my $firstTime = keys %{ $got } == 0 ;
512 # Allow the options to be passed as a hash reference or
513 # as the complete hash.
519 return $self->setError("Expected even number of parameters, got 1")
520 if ! defined $href or ! ref $href or ref $href ne "HASH" ;
522 foreach my $key (keys %$href) {
523 push @entered, $key ;
524 push @entered, \$href->{$key} ;
529 return $self->setError("Expected even number of parameters, got $count")
532 for my $i (0.. $count / 2 - 1) {
533 push @entered, $_[2* $i] ;
534 push @entered, \$_[2* $i+1] ;
539 while (my ($key, $v) = each %$default)
541 croak "need 4 params [@$v]"
544 my ($first_only, $sticky, $type, $value) = @$v ;
546 $self->_checkType($key, \$value, $type, 0, \$x)
551 if ($firstTime || ! $sticky) {
553 if $type & Parse_multiple;
555 $got->{$key} = [0, $type, $value, $x, $first_only, $sticky] ;
558 $got->{$key}[OFF_PARSED] = 0 ;
562 for my $i (0.. @entered / 2 - 1) {
563 my $key = $entered[2* $i] ;
564 my $value = $entered[2* $i+1] ;
566 #print "Key [$key] Value [$value]" ;
567 #print defined $$value ? "[$$value]\n" : "[undef]\n";
570 my $canonkey = lc $key;
572 if ($got->{$canonkey} && ($firstTime ||
573 ! $got->{$canonkey}[OFF_FIRST_ONLY] ))
575 my $type = $got->{$canonkey}[OFF_TYPE] ;
576 my $parsed = $parsed{$canonkey};
577 ++ $parsed{$canonkey};
579 return $self->setError("Muliple instances of '$key' found")
580 if $parsed && $type & Parse_multiple == 0 ;
583 $self->_checkType($key, $value, $type, 1, \$s)
587 if ($type & Parse_multiple) {
588 $got->{$canonkey}[OFF_PARSED] = 1;
589 push @{ $got->{$canonkey}[OFF_FIXED] }, $s ;
592 $got->{$canonkey} = [1, $type, $value, $s] ;
596 { push (@Bad, $key) }
600 my ($bad) = join(", ", @Bad) ;
601 return $self->setError("unknown key value(s) @Bad") ;
607 sub IO::Compress::Base::Parameters::_checkType
614 my $validate = shift ;
617 #local $Carp::CarpLevel = $level ;
618 #print "PARSE $type $key $value $validate $sub\n" ;
620 if ($type & Parse_writable_scalar)
622 return $self->setError("Parameter '$key' not writable")
623 if $validate && readonly $$value ;
627 return $self->setError("Parameter '$key' not a scalar reference")
628 if $validate && ref $$value ne 'SCALAR' ;
634 return $self->setError("Parameter '$key' not a scalar")
635 if $validate && ref $value ne 'SCALAR' ;
643 # if ($type & Parse_store_ref)
646 # # if ref ${ $value } ;
648 # $$output = $value ;
654 if ($type & Parse_any)
659 elsif ($type & Parse_unsigned)
661 return $self->setError("Parameter '$key' must be an unsigned int, got 'undef'")
662 if $validate && ! defined $value ;
663 return $self->setError("Parameter '$key' must be an unsigned int, got '$value'")
664 if $validate && $value !~ /^\d+$/;
666 $$output = defined $value ? $value : 0 ;
669 elsif ($type & Parse_signed)
671 return $self->setError("Parameter '$key' must be a signed int, got 'undef'")
672 if $validate && ! defined $value ;
673 return $self->setError("Parameter '$key' must be a signed int, got '$value'")
674 if $validate && $value !~ /^-?\d+$/;
676 $$output = defined $value ? $value : 0 ;
679 elsif ($type & Parse_boolean)
681 return $self->setError("Parameter '$key' must be an int, got '$value'")
682 if $validate && defined $value && $value !~ /^\d*$/;
683 $$output = defined $value ? $value != 0 : 0 ;
686 elsif ($type & Parse_string)
688 $$output = defined $value ? $value : "" ;
698 sub IO::Compress::Base::Parameters::parsed
703 return $self->{Got}{lc $name}[OFF_PARSED] ;
706 sub IO::Compress::Base::Parameters::value
713 $self->{Got}{lc $name}[OFF_PARSED] = 1;
714 $self->{Got}{lc $name}[OFF_DEFAULT] = $_[0] ;
715 $self->{Got}{lc $name}[OFF_FIXED] = $_[0] ;
718 return $self->{Got}{lc $name}[OFF_FIXED] ;
721 sub IO::Compress::Base::Parameters::valueOrDefault
725 my $default = shift ;
727 my $value = $self->{Got}{lc $name}[OFF_DEFAULT] ;
729 return $value if defined $value ;
733 sub IO::Compress::Base::Parameters::wantValue
738 return defined $self->{Got}{lc $name}[OFF_DEFAULT] ;
742 sub IO::Compress::Base::Parameters::clone
748 while (my ($k, $v) = each %{ $self->{Got} }) {
752 $obj->{Error} = $self->{Error};
753 $obj->{Got} = \%got ;
755 return bless $obj, 'IO::Compress::Base::Parameters' ;
760 use constant MAX32 => 0xFFFFFFFF ;
761 use constant LOW => 0 ;
762 use constant HIGH => 1;
779 bless [$low, $high], $class;
786 my ($low, $hi) = unpack "V V", $string ;
787 bless [ $low, $hi ], "U64";
794 my $low = unpack "V", $string ;
795 bless [ $low, 0 ], "U64";
801 $self->[HIGH] = $self->[LOW] = 0;
807 bless [ @$self ], ref $self ;
813 return $self->[HIGH];
833 if (ref $value eq 'U64') {
834 $self->[HIGH] += $value->[HIGH] ;
835 $value = $value->[LOW];
838 my $available = MAX32 - $self->[LOW] ;
840 if ($value > $available) {
842 $self->[LOW] = $value - $available - 1;
845 $self->[LOW] += $value ;
854 return $self->[LOW] == $other->[LOW] &&
855 $self->[HIGH] == $other->[HIGH] ;
862 return pack "V V", @$self ;
869 return pack "V", $self->[LOW] ;
876 return pack "V V", $low, 0;
880 package IO::Compress::Base::Common;