| 1 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # File: Import.pm | ||||||||||||||||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||||||||||||||||
| 4 | # Description: Import CSV and JSON database files | ||||||||||||||||||||||||||||||||||||||||||
| 5 | # | ||||||||||||||||||||||||||||||||||||||||||
| 6 | # Revisions: 2011-03-05 - P. Harvey Created | ||||||||||||||||||||||||||||||||||||||||||
| 7 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 8 | package Image::ExifTool::Import; | ||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | 17 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 17 | require Exporter; | |||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | 17 | ▶ | use vars qw($VERSION @ISA @EXPORT_OK); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | 17 | $VERSION = '1.14'; | |||||||||||||||||||||||||||||||||||||||||
| 16 | 17 | @ISA = qw(Exporter); | |||||||||||||||||||||||||||||||||||||||||
| 17 | 17 | @EXPORT_OK = qw(ReadCSV ReadJSON); | |||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | sub ReadJSONObject($;$); | ||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | 17 | my %unescapeJSON = ( 't'=>"\t", 'n'=>"\n", 'r'=>"\r", 'b' => "\b", 'f' => "\f" ); | |||||||||||||||||||||||||||||||||||||||||
| 22 | 17 | my $charset; | |||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 25 | # Read CSV file | ||||||||||||||||||||||||||||||||||||||||||
| 26 | # Inputs: 0) CSV file name, file ref or RAF ref, 1) database hash ref, | ||||||||||||||||||||||||||||||||||||||||||
| 27 | # 2) missing tag value, 3) delimiter if other than ',' | ||||||||||||||||||||||||||||||||||||||||||
| 28 | # Returns: undef on success, or error string | ||||||||||||||||||||||||||||||||||||||||||
| 29 | # Notes: There are various flavours of CSV, but here we assume that only | ||||||||||||||||||||||||||||||||||||||||||
| 30 | # double quotes are escaped, and they are escaped by doubling them | ||||||||||||||||||||||||||||||||||||||||||
| 31 | sub ReadCSV($$;$$) | ||||||||||||||||||||||||||||||||||||||||||
| 32 | 0 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
ReadCSV: not called CC=35 SCAR=71.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | 0 | local ($_, $/); | |||||||||||||||||||||||||||||||||||||||||
| 34 | 0 | my ($file, $database, $missingValue, $delim) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 35 | 0 | my ($buff, @tags, $found, $err, $raf, $openedFile); | |||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | 0 | ▶ | if (UNIVERSAL::isa($file, 'File::RandomAccess')) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 38 | 0 | $raf = $file; | |||||||||||||||||||||||||||||||||||||||||
| 39 | 0 | $file = 'CSV file'; | |||||||||||||||||||||||||||||||||||||||||
| 40 | } elsif (ref $file eq 'GLOB') { | ||||||||||||||||||||||||||||||||||||||||||
| 41 | 0 | $raf = File::RandomAccess->new($file); | |||||||||||||||||||||||||||||||||||||||||
| 42 | 0 | $file = 'CSV file'; | |||||||||||||||||||||||||||||||||||||||||
| 43 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 44 | 0 | ▶ | open CSVFILE, $file or return "Error opening CSV file '${file}'"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | 0 | binmode CSVFILE; | |||||||||||||||||||||||||||||||||||||||||
| 46 | 0 | $openedFile = 1; | |||||||||||||||||||||||||||||||||||||||||
| 47 | 0 | $raf = File::RandomAccess->new(\*CSVFILE); | |||||||||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||||||||
| 49 | 0 | ▶ | $delim = ',' unless defined $delim; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | # set input record separator by first newline found in the file | ||||||||||||||||||||||||||||||||||||||||||
| 51 | # (safe because first line should contain only tag names) | ||||||||||||||||||||||||||||||||||||||||||
| 52 | 0 | ▶ | while ($raf->Read($buff, 65536)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | ▶ | $buff =~ /(\x0d\x0a|\x0d|\x0a)/ and $/ = $1, last; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||||||||||
| 55 | 0 | $raf->Seek(0,0); | |||||||||||||||||||||||||||||||||||||||||
| 56 | 0 | ▶ | while ($raf->ReadLine($buff)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | 0 | my (@vals, $v, $i, %fileInfo); | |||||||||||||||||||||||||||||||||||||||||
| 58 | 0 | my @toks = split /\Q$delim/, $buff; | |||||||||||||||||||||||||||||||||||||||||
| 59 | 0 | ▶ | while (@toks) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | 0 | ($v = shift @toks) =~ s/^ +//; # remove leading spaces | |||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | ▶ | if ($v =~ s/^"//) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | # quoted value must end in an odd number of quotes | ||||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | ▶ | while ($v !~ /("+)\s*$/ or not length($1) & 1) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$v =~ /("+)\s*$/ and length $1 & 1
MC/DC0%0/2
$v =~ /("+)\s*$/ and length $1 & 1
$v =~ /("+)\s*$/
length $1 & 1
Truth table0%0/3
A and B
A= $v =~ /("+)\s*$/B= length $1 & 1
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | 0 | ▶ | if (@toks) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | $v .= $delim . shift @toks; | |||||||||||||||||||||||||||||||||||||||||
| 66 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 67 | # read another line from the file | ||||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | ▶ | $raf->ReadLine($buff) or last; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | @toks = split /\Q$delim/, $buff; | |||||||||||||||||||||||||||||||||||||||||
| 70 | 0 | ▶ | last unless @toks; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | $v .= shift @toks; | |||||||||||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | $v =~ s/"\s*$//; # remove trailing quote and whitespace | |||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | $v =~ s/""/"/g; # un-escape quotes | |||||||||||||||||||||||||||||||||||||||||
| 76 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 77 | 0 | $v =~ s/[ \n\r]+$//;# remove trailing spaces/newlines | |||||||||||||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | push @vals, $v; | |||||||||||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | ▶ | if (@tags) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 82 | # save values for each tag | ||||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | $fileInfo{_ordered_keys_} = [ ]; | |||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | ▶ | for ($i=0; $i<@vals and $i<@tags; ++$i) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$i < @vals and $i < @tags
MC/DC0%0/2
$i < @vals and $i < @tags
$i < @vals
$i < @tags
Truth table0%0/3
A and B
A= $i < @valsB= $i < @tags
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | # ignore empty entries unless missingValue is empty too | ||||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | ▶ | next unless length $vals[$i] or defined $missingValue and $missingValue eq ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
length $vals[$i] or defined $missingValue and $missingValue eq ""
defined $missingValue and $missingValue eq ""
MC/DC0%0/3
length $vals[$i] or defined $missingValue and $missingValue eq ""
length $vals[$i]
defined $missingValue
$missingValue eq ""
Truth table0%0/4
A or B and C
A= length $vals[$i]B= defined $missingValueC= $missingValue eq ""
| |||||||||||||||||||||||||||||||||||||||||||
| 87 | # delete tag (set value to undef) if value is same as missing tag | ||||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | ▶ | $fileInfo{$tags[$i]} = | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $missingValue and $vals[$i] eq $missingValue
MC/DC0%0/2
defined $missingValue and $vals[$i] eq $missingValue
defined $missingValue
$vals[$i] eq $missingValue
Truth table0%0/3
A and B
A= defined $missingValueB= $vals[$i] eq $missingValue
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | (defined $missingValue and $vals[$i] eq $missingValue) ? undef : $vals[$i]; | ||||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | push @{$fileInfo{_ordered_keys_}}, $tags[$i]; | |||||||||||||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||||||||||||
| 92 | # figure out the file name to use | ||||||||||||||||||||||||||||||||||||||||||
| 93 | 0 | ▶ | if ($fileInfo{SourceFile}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | $$database{$fileInfo{SourceFile}} = \%fileInfo; | |||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | $found = 1; | |||||||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||||||||
| 97 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 98 | # the first row should be the tag names | ||||||||||||||||||||||||||||||||||||||||||
| 99 | 0 | foreach (@vals) { | |||||||||||||||||||||||||||||||||||||||||
| 100 | # terminate at first blank tag name (eg. extra comma at end of line) | ||||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | ▶ | last unless length $_; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | ▶ | @tags or s/^\xef\xbb\xbf//; # remove UTF-8 BOM if it exists | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | ▶ | /^([-_0-9A-Z]+:)*[-_0-9A-Z]+#?$/i or $err = "Invalid tag name '${_}'", last; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 104 | 0 | push(@tags, $_); | |||||||||||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||||||||||
| 106 | 0 | ▶ | last if $err; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 107 | 0 | ▶ | @tags or $err = 'No tags found', last; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 108 | # fix "SourceFile" case if necessary | ||||||||||||||||||||||||||||||||||||||||||
| 109 | 0 | ▶ | $tags[0] = 'SourceFile' if lc $tags[0] eq 'sourcefile'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | ▶ | close CSVFILE if $openedFile; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | undef $raf; | |||||||||||||||||||||||||||||||||||||||||
| 114 | 0 | ▶ | $err = 'No SourceFile column' unless $found or $err; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$found or $err
MC/DC0%0/2
$found or $err
$found
$err
Truth table0%0/3
A or B
A= $foundB= $err
| |||||||||||||||||||||||||||||||||||||||||||
| 115 | 0 | ▶ | return $err ? "$err in $file" : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||
| 118 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 119 | # Convert unicode code point to UTF-8 | ||||||||||||||||||||||||||||||||||||||||||
| 120 | # Inputs: 0) integer Unicode character | ||||||||||||||||||||||||||||||||||||||||||
| 121 | # Returns: UTF-8 bytes | ||||||||||||||||||||||||||||||||||||||||||
| 122 | sub ToUTF8($) | ||||||||||||||||||||||||||||||||||||||||||
| 123 | 0 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
ToUTF8: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 124 | 0 | require Image::ExifTool::Charset; | |||||||||||||||||||||||||||||||||||||||||
| 125 | 0 | return Image::ExifTool::Charset::Recompose(undef, [$_[0]], $charset); | |||||||||||||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||
| 128 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 129 | # Read JSON object from file | ||||||||||||||||||||||||||||||||||||||||||
| 130 | # Inputs: 0) RAF reference or undef, 1) optional scalar reference for data | ||||||||||||||||||||||||||||||||||||||||||
| 131 | # to read before reading from file (ie. the file read buffer) | ||||||||||||||||||||||||||||||||||||||||||
| 132 | # Returns: JSON object (scalar, hash ref, or array ref), or undef on EOF or | ||||||||||||||||||||||||||||||||||||||||||
| 133 | # empty object or array (and sets $$buffPt to empty string on EOF) | ||||||||||||||||||||||||||||||||||||||||||
| 134 | # Notes: position in buffer is significant | ||||||||||||||||||||||||||||||||||||||||||
| 135 | sub ReadJSONObject($;$) | ||||||||||||||||||||||||||||||||||||||||||
| 136 | 510 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
ReadJSONObject: called CC=39 SCAR=41.6
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 137 | 510 | my ($raf, $buffPt) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 138 | # initialize buffer if necessary | ||||||||||||||||||||||||||||||||||||||||||
| 139 | 510 | my ($pos, $readMore, $rtnVal, $tok, $key, $didBOM); | |||||||||||||||||||||||||||||||||||||||||
| 140 | 510 | ▶ | if ($buffPt) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 141 | 449 | $pos = pos $$buffPt; | |||||||||||||||||||||||||||||||||||||||||
| 142 | 449 | ▶ | $pos = pos($$buffPt) = 0 unless defined $pos; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 143 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 144 | 61 | my $buff = ''; | |||||||||||||||||||||||||||||||||||||||||
| 145 | 61 | $buffPt = \$buff; | |||||||||||||||||||||||||||||||||||||||||
| 146 | 61 | $pos = 0; | |||||||||||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||||||||||
| 148 | 510 | Tok: for (;;) { | |||||||||||||||||||||||||||||||||||||||||
| 149 | # (didn't spend the time to understand how $pos could be undef, but | ||||||||||||||||||||||||||||||||||||||||||
| 150 | # put a test here to be safe because one user reported this problem) | ||||||||||||||||||||||||||||||||||||||||||
| 151 | 510 | ▶ | last unless defined $pos; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 152 | 510 | ▶ | if ($pos >= length $$buffPt or $readMore) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$pos >= length $$buffPt or $readMore
MC/DC50%1/2
$pos >= length $$buffPt or $readMore
$pos >= length $$buffPt
$readMore
Truth table66%2/3
A or B
A= $pos >= length $$buffPtB= $readMore
| |||||||||||||||||||||||||||||||||||||||||||
| 153 | 61 | ▶ | last unless defined $raf; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 154 | # read another 64kB and add to unparsed data | ||||||||||||||||||||||||||||||||||||||||||
| 155 | 61 | my $offset = length($$buffPt) - $pos; | |||||||||||||||||||||||||||||||||||||||||
| 156 | 61 | ▶ | if ($offset) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 157 | 0 | my $buff; | |||||||||||||||||||||||||||||||||||||||||
| 158 | 0 | ▶ | $raf->Read($buff, 65536) or $$buffPt = '', last; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 159 | 0 | $$buffPt = substr($$buffPt, $pos) . $buff; | |||||||||||||||||||||||||||||||||||||||||
| 160 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 161 | 61 | ▶ | $raf->Read($$buffPt, 65536) or $$buffPt = '', last; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||||||||||||||||
| 163 | 61 | ▶ | unless ($didBOM) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 164 | 61 | $$buffPt =~ s/^\xef\xbb\xbf//; # remove UTF-8 BOM if it exists | |||||||||||||||||||||||||||||||||||||||||
| 165 | 61 | $didBOM = 1; | |||||||||||||||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||||||||||||||||
| 167 | 61 | $pos = pos($$buffPt) = 0; | |||||||||||||||||||||||||||||||||||||||||
| 168 | 61 | $readMore = 0; | |||||||||||||||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||||||||||||||||
| 170 | 510 | ▶ | unless ($tok) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 171 | # skip white space and find next character | ||||||||||||||||||||||||||||||||||||||||||
| 172 | 510 | ▶ | $$buffPt =~ /(\S)/g or $pos = length($$buffPt), next; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 173 | 510 | $tok = $1; | |||||||||||||||||||||||||||||||||||||||||
| 174 | 510 | $pos = pos $$buffPt; | |||||||||||||||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||||||||||||||||
| 176 | # see what type of object this is | ||||||||||||||||||||||||||||||||||||||||||
| 177 | 510 | ▶ | if ($tok eq '{') { # object (hash) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 178 | 107 | ▶ | $rtnVal = { _ordered_keys_ => [ ] } unless defined $rtnVal; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 179 | 107 | for (;;) { | |||||||||||||||||||||||||||||||||||||||||
| 180 | # read "KEY":"VALUE" pairs | ||||||||||||||||||||||||||||||||||||||||||
| 181 | 208 | ▶ | unless (defined $key) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 182 | 208 | $key = ReadJSONObject($raf, $buffPt); | |||||||||||||||||||||||||||||||||||||||||
| 183 | 208 | $pos = pos $$buffPt; | |||||||||||||||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||||||||||||||||
| 185 | # ($key may be undef for empty JSON object) | ||||||||||||||||||||||||||||||||||||||||||
| 186 | 208 | ▶ | if (defined $key) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 187 | # scan to delimiting ':' | ||||||||||||||||||||||||||||||||||||||||||
| 188 | 205 | ▶ | $$buffPt =~ /(\S)/g or $readMore = 1, next Tok; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 189 | 205 | ▶ | $1 eq ':' or return undef; # error if not a colon | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 190 | 205 | my $val = ReadJSONObject($raf, $buffPt); | |||||||||||||||||||||||||||||||||||||||||
| 191 | 205 | $pos = pos $$buffPt; | |||||||||||||||||||||||||||||||||||||||||
| 192 | 205 | ▶ | return undef unless defined $val; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 193 | 205 | $$rtnVal{$key} = $val; | |||||||||||||||||||||||||||||||||||||||||
| 194 | 205 | push @{$$rtnVal{_ordered_keys_}}, $key; | |||||||||||||||||||||||||||||||||||||||||
| 195 | 205 | undef $key; | |||||||||||||||||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||||||||||||||||
| 197 | # scan to delimiting ',' or bounding '}' | ||||||||||||||||||||||||||||||||||||||||||
| 198 | 208 | ▶ | $$buffPt =~ /(\S)/g or $readMore = 1, next Tok; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 199 | 208 | ▶ | last if $1 eq '}'; # check for end of object | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 200 | 101 | ▶ | $1 eq ',' or return undef; # error if not a comma | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||||||||||||||||
| 202 | } elsif ($tok eq '[') { # array | ||||||||||||||||||||||||||||||||||||||||||
| 203 | 17 | ▶ | $rtnVal = [ ] unless defined $rtnVal; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 204 | 17 | for (;;) { | |||||||||||||||||||||||||||||||||||||||||
| 205 | 33 | my $item = ReadJSONObject($raf, $buffPt); | |||||||||||||||||||||||||||||||||||||||||
| 206 | 33 | $pos = pos $$buffPt; | |||||||||||||||||||||||||||||||||||||||||
| 207 | # ($item may be undef for empty array) | ||||||||||||||||||||||||||||||||||||||||||
| 208 | 33 | ▶ | push @$rtnVal, $item if defined $item; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 209 | # scan to delimiting ',' or bounding ']' | ||||||||||||||||||||||||||||||||||||||||||
| 210 | 33 | ▶ | $$buffPt =~ /(\S)/g or $readMore = 1, next Tok; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 211 | 33 | ▶ | last if $1 eq ']'; # check for end of array | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 212 | 16 | ▶ | $1 eq ',' or return undef; # error if not a comma | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||||||||||||||||
| 214 | } elsif ($tok eq '"') { # quoted string | ||||||||||||||||||||||||||||||||||||||||||
| 215 | 295 | for (;;) { | |||||||||||||||||||||||||||||||||||||||||
| 216 | 295 | ▶ | $$buffPt =~ /(\\*)"/g or $readMore = 1, next Tok; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 217 | 295 | ▶ | last unless length($1) & 1; # check for escaped quote | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||||||||||||||||
| 219 | 295 | $rtnVal = substr($$buffPt, $pos, pos($$buffPt)-$pos-1); | |||||||||||||||||||||||||||||||||||||||||
| 220 | # unescape characters | ||||||||||||||||||||||||||||||||||||||||||
| 221 | 0 | $rtnVal =~ s/\\u([0-9a-f]{4})/ToUTF8(hex $1)/ige; | |||||||||||||||||||||||||||||||||||||||||
| 222 | 0 | ▶ | $rtnVal =~ s/\\(.)/$unescapeJSON{$1}||$1/sge; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$unescapeJSON{$1} || $1
MC/DC0%0/2
$unescapeJSON{$1} || $1
$unescapeJSON{$1}
$1
Truth table0%0/3
A || B
A= $unescapeJSON{$1}B= $1
| |||||||||||||||||||||||||||||||||||||||||||
| 223 | # decode base64 (binary data) values | ||||||||||||||||||||||||||||||||||||||||||
| 224 | 295 | ▶ | if ($rtnVal =~ /^base64:[A-Za-z0-9+\/]*={0,2}$/ and length($rtnVal) % 4 == 3) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$rtnVal =~ m[^base64:[A-Za-z0-9+/]*={0,2}$] and length($rtnVal) % 4 == 3
MC/DC0%0/2
$rtnVal =~ m[^base64:[A-Za-z0-9+/]*={0,2}$] and length($rtnVal) % 4 == 3
$rtnVal =~ m[^base64:[A-Za-z0-9+/]*={0,2}$]
length($rtnVal) % 4 == 3
Truth table33%1/3
A and B
A= $rtnVal =~ m[^base64:[A-Za-z0-9+/]*={0,2}$]B= length($rtnVal) % 4 == 3
| |||||||||||||||||||||||||||||||||||||||||||
| 225 | 0 | require Image::ExifTool::XMP; | |||||||||||||||||||||||||||||||||||||||||
| 226 | 0 | $rtnVal = ${Image::ExifTool::XMP::DecodeBase64(substr($rtnVal,7))}; | |||||||||||||||||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||||||||||||||||
| 228 | ▶ | } elsif ($tok eq ']' or $tok eq '}' or $tok eq ',') { | |||||||||||||||||||||||||||||||||||||||||
|
Condition83%5/6
$tok eq "]" or $tok eq "}" or $tok eq ","
$tok eq "]" or $tok eq "}"
MC/DC66%2/3
$tok eq "]" or $tok eq "}" or $tok eq ","
$tok eq "]"
$tok eq "}"
$tok eq ","
Truth table75%3/4
A or B or C
A= $tok eq "]"B= $tok eq "}"C= $tok eq ","
| |||||||||||||||||||||||||||||||||||||||||||
| 229 | # return undef for empty object, array, or list item | ||||||||||||||||||||||||||||||||||||||||||
| 230 | # (empty list item actually not valid JSON) | ||||||||||||||||||||||||||||||||||||||||||
| 231 | 8 | pos($$buffPt) = pos($$buffPt) - 1; | |||||||||||||||||||||||||||||||||||||||||
| 232 | } else { # number, 'true', 'false', 'null' | ||||||||||||||||||||||||||||||||||||||||||
| 233 | 83 | ▶ | $$buffPt =~ /([\s:,\}\]])/g or $readMore = 1, next; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 234 | 83 | pos($$buffPt) = pos($$buffPt) - 1; | |||||||||||||||||||||||||||||||||||||||||
| 235 | 83 | $rtnVal = $tok . substr($$buffPt, $pos, pos($$buffPt)-$pos); | |||||||||||||||||||||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||||||||||||||||||||||
| 237 | 510 | last; | |||||||||||||||||||||||||||||||||||||||||
| 238 | } | ||||||||||||||||||||||||||||||||||||||||||
| 239 | 510 | return $rtnVal; | |||||||||||||||||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||||||||||||
| 242 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 243 | # Read JSON file | ||||||||||||||||||||||||||||||||||||||||||
| 244 | # Inputs: 0) JSON file name, file ref, RAF ref or SCALAR ref, 1) database hash ref, | ||||||||||||||||||||||||||||||||||||||||||
| 245 | # 2) flag to delete "-" tags, 3) character set | ||||||||||||||||||||||||||||||||||||||||||
| 246 | # Returns: undef on success, or error string | ||||||||||||||||||||||||||||||||||||||||||
| 247 | sub ReadJSON($$;$$) | ||||||||||||||||||||||||||||||||||||||||||
| 248 | 61 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
ReadJSON: called CC=17 SCAR=35.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 249 | 61 | local $_; | |||||||||||||||||||||||||||||||||||||||||
| 250 | 61 | my ($file, $database, $missingValue, $chset) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 251 | 61 | my ($raf, $openedFile); | |||||||||||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||||||||||||
| 253 | # initialize character set for converting "\uHHHH" chars | ||||||||||||||||||||||||||||||||||||||||||
| 254 | 61 | ▶ | $charset = $chset || 'UTF8'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$chset || 'UTF8'
MC/DC0%0/1
$chset || 'UTF8'
$chset
Truth table50%1/2
A
A= $chset
| |||||||||||||||||||||||||||||||||||||||||||
| 255 | 61 | ▶ | if (UNIVERSAL::isa($file, 'File::RandomAccess')) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 256 | 61 | $raf = $file; | |||||||||||||||||||||||||||||||||||||||||
| 257 | 61 | $file = 'JSON file'; | |||||||||||||||||||||||||||||||||||||||||
| 258 | } elsif (ref $file eq 'GLOB') { | ||||||||||||||||||||||||||||||||||||||||||
| 259 | 0 | $raf = File::RandomAccess->new($file); | |||||||||||||||||||||||||||||||||||||||||
| 260 | 0 | $file = 'JSON file'; | |||||||||||||||||||||||||||||||||||||||||
| 261 | } elsif (ref $file eq 'SCALAR') { | ||||||||||||||||||||||||||||||||||||||||||
| 262 | 0 | $raf = File::RandomAccess->new($file); | |||||||||||||||||||||||||||||||||||||||||
| 263 | 0 | $file = 'in memory'; | |||||||||||||||||||||||||||||||||||||||||
| 264 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 265 | 0 | ▶ | open JSONFILE, $file or return "Error opening JSON file '${file}'"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 266 | 0 | binmode JSONFILE; | |||||||||||||||||||||||||||||||||||||||||
| 267 | 0 | $openedFile = 1; | |||||||||||||||||||||||||||||||||||||||||
| 268 | 0 | $raf = File::RandomAccess->new(\*JSONFILE); | |||||||||||||||||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||||||||||||||||
| 270 | 61 | my $obj = ReadJSONObject($raf); | |||||||||||||||||||||||||||||||||||||||||
| 271 | 61 | ▶ | close JSONFILE if $openedFile; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 272 | 61 | ▶ | unless (ref $obj eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 273 | 61 | ▶ | ref $obj eq 'HASH' or return "Format error in JSON file '${file}'"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 274 | 61 | $obj = [ $obj ]; | |||||||||||||||||||||||||||||||||||||||||
| 275 | } | ||||||||||||||||||||||||||||||||||||||||||
| 276 | 61 | my ($info, $found); | |||||||||||||||||||||||||||||||||||||||||
| 277 | 61 | foreach $info (@$obj) { | |||||||||||||||||||||||||||||||||||||||||
| 278 | 61 | ▶ | next unless ref $info eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 279 | # fix "SourceFile" case, or assume '*' if SourceFile not specified | ||||||||||||||||||||||||||||||||||||||||||
| 280 | 61 | ▶ | unless (defined $$info{SourceFile}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 281 | 61 | my ($key) = grep /^SourceFile$/i, keys %$info; | |||||||||||||||||||||||||||||||||||||||||
| 282 | 61 | ▶ | if ($key) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 283 | 0 | $$info{SourceFile} = $$info{$key}; | |||||||||||||||||||||||||||||||||||||||||
| 284 | 0 | delete $$info{$key}; | |||||||||||||||||||||||||||||||||||||||||
| 285 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 286 | 61 | $$info{SourceFile} = '*'; | |||||||||||||||||||||||||||||||||||||||||
| 287 | } | ||||||||||||||||||||||||||||||||||||||||||
| 288 | } | ||||||||||||||||||||||||||||||||||||||||||
| 289 | 61 | ▶ | if (defined $missingValue) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 290 | 1 | ▶ | $$info{$_} eq $missingValue and $$info{$_} = undef foreach keys %$info; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 291 | } | ||||||||||||||||||||||||||||||||||||||||||
| 292 | 61 | $$database{$$info{SourceFile}} = $info; | |||||||||||||||||||||||||||||||||||||||||
| 293 | 61 | $found = 1; | |||||||||||||||||||||||||||||||||||||||||
| 294 | } | ||||||||||||||||||||||||||||||||||||||||||
| 295 | 61 | ▶ | return $found ? undef : "No valid JSON objects in '${file}'"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 296 | } | ||||||||||||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||||||||||||||||
| 299 | 17 | 1; # end | |||||||||||||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||||||||||||
| 301 | __END__ | ||||||||||||||||||||||||||||||||||||||||||