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 14     14   83 use strict;
  14         24  
  14         388  
6 14     14   69 use vars qw(@ISA);
  14         23  
  14         589  
7              
8 14         761 use XML::SAX::PurePerl::Reader qw(
9             EOF
10             BUFFER
11             LINE
12             COLUMN
13             ENCODING
14             XML_VERSION
15 14     14   65 );
  14         22  
16 14     14   4218 use XML::SAX::Exception;
  14         7497  
  14         476  
17              
18             @ISA = ('XML::SAX::PurePerl::Reader');
19              
20             # subclassed by adding 1 to last element
21 14     14   77 use constant FH => 8;
  14         29  
  14         688  
22 14     14   70 use constant BUFFER_SIZE => 4096;
  14         25  
  14         5386  
23              
24             sub new {
25 21     21 0 42 my $class = shift;
26 21         34 my $ioref = shift;
27 21         77 XML::SAX::PurePerl::Reader::set_raw_stream($ioref);
28 21         98 my @parts;
29 21         85 @parts[FH, LINE, COLUMN, BUFFER, EOF, XML_VERSION] =
30             ($ioref, 1, 0, '', 0, '1.0');
31 21         79 return bless \@parts, $class;
32             }
33              
34             sub read_more {
35 111     111 0 150 my $self = shift;
36 111         124 my $buf;
37 111         1246 my $bytesread = read($self->[FH], $buf, BUFFER_SIZE);
38 111 100       1098 if ($bytesread) {
    50          
39 40         289 $self->[BUFFER] .= $buf;
40 40         121 return 1;
41             }
42             elsif (defined($bytesread)) {
43 71         89 $self->[EOF]++;
44 71         141 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 13955     13955 0 15844 my $self = shift;
55 13955         30927 my $discarded = substr($self->[BUFFER], 0, $_[0], '');
56            
57             # Wish I could skip this lot - tells us where we are in the file
58 13955         18955 my $lines = $discarded =~ tr/\n//;
59 13955         16843 $self->[LINE] += $lines;
60 13955 100       18680 if ($lines) {
61 1412         4025 $discarded =~ /\n([^\n]*)$/;
62 1412         2928 $self->[COLUMN] = length($1);
63             }
64             else {
65 12543         19041 $self->[COLUMN] += $_[0];
66             }
67             }
68              
69             sub set_encoding {
70 14     14 0 20 my $self = shift;
71 14         25 my ($encoding) = @_;
72             # warn("set encoding to: $encoding\n");
73 14         45 XML::SAX::PurePerl::Reader::switch_encoding_stream($self->[FH], $encoding);
74 14         10274 XML::SAX::PurePerl::Reader::switch_encoding_string($self->[BUFFER], $encoding);
75 14         661 $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