home *** CD-ROM | disk | FTP | other *** search
/ Australian Personal Computer 2004 July / APC0407D2.iso / workshop / apache / files / ActivePerl-5.6.1.638-MSWin32-x86.msi / _953b3dc34a35a2865bf4123508ef6901 < prev    next >
Encoding:
Text File  |  2004-04-13  |  1.4 KB  |  75 lines

  1. # Output file handle that calls a custom write routine
  2. # Ned Konz, March 2000
  3. # This is provided to help with writing zip files
  4. # when you have to process them a chunk at a time.
  5. #
  6. # See the examples.
  7. #
  8. # $Revision: 1.2 $
  9.  
  10. use strict;
  11. package Archive::Zip::MockFileHandle;
  12.  
  13. sub new
  14. {
  15.     my $class = shift || __PACKAGE__;
  16.     $class = ref($class) || $class;
  17.     my $self = bless( { 
  18.         'position' => 0, 
  19.         'size' => 0
  20.     }, $class );
  21.     return $self;
  22. }
  23.  
  24. sub eof
  25. {
  26.     my $self = shift;
  27.     return $self->{'position'} >= $self->{'size'};
  28. }
  29.  
  30. # Copy given buffer to me
  31. sub write
  32. {
  33.     my $self = shift;
  34.     my $buf = \($_[0]); shift;
  35.     my $len = shift;
  36.     my $offset = shift || 0;
  37.  
  38.     $$buf = '' if not defined($$buf);
  39.     my $bufLen = length($$buf);
  40.     my $bytesWritten = ($offset + $len > $bufLen)
  41.         ? $bufLen - $offset
  42.         : $len;
  43.     $bytesWritten = $self->writeHook(substr($$buf, $offset, $bytesWritten));
  44.     if ($self->{'position'} + $bytesWritten > $self->{'size'})
  45.     {
  46.         $self->{'size'} = $self->{'position'} + $bytesWritten
  47.     }
  48.     $self->{'position'} += $bytesWritten;
  49.     return $bytesWritten;
  50. }
  51.  
  52. # Called on each write.
  53. # Override in subclasses.
  54. # Return number of bytes written (0 on error).
  55. sub writeHook
  56. {
  57.     my $self = shift;
  58.     my $bytes = shift;
  59.     return length($bytes);
  60. }
  61.  
  62. sub binmode { 1 } 
  63.  
  64. sub close { 1 } 
  65.  
  66. sub clearerr { 1 } 
  67.  
  68. # I'm write-only!
  69. sub read { 0 } 
  70.  
  71. sub tell { return shift->{'position'} }
  72.  
  73. # vim: ts=4 sw=4
  74. 1;
  75.