| 1 | # Paranoid::IO::Line -- Paranoid Line-based I/O functions | ||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||
| 3 | # $Id: lib/Paranoid/IO/Line.pm, 2.10 2022/03/08 00:01:04 acorliss Exp $ | ||||||||||||||||||||||||||||
| 4 | # | ||||||||||||||||||||||||||||
| 5 | # This software is free software. Similar to Perl, you can redistribute it | ||||||||||||||||||||||||||||
| 6 | # and/or modify it under the terms of either: | ||||||||||||||||||||||||||||
| 7 | # | ||||||||||||||||||||||||||||
| 8 | # a) the GNU General Public License | ||||||||||||||||||||||||||||
| 9 | # <https://www.gnu.org/licenses/gpl-1.0.html> as published by the | ||||||||||||||||||||||||||||
| 10 | # Free Software Foundation <http://www.fsf.org/>; either version 1 | ||||||||||||||||||||||||||||
| 11 | # <https://www.gnu.org/licenses/gpl-1.0.html>, or any later version | ||||||||||||||||||||||||||||
| 12 | # <https://www.gnu.org/licenses/license-list.html#GNUGPL>, or | ||||||||||||||||||||||||||||
| 13 | # b) the Artistic License 2.0 | ||||||||||||||||||||||||||||
| 14 | # <https://opensource.org/licenses/Artistic-2.0>, | ||||||||||||||||||||||||||||
| 15 | # | ||||||||||||||||||||||||||||
| 16 | # subject to the following additional term: No trademark rights to | ||||||||||||||||||||||||||||
| 17 | # "Paranoid" have been or are conveyed under any of the above licenses. | ||||||||||||||||||||||||||||
| 18 | # However, "Paranoid" may be used fairly to describe this unmodified | ||||||||||||||||||||||||||||
| 19 | # software, in good faith, but not as a trademark. | ||||||||||||||||||||||||||||
| 20 | # | ||||||||||||||||||||||||||||
| 21 | # (c) 2005 - 2020, Arthur Corliss (corliss@digitalmages.com) | ||||||||||||||||||||||||||||
| 22 | # (tm) 2008 - 2020, Paranoid Inc. (www.paranoid.com) | ||||||||||||||||||||||||||||
| 23 | # | ||||||||||||||||||||||||||||
| 24 | ##################################################################### | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | ##################################################################### | ||||||||||||||||||||||||||||
| 27 | # | ||||||||||||||||||||||||||||
| 28 | # Environment definitions | ||||||||||||||||||||||||||||
| 29 | # | ||||||||||||||||||||||||||||
| 30 | ##################################################################### | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | package Paranoid::IO::Line; | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 33 | ▶ | use 5.008; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 33 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | 33 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 38 | 33 | ▶ | use vars qw($VERSION @EXPORT @EXPORT_OK %EXPORT_TAGS); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 39 | 33 | ▶ | use base qw(Exporter); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 40 | 33 | ▶ | use Fcntl qw(:DEFAULT :seek :flock :mode); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 41 | 33 | ▶ | use Paranoid qw(:all); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 42 | 33 | ▶ | use Paranoid::Debug qw(:all); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 43 | 33 | ▶ | use Paranoid::IO qw(:all); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 44 | 33 | ▶ | use Paranoid::Input qw(:all); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 33 | ($VERSION) = ( q$Revision: 2.10 $ =~ /(\d+(?:\.\d+)+)/sm ); | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 33 | @EXPORT = qw(sip nlsip tailf nltailf slurp nlslurp piolClose); | |||||||||||||||||||||||||||
| 49 | 33 | @EXPORT_OK = ( @EXPORT, qw(PIOMAXLNSIZE) ); | |||||||||||||||||||||||||||
| 50 | 33 | %EXPORT_TAGS = ( all => [@EXPORT_OK], ); | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 33 | ▶ | use constant STAT_INO => 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 53 | 33 | ▶ | use constant STAT_SIZ => 7; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 54 | 33 | ▶ | use constant PDEFLNSZ => 2048; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 33 | ▶ | use constant PBFLAG => 0; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 57 | 33 | ▶ | use constant PBBUFF => 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 33 | ▶ | use constant PBF_DRAIN => 0; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 60 | 33 | ▶ | use constant PBF_NORMAL => 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 61 | 33 | ▶ | use constant PBF_DELETE => -1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | ##################################################################### | ||||||||||||||||||||||||||||
| 64 | # | ||||||||||||||||||||||||||||
| 65 | # Module code follows | ||||||||||||||||||||||||||||
| 66 | # | ||||||||||||||||||||||||||||
| 67 | ##################################################################### | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | { | ||||||||||||||||||||||||||||
| 70 | 33 | my $mlnsz = PDEFLNSZ; | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 3146 | ▶ | sub PIOMAXLNSIZE : lvalue { | ||||||||||||||||||||||||||
|
Subroutine
PIOMAXLNSIZE: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | # Purpose: Gets/sets default line size of I/O | ||||||||||||||||||||||||||||
| 75 | # Returns: $mlnsz | ||||||||||||||||||||||||||||
| 76 | # Usage: $limit = PIOMAXLNSIZE; | ||||||||||||||||||||||||||||
| 77 | # Usage: FSZLIMIT = 100; | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 3146 | $mlnsz; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | # Manage buffers: $buffers{$name} => [$flag, $content ]; | ||||||||||||||||||||||||||||
| 83 | 33 | my %buffers; | |||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 18 | ▶ | sub _chkBuffer { return exists $buffers{ $_[0] } } | ||||||||||||||||||||||||||
|
Subroutine
_chkBuffer: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 100 | ▶ | sub _chkStat { | ||||||||||||||||||||||||||
|
Subroutine
_chkStat: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | # Purpose: Checks stat data to see if the underlying | ||||||||||||||||||||||||||||
| 90 | # file has changed | ||||||||||||||||||||||||||||
| 91 | # Returns: Boolean | ||||||||||||||||||||||||||||
| 92 | # Usage: $rv = _chkStat($file); | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 100 | my $file = shift; | |||||||||||||||||||||||||||
| 95 | 100 | my $rv = 0; | |||||||||||||||||||||||||||
| 96 | 100 | my ( $fh, $fpos, @fstat, @fhstat ); | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 100 | subPreamble( PDLEVEL3, '$', $file ); | |||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | # Check to see if we can get a valid file handle | ||||||||||||||||||||||||||||
| 101 | 100 | ▶ | if ( defined( $fh = popen( $file, O_RDONLY ) ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 96 | @fhstat = stat $fh; | |||||||||||||||||||||||||||
| 103 | 96 | $fpos = ptell($fh); | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 96 | ▶ | if ( @fhstat and $fpos < $fhstat[STAT_SIZ] ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@fhstat and $fpos < $fhstat[7]
MC/DC50%1/2
@fhstat and $fpos < $fhstat[7]
@fhstat
$fpos < $fhstat[7]
Truth table66%2/3
A and B
A= @fhstatB= $fpos < $fhstat[7]
| |||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | # Still have content to read, continue on | ||||||||||||||||||||||||||||
| 108 | 80 | pdebug( 'still have content to drain', PDLEVEL3 ); | |||||||||||||||||||||||||||
| 109 | 80 | $rv = 1; | |||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | } else { | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | # Check the file system to see if we're still | ||||||||||||||||||||||||||||
| 114 | # operating on the same file | ||||||||||||||||||||||||||||
| 115 | 16 | @fstat = stat $file; | |||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 16 | ▶ | if ( scalar @fstat ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | # Check inode | ||||||||||||||||||||||||||||
| 120 | 14 | ▶ | if ( $fhstat[STAT_INO] != $fstat[STAT_INO] ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | 2 | pdebug( 'file was replaced with a new file', | |||||||||||||||||||||||||||
| 122 | PDLEVEL3 ); | ||||||||||||||||||||||||||||
| 123 | } else { | ||||||||||||||||||||||||||||
| 124 | 12 | ▶ | if ( $fstat[STAT_SIZ] < $fpos ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | 2 | pdebug( 'file was truncated', PDLEVEL3 ); | |||||||||||||||||||||||||||
| 126 | } else { | ||||||||||||||||||||||||||||
| 127 | 10 | pdebug( 'file is unchanged', PDLEVEL3 ); | |||||||||||||||||||||||||||
| 128 | 10 | $rv = 1; | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | } else { | ||||||||||||||||||||||||||||
| 133 | 2 | pdebug( 'file was deleted', PDLEVEL3 ); | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | } else { | ||||||||||||||||||||||||||||
| 137 | 4 | pdebug( 'invalid/non-existent file', PDLEVEL3 ); | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 100 | subPostamble( PDLEVEL3, '$', $rv ); | |||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 100 | return $rv; | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 64 | ▶ | sub piolClose { | ||||||||||||||||||||||||||
|
Subroutine
piolClose: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | # Purpose: Closes file handles and deletes the associated | ||||||||||||||||||||||||||||
| 148 | # buffer | ||||||||||||||||||||||||||||
| 149 | # Returns: Boolean | ||||||||||||||||||||||||||||
| 150 | # Usage: $rv = piolClose($file); | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 64 | my $file = shift; | |||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 64 | delete $buffers{$file}; | |||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 64 | return pclose($file); | |||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 100 | ▶ | sub sip ($\@;$$) { | ||||||||||||||||||||||||||
|
Subroutine
sip: called CC=22 SCAR=30.9
Pod
documented
| |||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | # Purpose: Reads a chunk from the passwed handle or file name | ||||||||||||||||||||||||||||
| 162 | # Returns: Number of lines read or undef critical failures | ||||||||||||||||||||||||||||
| 163 | # Usage: $nlines = sip($fh, @lines); | ||||||||||||||||||||||||||||
| 164 | # Usage: $nlines = sip($filename, @lines); | ||||||||||||||||||||||||||||
| 165 | # Usage: $nlines = sip($filename, @lines, 1); | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 100 | my $file = shift; | |||||||||||||||||||||||||||
| 168 | 100 | my $aref = shift; | |||||||||||||||||||||||||||
| 169 | 100 | my $doChomp = shift; | |||||||||||||||||||||||||||
| 170 | 100 | my $noLocks = shift; | |||||||||||||||||||||||||||
| 171 | 100 | my $rv = 1; | |||||||||||||||||||||||||||
| 172 | 100 | my ( $buffer, $bflag, $in, $content, $bread, $irv, @tmp, $line ); | |||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 100 | subPreamble( PDLEVEL1, '$\@;$$', $file, $aref, $doChomp, $noLocks ); | |||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 100 | @$aref = (); | |||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | # Check the file | ||||||||||||||||||||||||||||
| 179 | 100 | ▶ | piolClose($file) unless _chkStat($file); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | # Get/initialize buffer | ||||||||||||||||||||||||||||
| 182 | 100 | ▶ | if ( exists $buffers{$file} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 183 | 42 | $bflag = $buffers{$file}[PBFLAG]; | |||||||||||||||||||||||||||
| 184 | 42 | $buffer = $buffers{$file}[PBBUFF]; | |||||||||||||||||||||||||||
| 185 | } else { | ||||||||||||||||||||||||||||
| 186 | 58 | $buffers{$file} = [ PBF_NORMAL, '' ]; | |||||||||||||||||||||||||||
| 187 | 58 | $buffer = ''; | |||||||||||||||||||||||||||
| 188 | 58 | $bflag = PBF_NORMAL; | |||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | # Read what we can | ||||||||||||||||||||||||||||
| 192 | 100 | $content = ''; | |||||||||||||||||||||||||||
| 193 | 100 | $bread = 0; | |||||||||||||||||||||||||||
| 194 | 100 | ▶ | while ( $bread < PIOMAXFSIZE ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 195 | 425 | ▶ | $irv = $noLocks ? pnlread( $file, $in ) : pread( $file, $in ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 196 | 425 | ▶ | if ( defined $irv ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 197 | 419 | $bread += $irv; | |||||||||||||||||||||||||||
| 198 | 419 | $content .= $in; | |||||||||||||||||||||||||||
| 199 | 419 | ▶ | last if $irv < PIOBLKSIZE; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 200 | } else { | ||||||||||||||||||||||||||||
| 201 | 6 | $rv = undef; | |||||||||||||||||||||||||||
| 202 | 6 | last; | |||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | # Post processing | ||||||||||||||||||||||||||||
| 207 | 100 | ▶ | if ($rv) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | 94 | ▶ | if ( length $content ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | # Add the buffer | ||||||||||||||||||||||||||||
| 212 | 84 | $content = "$buffer$content"; | |||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | # Process buffer drain conditions | ||||||||||||||||||||||||||||
| 215 | 84 | pdebug( 'starting buffer flag: (%s)', PDLEVEL4, $bflag ); | |||||||||||||||||||||||||||
| 216 | 84 | pdebug( 'starting buffer: (%s)', PDLEVEL4, $buffer ); | |||||||||||||||||||||||||||
| 217 | 84 | ▶ | if ( !$bflag and $content =~ /@{[NEWLINE_REGEX]}/so ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not $bflag and $content =~ m[@{[qr/(?:\15\12|\15|\12)/s];}]so
MC/DC100%2/2
not $bflag and $content =~ m[@{[qr/(?:\15\12|\15|\12)/s];}]so
not $bflag
$content =~ m[@{[qr/(?:\15\12|\15|\12)/s];}]so
Truth table100%3/3
not(A) and B
A= not $bflagB= $content =~ m[@{[qr/(?:\15\12|\15|\12)/s];}]so
| |||||||||||||||||||||||||||||
| 218 | 8 | pdebug( 'draining to next newline', PDLEVEL4 ); | |||||||||||||||||||||||||||
| 219 | 8 | $content =~ s/^.*?@{[NEWLINE_REGEX]}//so; | |||||||||||||||||||||||||||
| 220 | 8 | $bflag = PBF_NORMAL; | |||||||||||||||||||||||||||
| 221 | 8 | $buffer = ''; | |||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | # Check for newlines | ||||||||||||||||||||||||||||
| 225 | 84 | ▶ | if ( $content =~ /@{[NEWLINE_REGEX]}/so ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | # Split lines along newline boundaries | ||||||||||||||||||||||||||||
| 228 | 78 | @tmp = split m/(@{[NEWLINE_REGEX]})/so, $content; | |||||||||||||||||||||||||||
| 229 | 78 | ▶ | while ( scalar @tmp > 1 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 230 | 3108 | ▶ | if ( length $tmp[0] > PIOMAXLNSIZE ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 231 | 2 | splice @tmp, 0, 2; | |||||||||||||||||||||||||||
| 232 | 2 | $line = undef; | |||||||||||||||||||||||||||
| 233 | } else { | ||||||||||||||||||||||||||||
| 234 | 3106 | $line = join '', splice @tmp, 0, 2; | |||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||
| 236 | 3108 | push @$aref, $line; | |||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | # Check for undefined lines | ||||||||||||||||||||||||||||
| 240 | 78 | $rv = scalar @$aref; | |||||||||||||||||||||||||||
| 241 | 78 | @$aref = grep {defined} @$aref; | |||||||||||||||||||||||||||
| 242 | 78 | ▶ | if ( $rv != scalar @$aref ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 243 | 2 | Paranoid::ERROR = | |||||||||||||||||||||||||||
| 244 | pdebug( 'found %s lines over PIOMAXLNSIZE', | ||||||||||||||||||||||||||||
| 245 | PDLEVEL1, $rv - @$aref ); | ||||||||||||||||||||||||||||
| 246 | 2 | $rv = undef; | |||||||||||||||||||||||||||
| 247 | } | ||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | # Check for an unterminated line at the end and | ||||||||||||||||||||||||||||
| 250 | # buffer appropriately | ||||||||||||||||||||||||||||
| 251 | 78 | ▶ | if ( scalar @tmp ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | # Content left over, update the buffer | ||||||||||||||||||||||||||||
| 254 | 26 | ▶ | if ( length $tmp[0] > PIOMAXLNSIZE ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 255 | 8 | $buffer = ''; | |||||||||||||||||||||||||||
| 256 | 8 | $bflag = PBF_DRAIN; | |||||||||||||||||||||||||||
| 257 | 8 | $rv = undef; | |||||||||||||||||||||||||||
| 258 | 8 | Paranoid::ERROR = | |||||||||||||||||||||||||||
| 259 | pdebug( 'buffer is over PIOMAXLNSIZE', | ||||||||||||||||||||||||||||
| 260 | PDLEVEL1 ); | ||||||||||||||||||||||||||||
| 261 | } else { | ||||||||||||||||||||||||||||
| 262 | 18 | $buffer = $tmp[0]; | |||||||||||||||||||||||||||
| 263 | 18 | $bflag = PBF_NORMAL; | |||||||||||||||||||||||||||
| 264 | } | ||||||||||||||||||||||||||||
| 265 | } else { | ||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | # Nothing left over, make sure the buffer is empty | ||||||||||||||||||||||||||||
| 268 | 52 | $buffer = ''; | |||||||||||||||||||||||||||
| 269 | 52 | $bflag = PBF_NORMAL; | |||||||||||||||||||||||||||
| 270 | } | ||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||
| 272 | } else { | ||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||
| 274 | # Check buffered block for PIOILNSIZE limit | ||||||||||||||||||||||||||||
| 275 | 6 | ▶ | if ( length $content > PIOMAXLNSIZE ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 276 | 2 | $buffer = ''; | |||||||||||||||||||||||||||
| 277 | 2 | $bflag = PBF_DRAIN; | |||||||||||||||||||||||||||
| 278 | 2 | $rv = undef; | |||||||||||||||||||||||||||
| 279 | 2 | Paranoid::ERROR = | |||||||||||||||||||||||||||
| 280 | pdebug( 'block is over PIOMAXLNSIZE', PDLEVEL1 ); | ||||||||||||||||||||||||||||
| 281 | } else { | ||||||||||||||||||||||||||||
| 282 | 4 | $rv = 0; | |||||||||||||||||||||||||||
| 283 | 4 | $buffer = $content; | |||||||||||||||||||||||||||
| 284 | 4 | $bflag = PBF_NORMAL; | |||||||||||||||||||||||||||
| 285 | } | ||||||||||||||||||||||||||||
| 286 | } | ||||||||||||||||||||||||||||
| 287 | 84 | pdebug( 'ending buffer flag: (%s)', PDLEVEL4, $bflag ); | |||||||||||||||||||||||||||
| 288 | 84 | pdebug( 'ending buffer: (%s)', PDLEVEL4, $buffer ); | |||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||
| 290 | } else { | ||||||||||||||||||||||||||||
| 291 | 10 | $rv = 0; | |||||||||||||||||||||||||||
| 292 | } | ||||||||||||||||||||||||||||
| 293 | } | ||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||
| 295 | # Set PTRUE_ZERO if needed | ||||||||||||||||||||||||||||
| 296 | 100 | ▶ | $rv = PTRUE_ZERO if defined $rv and $rv == 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $rv and $rv == 0
MC/DC100%2/2
defined $rv and $rv == 0
defined $rv
$rv == 0
Truth table100%3/3
A and B
A= defined $rvB= $rv == 0
| |||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||
| 298 | # Save the buffer | ||||||||||||||||||||||||||||
| 299 | 100 | $buffers{$file}[PBFLAG] = $bflag; | |||||||||||||||||||||||||||
| 300 | 100 | $buffers{$file}[PBBUFF] = $buffer; | |||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||
| 302 | # Chomp if necessary | ||||||||||||||||||||||||||||
| 303 | 100 | ▶ | pchomp(@$aref) if $doChomp and scalar @$aref; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$doChomp and scalar @$aref
MC/DC100%1/1
$doChomp and scalar @$aref
$doChomp
Truth table100%2/2
A
A= $doChomp
| |||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||
| 305 | 100 | pdebug( 'returning %s lines', PDLEVEL2, scalar @$aref ); | |||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||
| 307 | 100 | subPostamble( PDLEVEL1, '$', $rv ); | |||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||
| 309 | 100 | return $rv; | |||||||||||||||||||||||||||
| 310 | } | ||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||
| 312 | } | ||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||
| 314 | 33 | ▶ | sub nlsip { | ||||||||||||||||||||||||||
|
Subroutine
nlsip: not called CC=1 SCAR=4.1
Pod
documented
| |||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | # Purpose: Wrapper for sip that enables non-locking reads | ||||||||||||||||||||||||||||
| 317 | # Returns: Return value from sip | ||||||||||||||||||||||||||||
| 318 | # Usage: $nlines = nlsip($file, @lines); | ||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||
| 320 | 0 | my $file = shift; | |||||||||||||||||||||||||||
| 321 | 0 | my $aref = shift; | |||||||||||||||||||||||||||
| 322 | 0 | my $doChomp = shift; | |||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||
| 324 | 0 | return sip( $file, @$aref, $doChomp, 1 ); | |||||||||||||||||||||||||||
| 325 | } | ||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||
| 327 | 18 | ▶ | sub tailf ($\@;$$$) { | ||||||||||||||||||||||||||
|
Subroutine
tailf: called CC=12 SCAR=25.3
Pod
documented
| |||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||
| 329 | # Purpose: Augments sip's tailing abilities by seeking to | ||||||||||||||||||||||||||||
| 330 | # the end (or, optionally, backwards) | ||||||||||||||||||||||||||||
| 331 | # Returns: Number of lines tailed | ||||||||||||||||||||||||||||
| 332 | # Usage: $nlines = tail($filename, @lines); | ||||||||||||||||||||||||||||
| 333 | # Usage: $nlines = tail($filename, @lines, $chomp); | ||||||||||||||||||||||||||||
| 334 | # Usage: $nlines = tail($filename, @lines, $lnOffset); | ||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||
| 336 | 18 | my $file = shift; | |||||||||||||||||||||||||||
| 337 | 18 | my $aref = shift; | |||||||||||||||||||||||||||
| 338 | 18 | ▶ | my $doChomp = shift || 0; | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() || 0
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 339 | 18 | ▶ | my $offset = shift || -10; | ||||||||||||||||||||||||||
|
Condition100%2/2
shift() || -10
MC/DC100%1/1
shift() || -10
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 340 | 18 | my $noLocks = shift; | |||||||||||||||||||||||||||
| 341 | 18 | my ( $rv, $ofsb, @lines ); | |||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||
| 343 | 18 | subPreamble( PDLEVEL1, '$\@;$$$', $file, $aref, $doChomp, $offset, | |||||||||||||||||||||||||||
| 344 | $noLocks ); | ||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||
| 346 | 18 | @$aref = (); | |||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||
| 348 | # Check to see if we've already opened this file | ||||||||||||||||||||||||||||
| 349 | 18 | ▶ | if ( _chkBuffer($file) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||
| 351 | # Offset is only used on the initial open | ||||||||||||||||||||||||||||
| 352 | 12 | $offset = 0; | |||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||
| 354 | } else { | ||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||
| 356 | # TODO: At some point we might want to honor positive offsets to mimic | ||||||||||||||||||||||||||||
| 357 | # the behavior of UNIX tail | ||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||
| 359 | # Calculate how far back we need to go from the end | ||||||||||||||||||||||||||||
| 360 | 6 | $ofsb = $offset * ( PIOMAXLNSIZE +1 ); | |||||||||||||||||||||||||||
| 361 | 6 | ▶ | Paranoid::ERROR = | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 362 | pdebug( 'WARNING: called with a positive line offset', PDLEVEL1 ) | ||||||||||||||||||||||||||||
| 363 | unless $ofsb < 0; | ||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||
| 365 | # Open the file and move the cursor | ||||||||||||||||||||||||||||
| 366 | 6 | ▶ | pseek( $file, $ofsb, SEEK_END ) if popen( $file, O_RDONLY ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||
| 368 | } | ||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||
| 370 | # If $offset is set we have trailing lines to handle | ||||||||||||||||||||||||||||
| 371 | 18 | ▶ | if ($offset) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||
| 373 | # Consume everything to the end of the file | ||||||||||||||||||||||||||||
| 374 | 6 | ▶ | do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 375 | 22 | ▶ | $noLocks | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 376 | ? nlsip( $file, @lines, $doChomp ) | ||||||||||||||||||||||||||||
| 377 | : sip( $file, @lines, $doChomp ); | ||||||||||||||||||||||||||||
| 378 | 22 | push @$aref, @lines; | |||||||||||||||||||||||||||
| 379 | } while scalar @lines; | ||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||
| 381 | # Trim list to the request size | ||||||||||||||||||||||||||||
| 382 | 6 | ▶ | if ( scalar @$aref > abs $offset ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 383 | 6 | splice @$aref, 0, @$aref - abs $offset; | |||||||||||||||||||||||||||
| 384 | } | ||||||||||||||||||||||||||||
| 385 | 6 | $rv = scalar @$aref; | |||||||||||||||||||||||||||
| 386 | 6 | ▶ | $rv = PTRUE_ZERO unless $rv; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||
| 388 | } else { | ||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||
| 390 | # Do a single sip | ||||||||||||||||||||||||||||
| 391 | 12 | ▶ | $rv = | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 392 | $noLocks | ||||||||||||||||||||||||||||
| 393 | ? nlsip( $file, @$aref, $doChomp ) | ||||||||||||||||||||||||||||
| 394 | : sip( $file, @$aref, $doChomp ); | ||||||||||||||||||||||||||||
| 395 | } | ||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||
| 397 | 18 | subPostamble( PDLEVEL1, '$', $rv ); | |||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||
| 399 | 18 | return $rv; | |||||||||||||||||||||||||||
| 400 | } | ||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||
| 402 | 0 | ▶ | sub nltailf ($\@;$$$) { | ||||||||||||||||||||||||||
|
Subroutine
nltailf: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||
| 404 | # Purpose: Wrapper for sip that enables non-locking reads | ||||||||||||||||||||||||||||
| 405 | # Returns: Return value from sip | ||||||||||||||||||||||||||||
| 406 | # Usage: $nlines = nlsip($file, @lines); | ||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||
| 408 | 0 | my $file = shift; | |||||||||||||||||||||||||||
| 409 | 0 | my $aref = shift; | |||||||||||||||||||||||||||
| 410 | 0 | my $doChomp = shift; | |||||||||||||||||||||||||||
| 411 | 0 | my $offset = shift; | |||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||
| 413 | 0 | return tailf( $file, @$aref, $doChomp, $offset, 1 ); | |||||||||||||||||||||||||||
| 414 | } | ||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||
| 416 | 46 | ▶ | sub slurp ($\@;$$) { | ||||||||||||||||||||||||||
|
Subroutine
slurp: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||
| 418 | # Purpose: Reads a file into memory | ||||||||||||||||||||||||||||
| 419 | # Returns: Number of lines read/undef | ||||||||||||||||||||||||||||
| 420 | # Usage: $nlines = slurp($filename, @lines; | ||||||||||||||||||||||||||||
| 421 | # Usage: $nlines = slurp($filename, @lines, 1); | ||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||
| 423 | 46 | my $file = shift; | |||||||||||||||||||||||||||
| 424 | 46 | my $aref = shift; | |||||||||||||||||||||||||||
| 425 | 46 | ▶ | my $doChomp = shift || 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
shift() || 0
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 426 | 46 | my $noLocks = shift; | |||||||||||||||||||||||||||
| 427 | 46 | my $rv = 1; | |||||||||||||||||||||||||||
| 428 | 46 | my @fstat; | |||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||
| 430 | 46 | subPreamble( PDLEVEL1, '$\@;$$', $file, $aref, $doChomp, $noLocks ); | |||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||
| 432 | # Start sipping | ||||||||||||||||||||||||||||
| 433 | 46 | $rv = sip( $file, @$aref, $doChomp, $noLocks ); | |||||||||||||||||||||||||||
| 434 | 46 | ▶ | if ( ref $file eq 'GLOB' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 435 | 4 | ▶ | @fstat = stat $file if fileno $file; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 436 | } else { | ||||||||||||||||||||||||||||
| 437 | 42 | @fstat = stat $file; | |||||||||||||||||||||||||||
| 438 | } | ||||||||||||||||||||||||||||
| 439 | 46 | ▶ | if ( scalar @fstat and $fstat[STAT_SIZ] > PIOMAXFSIZE ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
scalar @fstat and $fstat[7] > PIOMAXFSIZE()
MC/DC100%2/2
scalar @fstat and $fstat[7] > PIOMAXFSIZE()
scalar @fstat
$fstat[7] > PIOMAXFSIZE()
Truth table100%3/3
A and B
A= scalar @fstatB= $fstat[7] > PIOMAXFSIZE()
| |||||||||||||||||||||||||||||
| 440 | 2 | Paranoid::ERROR = pdebug( 'file size exceeds PIOMAXFSIZE', PDLEVEL1 ); | |||||||||||||||||||||||||||
| 441 | 2 | $rv = undef; | |||||||||||||||||||||||||||
| 442 | } | ||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||
| 444 | # Count lins if sip never complained | ||||||||||||||||||||||||||||
| 445 | 46 | ▶ | $rv = scalar @$aref if defined $rv; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||
| 447 | # Close everything out | ||||||||||||||||||||||||||||
| 448 | 46 | piolClose($file); | |||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||
| 450 | 46 | subPostamble( PDLEVEL1, '$', $rv ); | |||||||||||||||||||||||||||
| 451 | |||||||||||||||||||||||||||||
| 452 | 46 | return $rv; | |||||||||||||||||||||||||||
| 453 | } | ||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||
| 455 | 0 | ▶ | sub nlslurp ($\@;$$) { | ||||||||||||||||||||||||||
|
Subroutine
nlslurp: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||
| 457 | # Purpose: Performs a non-locking slurp | ||||||||||||||||||||||||||||
| 458 | # Returns: Number of lines/undef | ||||||||||||||||||||||||||||
| 459 | # Usage: $nlines = nlslurp($filename, @lines); | ||||||||||||||||||||||||||||
| 460 | # Usage: $nlines = nlslurp($filename, @lines, 1); | ||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||
| 462 | 0 | my $file = shift; | |||||||||||||||||||||||||||
| 463 | 0 | my $aref = shift; | |||||||||||||||||||||||||||
| 464 | 0 | ▶ | my $doChomp = shift || 0; | ||||||||||||||||||||||||||
|
Condition0%0/2
shift() || 0
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||
| 466 | 0 | return slurp( $file, @$aref, $doChomp, 1 ); | |||||||||||||||||||||||||||
| 467 | } | ||||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||
| 469 | 33 | 1; | |||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||
| 471 | __END__ | ||||||||||||||||||||||||||||