Image/ExifTool/Text.pm

filter: Statementstmt 72.8% 75 / 103 Branchbran 52.2% 46 / 88 Conditioncond 38.8% 14 / 36 MC/DCmcdc 4.5% 1 / 22 Subroutinesub 100% 4 / 4 Podpod 0.0% 0 / 1 totaltotal 55.1% 140 / 254 CC 59 SCAR 57.5
CC
59
Cov
59%
ProcessTXT
331.7
BEGIN
1.0
BEGIN
1.0
CRAP 315.7
« Image/ExifTool/Geolocation.pm ↑ Summary Image/ExifTool/Charset.pm »
1 #------------------------------------------------------------------------------
2 # File: Text.pm
3 #
4 # Description: Deduce characteristics of TXT and CSV files
5 #
6 # Revisions: 2019-11-01 - P. Harvey Created
7 # 2020-02-13 - PH Added CSV file support
8 #
9 # References: 1) https://github.com/file/file
10 #------------------------------------------------------------------------------
11  
12 package Image::ExifTool::Text;
13  
14 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 5 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 5 ▶use Image::ExifTool qw(:DataAccess :Utils);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 5 $VERSION = '1.05';
19  
20 # Text tags
21 5 %Image::ExifTool::Text::Main = (
22 VARS => { ID_FMT => 'none' },
23 GROUPS => { 0 => 'File', 1 => 'File', 2 => 'Document' },
24 NOTES => q{
25 Although basic text files contain no metadata, the following tags are
26 determined from a simple analysis of the data in TXT and CSV files.
27 Statistics are generated only for 8-bit encodings, but the L<FastScan|../ExifTool.html#FastScan> (-fast)
28 option may be used to limit processing to the first 64 KiB in which case
29 some tags are not produced. To avoid long processing delays, ExifTool will
30 issue a minor warning and process only the first 64 KiB of any file larger
31 than 20 MiB unless the L<IgnoreMinorErrors|../ExifTool.html#IgnoreMinorErrors> (-m) option is used.
32 },
33 MIMEEncoding => { Groups => { 2 => 'Other' } },
34 Newlines => {
35 PrintConv => {
36 "\r\n" => 'Windows CRLF',
37 "\r" => 'Macintosh CR',
38 "\n" => 'Unix LF',
39 '' => '(none)',
40 },
41 },
42 ByteOrderMark => { PrintConv => { 0 => 'No', 1 => 'Yes' } },
43 LineCount => { },
44 WordCount => { },
45 Delimiter => { PrintConv => { '' => '(none)', ',' => 'Comma', ';' => 'Semicolon', "\t" => 'Tab' }},
46 Quoting => { PrintConv => { '' => '(none)', '"' => 'Double quotes', "'" => 'Single quotes' }},
47 RowCount => { },
48 ColumnCount => { },
49 );
50  
51 #------------------------------------------------------------------------------
52 # Extract some stats from a text file
53 # Inputs: 0) ExifTool ref, 1) dirInfo ref
54 # Returns: 1 on success, 0 if this wasn't a text file
55 sub ProcessTXT($$)
56 17 ▶{
Subroutine ProcessTXT: called CC=59 SCAR=58.0
Pod undocumented
57 17 my ($et, $dirInfo) = @_;
58 17 my $dataPt = $$dirInfo{TestBuff};
59 17 my $raf = $$dirInfo{RAF};
60 17 ▶ my $fast = $et->Options('FastScan') || 0;
Condition50%1/2
$et->Options('FastScan') || 0
l!l
017
MC/DC0%0/1
$et->Options('FastScan') || 0
$et->Options('FastScan')
Truth table50%1/2
A
A= $et->Options('FastScan')
Aresult
11
00
61 17 my ($buff, $enc, $isBOM, $isUTF8);
62 17 my $nl = '';
63  
64 17 ▶ return 0 unless length $$dataPt; # can't call it a text file if it has no text
Branch: 1 branch
BranchTrueFalseTotal
unless length $$dataPt 3 14 17
65  
66 # read more from the file if necessary
67 14 ▶ if ($fast < 3 and length($$dataPt) == $Image::ExifTool::testLen) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fast < 3 and length $$dataPt == $Image::ExifTool::testLen) 0 14 14
Condition33%1/3
$fast < 3 and length $$dataPt == $Image::ExifTool::testLen
!ll&&!rl&&r
0140
MC/DC0%0/2
$fast < 3 and length $$dataPt == $Image::ExifTool::testLen
$fast < 3 length $$dataPt == $Image::ExifTool::testLen
Truth table33%1/3
A and B
A= $fast < 3B= length $$dataPt == $Image::ExifTool::testLen
ABresult
0X0
100
111
68 0 ▶ $raf->Read($buff, 65536) or return 0;
Branch: 1 branch
BranchTrueFalseTotal
$raf->Read($buff, 65536) or return 0 0 0 0
69 0 $dataPt = \$buff;
70 }
71 #
72 # make our best guess at the character encoding (EBCDIC is not supported)
73 #
74 14 ▶ if ($$dataPt =~ /([\0-\x06\x0e-\x1a\x1c-\x1f\x7f])/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$dataPt =~ /([\0-\x06\x0e-\x1a\x1c-\x1f\x7f])/) { } 1 13 14
75 # file contains weird control characters, could be multi-byte Unicode
76 1 ▶ if ($$dataPt =~ /^(\xff\xfe\0\0|\0\0\xfe\xff)/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($$dataPt =~ /^(\xff\xfe\0\0|\0\0\xfe\xff)/) { } 0 1 1
elsif ($$dataPt =~ /^(\xff\xfe|\xfe\xff)/) { } 1 0 1
77 0 ▶ if ($1 eq "\xff\xfe\0\0") {
Branch: 1 branch
BranchTrueFalseTotal
if ($1 eq "\377\376\000\000") { } 0 0 0
78 0 $enc = 'utf-32le';
79 0 ▶ $nl = $1 if $$dataPt =~ /(\r\0\0\0\n|\r|\n)\0\0\0/;
Branch: 1 branch
BranchTrueFalseTotal
if $$dataPt =~ /(\r\0\0\0\n|\r|\n)\0\0\0/ 0 0 0
80 } else {
81 0 $enc = 'utf-32be';
82 0 ▶ $nl = $1 if $$dataPt =~ /\0\0\0(\r\0\0\0\n|\r|\n)/;
Branch: 1 branch
BranchTrueFalseTotal
if $$dataPt =~ /\0\0\0(\r\0\0\0\n|\r|\n)/ 0 0 0
83 }
84 } elsif ($$dataPt =~ /^(\xff\xfe|\xfe\xff)/) {
85 1 ▶ if ($1 eq "\xff\xfe") {
Branch: 1 branch
BranchTrueFalseTotal
if ($1 eq "\377\376") { } 0 1 1
86 0 $enc = 'utf-16le';
87 0 ▶ $nl = $1 if $$dataPt =~ /(\r\0\n|\r|\n)\0/;
Branch: 1 branch
BranchTrueFalseTotal
if $$dataPt =~ /(\r\0\n|\r|\n)\0/ 0 0 0
88 } else {
89 1 $enc = 'utf-16be';
90 1 ▶ $nl = $1 if $$dataPt =~ /\0(\r\0\n|\r|\n)/;
Branch: 1 branch
BranchTrueFalseTotal
if $$dataPt =~ /\0(\r\0\n|\r|\n)/ 1 0 1
91 }
92 } else {
93 0 return 0; # probably not a text file
94 }
95 1 $nl =~ tr/\0//d; # remove nulls from newline sequence
96 1 $isBOM = 1; # (we don't recognize UTF-16/UTF-32 without one)
97 } else {
98 13 $isUTF8 = Image::ExifTool::IsUTF8($dataPt, 1);
99 13 ▶ if ($isUTF8 == 0) {
Branch: 3 branches
BranchTrueFalseTotal
if ($isUTF8 == 0) { } 10 3 13
elsif ($isUTF8 > 0) { } 1 2 3
elsif (not $$dataPt =~ /[\x80-\x9f]/) { } 1 1 2
100 10 $enc = 'us-ascii';
101 } elsif ($isUTF8 > 0) {
102 1 $enc = 'utf-8';
103 1 ▶ $isBOM = ($$dataPt =~ /^\xef\xbb\xbf/ ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$$dataPt =~ /^\xef\xbb\xbf/ ? : 1 0 1
104 } elsif ($$dataPt !~ /[\x80-\x9f]/) {
105 1 $enc = 'iso-8859-1';
106 } else {
107 1 $enc = 'unknown-8bit';
108 }
109 13 ▶ $nl = $1 if $$dataPt =~ /(\r\n|\r|\n)/;
Branch: 1 branch
BranchTrueFalseTotal
if $$dataPt =~ /(\r\n|\r|\n)/ 5 8 13
110 }
111  
112 14 my $tagTablePtr = GetTagTable('Image::ExifTool::Text::Main');
113  
114 14 $et->SetFileType();
115 14 $et->HandleTag($tagTablePtr, MIMEEncoding => $enc);
116  
117 14 ▶ return 1 if $fast == 3 or not $raf->Seek(0,0);
Branch: 1 branch
BranchTrueFalseTotal
if $fast == 3 or not $raf->Seek(0, 0) 0 14 14
Condition33%1/3
$fast == 3 or not $raf->Seek(0, 0)
l!l&&r!l&&!r
0014
MC/DC0%0/2
$fast == 3 or not $raf->Seek(0, 0)
$fast == 3 not $raf->Seek(0, 0)
Truth table33%1/3
A or not(B)
A= $fast == 3B= not $raf->Seek(0, 0)
ABresult
1X1
011
000
118  
119 14 ▶ $et->HandleTag($tagTablePtr, ByteOrderMark => $isBOM) if defined $isBOM;
Branch: 1 branch
BranchTrueFalseTotal
if defined $isBOM 2 12 14
120 14 $et->HandleTag($tagTablePtr, Newlines => $nl);
121  
122 14 ▶ return 1 if $fast or not defined $isUTF8;
Branch: 1 branch
BranchTrueFalseTotal
if $fast or not defined $isUTF8 1 13 14
Condition66%2/3
$fast or not defined $isUTF8
l!l&&r!l&&!r
0113
MC/DC50%1/2
$fast or not defined $isUTF8
$fast not defined $isUTF8
Truth table66%2/3
A or not(B)
A= $fastB= not defined $isUTF8
ABresult
1X1
011
000
123 #
124 # generate stats for CSV files
125 #
126 13 ▶ if ($$et{FileType} eq 'CSV') {
Branch: 1 branch
BranchTrueFalseTotal
if ($et->{"FileType"} eq "CSV") 1 12 13
127 1 my ($delim, $quot, $ncols);
128 1 my $nrows = 0;
129 1 ▶ while ($raf->ReadLine($buff)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($raf->ReadLine($buff)) 3 1 4
130 3 ▶ if (not defined $delim) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $delim) { } 1 2 3
elsif (not $quot) { } 2 0 2
131 1 my %count = ( ',' => 0, ';' => 0, "\t" => 0 );
132 1 ++$count{$_} foreach $buff =~ /[,;\t]/g;
133 1 ▶ if ($count{','} > $count{';'} and $count{','} > $count{"\t"}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($count{','} > $count{';'} and $count{','} > $count{"\t"}) { } 1 0 1
elsif ($count{';'} > $count{"\t"}) { } 0 0 0
elsif ($count{"\t"}) { } 0 0 0
Condition33%1/3
$count{","} > $count{";"} and $count{","} > $count{"\t"}
!ll&&!rl&&r
001
MC/DC0%0/2
$count{","} > $count{";"} and $count{","} > $count{"\t"}
$count{","} > $count{";"} $count{","} > $count{"\t"}
Truth table33%1/3
A and B
A= $count{","} > $count{";"}B= $count{","} > $count{"\t"}
ABresult
0X0
100
111
134 1 $delim = ',';
135 } elsif ($count{';'} > $count{"\t"}) {
136 0 $delim = ';';
137 } elsif ($count{"\t"}) {
138 0 $delim = "\t";
139 } else {
140 0 $delim = '';
141 0 $ncols = 1;
142 }
143 1 ▶ unless ($ncols) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ncols) 1 0 1
144 # account for delimiters in quotes (simplistically)
145 1 ▶ while ($buff =~ /(^|$delim)(["'])(.*?)\2(?=$delim|$)/sg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($buff =~ /(^|$delim)(["'])(.*?)\2(?=$delim|$)/gs) 0 1 1
146 0 $quot = $2;
147 0 my $field = $3;
148 0 $count{$delim} -= () = $field =~ /$delim/g;
149 }
150 1 $ncols = $count{$delim} + 1;
151 }
152 } elsif (not $quot) {
153 2 ▶ $quot = $2 if $buff =~ /(^|$delim)(["'])(.*?)\2(?=$delim|$)/sg;
Branch: 1 branch
BranchTrueFalseTotal
if $buff =~ /(^|$delim)(["'])(.*?)\2(?=$delim|$)/gs 0 2 2
154 }
155 3 ▶ if (++$nrows == 1000 and $et->Warn('Not counting rows past 1000', 2)) {
Branch: 1 branch
BranchTrueFalseTotal
if (++$nrows == 1000 and $et->Warn("Not counting rows past 1000", 2)) 0 3 3
Condition33%1/3
++$nrows == 1000 and $et->Warn("Not counting rows past 1000", 2)
!ll&&!rl&&r
300
MC/DC0%0/2
++$nrows == 1000 and $et->Warn("Not counting rows past 1000", 2)
++$nrows == 1000 $et->Warn("Not counting rows past 1000", 2)
Truth table33%1/3
A and B
A= ++$nrows == 1000B= $et->Warn("Not counting rows past 1000", 2)
ABresult
0X0
100
111
156 0 undef $nrows;
157 0 last;
158 }
159 }
160 1 ▶ $et->HandleTag($tagTablePtr, Delimiter => ($delim || ''));
Condition50%1/2
$delim || ''
l!l
10
MC/DC0%0/1
$delim || ''
$delim
Truth table50%1/2
A
A= $delim
Aresult
11
00
161 1 ▶ $et->HandleTag($tagTablePtr, Quoting => ($quot || ''));
Condition50%1/2
$quot || ''
l!l
01
MC/DC0%0/1
$quot || ''
$quot
Truth table50%1/2
A
A= $quot
Aresult
11
00
162 1 $et->HandleTag($tagTablePtr, ColumnCount => $ncols);
163 1 ▶ $et->HandleTag($tagTablePtr, RowCount => $nrows) if $nrows;
Branch: 1 branch
BranchTrueFalseTotal
if $nrows 1 0 1
164 1 return 1;
165 }
166 12 ▶ return 1 if $$et{VALUE}{FileSize} and $$et{VALUE}{FileSize} > 20000000 and
Branch: 1 branch
BranchTrueFalseTotal
if $et->{"VALUE"}{"FileSize"} and $et->{"VALUE"}{"FileSize"} > 20000000 and $et->Warn("Not counting lines/words in text file larger than 20 MB", 2) 0 12 12
Condition33%2/6
$et->{"VALUE"}{"FileSize"} and $et->{"VALUE"}{"FileSize"} > 20000000 and $et->Warn("Not counting lines/words in text file larger than 20 MB", 2)
!ll&&!rl&&r
1200
$et->{"VALUE"}{"FileSize"} and $et->{"VALUE"}{"FileSize"} > 20000000
!ll&&!rl&&r
0120
MC/DC0%0/3
$et->{"VALUE"}{"FileSize"} and $et->{"VALUE"}{"FileSize"} > 20000000 and $et->Warn("Not counting lines/words in text file larger than 20 MB", 2)
$et->{"VALUE"}{"FileSize"} $et->{"VALUE"}{"FileSize"} > 20000000 $et->Warn("Not counting lines/words in text file larger than 20 MB", 2)
Truth table25%1/4
A and B and C
A= $et->{"VALUE"}{"FileSize"}B= $et->{"VALUE"}{"FileSize"} > 20000000C= $et->Warn("Not counting lines/words in text file larger than 20 MB", 2)
ABCresult
0XX0
10X0
1100
1111
167 $et->Warn('Not counting lines/words in text file larger than 20 MB', 2);
168 #
169 # count lines/words and check encoding of the rest of the file
170 #
171 12 my ($lines, $words) = (0, 0);
172 12 my $oldNL = $/;
173 12 ▶ $/ = $nl if $nl;
Branch: 1 branch
BranchTrueFalseTotal
if $nl 4 8 12
174 12 ▶ while ($raf->ReadLine($buff)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($raf->ReadLine($buff)) 12 12 24
175 12 ++$lines;
176 12 ▶ ++$words while $buff =~ /\S+/g;
Branch: 1 branch
BranchTrueFalseTotal
$buff =~ /\S+/g and do { ++$words } 33 12 45
177 12 ▶ if (not $nl and $buff =~ /(\r\n|\r|\n)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $nl and $buff =~ /(\r\n|\r|\n)$/) 0 12 12
Condition66%2/3
not $nl and $buff =~ /(\r\n|\r|\n)$/
!ll&&!rl&&r
480
MC/DC0%0/2
not $nl and $buff =~ /(\r\n|\r|\n)$/
not $nl $buff =~ /(\r\n|\r|\n)$/
Truth table66%2/3
not(A) and B
A= not $nlB= $buff =~ /(\r\n|\r|\n)$/
ABresult
0X0
100
111
178 # (the first line must have been longer than 64 kB)
179 0 $$et{VALUE}{Newlines} = $nl = $1;
180 }
181 12 ▶ next if $raf->Tell() < 65536;
Branch: 1 branch
BranchTrueFalseTotal
if $raf->Tell < 65536 12 0 12
182 # continue to check encoding after the first 64 kB
183 0 ▶ if ($isUTF8 >= 0) { # (if ascii or utf8)
Branch: 2 branches
BranchTrueFalseTotal
if ($isUTF8 >= 0) { } 0 0 0
elsif ($enc eq 'iso-8859-1' and $buff =~ /[\x80-\x9f]/) { } 0 0 0
184 0 $isUTF8 = Image::ExifTool::IsUTF8(\$buff);
185 0 ▶ if ($isUTF8 > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($isUTF8 > 0) { } 0 0 0
elsif ($isUTF8 < 0) { } 0 0 0
186 0 $enc = 'utf-8';
187 } elsif ($isUTF8 < 0) {
188 0 ▶ $enc = $buff =~ /[\x80-\x9f]/ ? 'unknown-8bit' : 'iso-8859-1';
Branch: 1 branch
BranchTrueFalseTotal
$buff =~ /[\x80-\x9f]/ ? : 0 0 0
189 }
190 ▶ } elsif ($enc eq 'iso-8859-1' and $buff =~ /[\x80-\x9f]/) {
Condition0%0/3
$enc eq "iso-8859-1" and $buff =~ /[\x80-\x9f]/
!ll&&!rl&&r
000
MC/DC0%0/2
$enc eq "iso-8859-1" and $buff =~ /[\x80-\x9f]/
$enc eq "iso-8859-1" $buff =~ /[\x80-\x9f]/
Truth table0%0/3
A and B
A= $enc eq "iso-8859-1"B= $buff =~ /[\x80-\x9f]/
ABresult
0X0
100
111
191 0 $enc = 'unknown-8bit';
192 }
193 }
194 # ($$et{VALUE}{MIMEEncoding} may be undef if it was ignored)
195 12 ▶ if (defined $$et{VALUE}{MIMEEncoding} and $$et{VALUE}{MIMEEncoding} ne $enc) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $et->{"VALUE"}{"MIMEEncoding"} and $et->{"VALUE"}{"MIMEEncoding"} ne $enc) 0 12 12
Condition33%1/3
defined $et->{"VALUE"}{"MIMEEncoding"} and $et->{"VALUE"}{"MIMEEncoding"} ne $enc
!ll&&!rl&&r
0120
MC/DC0%0/2
defined $et->{"VALUE"}{"MIMEEncoding"} and $et->{"VALUE"}{"MIMEEncoding"} ne $enc
defined $et->{"VALUE"}{"MIMEEncoding"} $et->{"VALUE"}{"MIMEEncoding"} ne $enc
Truth table33%1/3
A and B
A= defined $et->{"VALUE"}{"MIMEEncoding"}B= $et->{"VALUE"}{"MIMEEncoding"} ne $enc
ABresult
0X0
100
111
196 0 $$et{VALUE}{MIMEEncoding} = $enc;
197 0 $et->VPrint(0," MIMEEncoding [override] = $enc\n");
198 }
199 12 $/ = $oldNL;
200 12 $et->HandleTag($tagTablePtr, LineCount => $lines);
201 12 $et->HandleTag($tagTablePtr, WordCount => $words);
202 12 return 1;
203 }
204  
205  
206 5 1; # end
207  
208 __END__
« Image/ExifTool/Geolocation.pm ↑ Summary Image/ExifTool/Charset.pm »