File Coverage

blib/lib/XML/SAX/PurePerl/Reader/Stream.pm
Criterion Covered Total %
statement 45 48 93.7
branch 5 6 83.3
condition n/a
subroutine 10 11 90.9
pod 0 5 0.0
total 60 70 85.7


line stmt bran cond sub pod time code
1             # $Id$
2              
3             package XML::SAX::PurePerl::Reader::Stream;
4              
5 12     12   75 use strict;
  12         20  
  12         488  
6 12     12   148 use vars qw(@ISA);
  12         21  
  12         1320  
7              
8 12         1176 use XML::SAX::PurePerl::Reader qw(
9             EOF
10             BUFFER
11             LINE
12             COLUMN
13             ENCODING
14             XML_VERSION
15 12     12   70 );
  12         20  
16 12     12   9474 use XML::SAX::Exception;
  12         8842  
  12         463  
17              
18             @ISA = ('XML::SAX::PurePerl::Reader');
19              
20             # subclassed by adding 1 to last element
21 12     12   213 use constant FH => 8;
  12         24  
  12         745  
22 12     12   64 use constant BUFFER_SIZE => 4096;
  12         30  
  12         9129  
23              
24             sub new {
25 19     19 0 39 my $class = shift;
26 19         35 my $ioref = shift;
27 19         112 XML::SAX::PurePerl::Reader::set_raw_stream($ioref);
28 19         26 my @parts;
29 19         115 @parts[FH, LINE, COLUMN, BUFFER, EOF, XML_VERSION] =
30             ($ioref, 1, 0, '', 0, '1.0');
31 19         111 return bless \@parts, $class;
32             }
33              
34             sub read_more {
35 99     99 0 173 my $self = shift;
36 99         117 my $buf;
37 99         352357 my $bytesread = read($self->[FH], $buf, BUFFER_SIZE);
38 99 100       3193 if ($bytesread) {
    50          
39 38         2077 $self->[BUFFER] .= $buf;
40 38         179 return 1;
41             }
42             elsif (defined($bytesread)) {
43 61         178 $self->[EOF]++;
44 61         162 return 0;
45             }
46             else {
47 0         0 throw XML::SAX::Exception::Parse(
48             Message => "Error reading from filehandle: $!",
49             );
50             }
51             }
52              
53             sub move_along {
54 13937     13937 0 18524 my $self = shift;
55 13937         43884 my $discarded = substr($self->[BUFFER], 0, $_[0], '');
56            
57             # Wish I could skip this lot - tells us where we are in the file
58 13937         20348 my $lines = $discarded =~ tr/\n//;
59 13937         21416 $self->[LINE] += $lines;
60 13937 100       27606 if ($lines) {
61 1408         5174 $discarded =~ /\n([^\n]*)$/;
62 1408         5309 $self->[COLUMN] = length($1);
63             }
64             else {
65 12529         34175 $self->[COLUMN] += $_[0];
66             }
67             }
68              
69             sub set_encoding {
70 14     14 0 29 my $self = shift;
71 14         25 my ($encoding) = @_;
72             # warn("set encoding to: $encoding\n");
73 14         77 XML::SAX::PurePerl::Reader::switch_encoding_stream($self->[FH], $encoding);
74 14         18197 XML::SAX::PurePerl::Reader::switch_encoding_string($self->[BUFFER], $encoding);
75 14         974 $self->[ENCODING] = $encoding;
76             }
77              
78             sub bytepos {
79 0     0 0   my $self = shift;
80 0           tell($self->[FH]);
81             }
82              
83             1;
84