| 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
MC/DC0%0/1
$et->Options('FastScan') || 0
$et->Options('FastScan')
Truth table50%1/2
A
A= $et->Options('FastScan')
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | # read more from the file if necessary | ||||||||||||||||||||||||||||||||||||||||||
| 67 | 14 | ▶ | if ($fast < 3 and length($$dataPt) == $Image::ExifTool::testLen) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$fast < 3 and length $$dataPt == $Image::ExifTool::testLen
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
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | ▶ | $raf->Read($buff, 65536) or return 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | 0 | ▶ | if ($1 eq "\xff\xfe\0\0") { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | $enc = 'utf-32le'; | |||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | ▶ | $nl = $1 if $$dataPt =~ /(\r\0\0\0\n|\r|\n)\0\0\0/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||||||||||||
| 84 | } elsif ($$dataPt =~ /^(\xff\xfe|\xfe\xff)/) { | ||||||||||||||||||||||||||||||||||||||||||
| 85 | 1 | ▶ | if ($1 eq "\xff\xfe") { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | $enc = 'utf-16le'; | |||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | ▶ | $nl = $1 if $$dataPt =~ /(\r\0\n|\r|\n)\0/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 89 | 1 | $enc = 'utf-16be'; | |||||||||||||||||||||||||||||||||||||||||
| 90 | 1 | ▶ | $nl = $1 if $$dataPt =~ /\0(\r\0\n|\r|\n)/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
$fast == 3 or not $raf->Seek(0, 0)
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)
| |||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | 14 | ▶ | $et->HandleTag($tagTablePtr, ByteOrderMark => $isBOM) if defined $isBOM; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 120 | 14 | $et->HandleTag($tagTablePtr, Newlines => $nl); | |||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||
| 122 | 14 | ▶ | return 1 if $fast or not defined $isUTF8; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$fast or not defined $isUTF8
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
| |||||||||||||||||||||||||||||||||||||||||||
| 123 | # | ||||||||||||||||||||||||||||||||||||||||||
| 124 | # generate stats for CSV files | ||||||||||||||||||||||||||||||||||||||||||
| 125 | # | ||||||||||||||||||||||||||||||||||||||||||
| 126 | 13 | ▶ | if ($$et{FileType} eq 'CSV') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 127 | 1 | my ($delim, $quot, $ncols); | |||||||||||||||||||||||||||||||||||||||||
| 128 | 1 | my $nrows = 0; | |||||||||||||||||||||||||||||||||||||||||
| 129 | 1 | ▶ | while ($raf->ReadLine($buff)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 130 | 3 | ▶ | if (not defined $delim) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
$count{","} > $count{";"} and $count{","} > $count{"\t"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 144 | # account for delimiters in quotes (simplistically) | ||||||||||||||||||||||||||||||||||||||||||
| 145 | 1 | ▶ | while ($buff =~ /(^|$delim)(["'])(.*?)\2(?=$delim|$)/sg) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||||||||||||||||
| 155 | 3 | ▶ | if (++$nrows == 1000 and $et->Warn('Not counting rows past 1000', 2)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
++$nrows == 1000 and $et->Warn("Not counting rows past 1000", 2)
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)
| |||||||||||||||||||||||||||||||||||||||||||
| 156 | 0 | undef $nrows; | |||||||||||||||||||||||||||||||||||||||||
| 157 | 0 | last; | |||||||||||||||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||||||||||||||
| 160 | 1 | ▶ | $et->HandleTag($tagTablePtr, Delimiter => ($delim || '')); | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$delim || ''
MC/DC0%0/1
$delim || ''
$delim
Truth table50%1/2
A
A= $delim
| |||||||||||||||||||||||||||||||||||||||||||
| 161 | 1 | ▶ | $et->HandleTag($tagTablePtr, Quoting => ($quot || '')); | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$quot || ''
MC/DC0%0/1
$quot || ''
$quot
Truth table50%1/2
A
A= $quot
| |||||||||||||||||||||||||||||||||||||||||||
| 162 | 1 | $et->HandleTag($tagTablePtr, ColumnCount => $ncols); | |||||||||||||||||||||||||||||||||||||||||
| 163 | 1 | ▶ | $et->HandleTag($tagTablePtr, RowCount => $nrows) if $nrows; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 164 | 1 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||||||||||||||||
| 166 | 12 | ▶ | return 1 if $$et{VALUE}{FileSize} and $$et{VALUE}{FileSize} > 20000000 and | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
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)
$et->{"VALUE"}{"FileSize"} and $et->{"VALUE"}{"FileSize"} > 20000000
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)
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 174 | 12 | ▶ | while ($raf->ReadLine($buff)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 175 | 12 | ++$lines; | |||||||||||||||||||||||||||||||||||||||||
| 176 | 12 | ▶ | ++$words while $buff =~ /\S+/g; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 177 | 12 | ▶ | if (not $nl and $buff =~ /(\r\n|\r|\n)$/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $nl and $buff =~ /(\r\n|\r|\n)$/
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)$/
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 182 | # continue to check encoding after the first 64 kB | ||||||||||||||||||||||||||||||||||||||||||
| 183 | 0 | ▶ | if ($isUTF8 >= 0) { # (if ascii or utf8) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 184 | 0 | $isUTF8 = Image::ExifTool::IsUTF8(\$buff); | |||||||||||||||||||||||||||||||||||||||||
| 185 | 0 | ▶ | if ($isUTF8 > 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 186 | 0 | $enc = 'utf-8'; | |||||||||||||||||||||||||||||||||||||||||
| 187 | } elsif ($isUTF8 < 0) { | ||||||||||||||||||||||||||||||||||||||||||
| 188 | 0 | ▶ | $enc = $buff =~ /[\x80-\x9f]/ ? 'unknown-8bit' : 'iso-8859-1'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||||||||||||||||
| 190 | ▶ | } elsif ($enc eq 'iso-8859-1' and $buff =~ /[\x80-\x9f]/) { | |||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$enc eq "iso-8859-1" and $buff =~ /[\x80-\x9f]/
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]/
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
defined $et->{"VALUE"}{"MIMEEncoding"} and $et->{"VALUE"}{"MIMEEncoding"} ne $enc
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||