home *** CD-ROM | disk | FTP | other *** search
/ Netrunner 2004 October / NETRUNNER0410.ISO / regular / ActivePerl-5.8.4.810-MSWin32-x86.msi / _8ae5bdc8cc4dc1920f55431b17bb96a8 < prev    next >
Text File  |  2004-06-01  |  6KB  |  321 lines

  1. # IO::Zlib.pm
  2. #
  3. # Copyright (c) 1998-2001 Tom Hughes <tom@compton.nu>.
  4. # All rights reserved. This program is free software; you can redistribute
  5. # it and/or modify it under the same terms as Perl itself.
  6.  
  7. package IO::Zlib;
  8.  
  9. =head1 NAME
  10.  
  11. IO::Zlib - IO:: style interface to L<Compress::Zlib>
  12.  
  13. =head1 SYNOPSIS
  14.  
  15. With any version of Perl 5 you can use the basic OO interface:
  16.  
  17.     use IO::Zlib;
  18.  
  19.     $fh = new IO::Zlib;
  20.     if ($fh->open("file.gz", "rb")) {
  21.         print <$fh>;
  22.         $fh->close;
  23.     }
  24.  
  25.     $fh = IO::Zlib->new("file.gz", "wb9");
  26.     if (defined $fh) {
  27.         print $fh "bar\n";
  28.         $fh->close;
  29.     }
  30.  
  31.     $fh = IO::Zlib->new("file.gz", "rb");
  32.     if (defined $fh) {
  33.         print <$fh>;
  34.         undef $fh;       # automatically closes the file
  35.     }
  36.  
  37. With Perl 5.004 you can also use the TIEHANDLE interface to access
  38. compressed files just like ordinary files:
  39.  
  40.     use IO::Zlib;
  41.  
  42.     tie *FILE, 'IO::Zlib', "file.gz", "wb";
  43.     print FILE "line 1\nline2\n";
  44.  
  45.     tie *FILE, 'IO::Zlib', "file.gz", "rb";
  46.     while (<FILE>) { print "LINE: ", $_ };
  47.  
  48. =head1 DESCRIPTION
  49.  
  50. C<IO::Zlib> provides an IO:: style interface to L<Compress::Zlib> and
  51. hence to gzip/zlib compressed files. It provides many of the same methods
  52. as the L<IO::Handle> interface.
  53.  
  54. =head1 CONSTRUCTOR
  55.  
  56. =over 4
  57.  
  58. =item new ( [ARGS] )
  59.  
  60. Creates an C<IO::Zlib> object. If it receives any parameters, they are
  61. passed to the method C<open>; if the open fails, the object is destroyed.
  62. Otherwise, it is returned to the caller.
  63.  
  64. =back
  65.  
  66. =head1 METHODS
  67.  
  68. =over 4
  69.  
  70. =item open ( FILENAME, MODE )
  71.  
  72. C<open> takes two arguments. The first is the name of the file to open
  73. and the second is the open mode. The mode can be anything acceptable to
  74. L<Compress::Zlib> and by extension anything acceptable to I<zlib> (that
  75. basically means POSIX fopen() style mode strings plus an optional number
  76. to indicate the compression level).
  77.  
  78. =item opened
  79.  
  80. Returns true if the object currently refers to a opened file.
  81.  
  82. =item close
  83.  
  84. Close the file associated with the object and disassociate
  85. the file from the handle.
  86. Done automatically on destroy.
  87.  
  88. =item getc
  89.  
  90. Return the next character from the file, or undef if none remain.
  91.  
  92. =item getline
  93.  
  94. Return the next line from the file, or undef on end of string.
  95. Can safely be called in an array context.
  96. Currently ignores $/ ($INPUT_RECORD_SEPARATOR or $RS when L<English>
  97. is in use) and treats lines as delimited by "\n".
  98.  
  99. =item getlines
  100.  
  101. Get all remaining lines from the file.
  102. It will croak() if accidentally called in a scalar context.
  103.  
  104. =item print ( ARGS... )
  105.  
  106. Print ARGS to the  file.
  107.  
  108. =item read ( BUF, NBYTES, [OFFSET] )
  109.  
  110. Read some bytes from the file.
  111. Returns the number of bytes actually read, 0 on end-of-file, undef on error.
  112.  
  113. =item eof
  114.  
  115. Returns true if the handle is currently positioned at end of file?
  116.  
  117. =item seek ( OFFSET, WHENCE )
  118.  
  119. Seek to a given position in the stream.
  120. Not yet supported.
  121.  
  122. =item tell
  123.  
  124. Return the current position in the stream, as a numeric offset.
  125. Not yet supported.
  126.  
  127. =item setpos ( POS )
  128.  
  129. Set the current position, using the opaque value returned by C<getpos()>.
  130. Not yet supported.
  131.  
  132. =item getpos ( POS )
  133.  
  134. Return the current position in the string, as an opaque object.
  135. Not yet supported.
  136.  
  137. =back
  138.  
  139. =head1 SEE ALSO
  140.  
  141. L<perlfunc>,
  142. L<perlop/"I/O Operators">,
  143. L<IO::Handle>,
  144. L<Compress::Zlib>
  145.  
  146. =head1 HISTORY
  147.  
  148. Created by Tom Hughes E<lt>F<tom@compton.nu>E<gt>.
  149.  
  150. =head1 COPYRIGHT
  151.  
  152. Copyright (c) 1998-2001 Tom Hughes E<lt>F<tom@compton.nu>E<gt>.
  153. All rights reserved. This program is free software; you can redistribute
  154. it and/or modify it under the same terms as Perl itself.
  155.  
  156. =cut
  157.  
  158. require 5.004;
  159.  
  160. use strict;
  161. use vars qw($VERSION $AUTOLOAD @ISA);
  162.  
  163. use Carp;
  164. use Compress::Zlib;
  165. use Symbol;
  166. use Tie::Handle;
  167.  
  168. $VERSION = "1.01";
  169.  
  170. @ISA = qw(Tie::Handle);
  171.  
  172. sub TIEHANDLE
  173. {
  174.     my $class = shift;
  175.     my @args = @_;
  176.  
  177.     my $self = bless {}, $class;
  178.  
  179.     return @args ? $self->OPEN(@args) : $self;
  180. }
  181.  
  182. sub DESTROY
  183. {
  184. }
  185.  
  186. sub OPEN
  187. {
  188.     my $self = shift;
  189.     my $filename = shift;
  190.     my $mode = shift;
  191.  
  192.     croak "open() needs a filename" unless defined($filename);
  193.  
  194.     $self->{'file'} = gzopen($filename,$mode);
  195.     $self->{'eof'} = 0;
  196.  
  197.     return defined($self->{'file'}) ? $self : undef;
  198. }
  199.  
  200. sub CLOSE
  201. {
  202.     my $self = shift;
  203.  
  204.     return undef unless defined($self->{'file'});
  205.  
  206.     my $status = $self->{'file'}->gzclose();
  207.  
  208.     delete $self->{'file'};
  209.     delete $self->{'eof'};
  210.  
  211.     return ($status == 0) ? 1 : undef;
  212. }
  213.  
  214. sub READ
  215. {
  216.     my $self = shift;
  217.     my $bufref = \$_[0];
  218.     my $nbytes = $_[1];
  219.     my $offset = $_[2];
  220.  
  221.     croak "NBYTES must be specified" unless defined($nbytes);
  222.     croak "OFFSET not supported" if defined($offset) && $offset != 0;
  223.  
  224.     return 0 if $self->{'eof'};
  225.  
  226.     my $bytesread = $self->{'file'}->gzread($$bufref,$nbytes);
  227.  
  228.     return undef if $bytesread < 0;
  229.  
  230.     $self->{'eof'} = 1 if $bytesread < $nbytes;
  231.  
  232.     return $bytesread;
  233. }
  234.  
  235. sub READLINE
  236. {
  237.     my $self = shift;
  238.  
  239.     my $line;
  240.  
  241.     return () if $self->{'file'}->gzreadline($line) <= 0;
  242.  
  243.     return $line unless wantarray;
  244.  
  245.     my @lines = $line;
  246.  
  247.     while ($self->{'file'}->gzreadline($line) > 0)
  248.     {
  249.         push @lines, $line;
  250.     }
  251.  
  252.     return @lines;
  253. }
  254.  
  255. sub WRITE
  256. {
  257.     my $self = shift;
  258.     my $buf = shift;
  259.     my $length = shift;
  260.     my $offset = shift;
  261.  
  262.     croak "bad LENGTH" unless $length <= length($buf);
  263.     croak "OFFSET not supported" if defined($offset) && $offset != 0;
  264.  
  265.     return $self->{'file'}->gzwrite(substr($buf,0,$length));
  266. }
  267.  
  268. sub EOF
  269. {
  270.     my $self = shift;
  271.  
  272.     return $self->{'eof'};
  273. }
  274.  
  275. sub new
  276. {
  277.     my $class = shift;
  278.     my @args = @_;
  279.  
  280.     my $self = gensym();
  281.  
  282.     tie *{$self}, $class, @args;
  283.  
  284.     return tied(${$self}) ? bless $self, $class : undef;
  285. }
  286.  
  287. sub getline
  288. {
  289.     my $self = shift;
  290.  
  291.     return scalar tied(*{$self})->READLINE();
  292. }
  293.  
  294. sub getlines
  295. {
  296.     my $self = shift;
  297.  
  298.     croak unless wantarray;
  299.  
  300.     return tied(*{$self})->READLINE();
  301. }
  302.  
  303. sub opened
  304. {
  305.     my $self = shift;
  306.  
  307.     return defined tied(*{$self})->{'file'};
  308. }
  309.  
  310. sub AUTOLOAD
  311. {
  312.     my $self = shift;
  313.  
  314.     $AUTOLOAD =~ s/.*:://;
  315.     $AUTOLOAD =~ tr/a-z/A-Z/;
  316.  
  317.     return tied(*{$self})->$AUTOLOAD(@_);
  318. }
  319.  
  320. 1;
  321.