home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / tsw / TSW_3.4.0.exe / Apache2 / perl / Stream.pm < prev    next >
Encoding:
Perl POD Document  |  2002-02-03  |  1.8 KB  |  83 lines

  1. # $Id: Stream.pm,v 1.5 2002/02/03 12:25:45 matt Exp $
  2.  
  3. package XML::SAX::PurePerl::Reader::Stream;
  4.  
  5. use strict;
  6. use vars qw(@ISA);
  7.  
  8. use XML::SAX::PurePerl::Reader qw(
  9.     EOF
  10.     BUFFER
  11.     INTERNAL_BUFFER
  12.     LINE
  13.     COLUMN
  14.     CURRENT
  15.     ENCODING
  16. );
  17. use XML::SAX::Exception;
  18.  
  19. @ISA = ('XML::SAX::PurePerl::Reader');
  20.  
  21. use constant FH => 11;
  22. use constant BUFFER_SIZE => 12;
  23.  
  24. sub new {
  25.     my $class = shift;
  26.     my $ioref = shift;
  27.     XML::SAX::PurePerl::Reader::set_raw_stream($ioref);
  28.     my @parts;
  29.     @parts[FH, LINE, COLUMN, BUFFER, EOF, INTERNAL_BUFFER, BUFFER_SIZE] =
  30.         ($ioref, 1,   0,      '',     0,   '',              1);
  31.     return bless \@parts, $class;
  32. }
  33.  
  34. sub next {
  35.     my $self = shift;
  36.     
  37.     # check for chars in buffer first.
  38.     if (length($self->[BUFFER])) {
  39.         return $self->[CURRENT] = substr($self->[BUFFER], 0, 1, ''); # last param truncates buffer
  40.     }
  41.     
  42.  
  43.     if (length($self->[INTERNAL_BUFFER])) {
  44. BUFFERED_READ:
  45.         $self->[CURRENT] = substr($self->[INTERNAL_BUFFER], 0, 1, '');
  46.         if ($self->[CURRENT] eq "\x0A") {
  47.             $self->[LINE]++;
  48.             $self->[COLUMN] = 1;
  49.         }
  50.         else { $self->[COLUMN]++ }
  51.         return;
  52.     }
  53.     
  54.     my $bytesread = read($self->[FH], $self->[INTERNAL_BUFFER], $self->[BUFFER_SIZE]);
  55.     if ($bytesread) {
  56.         goto BUFFERED_READ;
  57.     }
  58.     elsif (defined($bytesread)) {
  59.         $self->[EOF]++;
  60.         return $self->[CURRENT] = undef;
  61.     }
  62.     throw XML::SAX::Exception::Parse(
  63.         Message => "Error reading from filehandle: $!",
  64.     );
  65. }
  66.  
  67. sub set_encoding {
  68.     my $self = shift;
  69.     my ($encoding) = @_;
  70.     # warn("set encoding to: $encoding\n");
  71.     XML::SAX::PurePerl::Reader::switch_encoding_stream($self->[FH], $encoding);
  72.     $self->[BUFFER_SIZE] = 1024;
  73.     $self->[ENCODING] = $encoding;
  74. }
  75.  
  76. sub bytepos {
  77.     my $self = shift;
  78.     tell($self->[FH]);
  79. }
  80.  
  81. 1;
  82.  
  83.