| 1 | package IO::Framed::Read; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 5 | ▶ | use IO::Framed::X (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | sub new { | ||||||||||||||||||||||||||||
| 9 | 6 | ▶ | my ( $class, $in_fh, $initial_buffer ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 6 | ▶ | if ( !defined $initial_buffer ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 12 | 6 | $initial_buffer = q<>; | |||||||||||||||||||||||||||
| 13 | } | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 6 | my $self = { | |||||||||||||||||||||||||||
| 16 | _in_fh => $in_fh, | ||||||||||||||||||||||||||||
| 17 | _read_buffer => $initial_buffer, | ||||||||||||||||||||||||||||
| 18 | _bytes_to_read => 0, | ||||||||||||||||||||||||||||
| 19 | }; | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 6 | return bless $self, $class; | |||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 0 | ▶ | sub get_read_fh { return $_[0]->{'_in_fh'} } | ||||||||||||||||||||||||||
|
Subroutine
get_read_fh: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | #---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 27 | # IO subclass interface | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | sub allow_empty_read { | ||||||||||||||||||||||||||||
| 30 | 2 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
allow_empty_read: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 31 | 2 | $self->{'_ALLOW_EMPTY_READ'} = 1; | |||||||||||||||||||||||||||
| 32 | 2 | return $self; | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | sub READ { | ||||||||||||||||||||||||||||
| 36 | 3 | ▶ | require IO::SigGuard; | ||||||||||||||||||||||||||
|
Subroutine
READ: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | 3 | IO::SigGuard->import('sysread'); | |||||||||||||||||||||||||||
| 38 | 3 | *READ = *IO::SigGuard::sysread; | |||||||||||||||||||||||||||
| 39 | 3 | goto &READ; | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | #We assume here that whatever read may be incomplete at first | ||||||||||||||||||||||||||||
| 43 | #will eventually be repeated so that we can complete it. e.g.: | ||||||||||||||||||||||||||||
| 44 | # | ||||||||||||||||||||||||||||
| 45 | # - read 4 bytes, receive 1, cache it - return undef | ||||||||||||||||||||||||||||
| 46 | # - select() | ||||||||||||||||||||||||||||
| 47 | # - read 4 bytes again; since we already have 1 byte, only read 3 | ||||||||||||||||||||||||||||
| 48 | # … and now we get the remaining 3, so return the buffer. | ||||||||||||||||||||||||||||
| 49 | # | ||||||||||||||||||||||||||||
| 50 | sub read { | ||||||||||||||||||||||||||||
| 51 | 10 | ▶ | my ( $self, $bytes ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
read: called CC=7 SCAR=19.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 10 | ▶ | die "I refuse to read zero!" if !$bytes; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 10 | ▶ | if ( length $self->{'_read_buffer'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 1 | ▶ | if ( length($self->{'_read_buffer'}) + $self->{'_bytes_to_read'} != $bytes ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 0 | my $should_be = length($self->{'_read_buffer'}) + $self->{'_bytes_to_read'}; | |||||||||||||||||||||||||||
| 58 | 0 | die "Continuation: should want “$should_be” bytes, not $bytes!"; | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 10 | ▶ | if ( $bytes > length($self->{'_read_buffer'}) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 10 | $bytes -= length($self->{'_read_buffer'}); | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 10 | local $!; | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 10 | local $self->{'_return'}; | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 10 | $bytes -= $self->_expand_read_buffer( $bytes ); | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 8 | ▶ | return q<> if $self->{'_return'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 7 | $self->{'_bytes_to_read'} = $bytes; | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 7 | ▶ | if ($bytes) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 2 | return undef; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 5 | return substr( $self->{'_read_buffer'}, 0, length($self->{'_read_buffer'}), q<> ); | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | sub _expand_read_buffer { | ||||||||||||||||||||||||||||
| 84 | 14 | ▶ | return $_[0]->can('READ')->( $_[0]->{'_in_fh'}, $_[0]->{'_read_buffer'}, $_[1], length($_[0]->{'_read_buffer'}) ) || do { | ||||||||||||||||||||||||||
|
Subroutine
_expand_read_buffer: called CC=6 SCAR=17.9
Condition100%3/3
$_[0]->can('READ')->($_[0]{'_in_fh'}, $_[0]{'_read_buffer'}, $_[1], length $_[0]{'_read_buffer'}) || do {
if ($!) {
if (not $!{'EAGAIN'} || $!{'EWOULDBLOCK'}) {
die 'IO::Framed::X'->create('ReadError', $!);
};
}
elsif ($_[0]{'_ALLOW_EMPTY_READ'}) {
$_[0]{'_return'} = 1;
0;
}
else {
die 'IO::Framed::X'->create('EmptyRead');
}
}
MC/DC50%1/2
$_[0]->can('READ')->($_[0]{'_in_fh'}, $_[0]{'_read_buffer'}, $_[1], length $_[0]{'_read_buffer'}) || do {
if ($!) {
if (not $!{'EAGAIN'} || $!{'EWOULDBLOCK'}) {
die 'IO::Framed::X'->create('ReadError', $!);
};
}
elsif ($_[0]{'_ALLOW_EMPTY_READ'}) {
$_[0]{'_return'} = 1;
0;
}
else {
die 'IO::Framed::X'->create('EmptyRead');
}
}
$_[0]->can('READ')->($_[0]{'_in_fh'}, $_[0]{'_read_buffer'}, $_[1], length $_[0]{'_read_buffer'})
do {
if ($!) {
if (not $!{'EAGAIN'} || $!{'EWOULDBLOCK'}) {
die 'IO::Framed::X'->create('ReadError', $!);
};
}
elsif ($_[0]{'_ALLOW_EMPTY_READ'}) {
$_[0]{'_return'} = 1;
0;
}
else {
die 'IO::Framed::X'->create('EmptyRead');
}
}
Truth table66%2/3
A || B
A= $_[0]->can('READ')->($_[0]{'_in_fh'}, $_[0]{'_read_buffer'}, $_[1], length $_[0]{'_read_buffer'})B= do {
if ($!) {
if (not $!{'EAGAIN'} || $!{'EWOULDBLOCK'}) {
die 'IO::Framed::X'->create('ReadError', $!);
};
}
elsif ($_[0]{'_ALLOW_EMPTY_READ'}) {
$_[0]{'_return'} = 1;
0;
}
else {
die 'IO::Framed::X'->create('EmptyRead');
}
}
| |||||||||||||||||||||||||||||
| 85 | 6 | ▶ | if ($!) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 86 | 4 | ▶ | if ( !$!{'EAGAIN'} && !$!{'EWOULDBLOCK'}) { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
Condition66%2/3
$!{'EAGAIN'} || $!{'EWOULDBLOCK'}
MC/DC50%1/2
$!{'EAGAIN'} || $!{'EWOULDBLOCK'}
$!{'EAGAIN'}
$!{'EWOULDBLOCK'}
Truth table66%2/3
A || B
A= $!{'EAGAIN'}B= $!{'EWOULDBLOCK'}
| |||||||||||||||||||||||||||||
| 87 | 1 | die IO::Framed::X->create( 'ReadError', $! ); | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | elsif ($_[0]->{'_ALLOW_EMPTY_READ'}) { | ||||||||||||||||||||||||||||
| 91 | 2 | $_[0]->{'_return'} = 1; | |||||||||||||||||||||||||||
| 92 | 2 | 0; | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | else { | ||||||||||||||||||||||||||||
| 95 | 2 | die IO::Framed::X->create('EmptyRead'); | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | }; | ||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | sub read_until { | ||||||||||||||||||||||||||||
| 101 | 5 | ▶ | my ( $self, $seq ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
read_until: called CC=7 SCAR=19.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 5 | ▶ | if ( $self->{'_bytes_to_read'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | 0 | die "Don’t call read_until() after an incomplete read()!"; | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 5 | ▶ | die "Missing read-until sequence!" if !defined $seq || !length $seq; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $seq and length $seq
MC/DC0%0/2
defined $seq and length $seq
defined $seq
length $seq
Truth table33%1/3
A and B
A= defined $seqB= length $seq
| |||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 5 | my $at = index( $self->{'_read_buffer'}, $seq ); | |||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 5 | ▶ | if ($at > -1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | 1 | return substr( $self->{'_read_buffer'}, 0, $at + length($seq), q<> ); | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 4 | local $self->{'_return'}; | |||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 4 | $self->_expand_read_buffer( 65536 ); | |||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 3 | ▶ | return q<> if $self->{'_return'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 2 | $at = index( $self->{'_read_buffer'}, $seq ); | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 2 | ▶ | if ($at > -1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | 1 | return substr( $self->{'_read_buffer'}, 0, $at + length($seq), q<> ); | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 1 | return undef; | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | #---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 5 | 1; | |||||||||||||||||||||||||||