| 1 | package File::Index; | ||||||||||||||||||||||||||||
| 2 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use Exporter (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use vars qw($VERSION @ISA @EXPORT); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 2 | $VERSION = "0.06"; | |||||||||||||||||||||||||||
| 10 | 2 | @ISA = qw(Exporter); | |||||||||||||||||||||||||||
| 11 | 2 | @EXPORT = qw(indexf rindexf); | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 0 | ▶ | sub indexf { | ||||||||||||||||||||||||||
|
Subroutine
indexf: not called CC=11 SCAR=48.8
Pod
documented
| |||||||||||||||||||||||||||||
| 14 | 0 | my $filehandle=shift; | |||||||||||||||||||||||||||
| 15 | 0 | my $substring=shift; | |||||||||||||||||||||||||||
| 16 | 0 | ▶ | my $start=shift||0; | ||||||||||||||||||||||||||
|
Condition0%0/2
shift() || 0
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 17 | 0 | ▶ | my $bufferSize=shift||131072; | ||||||||||||||||||||||||||
|
Condition0%0/2
shift() || 131072
MC/DC0%0/1
shift() || 131072
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 18 | 0 | my $k=length($substring); | |||||||||||||||||||||||||||
| 19 | 0 | my $offset=0; | |||||||||||||||||||||||||||
| 20 | 0 | my $s=""; | |||||||||||||||||||||||||||
| 21 | 0 | ▶ | croak "BufferSize must not be less than substring length" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 22 | if $bufferSize<$k; | ||||||||||||||||||||||||||||
| 23 | # Seek to start point; use successive reads if file isn't seekable | ||||||||||||||||||||||||||||
| 24 | 0 | ▶ | if ( ! seek($filehandle,$start,0) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 25 | 0 | ▶ | for (my $j=0;$j<int($start/$bufferSize);$j++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | 0 | ▶ | read($filehandle,$s,$bufferSize) or return(-1) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | 0 | ▶ | if ( $start%($bufferSize) > 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | 0 | ▶ | read($filehandle,$s,$start%($bufferSize)) or return(-1) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | 0 | $s="" | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | # Read and append to end of preserved string | ||||||||||||||||||||||||||||
| 34 | 0 | ▶ | while ( read($filehandle,substr($s,length($s)),$bufferSize) > 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | 0 | ▶ | if ( (my $n=index($s,$substring)) > -1 ) { return($n+$offset+$start) } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | 0 | $offset+=(length($s)-$k+1); | |||||||||||||||||||||||||||
| 37 | # Preserve last ($k-1) characters | ||||||||||||||||||||||||||||
| 38 | 0 | $s=substr($s,-$k+1) | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | 0 | return(-1) | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 0 | ▶ | sub rindexf { | ||||||||||||||||||||||||||
|
Subroutine
rindexf: not called CC=8 SCAR=42.8
Pod
documented
| |||||||||||||||||||||||||||||
| 44 | 0 | my $filehandle=shift; | |||||||||||||||||||||||||||
| 45 | 0 | my $substring=shift; | |||||||||||||||||||||||||||
| 46 | 0 | ▶ | my $beg=shift||-1; | ||||||||||||||||||||||||||
|
Condition0%0/2
shift() || -1
MC/DC0%0/1
shift() || -1
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 47 | 0 | ▶ | my $bufferSize=shift||131072; | ||||||||||||||||||||||||||
|
Condition0%0/2
shift() || 131072
MC/DC0%0/1
shift() || 131072
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 48 | 0 | my $k=length($substring); | |||||||||||||||||||||||||||
| 49 | 0 | my $offset=0; | |||||||||||||||||||||||||||
| 50 | 0 | my $s=""; | |||||||||||||||||||||||||||
| 51 | 0 | my $match=-1; | |||||||||||||||||||||||||||
| 52 | 0 | ▶ | croak "BufferSize must not be less than substring length" if $bufferSize<$k; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 0 | seek($filehandle,0,0); | |||||||||||||||||||||||||||
| 54 | 0 | ▶ | while (read($filehandle,substr($s,length($s)),$bufferSize)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | # Read and append to end of preserved string | ||||||||||||||||||||||||||||
| 56 | 0 | my $j=0; | |||||||||||||||||||||||||||
| 57 | 0 | ▶ | while ( (my $n=index($s,$substring,$j)) > -1 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | 0 | ▶ | if ( ($beg>=0) && (($n+$offset)>$beg) ) { return($match) } | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$beg >= 0 and $n + $offset > $beg
MC/DC0%0/2
$beg >= 0 and $n + $offset > $beg
$beg >= 0
$n + $offset > $beg
Truth table0%0/3
A and B
A= $beg >= 0B= $n + $offset > $beg
| |||||||||||||||||||||||||||||
| 59 | 0 | else { $match=$n+$offset; $j=$n+1 } | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | 0 | $offset+=(length($s)-$k+1); | |||||||||||||||||||||||||||
| 62 | # Preserve last ($k-1) characters | ||||||||||||||||||||||||||||
| 63 | 0 | $s=substr($s,-$k+1) | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | # Return last match-position | ||||||||||||||||||||||||||||
| 66 | 0 | return($match) | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | =head1 NAME | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | File::Index - an index function for files | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | use File::Index; | ||||||||||||||||||||||||||||
| 76 | open(FILE,$myfile); | ||||||||||||||||||||||||||||
| 77 | my $pos=indexf(*FILE,"Foo"); | ||||||||||||||||||||||||||||
| 78 | print "Foo found at position: $pos\n" if $pos > -1; | ||||||||||||||||||||||||||||
| 79 | open(FILE2,$myfile2); | ||||||||||||||||||||||||||||
| 80 | my $pos2=rindexf(*FILE2,"Bar"); | ||||||||||||||||||||||||||||
| 81 | print "Bar found at position: $pos2\n" if $pos2 > -1; | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | This module provides the indexf and rindexf functions which operate on files in the | ||||||||||||||||||||||||||||
| 86 | same way that the index and rindex functions operate on strings. It can be used where | ||||||||||||||||||||||||||||
| 87 | memory limitations prohibit the slurping of an entire file. | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | =over 4 | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | =item C<indexf( *FH, $string, [$start], [$buffersize] )> | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | Starts at the position specified by '$start' (or at the beginning) of | ||||||||||||||||||||||||||||
| 94 | the file associated with filehandle 'FH', and returns the absolute start | ||||||||||||||||||||||||||||
| 95 | position of the string '$string'. | ||||||||||||||||||||||||||||
| 96 | The buffer-size can be adjusted by specifying '$buffersize'. | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | =item C<rindexf( *FH, $string, [$position], [$buffersize] )> | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | Returns the position of the last occurrence of '$string' in the file | ||||||||||||||||||||||||||||
| 101 | associated with filehandle 'FH'. If '$position' is specified, returns | ||||||||||||||||||||||||||||
| 102 | the last occurrence beginning at or before '$position'. | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | The buffer-size can be adjusted by specifying '$buffersize'. If you | ||||||||||||||||||||||||||||
| 105 | wish to specify a '$buffersize' value without specifying a '$position' | ||||||||||||||||||||||||||||
| 106 | value, use a negative value (e.g. '-1') for the latter. | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | =back | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | Graham Jenkins, C<< <grahjenk at cpan.org> >> | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | =head1 BUGS | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | Please report any bugs or feature requests to C<bug-file-index at rt.cpan.org>, or through | ||||||||||||||||||||||||||||
| 118 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=File-Index>. I will be notified, and then you'll | ||||||||||||||||||||||||||||
| 119 | automatically be notified of progress on your bug as I make changes. | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | perldoc File::Index | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | You can also look for information at: | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | =over 4 | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | =item * RT: CPAN's request tracker (report bugs here) | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=File-Index> | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | L<http://annocpan.org/dist/File-Index> | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | =item * CPAN Ratings | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | L<http://cpanratings.perl.org/d/File-Index> | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | =item * Search CPAN | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | L<http://search.cpan.org/dist/File-Index/> | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | =back | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | =head1 ACKNOWLEDGEMENTS | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | Copyright 2012 Graham Jenkins. | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||
| 161 | under the terms of either: the GNU General Public License as published | ||||||||||||||||||||||||||||
| 162 | by the Free Software Foundation; or the Artistic License. | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | See http://dev.perl.org/licenses/ for more information. | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | =cut | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 2 | 1; # End of File::Index | |||||||||||||||||||||||||||