| 1 | package MIME::Parser::Reader; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | MIME::Parser::Reader - a line-oriented reader for a MIME::Parser | ||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | This module is used internally by MIME::Parser; you probably | ||||||||||||||||||||||||||||||||||||||||||
| 11 | don't need to be looking at it at all. But just in case... | ||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | ### Create a top-level reader, where chunks end at EOF: | ||||||||||||||||||||||||||||||||||||||||||
| 14 | $rdr = MIME::Parser::Reader->new(); | ||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | ### Spawn a child reader, where chunks also end at a boundary: | ||||||||||||||||||||||||||||||||||||||||||
| 17 | $subrdr = $rdr->spawn->add_boundary($bound); | ||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | ### Spawn a child reader, where chunks also end at a given string: | ||||||||||||||||||||||||||||||||||||||||||
| 20 | $subrdr = $rdr->spawn->add_terminator($string); | ||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | ### Read until boundary or terminator: | ||||||||||||||||||||||||||||||||||||||||||
| 23 | $subrdr->read_chunk($in, $out); | ||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | A line-oriented reader which can deal with virtual end-of-stream | ||||||||||||||||||||||||||||||||||||||||||
| 29 | defined by a collection of boundaries. | ||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | B<Warning:> this is a private class solely for use by MIME::Parser. | ||||||||||||||||||||||||||||||||||||||||||
| 32 | This class has no official public interface | ||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | 26 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | ### All possible end-of-line sequences. | ||||||||||||||||||||||||||||||||||||||||||
| 39 | ### Note that "" is included because last line of stream may have no newline! | ||||||||||||||||||||||||||||||||||||||||||
| 40 | 26 | my @EOLs = ("", "\r", "\n", "\r\n", "\n\r"); | |||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | ### Long line: | ||||||||||||||||||||||||||||||||||||||||||
| 43 | 26 | my $LONGLINE = ' ' x 1000; | |||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | #------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 47 | # | ||||||||||||||||||||||||||||||||||||||||||
| 48 | # new | ||||||||||||||||||||||||||||||||||||||||||
| 49 | # | ||||||||||||||||||||||||||||||||||||||||||
| 50 | # I<Class method.> | ||||||||||||||||||||||||||||||||||||||||||
| 51 | # Construct an empty (top-level) reader. | ||||||||||||||||||||||||||||||||||||||||||
| 52 | # | ||||||||||||||||||||||||||||||||||||||||||
| 53 | 94 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | 94 | my ($class) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 55 | 94 | my $eos; | |||||||||||||||||||||||||||||||||||||||||
| 56 | 94 | return bless { | |||||||||||||||||||||||||||||||||||||||||
| 57 | Bounds => [], | ||||||||||||||||||||||||||||||||||||||||||
| 58 | BH => {}, | ||||||||||||||||||||||||||||||||||||||||||
| 59 | TH => {}, | ||||||||||||||||||||||||||||||||||||||||||
| 60 | LocalBH => {}, | ||||||||||||||||||||||||||||||||||||||||||
| 61 | LocalTH => {}, | ||||||||||||||||||||||||||||||||||||||||||
| 62 | Depth => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 63 | EOS => \$eos, | ||||||||||||||||||||||||||||||||||||||||||
| 64 | }, $class; | ||||||||||||||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | #------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 68 | # | ||||||||||||||||||||||||||||||||||||||||||
| 69 | # spawn | ||||||||||||||||||||||||||||||||||||||||||
| 70 | # | ||||||||||||||||||||||||||||||||||||||||||
| 71 | # I<Instance method.> | ||||||||||||||||||||||||||||||||||||||||||
| 72 | # Return a reader which is mostly a shallow copy. | ||||||||||||||||||||||||||||||||||||||||||
| 73 | # | ||||||||||||||||||||||||||||||||||||||||||
| 74 | 5772 | ▶ | sub spawn { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
spawn: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 75 | 5772 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 76 | 5772 | my $dup = bless {}, ref($self); | |||||||||||||||||||||||||||||||||||||||||
| 77 | 5772 | $dup->{Parent} = $self; ### Parent we were spawned from | |||||||||||||||||||||||||||||||||||||||||
| 78 | 5772 | $dup->{Bounds} = [ ]; ### do not copy | |||||||||||||||||||||||||||||||||||||||||
| 79 | 5772 | $dup->{BH} = $self->{BH}; ### shallow copy | |||||||||||||||||||||||||||||||||||||||||
| 80 | 5772 | $dup->{TH} = $self->{TH}; ### shallow copy | |||||||||||||||||||||||||||||||||||||||||
| 81 | 5772 | $dup->{EOS} = $self->{EOS}; ### shallow copy; same ref! | |||||||||||||||||||||||||||||||||||||||||
| 82 | 5772 | $dup->{LocalBH} = {}; ### do not copy | |||||||||||||||||||||||||||||||||||||||||
| 83 | 5772 | $dup->{LocalTH} = {}; ### do not copy | |||||||||||||||||||||||||||||||||||||||||
| 84 | 5772 | $dup->{Depth} = $self->{Depth} + 1; ### increment Depth | |||||||||||||||||||||||||||||||||||||||||
| 85 | 5772 | $dup; | |||||||||||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||
| 88 | #------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 89 | # | ||||||||||||||||||||||||||||||||||||||||||
| 90 | # add_boundary BOUND | ||||||||||||||||||||||||||||||||||||||||||
| 91 | # | ||||||||||||||||||||||||||||||||||||||||||
| 92 | # I<Instance method.> | ||||||||||||||||||||||||||||||||||||||||||
| 93 | # Let BOUND be the new innermost boundary. Returns self. | ||||||||||||||||||||||||||||||||||||||||||
| 94 | # | ||||||||||||||||||||||||||||||||||||||||||
| 95 | 1797 | ▶ | sub add_boundary { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add_boundary: called CC=3 SCAR=11.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | 1797 | my ($self, $bound) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 97 | 1797 | unshift @{$self->{Bounds}}, $bound; ### now at index 0 | |||||||||||||||||||||||||||||||||||||||||
| 98 | 1797 | ▶ | if (!exists($self->{BH}{"--$bound"})) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | 1797 | $self->{BH}{"--$bound"} = "DELIM $bound"; | |||||||||||||||||||||||||||||||||||||||||
| 100 | 1797 | $self->{LocalBH}{"--$bound"} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||
| 102 | 1797 | ▶ | if (!exists($self->{BH}{"--$bound--"})) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 103 | 1797 | $self->{BH}{"--$bound--"} = "CLOSE $bound"; | |||||||||||||||||||||||||||||||||||||||||
| 104 | 1797 | $self->{LocalBH}{"--$bound--"} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||||||||||
| 106 | 1797 | $self; | |||||||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | 5866 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 110 | 5866 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 111 | 5866 | foreach my $thing (keys(%{$self->{LocalBH}})) { | |||||||||||||||||||||||||||||||||||||||||
| 112 | 3594 | delete($self->{BH}{$thing}); | |||||||||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||||||||||
| 114 | 5866 | foreach my $thing (keys(%{$self->{LocalTH}})) { | |||||||||||||||||||||||||||||||||||||||||
| 115 | 31800 | delete($self->{TH}{$thing}); | |||||||||||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | #------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 120 | # | ||||||||||||||||||||||||||||||||||||||||||
| 121 | # add_terminator LINE | ||||||||||||||||||||||||||||||||||||||||||
| 122 | # | ||||||||||||||||||||||||||||||||||||||||||
| 123 | # I<Instance method.> | ||||||||||||||||||||||||||||||||||||||||||
| 124 | # Let LINE be another terminator. Returns self. | ||||||||||||||||||||||||||||||||||||||||||
| 125 | # | ||||||||||||||||||||||||||||||||||||||||||
| 126 | 7950 | ▶ | sub add_terminator { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add_terminator: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 127 | 7950 | my ($self, $line) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 128 | 7950 | foreach (@EOLs) { | |||||||||||||||||||||||||||||||||||||||||
| 129 | 39750 | ▶ | if (!exists($self->{TH}{"$line$_"})) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 130 | 31800 | $self->{TH}{"$line$_"} = "DONE $line"; | |||||||||||||||||||||||||||||||||||||||||
| 131 | 31800 | $self->{LocalTH}{"$line$_"} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||||||||||||
| 134 | 7950 | $self; | |||||||||||||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||
| 137 | #------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 138 | # | ||||||||||||||||||||||||||||||||||||||||||
| 139 | # has_bounds | ||||||||||||||||||||||||||||||||||||||||||
| 140 | # | ||||||||||||||||||||||||||||||||||||||||||
| 141 | # I<Instance method.> | ||||||||||||||||||||||||||||||||||||||||||
| 142 | # Are there boundaries to contend with? | ||||||||||||||||||||||||||||||||||||||||||
| 143 | # | ||||||||||||||||||||||||||||||||||||||||||
| 144 | 6095 | ▶ | sub has_bounds { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
has_bounds: called CC=3 SCAR=11.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 145 | 6095 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 146 | 6095 | ▶ | if (scalar(@{$self->{Bounds}})) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 147 | 6067 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||||||||||||||
| 149 | 28 | ▶ | if ($self->{Parent}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 150 | 0 | return $self->{Parent}->has_bounds(); | |||||||||||||||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||||||||||||||
| 152 | 28 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||
| 155 | #------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 156 | # | ||||||||||||||||||||||||||||||||||||||||||
| 157 | # depth | ||||||||||||||||||||||||||||||||||||||||||
| 158 | # | ||||||||||||||||||||||||||||||||||||||||||
| 159 | # I<Instance method.> | ||||||||||||||||||||||||||||||||||||||||||
| 160 | # How many levels are there? | ||||||||||||||||||||||||||||||||||||||||||
| 161 | # | ||||||||||||||||||||||||||||||||||||||||||
| 162 | 1297 | ▶ | sub depth { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
depth: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 163 | 1297 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 164 | 1297 | return $self->{Depth}; | |||||||||||||||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||||||
| 167 | #------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 168 | # | ||||||||||||||||||||||||||||||||||||||||||
| 169 | # eos [EOS] | ||||||||||||||||||||||||||||||||||||||||||
| 170 | # | ||||||||||||||||||||||||||||||||||||||||||
| 171 | # I<Instance method.> | ||||||||||||||||||||||||||||||||||||||||||
| 172 | # Return the last end-of-stream token seen. | ||||||||||||||||||||||||||||||||||||||||||
| 173 | # See read_chunk() for what these might be. | ||||||||||||||||||||||||||||||||||||||||||
| 174 | # | ||||||||||||||||||||||||||||||||||||||||||
| 175 | 15210 | ▶ | sub eos { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
eos: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 176 | 15210 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 177 | 15210 | ▶ | ${$self->{EOS}} = $_[0] if @_; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 178 | 15210 | ${$self->{EOS}}; | |||||||||||||||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||
| 181 | 7135 | ▶ | sub most_recent_boundary { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
most_recent_boundary: called CC=3 SCAR=11.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 182 | 7135 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 183 | 7135 | ▶ | return $self->{Bounds}[0] if scalar(@{$self->{Bounds}}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 184 | 4 | ▶ | return $self->{Parent}->most_recent_boundary() if $self->{Parent}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 185 | 0 | return undef; | |||||||||||||||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||||||
| 188 | #------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 189 | # | ||||||||||||||||||||||||||||||||||||||||||
| 190 | # eos_type [EOSTOKEN] | ||||||||||||||||||||||||||||||||||||||||||
| 191 | # | ||||||||||||||||||||||||||||||||||||||||||
| 192 | # I<Instance method.> | ||||||||||||||||||||||||||||||||||||||||||
| 193 | # Return the high-level type of the given token (defaults to our token). | ||||||||||||||||||||||||||||||||||||||||||
| 194 | # | ||||||||||||||||||||||||||||||||||||||||||
| 195 | # DELIM saw an innermost boundary like --xyz | ||||||||||||||||||||||||||||||||||||||||||
| 196 | # CLOSE saw an innermost boundary like --xyz-- | ||||||||||||||||||||||||||||||||||||||||||
| 197 | # DONE callback returned false | ||||||||||||||||||||||||||||||||||||||||||
| 198 | # EOF end of file | ||||||||||||||||||||||||||||||||||||||||||
| 199 | # EXT saw boundary of some higher-level | ||||||||||||||||||||||||||||||||||||||||||
| 200 | # | ||||||||||||||||||||||||||||||||||||||||||
| 201 | 15184 | ▶ | sub eos_type { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
eos_type: called CC=5 SCAR=17.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 202 | 15184 | my ($self, $eos) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 203 | 15184 | ▶ | $eos = $self->eos if (@_ == 1); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||||||||||
| 205 | 15184 | ▶ | if ($eos =~ /^(DONE|EOF)/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 206 | 8053 | return $1; | |||||||||||||||||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||||||||||||||||
| 208 | elsif ($eos =~ /^(DELIM|CLOSE) (.*)$/) { | ||||||||||||||||||||||||||||||||||||||||||
| 209 | 7131 | ▶ | return (($2 eq $self->most_recent_boundary()) ? $1 : 'EXT'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||||||||||||||||
| 211 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 212 | 0 | die("internal error: unable to classify boundary token ($eos)"); | |||||||||||||||||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||||||||
| 216 | #------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 217 | # | ||||||||||||||||||||||||||||||||||||||||||
| 218 | # native_handle HANDLE | ||||||||||||||||||||||||||||||||||||||||||
| 219 | # | ||||||||||||||||||||||||||||||||||||||||||
| 220 | # I<Function.> | ||||||||||||||||||||||||||||||||||||||||||
| 221 | # Can we do native i/o on HANDLE? If true, returns the handle | ||||||||||||||||||||||||||||||||||||||||||
| 222 | # that will respond to native I/O calls; else, returns undef. | ||||||||||||||||||||||||||||||||||||||||||
| 223 | # | ||||||||||||||||||||||||||||||||||||||||||
| 224 | 18069 | ▶ | sub native_handle { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
native_handle: called CC=4 SCAR=14.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 225 | 18069 | my $fh = shift; | |||||||||||||||||||||||||||||||||||||||||
| 226 | 18069 | ▶ | return $fh if ($fh->isa('IO::File') || $fh->isa('IO::Handle')); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$fh->isa("IO::File") or $fh->isa("IO::Handle")
MC/DC100%2/2
$fh->isa("IO::File") or $fh->isa("IO::Handle")
$fh->isa("IO::File")
$fh->isa("IO::Handle")
Truth table100%3/3
A or B
A= $fh->isa("IO::File")B= $fh->isa("IO::Handle")
| |||||||||||||||||||||||||||||||||||||||||||
| 227 | 6994 | ▶ | return $fh if (ref $fh eq 'GLOB'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 228 | 0 | undef; | |||||||||||||||||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||||||||||||
| 231 | #------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 232 | # | ||||||||||||||||||||||||||||||||||||||||||
| 233 | # read_chunk INHANDLE, OUTHANDLE | ||||||||||||||||||||||||||||||||||||||||||
| 234 | # | ||||||||||||||||||||||||||||||||||||||||||
| 235 | # I<Instance method.> | ||||||||||||||||||||||||||||||||||||||||||
| 236 | # Get lines until end-of-stream. | ||||||||||||||||||||||||||||||||||||||||||
| 237 | # Returns the terminating-condition token: | ||||||||||||||||||||||||||||||||||||||||||
| 238 | # | ||||||||||||||||||||||||||||||||||||||||||
| 239 | # DELIM xyz saw boundary line "--xyz" | ||||||||||||||||||||||||||||||||||||||||||
| 240 | # CLOSE xyz saw boundary line "--xyz--" | ||||||||||||||||||||||||||||||||||||||||||
| 241 | # DONE xyz saw terminator line "xyz" | ||||||||||||||||||||||||||||||||||||||||||
| 242 | # EOF end of file | ||||||||||||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||||||||||||
| 244 | # Parse up to (and including) the boundary, and dump output. | ||||||||||||||||||||||||||||||||||||||||||
| 245 | # Follows the RFC 2046 specification, that the CRLF immediately preceding | ||||||||||||||||||||||||||||||||||||||||||
| 246 | # the boundary is part of the boundary, NOT part of the input! | ||||||||||||||||||||||||||||||||||||||||||
| 247 | # | ||||||||||||||||||||||||||||||||||||||||||
| 248 | # NOTE: while parsing bodies, we take care to remember the EXACT end-of-line | ||||||||||||||||||||||||||||||||||||||||||
| 249 | # sequence. This is because we *may* be handling 'binary' encoded data, and | ||||||||||||||||||||||||||||||||||||||||||
| 250 | # in that case we can't just massage \r\n into \n! Don't worry... if the | ||||||||||||||||||||||||||||||||||||||||||
| 251 | # data is styled as '7bit' or '8bit', the "decoder" will massage the CRLF | ||||||||||||||||||||||||||||||||||||||||||
| 252 | # for us. For now, we're just trying to chop up the data stream. | ||||||||||||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||||||||||||
| 254 | # NBK - Oct 12, 1999 | ||||||||||||||||||||||||||||||||||||||||||
| 255 | # The CRLF at the end of the current line is considered part | ||||||||||||||||||||||||||||||||||||||||||
| 256 | # of the boundary. I buffer the current line and output the | ||||||||||||||||||||||||||||||||||||||||||
| 257 | # last. I strip the last CRLF when I hit the boundary. | ||||||||||||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||||||||||||
| 259 | 9033 | ▶ | sub read_chunk { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
read_chunk: called CC=34 SCAR=55.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 260 | 9033 | my ($self, $in, $out, $keep_newline, $normalize_newlines) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||||||||||||
| 262 | # If we're parsing a preamble or epilogue, we need to keep the blank line | ||||||||||||||||||||||||||||||||||||||||||
| 263 | # that precedes the boundary line. | ||||||||||||||||||||||||||||||||||||||||||
| 264 | 9033 | ▶ | $keep_newline ||= 0; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$keep_newline ||= 0
MC/DC100%1/1
$keep_newline ||= 0
$keep_newline
Truth table100%2/2
A
A= $keep_newline
| |||||||||||||||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||||||||||||||
| 266 | 9033 | ▶ | $normalize_newlines ||= 0; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$normalize_newlines ||= 0
MC/DC100%1/1
$normalize_newlines ||= 0
$normalize_newlines
Truth table100%2/2
A
A= $normalize_newlines
| |||||||||||||||||||||||||||||||||||||||||||
| 267 | ### Init: | ||||||||||||||||||||||||||||||||||||||||||
| 268 | 9033 | my $bh = $self->{BH}; | |||||||||||||||||||||||||||||||||||||||||
| 269 | 9033 | my $th = $self->{TH}; | |||||||||||||||||||||||||||||||||||||||||
| 270 | 9033 | my $thx = keys %$th; | |||||||||||||||||||||||||||||||||||||||||
| 271 | 9033 | local $_ = $LONGLINE; | |||||||||||||||||||||||||||||||||||||||||
| 272 | 9033 | my $maybe; | |||||||||||||||||||||||||||||||||||||||||
| 273 | 9033 | my $last = ''; | |||||||||||||||||||||||||||||||||||||||||
| 274 | 9033 | my $eos = ''; | |||||||||||||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||||||||||||||
| 276 | ### Determine types: | ||||||||||||||||||||||||||||||||||||||||||
| 277 | 9033 | my $n_in = native_handle($in); | |||||||||||||||||||||||||||||||||||||||||
| 278 | 9033 | my $n_out = native_handle($out); | |||||||||||||||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||||||||||||||||
| 280 | ### Handle efficiently by type: | ||||||||||||||||||||||||||||||||||||||||||
| 281 | 9033 | ▶ | if ($n_in) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 282 | 9033 | ▶ | if ($n_out) { ### native input, native output [fastest] | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 283 | 9033 | ▶ | while (<$n_in>) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 284 | # Normalize line ending | ||||||||||||||||||||||||||||||||||||||||||
| 285 | 18065 | ▶ | $_ =~ s/(?:\n\r|\r\n|\r)$/\n/ if $normalize_newlines; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 286 | 18065 | ▶ | if (substr($_, 0, 2) eq '--') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 287 | 5025 | ($maybe = $_) =~ s/[ \t\r\n]+\Z//; | |||||||||||||||||||||||||||||||||||||||||
| 288 | 5025 | ▶ | $bh->{$maybe} and do { $eos = $bh->{$maybe}; last }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 289 | } | ||||||||||||||||||||||||||||||||||||||||||
| 290 | 13069 | ▶ | $thx and $th->{$_} and do { $eos = $th->{$_}; last }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$thx and $th->{$_}
MC/DC100%2/2
$thx and $th->{$_}
$thx
$th->{$_}
Truth table100%3/3
A and B
A= $thxB= $th->{$_}
| |||||||||||||||||||||||||||||||||||||||||||
| 291 | 9097 | print $n_out $last; $last = $_; | |||||||||||||||||||||||||||||||||||||||||
| 292 | } | ||||||||||||||||||||||||||||||||||||||||||
| 293 | } | ||||||||||||||||||||||||||||||||||||||||||
| 294 | else { ### native input, OO output [slower] | ||||||||||||||||||||||||||||||||||||||||||
| 295 | 0 | ▶ | while (<$n_in>) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 296 | # Normalize line ending | ||||||||||||||||||||||||||||||||||||||||||
| 297 | 0 | ▶ | $_ =~ s/(?:\n\r|\r\n|\r)$/\n/ if $normalize_newlines; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 298 | 0 | ▶ | if (substr($_, 0, 2) eq '--') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 299 | 0 | ($maybe = $_) =~ s/[ \t\r\n]+\Z//; | |||||||||||||||||||||||||||||||||||||||||
| 300 | 0 | ▶ | $bh->{$maybe} and do { $eos = $bh->{$maybe}; last }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 301 | } | ||||||||||||||||||||||||||||||||||||||||||
| 302 | 0 | ▶ | $thx and $th->{$_} and do { $eos = $th->{$_}; last }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$thx and $th->{$_}
MC/DC0%0/2
$thx and $th->{$_}
$thx
$th->{$_}
Truth table0%0/3
A and B
A= $thxB= $th->{$_}
| |||||||||||||||||||||||||||||||||||||||||||
| 303 | 0 | $out->print($last); $last = $_; | |||||||||||||||||||||||||||||||||||||||||
| 304 | } | ||||||||||||||||||||||||||||||||||||||||||
| 305 | } | ||||||||||||||||||||||||||||||||||||||||||
| 306 | } | ||||||||||||||||||||||||||||||||||||||||||
| 307 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 308 | 0 | ▶ | if ($n_out) { ### OO input, native output [even slower] | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 309 | 0 | ▶ | while (defined($_ = $in->getline)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 310 | # Normalize line ending | ||||||||||||||||||||||||||||||||||||||||||
| 311 | 0 | ▶ | $_ =~ s/(?:\n\r|\r\n|\r)$/\n/ if $normalize_newlines; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 312 | 0 | ▶ | if (substr($_, 0, 2) eq '--') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 313 | 0 | ($maybe = $_) =~ s/[ \t\r\n]+\Z//; | |||||||||||||||||||||||||||||||||||||||||
| 314 | 0 | ▶ | $bh->{$maybe} and do { $eos = $bh->{$maybe}; last }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 315 | } | ||||||||||||||||||||||||||||||||||||||||||
| 316 | 0 | ▶ | $thx and $th->{$_} and do { $eos = $th->{$_}; last }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$thx and $th->{$_}
MC/DC0%0/2
$thx and $th->{$_}
$thx
$th->{$_}
Truth table0%0/3
A and B
A= $thxB= $th->{$_}
| |||||||||||||||||||||||||||||||||||||||||||
| 317 | 0 | print $n_out $last; $last = $_; | |||||||||||||||||||||||||||||||||||||||||
| 318 | } | ||||||||||||||||||||||||||||||||||||||||||
| 319 | } | ||||||||||||||||||||||||||||||||||||||||||
| 320 | else { ### OO input, OO output [slowest] | ||||||||||||||||||||||||||||||||||||||||||
| 321 | 0 | ▶ | while (defined($_ = $in->getline)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 322 | # Normalize line ending | ||||||||||||||||||||||||||||||||||||||||||
| 323 | 0 | ▶ | $_ =~ s/(?:\n\r|\r\n|\r)$/\n/ if $normalize_newlines; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 324 | 0 | ▶ | if (substr($_, 0, 2) eq '--') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 325 | 0 | ($maybe = $_) =~ s/[ \t\r\n]+\Z//; | |||||||||||||||||||||||||||||||||||||||||
| 326 | 0 | ▶ | $bh->{$maybe} and do { $eos = $bh->{$maybe}; last }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 327 | } | ||||||||||||||||||||||||||||||||||||||||||
| 328 | 0 | ▶ | $thx and $th->{$_} and do { $eos = $th->{$_}; last }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$thx and $th->{$_}
MC/DC0%0/2
$thx and $th->{$_}
$thx
$th->{$_}
Truth table0%0/3
A and B
A= $thxB= $th->{$_}
| |||||||||||||||||||||||||||||||||||||||||||
| 329 | 0 | $out->print($last); $last = $_; | |||||||||||||||||||||||||||||||||||||||||
| 330 | } | ||||||||||||||||||||||||||||||||||||||||||
| 331 | } | ||||||||||||||||||||||||||||||||||||||||||
| 332 | } | ||||||||||||||||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||||||||||||||||
| 334 | # Write out last held line, removing terminating CRLF if ended on bound, | ||||||||||||||||||||||||||||||||||||||||||
| 335 | # unless the line consists only of CRLF and we're wanting to keep the | ||||||||||||||||||||||||||||||||||||||||||
| 336 | # preceding blank line (as when parsing a preamble) | ||||||||||||||||||||||||||||||||||||||||||
| 337 | 9033 | ▶ | $last =~ s/[\r\n]+\Z// if ($eos =~ /^(DELIM|CLOSE)/ && !($keep_newline && $last =~ m/^(\r\n|\n\r|\n|\r)\z/)); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$eos =~ /^(DELIM|CLOSE)/ and not $keep_newline && $last =~ /^(\r\n|\n\r|\n|\r)\z/
$keep_newline && $last =~ /^(\r\n|\n\r|\n|\r)\z/
MC/DC100%3/3
$eos =~ /^(DELIM|CLOSE)/ and not $keep_newline && $last =~ /^(\r\n|\n\r|\n|\r)\z/
$eos =~ /^(DELIM|CLOSE)/
$keep_newline
$last =~ /^(\r\n|\n\r|\n|\r)\z/
Truth table100%4/4
A and not(B && C)
A= $eos =~ /^(DELIM|CLOSE)/B= $keep_newlineC= $last =~ /^(\r\n|\n\r|\n|\r)\z/
| |||||||||||||||||||||||||||||||||||||||||||
| 338 | 9033 | $out->print($last); | |||||||||||||||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||||||||||||||||
| 340 | ### Save and return what we finished on: | ||||||||||||||||||||||||||||||||||||||||||
| 341 | 9033 | ▶ | ${$self->{EOS}} = ($eos || 'EOF'); | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$eos || 'EOF'
MC/DC100%1/1
$eos || 'EOF'
$eos
Truth table100%2/2
A
A= $eos
| |||||||||||||||||||||||||||||||||||||||||||
| 342 | 9033 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 343 | } | ||||||||||||||||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||||||||||||||||
| 345 | #------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 346 | # | ||||||||||||||||||||||||||||||||||||||||||
| 347 | # read_lines INHANDLE, \@OUTLINES | ||||||||||||||||||||||||||||||||||||||||||
| 348 | # | ||||||||||||||||||||||||||||||||||||||||||
| 349 | # I<Instance method.> | ||||||||||||||||||||||||||||||||||||||||||
| 350 | # Read lines into the given array. | ||||||||||||||||||||||||||||||||||||||||||
| 351 | # | ||||||||||||||||||||||||||||||||||||||||||
| 352 | 1126 | ▶ | sub read_lines { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
read_lines: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 353 | 1126 | my ($self, $in, $outlines) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||||||||||||||||
| 355 | 1126 | my $data = ''; | |||||||||||||||||||||||||||||||||||||||||
| 356 | 1126 | ▶ | open(my $fh, '>', \$data) or die $!; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 357 | 1126 | $self->read_chunk($in, $fh); | |||||||||||||||||||||||||||||||||||||||||
| 358 | 1126 | @$outlines = split(/^/, $data); | |||||||||||||||||||||||||||||||||||||||||
| 359 | 1126 | close $fh; | |||||||||||||||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||||||||||||||||
| 361 | 1126 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 362 | } | ||||||||||||||||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||||||||||||||||
| 364 | 26 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 365 | __END__ | ||||||||||||||||||||||||||||||||||||||||||