IO::Compress::* 2.000_13
[p5sagit/p5-mst-13.2.git] / ext / Compress / IO / Base / lib / IO / Compress / Base.pm
1
2 package IO::Compress::Base ;
3
4 require 5.004 ;
5
6 use strict ;
7 use warnings;
8
9 use IO::Compress::Base::Common;
10
11 use IO::File ;
12 use Scalar::Util qw(blessed readonly);
13
14 #use File::Glob;
15 #require Exporter ;
16 use Carp ;
17 use Symbol;
18 use bytes;
19
20 our (@ISA, $VERSION, $got_encode);
21 #@ISA    = qw(Exporter IO::File);
22
23 $VERSION = '2.000_13';
24
25 #Can't locate object method "SWASHNEW" via package "utf8" (perhaps you forgot to load "utf8"?) at .../ext/Compress-Zlib/Gzip/blib/lib/Compress/Zlib/Common.pm line 16.
26
27 #$got_encode = 0;
28 #eval
29 #{
30 #    require Encode;
31 #    Encode->import('encode', 'find_encoding');
32 #};
33 #
34 #$got_encode = 1 unless $@;
35
36 sub saveStatus
37 {
38     my $self   = shift ;
39     ${ *$self->{ErrorNo} } = shift() + 0 ;
40     ${ *$self->{Error} } = '' ;
41
42     return ${ *$self->{ErrorNo} } ;
43 }
44
45
46 sub saveErrorString
47 {
48     my $self   = shift ;
49     my $retval = shift ;
50     ${ *$self->{Error} } = shift ;
51     ${ *$self->{ErrorNo} } = shift() + 0 if @_ ;
52
53     return $retval;
54 }
55
56 sub croakError
57 {
58     my $self   = shift ;
59     $self->saveErrorString(0, $_[0]);
60     croak $_[0];
61 }
62
63 sub closeError
64 {
65     my $self = shift ;
66     my $retval = shift ;
67
68     my $errno = *$self->{ErrorNo};
69     my $error = ${ *$self->{Error} };
70
71     $self->close();
72
73     *$self->{ErrorNo} = $errno ;
74     ${ *$self->{Error} } = $error ;
75
76     return $retval;
77 }
78
79
80
81 sub error
82 {
83     my $self   = shift ;
84     return ${ *$self->{Error} } ;
85 }
86
87 sub errorNo
88 {
89     my $self   = shift ;
90     return ${ *$self->{ErrorNo} } ;
91 }
92
93
94 sub writeAt
95 {
96     my $self = shift ;
97     my $offset = shift;
98     my $data = shift;
99
100     if (defined *$self->{FH}) {
101         my $here = tell(*$self->{FH});
102         return $self->saveErrorString(undef, "Cannot seek to end of output filehandle: $!", $!) 
103             if $here < 0 ;
104         seek(*$self->{FH}, $offset, SEEK_SET)
105             or return $self->saveErrorString(undef, "Cannot seek to end of output filehandle: $!", $!) ;
106         defined *$self->{FH}->write($data, length $data)
107             or return $self->saveErrorString(undef, $!, $!) ;
108         seek(*$self->{FH}, $here, SEEK_SET)
109             or return $self->saveErrorString(undef, "Cannot seek to end of output filehandle: $!", $!) ;
110     }
111     else {
112         substr(${ *$self->{Buffer} }, $offset, length($data)) = $data ;
113     }
114
115     return 1;
116 }
117
118 sub output
119 {
120     my $self = shift ;
121     my $data = shift ;
122     my $last = shift ;
123
124     return 1 
125         if length $data == 0 && ! $last ;
126
127     if ( *$self->{FilterEnvelope} ) {
128         *_ = \$data;
129         &{ *$self->{FilterEnvelope} }();
130     }
131
132     if ( defined *$self->{FH} ) {
133         defined *$self->{FH}->write( $data, length $data )
134           or return $self->saveErrorString(0, $!, $!); 
135     }
136     else {
137         ${ *$self->{Buffer} } .= $data ;
138     }
139
140     return 1;
141 }
142
143 sub getOneShotParams
144 {
145     return ( 'MultiStream' => [1, 1, Parse_boolean,   1],
146            );
147 }
148
149 sub checkParams
150 {
151     my $self = shift ;
152     my $class = shift ;
153
154     my $got = shift || IO::Compress::Base::Parameters::new();
155
156     $got->parse(
157         {
158             # Generic Parameters
159             'AutoClose' => [1, 1, Parse_boolean,   0],
160            #'Encoding'  => [1, 1, Parse_any,       undef],
161             'Strict'    => [0, 1, Parse_boolean,   1],
162             'Append'    => [1, 1, Parse_boolean,   0],
163             'BinModeIn' => [1, 1, Parse_boolean,   0],
164
165             'FilterEnvelope' => [1, 1, Parse_any,   undef],
166
167             $self->getExtraParams(),
168             *$self->{OneShot} ? $self->getOneShotParams() 
169                               : (),
170         }, 
171         @_) or $self->croakError("${class}: $got->{Error}")  ;
172
173     return $got ;
174 }
175
176 sub _create
177 {
178     my $obj = shift;
179     my $got = shift;
180
181     *$obj->{Closed} = 1 ;
182
183     my $class = ref $obj;
184     $obj->croakError("$class: Missing Output parameter")
185         if ! @_ && ! $got ;
186
187     my $outValue = shift ;
188     my $oneShot = 1 ;
189
190     if (! $got)
191     {
192         $oneShot = 0 ;
193         $got = $obj->checkParams($class, undef, @_)
194             or return undef ;
195     }
196
197     my $lax = ! $got->value('Strict') ;
198
199     my $outType = whatIsOutput($outValue);
200
201     $obj->ckOutputParam($class, $outValue)
202         or return undef ;
203
204     if ($outType eq 'buffer') {
205         *$obj->{Buffer} = $outValue;
206     }
207     else {
208         my $buff = "" ;
209         *$obj->{Buffer} = \$buff ;
210     }
211
212     # Merge implies Append
213     my $merge = $got->value('Merge') ;
214     my $appendOutput = $got->value('Append') || $merge ;
215     *$obj->{Append} = $appendOutput;
216     *$obj->{FilterEnvelope} = $got->value('FilterEnvelope') ;
217
218     if ($merge)
219     {
220         # Switch off Merge mode if output file/buffer is empty/doesn't exist
221         if (($outType eq 'buffer' && length $$outValue == 0 ) ||
222             ($outType ne 'buffer' && (! -e $outValue || (-w _ && -z _))) )
223           { $merge = 0 }
224     }
225
226     # If output is a file, check that it is writable
227     if ($outType eq 'filename' && -e $outValue && ! -w _)
228       { return $obj->saveErrorString(undef, "Output file '$outValue' is not writable" ) }
229
230     elsif ($outType eq 'handle'  && ! -w $outValue)
231       { return $obj->saveErrorString(undef, "Output filehandle is not writable" ) }
232
233
234 #    TODO - encoding
235 #    if ($got->parsed('Encoding')) { 
236 #        $obj->croakError("$class: Encode module needed to use -Encoding")
237 #            if ! $got_encode;
238 #
239 #        my $want_encoding = $got->value('Encoding');
240 #        my $encoding = find_encoding($want_encoding);
241 #
242 #        $obj->croakError("$class: Encoding '$want_encoding' is not available")
243 #           if ! $encoding;
244 #
245 #        *$obj->{Encoding} = $encoding;
246 #    }
247
248     $obj->ckParams($got)
249         or $obj->croakError("${class}: " . $obj->error());
250
251
252     $obj->saveStatus(STATUS_OK) ;
253
254     my $status ;
255     if (! $merge)
256     {
257         *$obj->{Compress} = $obj->mkComp($class, $got)
258             or return undef;
259         
260         *$obj->{UnCompSize} = new U64 ;
261         *$obj->{CompSize} = new U64 ;
262
263         if ( $outType eq 'buffer') {
264             ${ *$obj->{Buffer} }  = ''
265                 unless $appendOutput ;
266         }
267         else {
268             if ($outType eq 'handle') {
269                 *$obj->{FH} = $outValue ;
270                 setBinModeOutput(*$obj->{FH}) ;
271                 $outValue->flush() ;
272                 *$obj->{Handle} = 1 ;
273                 if ($appendOutput)
274                 {
275                     seek(*$obj->{FH}, 0, SEEK_END)
276                         or return $obj->saveErrorString(undef, "Cannot seek to end of output filehandle: $!", $!) ;
277
278                 }
279             }
280             elsif ($outType eq 'filename') {    
281                 my $mode = '>' ;
282                 $mode = '>>'
283                     if $appendOutput;
284                 *$obj->{FH} = new IO::File "$mode $outValue" 
285                     or return $obj->saveErrorString(undef, "cannot open file '$outValue': $!", $!) ;
286                 *$obj->{StdIO} = ($outValue eq '-'); 
287                 setBinModeOutput(*$obj->{FH}) ;
288             }
289         }
290
291         *$obj->{Header} = $obj->mkHeader($got) ;
292         $obj->output( *$obj->{Header} )
293             or return undef;
294     }
295     else
296     {
297         *$obj->{Compress} = $obj->createMerge($outValue, $outType)
298             or return undef;
299     }
300
301     *$obj->{Closed} = 0 ;
302     *$obj->{AutoClose} = $got->value('AutoClose') ;
303     *$obj->{Output} = $outValue;
304     *$obj->{ClassName} = $class;
305     *$obj->{Got} = $got;
306     *$obj->{OneShot} = 0 ;
307
308     return $obj ;
309 }
310
311 sub ckOutputParam 
312 {
313     my $self = shift ;
314     my $from = shift ;
315     my $outType = whatIsOutput($_[0]);
316
317     $self->croakError("$from: output parameter not a filename, filehandle or scalar ref")
318         if ! $outType ;
319
320     $self->croakError("$from: output filename is undef or null string")
321         if $outType eq 'filename' && (! defined $_[0] || $_[0] eq '')  ;
322
323     $self->croakError("$from: output buffer is read-only")
324         if $outType eq 'buffer' && readonly(${ $_[0] });
325     
326     return 1;    
327 }
328
329
330 sub _def
331 {
332     my $obj = shift ;
333     
334     my $class= (caller)[0] ;
335     my $name = (caller(1))[3] ;
336
337     $obj->croakError("$name: expected at least 1 parameters\n")
338         unless @_ >= 1 ;
339
340     my $input = shift ;
341     my $haveOut = @_ ;
342     my $output = shift ;
343
344     my $x = new Validator($class, *$obj->{Error}, $name, $input, $output)
345         or return undef ;
346
347     push @_, $output if $haveOut && $x->{Hash};
348
349     *$obj->{OneShot} = 1 ;
350
351     my $got = $obj->checkParams($name, undef, @_)
352         or return undef ;
353
354     $x->{Got} = $got ;
355
356 #    if ($x->{Hash})
357 #    {
358 #        while (my($k, $v) = each %$input)
359 #        {
360 #            $v = \$input->{$k} 
361 #                unless defined $v ;
362 #
363 #            $obj->_singleTarget($x, 1, $k, $v, @_)
364 #                or return undef ;
365 #        }
366 #
367 #        return keys %$input ;
368 #    }
369
370     if ($x->{GlobMap})
371     {
372         $x->{oneInput} = 1 ;
373         foreach my $pair (@{ $x->{Pairs} })
374         {
375             my ($from, $to) = @$pair ;
376             $obj->_singleTarget($x, 1, $from, $to, @_)
377                 or return undef ;
378         }
379
380         return scalar @{ $x->{Pairs} } ;
381     }
382
383     if (! $x->{oneOutput} )
384     {
385         my $inFile = ($x->{inType} eq 'filenames' 
386                         || $x->{inType} eq 'filename');
387
388         $x->{inType} = $inFile ? 'filename' : 'buffer';
389         
390         foreach my $in ($x->{oneInput} ? $input : @$input)
391         {
392             my $out ;
393             $x->{oneInput} = 1 ;
394
395             $obj->_singleTarget($x, $inFile, $in, \$out, @_)
396                 or return undef ;
397
398             push @$output, \$out ;
399             #if ($x->{outType} eq 'array')
400             #  { push @$output, \$out }
401             #else
402             #  { $output->{$in} = \$out }
403         }
404
405         return 1 ;
406     }
407
408     # finally the 1 to 1 and n to 1
409     return $obj->_singleTarget($x, 1, $input, $output, @_);
410
411     croak "should not be here" ;
412 }
413
414 sub _singleTarget
415 {
416     my $obj             = shift ;
417     my $x               = shift ;
418     my $inputIsFilename = shift;
419     my $input           = shift;
420     
421     if ($x->{oneInput})
422     {
423         $obj->getFileInfo($x->{Got}, $input)
424             if isaFilename($input) and $inputIsFilename ;
425
426         my $z = $obj->_create($x->{Got}, @_)
427             or return undef ;
428
429
430         defined $z->_wr2($input, $inputIsFilename) 
431             or return $z->closeError(undef) ;
432
433         return $z->close() ;
434     }
435     else
436     {
437         my $afterFirst = 0 ;
438         my $inputIsFilename = ($x->{inType} ne 'array');
439         my $keep = $x->{Got}->clone();
440
441         #for my $element ( ($x->{inType} eq 'hash') ? keys %$input : @$input)
442         for my $element ( @$input)
443         {
444             my $isFilename = isaFilename($element);
445
446             if ( $afterFirst ++ )
447             {
448                 defined addInterStream($obj, $element, $isFilename)
449                     or return $obj->closeError(undef) ;
450             }
451             else
452             {
453                 $obj->getFileInfo($x->{Got}, $element)
454                     if $isFilename;
455
456                 $obj->_create($x->{Got}, @_)
457                     or return undef ;
458             }
459
460             defined $obj->_wr2($element, $isFilename) 
461                 or return $obj->closeError(undef) ;
462
463             *$obj->{Got} = $keep->clone();
464         }
465         return $obj->close() ;
466     }
467
468 }
469
470 sub _wr2
471 {
472     my $self = shift ;
473
474     my $source = shift ;
475     my $inputIsFilename = shift;
476
477     my $input = $source ;
478     if (! $inputIsFilename)
479     {
480         $input = \$source 
481             if ! ref $source;
482     }
483
484     if ( ref $input && ref $input eq 'SCALAR' )
485     {
486         return $self->syswrite($input, @_) ;
487     }
488
489     if ( ! ref $input  || isaFilehandle($input))
490     {
491         my $isFilehandle = isaFilehandle($input) ;
492
493         my $fh = $input ;
494
495         if ( ! $isFilehandle )
496         {
497             $fh = new IO::File "<$input"
498                 or return $self->saveErrorString(undef, "cannot open file '$input': $!", $!) ;
499         }
500         binmode $fh if *$self->{Got}->valueOrDefault('BinModeIn') ;
501
502         my $status ;
503         my $buff ;
504         my $count = 0 ;
505         while (($status = read($fh, $buff, 16 * 1024)) > 0) {
506             $count += length $buff;
507             defined $self->syswrite($buff, @_) 
508                 or return undef ;
509         }
510
511         return $self->saveErrorString(undef, $!, $!) 
512             if $status < 0 ;
513
514         if ( (!$isFilehandle || *$self->{AutoClose}) && $input ne '-')
515         {    
516             $fh->close() 
517                 or return undef ;
518         }
519
520         return $count ;
521     }
522
523     croak "Should not be here";
524     return undef;
525 }
526
527 sub addInterStream
528 {
529     my $self = shift ;
530     my $input = shift ;
531     my $inputIsFilename = shift ;
532
533     if (*$self->{Got}->value('MultiStream'))
534     {
535         $self->getFileInfo(*$self->{Got}, $input)
536             #if isaFilename($input) and $inputIsFilename ;
537             if isaFilename($input) ;
538
539         # TODO -- newStream needs to allow gzip/zip header to be modified
540         return $self->newStream();
541     }
542     elsif (*$self->{Got}->value('AutoFlush'))
543     {
544         #return $self->flush(Z_FULL_FLUSH);
545     }
546
547     return 1 ;
548 }
549
550 sub TIEHANDLE
551 {
552     return $_[0] if ref($_[0]);
553     die "OOPS\n" ;
554 }
555   
556 sub UNTIE
557 {
558     my $self = shift ;
559 }
560
561 sub DESTROY
562 {
563     my $self = shift ;
564     $self->close() ;
565
566
567     # TODO - memory leak with 5.8.0 - this isn't called until 
568     #        global destruction
569     #
570     %{ *$self } = () ;
571     undef $self ;
572 }
573
574
575
576 sub filterUncompressed
577 {
578 }
579
580 sub syswrite
581 {
582     my $self = shift ;
583
584     my $buffer ;
585     if (ref $_[0] ) {
586         $self->croakError( *$self->{ClassName} . "::write: not a scalar reference" )
587             unless ref $_[0] eq 'SCALAR' ;
588         $buffer = $_[0] ;
589     }
590     else {
591         $buffer = \$_[0] ;
592     }
593
594
595     if (@_ > 1) {
596         my $slen = defined $$buffer ? length($$buffer) : 0;
597         my $len = $slen;
598         my $offset = 0;
599         $len = $_[1] if $_[1] < $len;
600
601         if (@_ > 2) {
602             $offset = $_[2] || 0;
603             $self->croakError(*$self->{ClassName} . "::write: offset outside string") 
604                 if $offset > $slen;
605             if ($offset < 0) {
606                 $offset += $slen;
607                 $self->croakError( *$self->{ClassName} . "::write: offset outside string") if $offset < 0;
608             }
609             my $rem = $slen - $offset;
610             $len = $rem if $rem < $len;
611         }
612
613         $buffer = \substr($$buffer, $offset, $len) ;
614     }
615
616     return 0 if ! defined $$buffer || length $$buffer == 0 ;
617
618     my $buffer_length = defined $$buffer ? length($$buffer) : 0 ;
619     *$self->{UnCompSize}->add($buffer_length) ;
620
621     $self->filterUncompressed($buffer);
622
623 #    if (*$self->{Encoding}) {
624 #        $$buffer = *$self->{Encoding}->encode($$buffer);
625 #    }
626
627     my $outBuffer='';
628     my $status = *$self->{Compress}->compr($buffer, $outBuffer) ;
629
630     return $self->saveErrorString(undef, *$self->{Compress}{Error}, 
631                                          *$self->{Compress}{ErrorNo})
632         if $status == STATUS_ERROR;
633
634     *$self->{CompSize}->add(length $outBuffer) ;
635
636     $self->output($outBuffer)
637         or return undef;
638
639     return $buffer_length;
640 }
641
642 sub print
643 {
644     my $self = shift;
645
646     #if (ref $self) {
647     #    $self = *$self{GLOB} ;
648     #}
649
650     if (defined $\) {
651         if (defined $,) {
652             defined $self->syswrite(join($,, @_) . $\);
653         } else {
654             defined $self->syswrite(join("", @_) . $\);
655         }
656     } else {
657         if (defined $,) {
658             defined $self->syswrite(join($,, @_));
659         } else {
660             defined $self->syswrite(join("", @_));
661         }
662     }
663 }
664
665 sub printf
666 {
667     my $self = shift;
668     my $fmt = shift;
669     defined $self->syswrite(sprintf($fmt, @_));
670 }
671
672
673
674 sub flush
675 {
676     my $self = shift ;
677
678     my $outBuffer='';
679     my $status = *$self->{Compress}->flush($outBuffer, @_) ;
680     return $self->saveErrorString(0, *$self->{Compress}{Error}, 
681                                     *$self->{Compress}{ErrorNo})
682         if $status == STATUS_ERROR;
683
684     if ( defined *$self->{FH} ) {
685         *$self->{FH}->clearerr();
686     }
687
688     *$self->{CompSize}->add(length $outBuffer) ;
689
690     $self->output($outBuffer)
691         or return 0;
692
693     if ( defined *$self->{FH} ) {
694         defined *$self->{FH}->flush()
695             or return $self->saveErrorString(0, $!, $!); 
696     }
697
698     return 1;
699 }
700
701 sub newStream
702 {
703     my $self = shift ;
704   
705     $self->_writeTrailer()
706         or return 0 ;
707
708     my $got = $self->checkParams('newStream', *$self->{Got}, @_)
709         or return 0 ;    
710
711     $self->ckParams($got)
712         or $self->croakError("newStream: $self->{Error}");
713
714     *$self->{Header} = $self->mkHeader($got) ;
715     $self->output(*$self->{Header} )
716         or return 0;
717     
718     my $status = $self->reset() ;
719     return $self->saveErrorString(0, *$self->{Compress}{Error}, 
720                                   *$self->{Compress}{ErrorNo})
721         if $status == STATUS_ERROR;
722
723     *$self->{UnCompSize}->reset();
724     *$self->{CompSize}->reset();
725
726     return 1 ;
727 }
728
729 sub reset
730 {
731     my $self = shift ;
732     return *$self->{Compress}->reset() ;
733 }
734
735 sub _writeTrailer
736 {
737     my $self = shift ;
738
739     my $trailer = '';
740
741     my $status = *$self->{Compress}->close($trailer) ;
742     return $self->saveErrorString(0, *$self->{Compress}{Error}, *$self->{Compress}{ErrorNo})
743         if $status == STATUS_ERROR;
744
745     *$self->{CompSize}->add(length $trailer) ;
746
747     $trailer .= $self->mkTrailer();
748     defined $trailer
749       or return 0;
750
751     return $self->output($trailer);
752 }
753
754 sub _writeFinalTrailer
755 {
756     my $self = shift ;
757
758     return $self->output($self->mkFinalTrailer());
759 }
760
761 sub close
762 {
763     my $self = shift ;
764
765     return 1 if *$self->{Closed} || ! *$self->{Compress} ;
766     *$self->{Closed} = 1 ;
767
768     untie *$self 
769         if $] >= 5.008 ;
770
771     $self->_writeTrailer()
772         or return 0 ;
773
774     $self->_writeFinalTrailer()
775         or return 0 ;
776
777     $self->output( "", 1 )
778         or return 0;
779
780     if (defined *$self->{FH}) {
781
782         #if (! *$self->{Handle} || *$self->{AutoClose}) {
783         if ((! *$self->{Handle} || *$self->{AutoClose}) && ! *$self->{StdIO}) {
784             $! = 0 ;
785             *$self->{FH}->close()
786                 or return $self->saveErrorString(0, $!, $!); 
787         }
788         delete *$self->{FH} ;
789         # This delete can set $! in older Perls, so reset the errno
790         $! = 0 ;
791     }
792   
793     return 1;
794 }
795
796
797 #sub total_in
798 #sub total_out
799 #sub msg
800 #
801 #sub crc
802 #{
803 #    my $self = shift ;
804 #    return *$self->{Compress}->crc32() ;
805 #}
806 #
807 #sub msg
808 #{
809 #    my $self = shift ;
810 #    return *$self->{Compress}->msg() ;
811 #}
812 #
813 #sub dict_adler
814 #{
815 #    my $self = shift ;
816 #    return *$self->{Compress}->dict_adler() ;
817 #}
818 #
819 #sub get_Level
820 #{
821 #    my $self = shift ;
822 #    return *$self->{Compress}->get_Level() ;
823 #}
824 #
825 #sub get_Strategy
826 #{
827 #    my $self = shift ;
828 #    return *$self->{Compress}->get_Strategy() ;
829 #}
830
831
832 sub tell
833 {
834     my $self = shift ;
835
836     return *$self->{UnCompSize}->get32bit() ;
837 }
838
839 sub eof
840 {
841     my $self = shift ;
842
843     return *$self->{Closed} ;
844 }
845
846
847 sub seek
848 {
849     my $self     = shift ;
850     my $position = shift;
851     my $whence   = shift ;
852
853     my $here = $self->tell() ;
854     my $target = 0 ;
855
856     #use IO::Handle qw(SEEK_SET SEEK_CUR SEEK_END);
857     use IO::Handle ;
858
859     if ($whence == IO::Handle::SEEK_SET) {
860         $target = $position ;
861     }
862     elsif ($whence == IO::Handle::SEEK_CUR || $whence == IO::Handle::SEEK_END) {
863         $target = $here + $position ;
864     }
865     else {
866         $self->croakError(*$self->{ClassName} . "::seek: unknown value, $whence, for whence parameter");
867     }
868
869     # short circuit if seeking to current offset
870     return 1 if $target == $here ;    
871
872     # Outlaw any attempt to seek backwards
873     $self->croakError(*$self->{ClassName} . "::seek: cannot seek backwards")
874         if $target < $here ;
875
876     # Walk the file to the new offset
877     my $offset = $target - $here ;
878
879     my $buffer ;
880     defined $self->syswrite("\x00" x $offset)
881         or return 0;
882
883     return 1 ;
884 }
885
886 sub binmode
887 {
888     1;
889 #    my $self     = shift ;
890 #    return defined *$self->{FH} 
891 #            ? binmode *$self->{FH} 
892 #            : 1 ;
893 }
894
895 sub fileno
896 {
897     my $self     = shift ;
898     return defined *$self->{FH} 
899             ? *$self->{FH}->fileno() 
900             : undef ;
901 }
902
903 sub opened
904 {
905     my $self     = shift ;
906     return ! *$self->{Closed} ;
907 }
908
909 sub autoflush
910 {
911     my $self     = shift ;
912     return defined *$self->{FH} 
913             ? *$self->{FH}->autoflush(@_) 
914             : undef ;
915 }
916
917 sub input_line_number
918 {
919     return undef ;
920 }
921
922
923 sub _notAvailable
924 {
925     my $name = shift ;
926     return sub { croak "$name Not Available: File opened only for output" ; } ;
927 }
928
929 *read     = _notAvailable('read');
930 *READ     = _notAvailable('read');
931 *readline = _notAvailable('readline');
932 *READLINE = _notAvailable('readline');
933 *getc     = _notAvailable('getc');
934 *GETC     = _notAvailable('getc');
935
936 *FILENO   = \&fileno;
937 *PRINT    = \&print;
938 *PRINTF   = \&printf;
939 *WRITE    = \&syswrite;
940 *write    = \&syswrite;
941 *SEEK     = \&seek; 
942 *TELL     = \&tell;
943 *EOF      = \&eof;
944 *CLOSE    = \&close;
945 *BINMODE  = \&binmode;
946
947 #*sysread  = \&_notAvailable;
948 #*syswrite = \&_write;
949
950 1; 
951
952 __END__
953
954 =head1 NAME
955
956
957 IO::Compress::Base - Base Class for IO::Compress modules 
958
959
960 =head1 SYNOPSIS
961
962     use IO::Compress::Base ;
963
964 =head1 DESCRIPTION
965
966
967 This module is not intended for direct use in application code. Its sole
968 purpose if to to be sub-classed by IO::Compress modules.
969
970
971
972
973 =head1 SEE ALSO
974
975 L<Compress::Zlib>, L<IO::Compress::Gzip>, L<IO::Uncompress::Gunzip>, L<IO::Compress::Deflate>, L<IO::Uncompress::Inflate>, L<IO::Compress::RawDeflate>, L<IO::Uncompress::RawInflate>, L<IO::Compress::Bzip2>, L<IO::Uncompress::Bunzip2>, L<IO::Compress::Lzop>, L<IO::Uncompress::UnLzop>, L<IO::Uncompress::AnyInflate>, L<IO::Uncompress::AnyUncompress>
976
977 L<Compress::Zlib::FAQ|Compress::Zlib::FAQ>
978
979 L<File::GlobMapper|File::GlobMapper>, L<Archive::Zip|Archive::Zip>,
980 L<Archive::Tar|Archive::Tar>,
981 L<IO::Zlib|IO::Zlib>
982
983
984
985
986
987 =head1 AUTHOR
988
989 This module was written by Paul Marquess, F<pmqs@cpan.org>. 
990
991
992
993 =head1 MODIFICATION HISTORY
994
995 See the Changes file.
996
997 =head1 COPYRIGHT AND LICENSE
998
999 Copyright (c) 2005-2006 Paul Marquess. All rights reserved.
1000
1001 This program is free software; you can redistribute it and/or
1002 modify it under the same terms as Perl itself.
1003
1004