Edit/IO.pm

filter: Statementstmt 55.5% 448 / 806 Branchbran 33.3% 176 / 528 Conditioncond 29.9% 70 / 234 MC/DCmcdc 7.8% 10 / 127 Subroutinesub 67.3% 68 / 101 Podpod 91.6% 11 / 12 totaltotal 43.3% 783 / 1808 CC 366 SCAR 61.5
CC
366
Cov
44%
_convert_using_openlibre
3422.0
_get_exclusive_lock
156.0
_extract_one_csv
156.0
CRAP 469.9
↑ Summary Edit/Log.pm »
1 # License: http://creativecommons.org/publicdomain/zero/1.0/
2 # (CC0 or Public Domain). To the extent possible under law, the author,
3 # Jim Avera (email jim.avera at gmail dot com) has waived all copyright and
4 # related or neighboring rights to this document. Attribution is requested
5 # but not required.
6 11 ▶use strict; use warnings FATAL => 'all'; use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
7 11 ▶use feature qw(say state lexical_subs current_sub);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 11 ▶no warnings qw(experimental::lexical_subs);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 package Spreadsheet::Edit::IO;
11  
12 # Allow "use <thismodule. VERSION ..." in development sandbox to not bomb
13 11 ▶{ no strict 'refs'; ${__PACKAGE__."::VER"."SION"} = 1999.999; }
Subroutine BEGIN: called CC=1 SCAR=0.0
14 11 our $VERSION = '1001.005'; # VERSION from Dist::Zilla::Plugin::OurPkgVersion
15 11 our $DATE = '2026-11-11'; # DATE from Dist::Zilla::Plugin::OurDate
16  
17 # This module is derived from the old never-released Text:CSV::Spreadsheet
18  
19 11 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 11 our @EXPORT = qw/convert_spreadsheet OpenAsCsv cx2let let2cx cxrx2sheetaddr
22 sheetname_from_spec filepath_from_spec
23 form_spec_with_sheetname/;
24  
25 11 our @EXPORT_OK = qw/can_cvt_spreadsheets can_extract_allsheets can_extract_named_sheet
26 openlibreoffice_path
27 @sane_CSV_read_options @sane_CSV_write_options/;
28  
29 # TODO: Provide "known_attributes" function ala Text::CSV::known_attributes()
30  
31 11 ▶use version ();
Subroutine BEGIN: called CC=1 SCAR=0.0
32 11 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 11 ▶use File::Copy ();
Subroutine BEGIN: called CC=1 SCAR=0.0
35 11 ▶use File::Copy::Recursive ();
Subroutine BEGIN: called CC=1 SCAR=0.0
36 11 ▶use File::Glob qw/bsd_glob/;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 11 ▶use Path::Tiny 0.146 qw/path/;
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 # Path::Tiny OBVIATES NEED for many but we still need this
41 11 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
42 11 ▶use File::Spec::Functions qw/devnull tmpdir rootdir catdir catfile/;
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 # Still sometimes convenient...
45 11 ▶use File::Basename qw(basename);
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47 11 ▶use File::BOM ();
Subroutine BEGIN: called CC=1 SCAR=0.0
48 11 ▶use File::Which qw/which/;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 11 ▶use URI::file ();
Subroutine BEGIN: called CC=1 SCAR=0.0
50 11 ▶use Guard qw(guard scope_guard);
Subroutine BEGIN: called CC=1 SCAR=0.0
51 11 ▶use Fcntl qw(:flock :seek);
Subroutine BEGIN: called CC=1 SCAR=0.0
52 11 ▶use Symbol qw/qualify_to_ref/;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 11 ▶use Scalar::Util qw/blessed openhandle/;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 11 ▶use List::Util qw/none all notall first min max/;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 11 ▶use Encode qw(encode decode);
Subroutine BEGIN: called CC=1 SCAR=0.0
56 11 ▶use File::Glob qw/bsd_glob GLOB_NOCASE/;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 11 ▶use Digest::MD5 qw/md5_base64/;
Subroutine BEGIN: called CC=1 SCAR=0.0
58 11 ▶use Data::Hexify qw/Hexify/;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 11 ▶use Text::CSV ();
Subroutine BEGIN: called CC=1 SCAR=0.0
60 # DDI 5.025 is needed for Windows-aware qsh()
61 11 ▶use Data::Dumper::Interp 5.025 qw/vis visq visO dvis dvisq dvisO ivis ivisq avis hvis qsh qshlist u visnew/;
Subroutine BEGIN: called CC=1 SCAR=0.0
62  
63 11 ▶use Spreadsheet::Edit::Log qw/log_call fmt_call log_methcall fmt_methcall oops :btw/;
Subroutine BEGIN: called CC=1 SCAR=0.0
64 our %SpreadsheetEdit_Log_Options = (
65 0 ▶ is_public_api => sub{
Subroutine __ANON__: not called CC=1 SCAR=6.9
66 0 $_[1][3] =~ /(?: ::|^ )(?: [a-z][^:]* | OpenAsCsv | ConvertSpreadsheet )$/x
67 },
68 11 subst_pkg => "IO",
69 );
70  
71 11 my $progname = path($0)->basename;
72  
73 18 ▶sub _get_username(;$) {
Subroutine _get_username: called CC=11 SCAR=36.5
74 18 my ($uid) = @_;
75 18 ▶ $uid //= eval{ $> } // -1; # default to EUID
Condition50%2/4
$uid //= eval { do { $> } } // -1
l!l
018
eval { do { $> } } // -1
l!l
180
MC/DC0%0/1
$uid //= eval { do { $> } } // -1
$uid
Truth table50%1/2
A
A= $uid
Aresult
11
00
76 18 state $answer = {};
77 18 ▶ return $answer->{$uid} //= do {
Condition66%2/3
$$answer{$uid} //= do { eval { do { getpwuid $uid // $uid } } or $^O =~ /MSWin/ and $uid == (eval { do { $> } } // -1) and eval { do { require Win32API::Net; Win32API::Net::UserGetInfo($ENV{"LOGONSERVER"} || "", Win32::LoginName(), 10, scalar(my $info = {})); $$info{"fullName"} } } or "UID$uid" }
l!l&&r!l&&!r
7110
MC/DC0%0/2
$$answer{$uid} //= do { eval { do { getpwuid $uid // $uid } } or $^O =~ /MSWin/ and $uid == (eval { do { $> } } // -1) and eval { do { require Win32API::Net; Win32API::Net::UserGetInfo($ENV{"LOGONSERVER"} || "", Win32::LoginName(), 10, scalar(my $info = {})); $$info{"fullName"} } } or "UID$uid" }
$$answer{$uid} do { eval { do { getpwuid $uid // $uid } } or $^O =~ /MSWin/ and $uid == (eval { do { $> } } // -1) and eval { do { require Win32API::Net; Win32API::Net::UserGetInfo($ENV{"LOGONSERVER"} || "", Win32::LoginName(), 10, scalar(my $info = {})); $$info{"fullName"} } } or "UID$uid" }
Truth table66%2/3
A //= B
A= $$answer{$uid}B= do { eval { do { getpwuid $uid // $uid } } or $^O =~ /MSWin/ and $uid == (eval { do { $> } } // -1) and eval { do { require Win32API::Net; Win32API::Net::UserGetInfo($ENV{"LOGONSERVER"} || "", Win32::LoginName(), 10, scalar(my $info = {})); $$info{"fullName"} } } or "UID$uid" }
ABresult
1X1
011
000
78 # https://stackoverflow.com/questions/12081246/how-to-get-system-user-full-name-on-windows-in-perl
79 11 ▶ eval { getpwuid($uid) // $uid }
Condition9%1/11
getpwuid $uid // $uid
l!l&&r!l&&!r
1100
$^O =~ /MSWin/ and $uid == (eval { do { $> } } // -1) and eval { do { require Win32API::Net; Win32API::Net::UserGetInfo($ENV{"LOGONSERVER"} || "", Win32::LoginName(), 10, scalar(my $info = {})); $$info{"fullName"} } }
!ll&&!rl&&r
000
$^O =~ /MSWin/ and $uid == (eval { do { $> } } // -1)
!ll&&!rl&&r
000
eval { do { $> } } // -1
l!l
00
MC/DC0%0/2
getpwuid $uid // $uid
getpwuid $uid $uid
MC/DC0%0/3
$^O =~ /MSWin/ and $uid == (eval { do { $> } } // -1) and eval { do { require Win32API::Net; Win32API::Net::UserGetInfo($ENV{"LOGONSERVER"} || "", Win32::LoginName(), 10, scalar(my $info = {})); $$info{"fullName"} } }
$^O =~ /MSWin/ $uid == (eval { do { $> } } // -1) eval { do { require Win32API::Net; Win32API::Net::UserGetInfo($ENV{"LOGONSERVER"} || "", Win32::LoginName(), 10, scalar(my $info = {})); $$info{"fullName"} } }
MC/DC0%0/1
eval { do { $> } } // -1
eval { do { $> } }
Truth table33%1/3
A // B
A= getpwuid $uidB= $uid
ABresult
1X1
011
000
Truth table0%0/4
A and B and C
A= $^O =~ /MSWin/B= $uid == (eval { do { $> } } // -1)C= eval { do { require Win32API::Net; Win32API::Net::UserGetInfo($ENV{"LOGONSERVER"} || "", Win32::LoginName(), 10, scalar(my $info = {})); $$info{"fullName"} } }
ABCresult
0XX0
10X0
1100
1111
Truth table0%0/2
A
A= eval { do { $> } }
Aresult
11
00
80 ||
81 0 ▶ ($^O =~ /MSWin/ && $uid == (eval{$>}//-1) && eval{ # untested...
Condition40%2/5
eval { do { getpwuid $uid // $uid } } || $^O =~ /MSWin/ && $uid == (eval { do { $> } } // -1) && eval { do { require Win32API::Net; Win32API::Net::UserGetInfo($ENV{'LOGONSERVER'} || '', Win32::LoginName(), 10, scalar(my $info = {})); $$info{'fullName'} } } || "UID$uid"
l!l
110
eval { do { getpwuid $uid // $uid } } or $^O =~ /MSWin/ and $uid == (eval { do { $> } } // -1) and eval { do { require Win32API::Net; Win32API::Net::UserGetInfo($ENV{"LOGONSERVER"} || "", Win32::LoginName(), 10, scalar(my $info = {})); $$info{"fullName"} } }
l!l&&r!l&&!r
1100
MC/DC0%0/2
eval { do { getpwuid $uid // $uid } } || $^O =~ /MSWin/ && $uid == (eval { do { $> } } // -1) && eval { do { require Win32API::Net; Win32API::Net::UserGetInfo($ENV{'LOGONSERVER'} || '', Win32::LoginName(), 10, scalar(my $info = {})); $$info{'fullName'} } } || "UID$uid"
eval { do { getpwuid $uid // $uid } } $^O =~ /MSWin/ and $uid == (eval { do { $> } } // -1) and eval { do { require Win32API::Net; Win32API::Net::UserGetInfo($ENV{"LOGONSERVER"} || "", Win32::LoginName(), 10, scalar(my $info = {})); $$info{"fullName"} } }
Truth table33%1/3
A or B
A= eval { do { getpwuid $uid // $uid } }B= $^O =~ /MSWin/ and $uid == (eval { do { $> } } // -1) and eval { do { require Win32API::Net; Win32API::Net::UserGetInfo($ENV{"LOGONSERVER"} || "", Win32::LoginName(), 10, scalar(my $info = {})); $$info{"fullName"} } }
ABresult
1X1
011
000
82 0 require Win32API::Net;
83 0 ▶ Win32API::Net::UserGetInfo($ENV{LOGONSERVER}||'',Win32::LoginName(),10,my $info={});
Condition0%0/2
$ENV{'LOGONSERVER'} || ''
l!l
00
MC/DC0%0/1
$ENV{'LOGONSERVER'} || ''
$ENV{'LOGONSERVER'}
Truth table0%0/2
A
A= $ENV{'LOGONSERVER'}
Aresult
11
00
84 $info->{fullName}
85 0 })
86 ||
87 "UID$uid"
88 };
89 }
90  
91  
92 # A private Libre/Open Office profile dir is needed to avoid conflicts
93 # with interactive sessions, see
94 # https://ask.libreoffice.org/en/question/290306/how-to-start-independent-lo-instance-process
95 #
96 # We use a persistent profile dir shared among processes for a given user
97 # (actually one for each unique external tool which needs one).
98 # Sharing is okay because we get an exclusive lock before actually using it.
99 11 state $profile_parent_dir = do{ # also used for lockfile
100 11 my $user = _get_username();
101 11 my $dname = __PACKAGE__."_${user}_profileparent";
102 11 $dname =~ s/::/-/g;
103 11 $dname =~ s/[^-\w.:]/_/g;
104 11 (my $path = path(File::Spec->tmpdir)->child($dname))->mkpath;
105 11 $path # Path::Tiny
106 };
107 0 ▶sub _get_tool_profdir($) {
Subroutine _get_tool_profdir: not called CC=1 SCAR=6.9
108 0 my ($tool_path) = @_;
109 0 my $fingerprint = _file_fingerprint($tool_path);
110 0 (my $toolname = path($tool_path)->basename(qw/\.\w+$/)) =~ s/[^-\w.:]/_/g;
111 0 my $path = $profile_parent_dir->child("${toolname}_$fingerprint");
112 0 $path->mkpath;
113 0 $path
114 }
115  
116 # Prevent concurrent document conversions.
117 # LO & OO can't handle concurrent access to the same profile.
118 11 my $locked_fh;
119 0 ▶sub _get_exclusive_lock($) { # returns lock object
Subroutine _get_exclusive_lock: not called CC=12 SCAR=50.5
120 0 my $opts = shift;
121 0 my $lockfile_path = $profile_parent_dir->child("LOCKFILE")->canonpath;
122 0 my $sleeptime = 1;
123 0 my $lock_fh;
124 0 ▶ while (! defined $lock_fh) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $lock_fh) 0 0 0
125 #warn "$$ : ### AA1 open $lockfile_path ...\n";
126 0 ▶ open $lock_fh, "+>>", $lockfile_path or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open $lock_fh, "+>>", $lockfile_path or die $! 0 0 0
127 #warn "$$ : ### AA2 open succeeded.\n";
128 0 eval { chmod 0666, $lock_fh; }; # sometimes not implemented
129 #warn "$$ : ### AA3 flock ...\n";
130 0 ▶ if (! flock($lock_fh, LOCK_EX|LOCK_NB)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not flock($lock_fh, 6)) { } 0 0 0
131 #warn "$$ : ### AA4 flock FAILED\n";
132 0 ▶ seek($lock_fh, 0, SEEK_SET) or die;
Branch: 1 branch
BranchTrueFalseTotal
seek $lock_fh, 0, 0 or die 0 0 0
133 0 my @lines = <$lock_fh>;
134 0 ▶ close($lock_fh) or die "close:$!"; $lock_fh = undef;
Branch: 1 branch
BranchTrueFalseTotal
close $lock_fh or die "close:$!" 0 0 0
135 #warn "$$ : ### AA6 fh closed...\n";
136 0 ▶ my $owner = $lines[-1] // ""; # pid NNN (progname)
Condition0%0/2
$lines[-1] // ""
l!l
00
MC/DC0%0/1
$lines[-1] // ""
$lines[-1]
Truth table0%0/2
A
A= $lines[-1]
Aresult
11
00
137 0 ▶ { my ($pid) = ($owner =~ /pid (\d+)/) or last;
Branch: 1 branch
BranchTrueFalseTotal
my($pid) = $owner =~ /pid (\d+)/ or last 0 0 0
138 0 ▶ my @s = stat("/proc/$pid") or last;
Branch: 1 branch
BranchTrueFalseTotal
my(@s) = stat "/proc/$pid" or last 0 0 0
139 0 $owner = _get_username($s[4])." ".$owner;
140 }
141 0 ▶ my $ownermsg = $owner ? " held by $owner" : "";
Branch: 1 branch
BranchTrueFalseTotal
$owner ? : 0 0 0
142 # Carp::longmess ...
143 warn ">> ($$) Waiting for exclusive lock${ownermsg}...\n",
144 " $lockfile_path\n"
145 0 ▶ unless $opts->{silent};
Branch: 1 branch
BranchTrueFalseTotal
unless $$opts{"silent"} 0 0 0
146 0 sleep $sleeptime;
147 } else {
148 0 $locked_fh = $lock_fh;
149 0 ▶ seek($lock_fh, 0, SEEK_END) or die;
Branch: 1 branch
BranchTrueFalseTotal
seek $lock_fh, 0, 2 or die 0 0 0
150 0 print $lock_fh "pid $$ ($progname)\n"; # always appends anyway on *nix
151 }
152 }
153 0 $opts->{lockfile_fh} = $lock_fh;
154 #warn "$$ : ### GOT LOCK\n";
155 }
156 11 ▶END{
Subroutine END: called CC=2 SCAR=12.4
157 11 ▶ if (defined $locked_fh) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $locked_fh) 0 11 11
158 0 flock($locked_fh, LOCK_UN);
159 0 close($locked_fh);
160 0 $locked_fh = undef;
161 0 warn "Did emergency unlock!\n";
162 }
163 #else { warn "(emergency unlock not needed)\n"; }
164 }
165 0 ▶sub _release_lock($) {
Subroutine _release_lock: not called CC=4 SCAR=30.0
166 0 my $opts = shift;
167 0 ▶ my $fh = delete($opts->{lockfile_fh}) // oops;
Condition0%0/3
delete $$opts{"lockfile_fh"} // &oops()
l!l&&r!l&&!r
000
MC/DC0%0/2
delete $$opts{"lockfile_fh"} // &oops()
delete $$opts{"lockfile_fh"} &oops()
Truth table0%0/3
A // B
A= delete $$opts{"lockfile_fh"}B= &oops()
ABresult
1X1
011
000
168 0 ▶oops unless $fh == $locked_fh;
Branch: 1 branch
BranchTrueFalseTotal
unless $fh == $locked_fh 0 0 0
169 #seek($fh, 0, SEEK_SET) or die;
170 #my @x = (<$fh>);
171 #seek($fh, 0, SEEK_SET) or die;
172 #warn dvis "$$ : Lockfile contains: @x\n";
173 ##warn "$$ : ###BBB stalling before unlock...\n"; sleep 3;
174 0 truncate($fh,0);
175 0 ▶ flock($fh, LOCK_UN) or die "flock UN: $!";
Branch: 1 branch
BranchTrueFalseTotal
flock $fh, 8 or die "flock UN: $!" 0 0 0
176 0 close $fh;
177 0 $locked_fh = undef;
178 #warn "$$ : ###BB0 unlocked and closed.\n";
179 }
180  
181 # Libre Office text converter "charset" numbers
182 11 my %LO_charsets = (
183 'WINDOWS1252' => 1, 'WINLATIN1' => 1,
184 'APPLEWESTERN' => 2,
185 'DOS/OS2437' => 3,
186 'DOS/OS2850' => 4,
187 'DOS/OS2860' => 5,
188 'DOS/OS2861' => 6,
189 'DOS/OS2863' => 7,
190 'DOS/OS2865' => 8,
191 'SYSTEM' => 9, 'SYSTEMDDEFAULT' => 9,
192 'SYMBOL' => 10,
193 'ASCII' => 11,
194 'ISO88591' => 12,
195 'ISO88592' => 13,
196 'ISO88593' => 14,
197 'ISO88594' => 15,
198 'ISO88595' => 16,
199 'ISO88596' => 17,
200 'ISO88597' => 18,
201 'ISO88598' => 19,
202 'ISO88599' => 20,
203 'ISO885914' => 21,
204 'ISO885915' => 22,
205 'OS2737' => 23,
206 'OS2775' => 24,
207 'OS2852' => 25,
208 'OS2855' => 26,
209 'OS2857' => 27,
210 'OS2862' => 28,
211 'OS2864' => 29,
212 'OS2866' => 30,
213 'OS2869' => 31,
214 'WINDOWS874' => 32,
215 'WINDOWS1250' => 33, 'WINLATIN2' => 33,
216 'WINDOWS1251' => 34,
217 'WINDOWS1253' => 35,
218 'WINDOWS1254' => 36,
219 'WINDOWS1255' => 37,
220 'WINDOWS1256' => 38,
221 'WINDOWS1257' => 39,
222 'WINDOWS1258' => 40,
223 'APPLEARABIC' => 41,
224 'APPLECENTRALEUROPEAN' => 42,
225 'APPLECROATIAN' => 43,
226 'APPLECYRILLIC' => 44,
227 'APPLEDEVANAGARI' => 45,
228 'APPLEFARSI' => 46,
229 'APPLEGREEK' => 47,
230 'APPLEGUJARATI' => 48,
231 'APPLEGURMUKHI' => 49,
232 'APPLEHEBREW' => 50,
233 'APPLEICELANDIC' => 51,
234 'APPLEROMANIAN' => 52,
235 'APPLETHAI' => 53,
236 'APPLETURKISH' => 54,
237 'APPLEUKRAINIAN' => 55,
238 'APPLECHINESESIMPLIFIED' => 56,
239 'APPLECHINESETRADITIONAL' => 57,
240 'APPLEJAPANESE' => 58,
241 'APPLEKOREAN' => 59,
242 'WINDOWS932' => 60,
243 'WINDOWS936' => 61,
244 'WINDOWSWANSUNG949' => 62,
245 'WINDOWS950' => 63,
246 'SHIFTJIS' => 64,
247 'GB2312' => 65,
248 'GBT12345' => 66,
249 'GBK' => 67, 'GB231280' => 67,
250 'BIG5' => 68,
251 'EUCJP' => 69,
252 'EUCCN' => 70,
253 'EUCTW' => 71,
254 'ISO2022JP' => 72,
255 'ISO2022CN' => 73,
256 'KOI8R' => 74,
257 'UTF7' => 75,
258 'UTF8' => 76,
259 'ISO885910' => 77,
260 'ISO885913' => 78,
261 'EUCKR' => 79,
262 'ISO2022KR' => 80,
263 'JIS0201' => 81,
264 'JIS0208' => 82,
265 'JIS0212' => 83,
266 'WINDOWSJOHAB1361' => 84,
267 'GB18030' => 85,
268 'BIG5HKSCS' => 86,
269 'TIS620' => 87,
270 'KOI8U' => 88,
271 'ISCIIDEVANAGARI' => 89,
272 'JAVAUTF8' => 90,
273 'ADOBESTANDARD' => 91,
274 'ADOBESYMBOL' => 92,
275 'PT154' => 93,
276 'UCS4' => 65534,
277 'UCS2' => 65535,
278 );
279  
280 =for Pod::Coverage _name2LOcharsetnum
281 =cut
282  
283 0 ▶sub _name2LOcharsetnum($) {
Subroutine _name2LOcharsetnum: not called CC=3 SCAR=24.8
284 0 my ($enc) = @_;
285 0 local $_ = uc $enc;
286 0 ▶ while (! $LO_charsets{$_}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($LO_charsets{$_}) 0 0 0
287 # successively remove - and other special characters
288 0 ▶ s/\W//a or confess "LO charset: Unknown encoding name '$enc'";
Branch: 1 branch
BranchTrueFalseTotal
s/\W//a or confess("LO charset: Unknown encoding name '${enc}'") 0 0 0
289 }
290 0 $LO_charsets{$_}
291 }
292  
293 # convert between 0-based index and spreadsheet column letter code.
294 # Default argument is $_
295 0 ▶sub cx2let(_) {
Subroutine cx2let: not called CC=2 SCAR=17.9
Pod documented
296 0 my $cx = shift;
297 0 my $ABC="A"; ++$ABC for (1..$cx);
298 0 return $ABC
299 }
300 0 ▶sub let2cx(_) {
Subroutine let2cx: not called CC=2 SCAR=17.9
Pod documented
301 0 my $ABC = shift;
302 0 my $n = ord(substr($ABC,0,1,"")) - ord('A');
303 0 ▶ while (length $ABC) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $ABC) 0 0 0
304 0 my $letter = substr($ABC,0,1,"");
305 0 $n = (($n+1) * 26) + (ord($letter) - ord('A'));
306 }
307 0 return $n;
308 }
309 0 ▶sub cxrx2sheetaddr($$) { # (1,99) -> "B100"
Subroutine cxrx2sheetaddr: not called CC=1 SCAR=6.9
Pod undocumented
310 0 my ($cx, $rx) = @_;
311 0 return cx2let($cx) . ($rx + 1);
312 }
313  
314 =for Pod::Coverage cxrx2sheetaddr oops btw
315 =cut
316  
317 11 our @sane_CSV_read_options = (
318 # Text::CSV pod says to not specify 'eol' to allow embedded newlines,
319 # and to automatically handle "\n", "\r", or "\r\n".
320 #eol => $/,
321 binary => 1, # Allow reading embedded newlines & unicode etc.
322 sep_char => ",",
323 quote_char => '"',
324 escape_char => '"', # Embedded "s appear as ""
325 allow_whitespace => 0, # Preserve leading & trailing white space
326 auto_diag => 2, # die on errors
327 );
328 11 our @sane_CSV_write_options = (
329 eol => $/, # Necessary when WRITING csv files
330 binary => 1,
331 sep_char => ",",
332 quote_char => '"',
333 escape_char => '"', # Embedded "s appear as ""
334 allow_whitespace => 0, # Preserve leading & trailing white space
335 auto_diag => 2, # die on errors
336 );
337  
338 11 my %Saved_Sigs;
339 0 ▶sub _sighandler {
Subroutine _sighandler: not called CC=3 SCAR=24.8
340 0 ▶ if (! $Saved_Sigs{$_[0]} or $Saved_Sigs{$_[0]} eq 'DEFAULT') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $Saved_Sigs{$_[0]} or $Saved_Sigs{$_[0]} eq "DEFAULT") 0 0 0
Condition0%0/3
not $Saved_Sigs{$_[0]} or $Saved_Sigs{$_[0]} eq "DEFAULT"
l!l&&r!l&&!r
000
MC/DC0%0/2
not $Saved_Sigs{$_[0]} or $Saved_Sigs{$_[0]} eq "DEFAULT"
not $Saved_Sigs{$_[0]} $Saved_Sigs{$_[0]} eq "DEFAULT"
Truth table0%0/3
not(A) or B
A= not $Saved_Sigs{$_[0]}B= $Saved_Sigs{$_[0]} eq "DEFAULT"
ABresult
1X1
011
000
341 # The user isn't catching this, so the process would have aborted
342 # without running destructors: Call exit instead
343 ##warn "($$)".__PACKAGE__." caught signal $_[0], exiting\n";
344 0 Carp::cluck "($$)".__PACKAGE__." caught signal $_[0], exiting\n";
345 0 exit 1;
346 }
347 0 $SIG{$_[0]} = $Saved_Sigs{$_[0]};
348 0 kill $_[0], $$;
349 0 oops "Default (or user-defined) sig $_[0] action was to ignore!";
350 }
351 0 ▶sub _signals_guard() {
Subroutine _signals_guard: not called CC=2 SCAR=17.9
352 0 foreach my $name (qw/HUP INT QUIT TERM/) {
353 0 $Saved_Sigs{$name} = $SIG{$name};
354 0 $SIG{$name} = \&_sighandler;
355 }
356 0 ▶ return guard { @SIG{keys %Saved_Sigs} = (values %Saved_Sigs) }
Subroutine __ANON__: not called CC=1 SCAR=6.9
357 0 }
358  
359 # Create a probably-unique fingerprint for a particular file
360 0 ▶sub _file_fingerprint($) {
Subroutine _file_fingerprint: not called CC=2 SCAR=17.9
361 0 my $path = shift;
362 0 my $ctx = Digest::MD5->new;
363 0 $ctx->add($_) for((stat($path))[0,1,9]); # dev,ino,mtime
364 0 substr($ctx->b64digest,0,6)
365 }
366  
367 # Find LibreOffice, or failing that OpenOffice
368 11 our $OLpath_answer = $ENV{SPREADSHEET_EDIT_LOPATH};
369 3 ▶sub openlibreoffice_path() {
Subroutine openlibreoffice_path: called CC=20 SCAR=43.3
Pod documented
370 3 ▶ return $OLpath_answer if $OLpath_answer;
Branch: 1 branch
BranchTrueFalseTotal
if $Spreadsheet::Edit::IO::OLpath_answer 0 3 3
371 3 ▶ unless ($ENV{SPREADSHEET_EDIT_IGNPATH}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ENV{"SPREADSHEET_EDIT_IGNPATH"}) 3 0 3
372 3 foreach my $short_name (qw(libreoffice loffice localc)) {
373 9 ▶ if ($OLpath_answer = which($short_name)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Spreadsheet::Edit::IO::OLpath_answer = which($short_name)) 0 9 9
374 0 return( ($OLpath_answer=path($OLpath_answer)->canonpath) );
375 }
376 }
377 }
378 # Search for an installation. On Windows it will usually be
379 # C:\Program Files\LibreOffice\...
380 # On *nix, a local/isolated install (i.e. the result of extracting files
381 # from a .deb or other archive) will be
382 # somwehere/opt/libreofficeA.B/...
383 # and "..." is the same standard hierarchy on all platforms.
384  
385 # Previously this did 'require Path::Iterator::Rule' only if needed,
386 # but we need to disable warnings (actually they throw exceptions!)
387 # for unreadable directories.
388 # Doing "warnings->unimport('Path::Iterator::Rule');" doesn't seem to work.
389 # So now Path::Iterator::Rule is loaded unconditionally
390 11 ▶ use Path::Iterator::Rule v1.015 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
391 11 ▶ no warnings 'Path::Iterator::Rule';
Subroutine BEGIN: called CC=1 SCAR=0.0
392  
393 #require Path::Iterator::Rule;
394 #warnings->unimport('Path::Iterator::Rule'); # silence if unreadable dir
395  
396 3 state $is_MSWin = ($^O eq "MSWin32");
397 3 my $debug = $ENV{SPREADSHEET_EDIT_FINDDEBUG};
398  
399 3 my @search_specs;
400 3 ▶ if ($is_MSWin) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_MSWin) { } 0 3 3
401 0 @search_specs = (
402 # [startdir, pdir_name, pdir_maxdepth]
403 ["C:\\Program Files", "Program Files", 0],
404 # depth: C:\Program Files\libreofficeXXX/program/
405 # 1
406 );
407 0 push @search_specs, [ "C:\\Program Files (x86)", @{$search_specs[0]}[1,2] ];
408 } else {
409 3 @search_specs = (
410 [ File::Spec->rootdir(), "opt", 5 ],
411 # depth: /home/admin/lotest/<unpackparent>/opt/libreofficeXXX/program/
412 # 1 2 3 4 5
413 );
414 # depth: /path/.../to/homedir/local/opt/libreofficeXXX/program/
415 # 1 2
416 3 ▶ push @search_specs, [ $ENV{HOME}, "opt", 2 ] if $ENV{HOME};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"HOME"} 3 0 3
417 }
418  
419 3 foreach (@search_specs) {
420 6 my ($start_dir, $pdir_name, $pdir_maxdepth) = @$_;
421 6 ▶ next if $ENV{SPREADSHEET_EDIT_NOLOSEARCH};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"SPREADSHEET_EDIT_NOLOSEARCH"} 0 6 6
422  
423 6 my $start_dir_depth = scalar(() = path($start_dir)->stringify =~ m#(/[^/])#g);
424 my $rule = Path::Iterator::Rule->new
425 ->not_dangling()
426 7854 ▶ ->and( sub{
Subroutine __ANON__: called CC=16 SCAR=35.8
427 7854 local $_ = path($_)->stringify; # get rid of extra leading /
428 # Immediately prune dirs we know won't contain an LO installation
429 7854 ▶ if (m#^/(bin|boot|dev|etc|lib|lib32|lost\+found|media|proc|run|sys|tmp|usr/(bin|doc|games|include|libexec|sbin|share|src)|var)$#
Branch: 1 branch
BranchTrueFalseTotal
if (m[^/(bin|boot|dev|etc|lib|lib32|lost\+found|media|proc|run|sys|tmp|usr/(bin|doc|games|include|libexec|sbin|share|src)|var)$] or m[^/snap/(?!.*ffice)] or /\$/ or not -r _ or not -s _ || $is_MSWin) 120 7734 7854
Condition80%12/15
m[^/(bin|boot|dev|etc|lib|lib32|lost\+found|media|proc|run|sys|tmp|usr/(bin|doc|games|include|libexec|sbin|share|src)|var)$] or m[^/snap/(?!.*ffice)] or /\$/ or not -r _ or not -s _ || $is_MSWin
l!l&&r!l&&!r
63577734
m[^/(bin|boot|dev|etc|lib|lib32|lost\+found|media|proc|run|sys|tmp|usr/(bin|doc|games|include|libexec|sbin|share|src)|var)$] or m[^/snap/(?!.*ffice)] or /\$/ or not -r _
l!l&&r!l&&!r
5497791
m[^/(bin|boot|dev|etc|lib|lib32|lost\+found|media|proc|run|sys|tmp|usr/(bin|doc|games|include|libexec|sbin|share|src)|var)$] or m[^/snap/(?!.*ffice)] or /\$/
l!l&&r!l&&!r
5407800
m[^/(bin|boot|dev|etc|lib|lib32|lost\+found|media|proc|run|sys|tmp|usr/(bin|doc|games|include|libexec|sbin|share|src)|var)$] or m[^/snap/(?!.*ffice)]
l!l&&r!l&&!r
5407800
-s _ || $is_MSWin
l!l&&r!l&&!r
7734057
MC/DC50%3/6
m[^/(bin|boot|dev|etc|lib|lib32|lost\+found|media|proc|run|sys|tmp|usr/(bin|doc|games|include|libexec|sbin|share|src)|var)$] or m[^/snap/(?!.*ffice)] or /\$/ or not -r _ or not -s _ || $is_MSWin
m[^/(bin|boot|dev|etc|lib|lib32|lost\+found|media|proc|run|sys|tmp|usr/(bin|doc|games|include|libexec|sbin|share|src)|var)$] m[^/snap/(?!.*ffice)] /\$/ not -r _ -s _ $is_MSWin
Truth table57%4/7
A or B or C or not(D) or not(E || F)
A= m[^/(bin|boot|dev|etc|lib|lib32|lost\+found|media|proc|run|sys|tmp|usr/(bin|doc|games|include|libexec|sbin|share|src)|var)$]B= m[^/snap/(?!.*ffice)]C= /\$/D= not -r _E= -s _F= $is_MSWin
ABCDEFresult
1XXXXX1
01XXXX1
001XXX1
0001XX1
0000001
00001X0
0000010
430 || m#^/snap/(?!.*ffice)# # other than e.g. /snap/libreoffice
431 || /\$/ # $WINDOWS_SPECIAL_THINGS$
432 || (! -r _) # unreadable
433 || (! -s _ && !$is_MSWin) # zero size ==> /proc etc.
434 ) {
435 120 ▶ warn "### prelim prune: $_\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 120 120
436 120 return \0 # omit and prune if directory
437 }
438 7734 my $depth = scalar(() = m#(/)#ag) - $start_dir_depth;
439 # Find an installation parentdir anywhere at $pdir_maxdepth or below
440 7734 ▶ if (! m#/$pdir_name(?:/(.*))?$#) {
Branch: 1 branch
BranchTrueFalseTotal
unless (m[/$pdir_name(?:/(.*))?$]) 7734 0 7734
441 7734 ▶ if ($depth == $pdir_maxdepth) {
Branch: 1 branch
BranchTrueFalseTotal
if ($depth == $pdir_maxdepth) 3825 3909 7734
442 3825 ▶ warn "### prune pdir_maxdepth ($pdir_name)(d=$depth md=$pdir_maxdepth sdd=$start_dir_depth): $_\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 3825 3825
443 3825 return \0; # omit and, if directory, prune
444 }
445 3909 ▶ warn "# continue search for pdir (d=$depth md=$pdir_maxdepth): $_\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 3909 3909
446 3909 return 0; # omit but keep searching
447 }
448 0 ▶ if (! $1) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($1) 0 0 0
449 0 ▶ warn "### found pdir, continuing. $_\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
450 0 return 0; # Found pdir, continue searching
451 }
452 # Found .../<pdir>/<some install dir>
453 0 my $instdir = $1;
454 0 ▶ if ($instdir !~ m#^(?:libre|open)office#) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($instdir =~ /^(?:libre|open)office/) 0 0 0
455 0 ▶ warn "### prune non-LO/OO instdir: $_\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
456 0 return \0; # prune install subtrees for other programs
457 }
458 0 return \1; # emit this instdir and do not descend further
459 6 } ) ;
460  
461 6 my @instdirs = $rule->all(
462 $start_dir,
463 {depthfirst => -1, follow_symlinks => 1, loop_safe => 1, sorted => 1,
464 error_handler => undef, # ignore errors e.g. unreadable dirs
465 }
466 );
467  
468 my @candidates = map {
469 0 my ($exe, $version);
470 # Screen out any instdirs without program/soffice
471 # On Windows both soffice.exe and soffice.com exist; use the .com because
472 # it writes to stdout/err instead of popping up windows.
473 0 ▶ if ($is_MSWin && -e "$_/program/soffice.com") {
Branch: 2 branches
BranchTrueFalseTotal
if ($is_MSWin and -e "$_/program/soffice.com") { } 0 0 0
elsif (-x "$_/program/soffice") { } 0 0 0
Condition0%0/3
$is_MSWin and -e "$_/program/soffice.com"
!ll&&!rl&&r
000
MC/DC0%0/2
$is_MSWin and -e "$_/program/soffice.com"
$is_MSWin -e "$_/program/soffice.com"
Truth table0%0/3
A and B
A= $is_MSWinB= -e "$_/program/soffice.com"
ABresult
0X0
100
111
474 0 $exe = "$_/program/soffice.com";
475 }
476 elsif (-x "$_/program/soffice") {
477 0 $exe = "$_/program/soffice";
478 }
479 else {
480 0 warn "INCOMPLETE INSTALL: No soffice exe in $_\n";
481 }
482 # Extract version from the path
483 # For isolated non-standard installations, the <pdir> e.g. "/opt"
484 # is itself under some directory dedicated to a single install,
485 # and this directory may encode the exact version. For example
486 # /home/admin/lotest/5.2.5.1/opt/libreoffice5.2
487 # encodes version 5.2.5.1 .
488 # If a standard install was found then this will not be the case, and
489 # there may not even be a version in the installdir name (defaults to "0")
490 0 ▶ $version =
Branch: 2 branches
BranchTrueFalseTotal
m[/(\d+\.[.\da-z_]+)/] ? : 0 0 0
/.*(\d+\.[.\da-z_]+)/ ? : 0 0 0
491 m#/(\d+\.[.\da-z_]+)/# ? $1 :
492 m#.*(\d+\.[.\da-z_]+)# ? $1 :
493 0 ;
494 0 ▶ warn ivis '# Found version $version in $exe\n' if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
495 0 ▶ $version = (/libre/ ? 1 : 0).".".$version; # prefer libreoffice
Branch: 1 branch
BranchTrueFalseTotal
/libre/ ? : 0 0 0
496 0 ▶ $exe ? ([$exe, $version]) : ();
Branch: 1 branch
BranchTrueFalseTotal
$exe ? : 0 0 0
497 } @instdirs;
498  
499 # Pick the "highest" version
500 @candidates = sort {
501 0 my $v1 = $a->[1];
502 0 my $v2 = $b->[1];
503 0 foreach ($v1, $v2) {
504 0 ▶ s/[a-z]+$/.0/ # 1.2.3alpha => 1.2.3.0
Branch: 1 branch
BranchTrueFalseTotal
s/[a-z]+$/.0/ or s/[a-z]+(\d.*)/0$1/ 0 0 0
505 ||
506 s/[a-z]+(\d.*)/0$1/; # 1.2.3.alpha2 => 1.2.3.02
507 }
508 #warn dvis '## $v1 $v2\n' if $debug;
509 0 version->parse($v1) <=> version->parse($v2)
510 } @candidates;
511  
512 6 ▶ if (@candidates) {
Branch: 1 branch
BranchTrueFalseTotal
if (@candidates) 0 6 6
513 0 $OLpath_answer = path($candidates[-1][0])->realpath->canonpath;
514 0 return $OLpath_answer;
515 }
516 } #foreach @search_specs
517 3 return undef;
518 }#openlibreoffice_path
519  
520 5 ▶sub _openlibre_features() {
Subroutine _openlibre_features: called CC=8 SCAR=37.6
521 5 state $hash;
522 5 ▶ return $hash if defined $hash;
Branch: 1 branch
BranchTrueFalseTotal
if defined $hash 3 2 5
523 2 ▶ my $prog = openlibreoffice_path() // return(($hash={ available => 0 }));
Condition50%1/2
&openlibreoffice_path() // (return $hash = {"available", 0})
l!l
02
MC/DC0%0/1
&openlibreoffice_path() // (return $hash = {"available", 0})
&openlibreoffice_path()
Truth table50%1/2
A
A= &openlibreoffice_path()
Aresult
11
00
524 0 my $raw_version;
525 # This is gross but fast and works in recent versions of LO
526 0 ▶ if (my $fh = eval{ path($prog)->realpath->parent->child("types/offapi.rdb")
Branch: 1 branch
BranchTrueFalseTotal
if (my $fh = eval { do { path($prog)->realpath->parent->child("types/offapi.rdb")->filehandle("<", ":raw") } }) 0 0 0
527 ->filehandle("<",":raw")} ) {
528 0 my $octets; sysread $fh, $octets, 100;
529 0 ▶ if ($octets =~ /Created by LibreOffice (\d+\.\d+\.\w+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($octets =~ /Created by LibreOffice (\d+\.\d+\.\w+)/) 0 0 0
530 0 $raw_version = $1;
531 }
532 }
533 0 ▶ unless ($raw_version) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($raw_version) 0 0 0
534 0 ▶ if (qx/$prog --version 2>&1/ =~ /Libre.*? (\d+\.\d+\.\w+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if (`$prog --version 2>&1` =~ /Libre.*? (\d+\.\d+\.\w+)/) { } 0 0 0
535 0 $raw_version = $1;
536 } else {
537 0 warn "$prog --version DID NOT WORK\n";
538 }
539 }
540 0 ▶ unless ($raw_version) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($raw_version) 0 0 0
541 0 warn "WARNING: Could not determine version of $prog\n";
542 0 $raw_version = "999.01";
543 }
544 0 my $version = version->parse("v$raw_version");
545 0 $hash = {
546 available => 1,
547 # LibreOffice 7.2 allows extracting all sheets at once
548 allsheets => ($version >= version->parse("v7.2")),
549 # ...but not yet extracting a single sheet by name.
550 # https://bugs.documentfoundation.org/show_bug.cgi?id=135762#c24
551 named_sheet => 0,
552 # Supported output formats are too many to list
553 ousuf_any => 1,
554 raw_version => $raw_version, version => "$version",
555 }
556 }
557  
558 2 ▶sub _openlibre_supports_allsheets() { _openlibre_features()->{allsheets} }
Subroutine _openlibre_supports_allsheets: called CC=1 SCAR=0.0
559 1 ▶sub _openlibre_supports_named_sheet() { _openlibre_features()->{named_sheet} }
Subroutine _openlibre_supports_named_sheet: called CC=1 SCAR=0.0
560 0 ▶sub _openlibre_supports_writing($) { _openlibre_features()->{available} }
Subroutine _openlibre_supports_writing: not called CC=1 SCAR=6.9
561  
562 5 ▶sub _ssconvert_features() { return { availble => 0 } } # TODO add back?
Subroutine _ssconvert_features: called CC=1 SCAR=0.0
563 2 ▶sub _ssconvert_supports_allsheets() { _ssconvert_features()->{allsheets} }
Subroutine _ssconvert_supports_allsheets: called CC=1 SCAR=0.0
564 1 ▶sub _ssconvert_supports_named_sheet() { _ssconvert_features()->{named_sheet} }
Subroutine _ssconvert_supports_named_sheet: called CC=1 SCAR=0.0
565 0 ▶sub _ssconvert_supports_writing($) { _ssconvert_features()->{available} }
Subroutine _ssconvert_supports_writing: not called CC=1 SCAR=6.9
566  
567 # These allow users (e.g. App-diff_spreadsheets tests) to determine
568 # if external tool(s) are available to convert between spreadsheet formats
569 # or to/from csv (CSVs are supported directly so can always be used)
570 # Currently used by t/io.pl to skip tests
571 2 ▶sub can_cvt_spreadsheets() {
Subroutine can_cvt_spreadsheets: called CC=2 SCAR=7.6
Pod documented
572 _openlibre_features()->{available} || _ssconvert_features()->{availble}
573 2 ▶}
Branch: 1 branch
BranchTrueFalseTotal
&_openlibre_features()->{"available"} or &_ssconvert_features()->{"availble"} 0 2 2
574 2 ▶sub can_extract_allsheets() {
Subroutine can_extract_allsheets: called CC=2 SCAR=7.6
Pod documented
575 2 ▶ _openlibre_supports_allsheets() || _ssconvert_supports_allsheets()
Branch: 1 branch
BranchTrueFalseTotal
&_openlibre_supports_allsheets() or &_ssconvert_supports_allsheets() 0 2 2
576 }
577 1 ▶sub can_extract_named_sheet() {
Subroutine can_extract_named_sheet: called CC=3 SCAR=14.2
Pod documented
578 1 ▶ can_extract_allsheets() # used to emulate
Branch: 1 branch
BranchTrueFalseTotal
&can_extract_allsheets() or &_openlibre_supports_named_sheet() or &_ssconvert_supports_named_sheet() 0 1 1
Condition33%1/3
&can_extract_allsheets() or &_openlibre_supports_named_sheet()
l!l&&r!l&&!r
001
MC/DC0%0/2
&can_extract_allsheets() or &_openlibre_supports_named_sheet()
&can_extract_allsheets() &_openlibre_supports_named_sheet()
Truth table33%1/3
A or B
A= &can_extract_allsheets()B= &_openlibre_supports_named_sheet()
ABresult
1X1
011
000
579 || _openlibre_supports_named_sheet() || _ssconvert_supports_named_sheet()
580 }
581  
582 =for Pod::Coverage can_cvt_spreadsheets can_extract_allsheets
583 -for Pod::Coverage can_extract_named_sheet
584 =cut
585  
586 0 ▶sub _runcmd($@) {
Subroutine _runcmd: not called CC=9 SCAR=45.0
587 0 my ($opts, @cmd) = @_;
588 0 my $guard = _signals_guard;
589 # This used to fork & exec but that blows up on MSWin32 because the child
590 # pseudo-process executes all END{} blocks everywhere after "exec"
591 0 my $redirs = "";
592 0 ▶ if ($opts->{suppress_stderr}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$opts{"suppress_stderr"}) 0 0 0
593 0 $redirs .= " 2>".devnull();
594 }
595 0 ▶ if ($opts->{stdout_to_stderr}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$opts{"stdout_to_stderr"}) 0 0 0
596 0 confess "Not portable";
597 0 $redirs .= " 1>&2";
598 }
599 0 ▶ if ($opts->{stderr_to_stdout}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$opts{"stderr_to_stdout"}) 0 0 0
600 0 confess "Not portable";
601 0 $redirs .= " 2>&1";
602 }
603 0 ▶ if ($opts->{suppress_stdout}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$opts{"suppress_stdout"}) 0 0 0
604 0 $redirs .= " >".devnull();
605 }
606 #my $cmdstr = join(" ", map{qsh} @cmd) . $redirs;
607 0 my $cmdstr = qshlist(@cmd)." $redirs";
608 0 ▶ warn "> $cmdstr\n" if $opts->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $$opts{"verbose"} 0 0 0
609 0 ▶ if ($redirs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($redirs) { } 0 0 0
610 0 foreach (@cmd) {
611 0 ▶ confess "Can not portably pass argument '$_'" if /["']/;
Branch: 1 branch
BranchTrueFalseTotal
if /["']/ 0 0 0
612 }
613 0 system $cmdstr;
614 } else {
615 0 system { $cmd[0] } @cmd;
616 }
617 0 return $?
618 }
619  
620 0 ▶sub _fmt_outpath_contents($) {
Subroutine _fmt_outpath_contents: not called CC=5 SCAR=34.0
621 0 ▶ my $outpath = $_[0]->{outpath} // oops;
Condition0%0/3
$_[0]{"outpath"} // &oops()
l!l&&r!l&&!r
000
MC/DC0%0/2
$_[0]{"outpath"} // &oops()
$_[0]{"outpath"} &oops()
Truth table0%0/3
A // B
A= $_[0]{"outpath"}B= &oops()
ABresult
1X1
011
000
622 0 ▶ return "(outpath does not exist)" unless -e $outpath;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $outpath 0 0 0
623 0 ▶ return "(outpath is a file)" if -f $outpath;
Branch: 1 branch
BranchTrueFalseTotal
if -f $outpath 0 0 0
624 0 ▶ return "(outpath is a STRANGE OBJECT)" unless -d $outpath;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $outpath 0 0 0
625 "\n outpath contains: "
626 0 .join(", ",map{qsh basename $_} path($outpath)->children);
627 }
628  
629 11 my ($proctempdir, $rm_proctempdir_at_exit);
630 131 ▶sub _create_proctempdir_ifneeded($) {
Subroutine _create_proctempdir_ifneeded: called CC=3 SCAR=11.2
631 131 my $opts = shift;
632 # Keep a per-process persistent temp directory, deleted at process exit
633 # (except if $opts{tempdir} was passed).
634 # It contains result files when the user did not specify {outpath},
635 # plus a cache of as-yet unrequested sheet .csv files, used when the
636 # external tool can only extract all sheets, not a single sheet by name:
637 #
638 # tempdir/
639 # <ifbase>_<sig>.xlsx etc. # single file returned to user
640 # <ifbase>_<sig>/*.csv # directory returned to user
641 # <ifbase>_<sig>_csvcache/*.csv
642 #
643 # <ifbase> is derived from the intput file name, and <sig> is a fingerprint
644 # based on input file's dev, inode, and modification timestamp.
645 #
646 131 ▶ $proctempdir //= do{
Condition66%2/3
$proctempdir //= do { my $pid = $$; my $user = &_get_username(); my $dname = ('Spreadsheet::Edit::IO' . "_${user}_${pid}_tempdir") =~ s/::/-/gr; my $parent; if ($$opts{"tempdir"}) { $parent = path($$opts{"tempdir"})->mkdir; } else { $parent = path("File::Spec"->tmpdir); $rm_proctempdir_at_exit = 1; } ; $parent->child($dname)->mkdir }
l!l&&r!l&&!r
12470
MC/DC0%0/2
$proctempdir //= do { my $pid = $$; my $user = &_get_username(); my $dname = ('Spreadsheet::Edit::IO' . "_${user}_${pid}_tempdir") =~ s/::/-/gr; my $parent; if ($$opts{"tempdir"}) { $parent = path($$opts{"tempdir"})->mkdir; } else { $parent = path("File::Spec"->tmpdir); $rm_proctempdir_at_exit = 1; } ; $parent->child($dname)->mkdir }
$proctempdir do { my $pid = $$; my $user = &_get_username(); my $dname = ('Spreadsheet::Edit::IO' . "_${user}_${pid}_tempdir") =~ s/::/-/gr; my $parent; if ($$opts{"tempdir"}) { $parent = path($$opts{"tempdir"})->mkdir; } else { $parent = path("File::Spec"->tmpdir); $rm_proctempdir_at_exit = 1; } ; $parent->child($dname)->mkdir }
Truth table66%2/3
A //= B
A= $proctempdirB= do { my $pid = $$; my $user = &_get_username(); my $dname = ('Spreadsheet::Edit::IO' . "_${user}_${pid}_tempdir") =~ s/::/-/gr; my $parent; if ($$opts{"tempdir"}) { $parent = path($$opts{"tempdir"})->mkdir; } else { $parent = path("File::Spec"->tmpdir); $rm_proctempdir_at_exit = 1; } ; $parent->child($dname)->mkdir }
ABresult
1X1
011
000
647 7 my $pid = $$;
648 7 my $user = _get_username();
649 7 my $dname = (__PACKAGE__."_${user}_${pid}_tempdir") =~ s/::/-/gr;
650 7 my $parent;
651 7 ▶ if ($opts->{tempdir}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$opts{'tempdir'}) { } 0 7 7
652 0 $parent = path($opts->{tempdir})->mkdir;
653 } else {
654 7 $parent = path(File::Spec->tmpdir);
655 7 $rm_proctempdir_at_exit = 1;
656 }
657 7 $parent->child($dname)->mkdir
658 };
659 }
660 11 ▶END{
Subroutine END: called CC=3 SCAR=11.1
661 11 local($., $@, $!, $^E, $?);
662 11 ▶ $proctempdir->remove_tree if $proctempdir && $rm_proctempdir_at_exit;
Branch: 1 branch
BranchTrueFalseTotal
if $proctempdir and $rm_proctempdir_at_exit 7 4 11
Condition66%2/3
$proctempdir and $rm_proctempdir_at_exit
!ll&&!rl&&r
407
MC/DC50%1/2
$proctempdir and $rm_proctempdir_at_exit
$proctempdir $rm_proctempdir_at_exit
Truth table66%2/3
A and B
A= $proctempdirB= $rm_proctempdir_at_exit
ABresult
0X0
100
111
663 }
664  
665 # Compose a unique path under $proctempdir.
666 # This is *not* a "tempfile" or "tempdir" object which auto-destructs,
667 # in fact it does not even exist yet and we don't know here whether it will
668 # be a file or subdirectory. The user must remove it when they are done
669 # with it, or it will be removed when $proctempdir is removed at process exit.
670 #
671 54 ▶sub _uniqpath_under_tempdir($@) {
Subroutine _uniqpath_under_tempdir: called CC=3 SCAR=11.0
672 54 my $opts = shift;
673 my %args = (
674 54 words => [$opts->{ifbase}, $opts->{sheetname}],
675 @_
676 );
677 54 my $bname = join "_", grep{defined} @{$args{words}};
678 # Collisions occur when recursing to emulate Extract-by-name,
679 # or if the user repeatedly reads the same thing, etc.
680 54 state $seqnums = {};
681 54 ▶ if ($seqnums->{$bname}++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$seqnums{$bname}++) 42 12 54
682 42 $bname .= "_".$seqnums->{$bname}; # append unique sequence number
683 }
684 54 ▶ $bname .= ".$args{suf}" if $args{suf};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"suf"} 52 2 54
685 54 return $proctempdir->child($bname);
686 }
687  
688 # Compose csv cache subdir path
689 0 ▶sub _cachedir($) {
Subroutine _cachedir: not called CC=1 SCAR=6.9
690 0 my $opts = shift;
691 0 _uniqpath_under_tempdir($opts,words => [$opts->{ifbase}, "csvcache"]);
692 }
693  
694 0 ▶sub _convert_using_openlibre($$$) {
Subroutine _convert_using_openlibre: not called CC=58 SCAR=81.4
695 0 my ($opts, $src, $dst) = @_;
696 0 ▶ oops unless all{ $opts->{$_} } qw/cvt_from cvt_to/;
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unless &all(sub { $$opts{$_}; } , "cvt_from", "cvt_to") 0 0 0
697 0 ▶ oops if $opts->{allsheets} && ! _openlibre_supports_allsheets();
Branch: 1 branch
BranchTrueFalseTotal
if $$opts{"allsheets"} and not &_openlibre_supports_allsheets() 0 0 0
Condition0%0/3
$$opts{"allsheets"} and not &_openlibre_supports_allsheets()
!ll&&!rl&&r
000
MC/DC0%0/2
$$opts{"allsheets"} and not &_openlibre_supports_allsheets()
$$opts{"allsheets"} not &_openlibre_supports_allsheets()
Truth table0%0/3
A and not(B)
A= $$opts{"allsheets"}B= not &_openlibre_supports_allsheets()
ABresult
0X0
100
111
698 0 ▶ oops if $opts->{sheetname} && ! _openlibre_supports_named_sheet();
Branch: 1 branch
BranchTrueFalseTotal
if $$opts{"sheetname"} and not &_openlibre_supports_named_sheet() 0 0 0
Condition0%0/3
$$opts{"sheetname"} and not &_openlibre_supports_named_sheet()
!ll&&!rl&&r
000
MC/DC0%0/2
$$opts{"sheetname"} and not &_openlibre_supports_named_sheet()
$$opts{"sheetname"} not &_openlibre_supports_named_sheet()
Truth table0%0/3
A and not(B)
A= $$opts{"sheetname"}B= not &_openlibre_supports_named_sheet()
ABresult
0X0
100
111
699 0 my $debug = $opts->{debug};
700  
701 0 ▶ my $prog = openlibreoffice_path() // oops;
Condition0%0/3
&openlibreoffice_path() // &oops()
l!l&&r!l&&!r
000
MC/DC0%0/2
&openlibreoffice_path() // &oops()
&openlibreoffice_path() &oops()
Truth table0%0/3
A // B
A= &openlibreoffice_path()B= &oops()
ABresult
1X1
011
000
702  
703 0 my $saved_UserInstallation = $ENV{UserInstallation};
704 # URI format is file://server/path where 'server' is empty. "file://path" is
705 # "never correct, but is often used" en.wikipedia.org/wiki/File_URI_scheme
706 # Correct examples: file::///tmp/something file:///C:/somewhere
707 0 $ENV{UserInstallation} = URI::file->new(_get_tool_profdir($prog)->canonpath);
708 0 ▶ warn "Temporarily set UserInstallation=$ENV{UserInstallation}\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
709 0 ▶ scope_guard {
Subroutine __ANON__: not called CC=2 SCAR=17.9
710 0 ▶ if (defined $saved_UserInstallation) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $saved_UserInstallation) { } 0 0 0
711 0 $ENV{UserInstallation} = $saved_UserInstallation;
712 } else {
713 delete $ENV{UserInstallation}
714 0 }
715 0 };
716  
717 # The --convert-to argument is "suffix:filtername:filteropts"
718  
719 # I think (RFFM_Members_2025-06-25.txtnot certain) that we can only specify the encoding of CSV files,
720 # either as input or output; .xlsx and .ods spreadsheets (which are based
721 # on XML) could in principle use any encoding internally, but I'm not sure
722 # we can control that, nor should anyone ever need to.
723  
724 # REFERENCES:
725 # https://help.libreoffice.org/7.5/en-US/text/shared/guide/start_parameters.html?&DbPAR=SHARED&System=UNIX
726 # http://wiki.openoffice.org/wiki/Documentation/DevGuide/Spreadsheets/Filter_Options
727 # https://wiki.documentfoundation.org/Documentation/DevGuide/Spreadsheet_Documents#Filter_Options_for_the_CSV_Filter
728  
729 # I think we never want to specify the filter unless we have parameters
730 # for it. Currently that is only for csv.
731 # If no filter is specified, the suffix (e.g. 'ods') should be enough
732 0 state $suf2ofilter = {
733 csv => "Text - txt - csv (StarCalc)",
734 txt => "Text - txt - csv (StarCalc)",
735 #xls => "MS Excel 97",
736 #xlsx => "Calc MS Excel 2007 XML",
737 #ods => "calc8",
738 };
739  
740 0 ▶ my $ifilter = $opts->{soffice_infilter} //= do{
Condition0%0/3
$$opts{"soffice_infilter"} //= do { if ($$opts{"cvt_from"} eq "csv") { my $filter_name = $$suf2ofilter{$$opts{"cvt_from"}} or &oops(); my $enc = $$opts{"input_encoding"}; my $charset = &_name2LOcharsetnum($enc); my $colformats = ""; if (my $cf = $$opts{"col_formats"}) { $cf = [split(m[[/,]], $cf, 0)] unless ref $cf; for (my $ix = 0; $ix <= $cf->$#*; ++$ix) { local $_ = $$cf[$ix] // 1; /^([123459]|10)$/ or s/^standard$/1/i or s/^text$/2/i or s[^M+/D+/Y+$][3]i or s[^D+/M+/Y+$][4]i or s[^Y+/M+/D+$][5]i or s/^ignore$/9/i or s/^US.*English$/10/i or croak("Unknown format code '${_}' in {col_formats}"); $colformats .= '/' if $colformats; $colformats .= ($ix + 1) . "/$_"; } ; } ; $filter_name . ':' . ord($$opts{"sep_char"} // ",") . ',' . ord($$opts{"quote_char"} // "\"") . ',' . "$charset,1" . ",$colformats" . ',' . ",false" . ',true' . ",," . ',' . "," . ',' . ","; } else { undef; } }
l!l&&r!l&&!r
000
MC/DC0%0/2
$$opts{"soffice_infilter"} //= do { if ($$opts{"cvt_from"} eq "csv") { my $filter_name = $$suf2ofilter{$$opts{"cvt_from"}} or &oops(); my $enc = $$opts{"input_encoding"}; my $charset = &_name2LOcharsetnum($enc); my $colformats = ""; if (my $cf = $$opts{"col_formats"}) { $cf = [split(m[[/,]], $cf, 0)] unless ref $cf; for (my $ix = 0; $ix <= $cf->$#*; ++$ix) { local $_ = $$cf[$ix] // 1; /^([123459]|10)$/ or s/^standard$/1/i or s/^text$/2/i or s[^M+/D+/Y+$][3]i or s[^D+/M+/Y+$][4]i or s[^Y+/M+/D+$][5]i or s/^ignore$/9/i or s/^US.*English$/10/i or croak("Unknown format code '${_}' in {col_formats}"); $colformats .= '/' if $colformats; $colformats .= ($ix + 1) . "/$_"; } ; } ; $filter_name . ':' . ord($$opts{"sep_char"} // ",") . ',' . ord($$opts{"quote_char"} // "\"") . ',' . "$charset,1" . ",$colformats" . ',' . ",false" . ',true' . ",," . ',' . "," . ',' . ","; } else { undef; } }
$$opts{"soffice_infilter"} do { if ($$opts{"cvt_from"} eq "csv") { my $filter_name = $$suf2ofilter{$$opts{"cvt_from"}} or &oops(); my $enc = $$opts{"input_encoding"}; my $charset = &_name2LOcharsetnum($enc); my $colformats = ""; if (my $cf = $$opts{"col_formats"}) { $cf = [split(m[[/,]], $cf, 0)] unless ref $cf; for (my $ix = 0; $ix <= $cf->$#*; ++$ix) { local $_ = $$cf[$ix] // 1; /^([123459]|10)$/ or s/^standard$/1/i or s/^text$/2/i or s[^M+/D+/Y+$][3]i or s[^D+/M+/Y+$][4]i or s[^Y+/M+/D+$][5]i or s/^ignore$/9/i or s/^US.*English$/10/i or croak("Unknown format code '${_}' in {col_formats}"); $colformats .= '/' if $colformats; $colformats .= ($ix + 1) . "/$_"; } ; } ; $filter_name . ':' . ord($$opts{"sep_char"} // ",") . ',' . ord($$opts{"quote_char"} // "\"") . ',' . "$charset,1" . ",$colformats" . ',' . ",false" . ',true' . ",," . ',' . "," . ',' . ","; } else { undef; } }
Truth table0%0/3
A //= B
A= $$opts{"soffice_infilter"}B= do { if ($$opts{"cvt_from"} eq "csv") { my $filter_name = $$suf2ofilter{$$opts{"cvt_from"}} or &oops(); my $enc = $$opts{"input_encoding"}; my $charset = &_name2LOcharsetnum($enc); my $colformats = ""; if (my $cf = $$opts{"col_formats"}) { $cf = [split(m[[/,]], $cf, 0)] unless ref $cf; for (my $ix = 0; $ix <= $cf->$#*; ++$ix) { local $_ = $$cf[$ix] // 1; /^([123459]|10)$/ or s/^standard$/1/i or s/^text$/2/i or s[^M+/D+/Y+$][3]i or s[^D+/M+/Y+$][4]i or s[^Y+/M+/D+$][5]i or s/^ignore$/9/i or s/^US.*English$/10/i or croak("Unknown format code '${_}' in {col_formats}"); $colformats .= '/' if $colformats; $colformats .= ($ix + 1) . "/$_"; } ; } ; $filter_name . ':' . ord($$opts{"sep_char"} // ",") . ',' . ord($$opts{"quote_char"} // "\"") . ',' . "$charset,1" . ",$colformats" . ',' . ",false" . ',true' . ",," . ',' . "," . ',' . ","; } else { undef; } }
ABresult
1X1
011
000
741 0 ▶ if ($opts->{cvt_from} eq "csv") {
Branch: 1 branch
BranchTrueFalseTotal
if ($$opts{'cvt_from'} eq 'csv') { } 0 0 0
742 0 ▶ my $filter_name = $suf2ofilter->{$opts->{cvt_from}} or oops;
Branch: 1 branch
BranchTrueFalseTotal
my $filter_name = $$suf2ofilter{$$opts{"cvt_from"}} or &oops() 0 0 0
743 0 my $enc = $opts->{input_encoding};
744 0 my $charset = _name2LOcharsetnum($enc); # dies if unknown enc
745 0 my $colformats = "";
746 0 ▶ if (my $cf = $opts->{col_formats}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cf = $$opts{"col_formats"}) 0 0 0
747 0 ▶ $cf = [split /[\/,]/, $cf] unless ref($cf); # fmtA/fmtB/...
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cf 0 0 0
748 0 ▶ for (my $ix=0; $ix <= $#$cf; $ix++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ix <= $cf->$#*) 0 0 0
749 0 ▶ local $_ = $cf->[$ix] // 1;
Condition0%0/2
$$cf[$ix] // 1
l!l
00
MC/DC0%0/1
$$cf[$ix] // 1
$$cf[$ix]
Truth table0%0/2
A
A= $$cf[$ix]
Aresult
11
00
750 0 ▶ m#^([123459]|10)$#
Branch: 1 branch
BranchTrueFalseTotal
/^([123459]|10)$/ or s/^standard$/1/i or s/^text$/2/i or s[^M+/D+/Y+$][3]i or s[^D+/M+/Y+$][4]i or s[^Y+/M+/D+$][5]i or s/^ignore$/9/i or s/^US.*English$/10/i or croak("Unknown format code '${_}' in {col_formats}") 0 0 0
Condition0%0/21
/^([123459]|10)$/ or s/^standard$/1/i or s/^text$/2/i or s[^M+/D+/Y+$][3]i or s[^D+/M+/Y+$][4]i or s[^Y+/M+/D+$][5]i or s/^ignore$/9/i or s/^US.*English$/10/i
l!l&&r!l&&!r
000
/^([123459]|10)$/ or s/^standard$/1/i or s/^text$/2/i or s[^M+/D+/Y+$][3]i or s[^D+/M+/Y+$][4]i or s[^Y+/M+/D+$][5]i or s/^ignore$/9/i
l!l&&r!l&&!r
000
/^([123459]|10)$/ or s/^standard$/1/i or s/^text$/2/i or s[^M+/D+/Y+$][3]i or s[^D+/M+/Y+$][4]i or s[^Y+/M+/D+$][5]i
l!l&&r!l&&!r
000
/^([123459]|10)$/ or s/^standard$/1/i or s/^text$/2/i or s[^M+/D+/Y+$][3]i or s[^D+/M+/Y+$][4]i
l!l&&r!l&&!r
000
/^([123459]|10)$/ or s/^standard$/1/i or s/^text$/2/i or s[^M+/D+/Y+$][3]i
l!l&&r!l&&!r
000
/^([123459]|10)$/ or s/^standard$/1/i or s/^text$/2/i
l!l&&r!l&&!r
000
/^([123459]|10)$/ or s/^standard$/1/i
l!l&&r!l&&!r
000
MC/DC0%0/8
/^([123459]|10)$/ or s/^standard$/1/i or s/^text$/2/i or s[^M+/D+/Y+$][3]i or s[^D+/M+/Y+$][4]i or s[^Y+/M+/D+$][5]i or s/^ignore$/9/i or s/^US.*English$/10/i
/^([123459]|10)$/ s/^standard$/1/i s/^text$/2/i s[^M+/D+/Y+$][3]i s[^D+/M+/Y+$][4]i s[^Y+/M+/D+$][5]i s/^ignore$/9/i s/^US.*English$/10/i
Truth table0%0/9
A or B or C or D or E or F or G or H
A= /^([123459]|10)$/B= s/^standard$/1/iC= s/^text$/2/iD= s[^M+/D+/Y+$][3]iE= s[^D+/M+/Y+$][4]iF= s[^Y+/M+/D+$][5]iG= s/^ignore$/9/iH= s/^US.*English$/10/i
ABCDEFGHresult
1XXXXXXX1
01XXXXXX1
001XXXXX1
0001XXXX1
00001XXX1
000001XX1
0000001X1
000000011
000000000
751 || s#^standard$#1#i
752 || s#^text$#2#i
753 || s#^M+/D+/Y+$#3#i
754 || s#^D+/M+/Y+$#4#i
755 || s#^Y+/M+/D+$#5#i
756 || s#^ignore$#9#i
757 || s#^US.*English$#10#i
758 || croak "Unknown format code '$_' in {col_formats}";
759 0 ▶ $colformats .= "/" if $colformats;
Branch: 1 branch
BranchTrueFalseTotal
if $colformats 0 0 0
760 0 $colformats .= ($ix+1)."/$_";
761 }
762 }
763 $filter_name.":"
764 # Tokens 1-4: FldSep=',' TxtDelim='"' Charset FirstLineNum
765 #. "44,34,$charset,1"
766 . ord($opts->{sep_char}//",").","
767 0 ▶ . ord($opts->{quote_char}//'"').","
Condition0%0/4
$$opts{"sep_char"} // ","
l!l
00
$$opts{"quote_char"} // "\""
l!l
00
MC/DC0%0/1
$$opts{"sep_char"} // ","
$$opts{"sep_char"}
MC/DC0%0/1
$$opts{"quote_char"} // "\""
$$opts{"quote_char"}
Truth table0%0/2
A
A= $$opts{"sep_char"}
Aresult
11
00
Truth table0%0/2
A
A= $$opts{"quote_char"}
Aresult
11
00
768 . "$charset,1"
769 # Token 5: Cell format codes:
770 # If variable-width cells (the norm): colnum/fmt/colnum/fmt...
771 # colnum: 1-based column number
772 # fmt: 1=Std 2=Text 3=MM/DD/YY 4=DD/MM/YY 5=YY/MM/DD 6-8 unused
773 # 9=ignore field (do not import),
774 # 10=US-English content (e.g. 3.14 not 3,14)
775 # (I'm guessing 1=Std means use current lang [or per Tok 6?])
776 # If fixed-width cells... [something else]
777 . ",$colformats"
778 # Token 6: MS-LCID Language Id; 0 or omitted means UI language
779 . "," # default: false
780 # Token 7: On input: true => Quoted cells are always read a 'text',
781 # effectively disabling Token 8. This must be false to recognize dates
782 # like "Jan 1, 2000" which by necessity must be quoted for the comma,
783 # but will **CORRUPT** zip codes with leading zeroes unless
784 # {col_formats} overrides (which it does now by default).
785 .",false" # default: false
786 # Token 8: on input: "Detect Special Numbers", i.e. date or time values
787 # in human form, numbers in scientific (expondntial) notation etc.
788 # If false, ONLY decimal numbers (thousands separators ok).
789 .",true" # default: false (for import)
790 # Tokens 9-10: not used on import
791 .",,"
792 # Token 11: Remove spaces; trim leading & trailing spaces when reading
793 ."," # default: false
794 # Token 12: not use on import
795 .","
796 # Token 13: Import "=..." as formulas instead of text?
797 ."," # default: false i.e. do not recognize formulas
798 # Token 14: "Automatically detected since LibreOffice 7.6" [BOM?]
799 .","
800 }
801 else {
802 undef
803 0 }
804 };
805  
806 0 ▶ my $ofilter = $opts->{soffice_outfilter} //= do{
Condition0%0/3
$$opts{"soffice_outfilter"} //= do { if ($$opts{"cvt_to"} =~ /^(?:csv|txt)$/) { my $filter_name = $$suf2ofilter{$$opts{"cvt_to"}} or &oops(); my($enc) = $$opts{"output_binmode"} =~ /:encoding\((.*?)\)/ or &oops(); my $charset = &_name2LOcharsetnum($enc); $filter_name . ':' . ($$opts{"cvt_to"} eq "txt" ? "FIX" : ord($$opts{"sep_char"} // ",")) . ',' . ord($$opts{"quote_char"} // "\"") . ',' . "$charset,1" . ',' . "," . ',false' . ",true" . ',false' . ",false" . ',' . "," . ($$opts{"allsheets"} ? -1 : ($$opts{"sheetname"} ? die("add named-sheet support here") : 0)) . ','; } else { undef; } }
l!l&&r!l&&!r
000
MC/DC0%0/2
$$opts{"soffice_outfilter"} //= do { if ($$opts{"cvt_to"} =~ /^(?:csv|txt)$/) { my $filter_name = $$suf2ofilter{$$opts{"cvt_to"}} or &oops(); my($enc) = $$opts{"output_binmode"} =~ /:encoding\((.*?)\)/ or &oops(); my $charset = &_name2LOcharsetnum($enc); $filter_name . ':' . ($$opts{"cvt_to"} eq "txt" ? "FIX" : ord($$opts{"sep_char"} // ",")) . ',' . ord($$opts{"quote_char"} // "\"") . ',' . "$charset,1" . ',' . "," . ',false' . ",true" . ',false' . ",false" . ',' . "," . ($$opts{"allsheets"} ? -1 : ($$opts{"sheetname"} ? die("add named-sheet support here") : 0)) . ','; } else { undef; } }
$$opts{"soffice_outfilter"} do { if ($$opts{"cvt_to"} =~ /^(?:csv|txt)$/) { my $filter_name = $$suf2ofilter{$$opts{"cvt_to"}} or &oops(); my($enc) = $$opts{"output_binmode"} =~ /:encoding\((.*?)\)/ or &oops(); my $charset = &_name2LOcharsetnum($enc); $filter_name . ':' . ($$opts{"cvt_to"} eq "txt" ? "FIX" : ord($$opts{"sep_char"} // ",")) . ',' . ord($$opts{"quote_char"} // "\"") . ',' . "$charset,1" . ',' . "," . ',false' . ",true" . ',false' . ",false" . ',' . "," . ($$opts{"allsheets"} ? -1 : ($$opts{"sheetname"} ? die("add named-sheet support here") : 0)) . ','; } else { undef; } }
Truth table0%0/3
A //= B
A= $$opts{"soffice_outfilter"}B= do { if ($$opts{"cvt_to"} =~ /^(?:csv|txt)$/) { my $filter_name = $$suf2ofilter{$$opts{"cvt_to"}} or &oops(); my($enc) = $$opts{"output_binmode"} =~ /:encoding\((.*?)\)/ or &oops(); my $charset = &_name2LOcharsetnum($enc); $filter_name . ':' . ($$opts{"cvt_to"} eq "txt" ? "FIX" : ord($$opts{"sep_char"} // ",")) . ',' . ord($$opts{"quote_char"} // "\"") . ',' . "$charset,1" . ',' . "," . ',false' . ",true" . ',false' . ",false" . ',' . "," . ($$opts{"allsheets"} ? -1 : ($$opts{"sheetname"} ? die("add named-sheet support here") : 0)) . ','; } else { undef; } }
ABresult
1X1
011
000
807 # OutputFilterName[:paramtoken,paramtoken,...]
808 0 ▶ if ($opts->{cvt_to} =~ /^(?:csv|txt)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$opts{'cvt_to'} =~ /^(?:csv|txt)$/) { } 0 0 0
809 # Output is csv format. If converting to "txt" then the output still
810 # uses the csv filter but with "FIX"ed width fields (no separators
811 # or quotes). Note that we are converating a *spreadsheet* input, not
812 # a writer document.
813 # 6/12/2025: csv --> txt IS NOT WORKING (truncates wide columns)
814 0 ▶ my $filter_name = $suf2ofilter->{$opts->{cvt_to}} or oops;
Branch: 1 branch
BranchTrueFalseTotal
my $filter_name = $$suf2ofilter{$$opts{"cvt_to"}} or &oops() 0 0 0
815 #my $enc = $opts->{output_encoding};
816 0 ▶ my ($enc) = ($opts->{output_binmode} =~ /:encoding\((.*?)\)/) or oops;
Branch: 1 branch
BranchTrueFalseTotal
my($enc) = $$opts{"output_binmode"} =~ /:encoding\((.*?)\)/ or &oops() 0 0 0
817 0 my $charset = _name2LOcharsetnum($enc); # dies if unknown enc
818 $filter_name.":"
819 # Tokens 1-4: FldSep=,(or FIX for "txt") TxtDelim=" Charset FirstLineNum
820 #."44,34,$charset,1"
821 . ($opts->{cvt_to} eq "txt" ? "FIX" : ord($opts->{sep_char}//",")).","
822 . ord($opts->{quote_char}//'"').","
823 . "$charset,1"
824 # Token 5: Cell format codes. Only used for import? (see above)
825 # What about fixed-width?
826 .","
827 # Token 6: Language identifier (uses Microsoft lang ids)
828 # 1033 means US-English (omitted => use UI's language)
829 .","
830 # Token 7: QuoteAllTextCells
831 # *** true will "quote" even single-bareword cells, which looks
832 # *** bad and makes t/ tests messier, but preserves information
833 # *** that such cells were not numbers or dates, etc. This ensures
834 # *** that Zip codes, etc. with leading zeroes won't be corrupted
835 # *** if converted back into a spreadsheet
836 # Option #1: Specify true to quote all cells on export, then post-process
837 # the result to un-quote obviously safe cells (for yet more overhead).
838 # Option #2: Specify false, and assume the resulting CSV will never
839 # be imported into a spreadsheet except via us, and we pre-scan
840 # the data to generate {col_formats} so will usually be safe.
841 # 5/30/23: Switching to Option #2...
842 .",false"
843 # Token 8: on output: true to store number as numbers; false to
844 # store number cells as text. No UI equivalent.
845 .",true" # default: documented as true (for export) BUT IS NOT!
846 # Token 9: "Save cell contents as shown"
847 # Generally we DO NOT want this because things like dates
848 # can be formatted many different ways.
849 ##.",".($opts->{raw_values} ? "false" : "true")
850 .",false"
851 # Token 10: "Export cell formulas"
852 .",false"
853 # Token 11: not used for export
854 .","
855 # Token 12: (LO 7.2+) sheet selections:
856 # 0 or absent => the "first" sheet
857 # 1-N => the Nth sheet (arrgh, can not specify name!!)
858 # -1 => export all sheets to files named filebasenamne.Sheetname.csv
859 .",".($opts->{allsheets} ? -1 :
860 0 ▶ $opts->{sheetname} ? die("add named-sheet support here") :
Branch: 3 branches
BranchTrueFalseTotal
$$opts{'cvt_to'} eq 'txt' ? : 0 0 0
$$opts{'allsheets'} ? : 0 0 0
$$opts{'sheetname'} ? : 0 0 0
Condition0%0/4
$$opts{"sep_char"} // ","
l!l
00
$$opts{"quote_char"} // "\""
l!l
00
MC/DC0%0/1
$$opts{"sep_char"} // ","
$$opts{"sep_char"}
MC/DC0%0/1
$$opts{"quote_char"} // "\""
$$opts{"quote_char"}
Truth table0%0/2
A
A= $$opts{"sep_char"}
Aresult
11
00
Truth table0%0/2
A
A= $$opts{"quote_char"}
Aresult
11
00
861 0)
862 # Token 13: Not used for export
863 .","
864 # Token 14: true to include BOM in the result
865 #.","
866 }
867 else {
868 undef
869 0 }
870 };
871  
872 # We can only control the output directory path, not the name of
873 # an individual result file. If $dst is a directory then the result
874 # could theoretically output into it directly, but instead we always
875 # output to an ephemeral temp directory and then move the results to $dst
876 #
877 # With 'allsheets' the resulting files must be renamed to conform to our
878 # external API (namely SHEETNAME.csv).
879 #
880 # ERROR DETECTION: As of LO 7.5 we always get zero exit status and the
881 # only way to detect errors is to notice that no files were written.
882 # https://bugs.documentfoundation.org/show_bug.cgi?id=155415
883 #
884 0 my $tdir = Path::Tiny->tempdir(path($dst)->basename."_cvt-to_XXXXX");
885 # will be deleted when $tdir goes out of scope
886  
887 my @cmd = ($prog,
888 "--headless", "--invisible",
889 #"--nolockcheck",
890 "--norestore",
891 "--view", # open read-only in case can't create lockfile
892 $ifilter ? ("--infilter=$ifilter") : (),
893 "--convert-to",
894 0 ▶ $opts->{cvt_to}.($ofilter ? ":$ofilter" : ""),
Branch: 2 branches
BranchTrueFalseTotal
$ifilter ? : 0 0 0
$ofilter ? : 0 0 0
895 "--outdir", $tdir->canonpath,
896 path($src)->canonpath);
897  
898 0 ▶ unless ($debug) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($debug) 0 0 0
899 0 $opts->{suppress_stdout} = 1;
900 #$opts->{suppress_stderr} = 1;
901 }
902  
903 0 my $cmdstatus = _runcmd($opts, @cmd);
904  
905 0 ▶ if ($cmdstatus != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmdstatus != 0) 0 0 0
906 # This should never happen, see ERROR DETECTION above
907 confess sprintf("($$) UNEXPECTED FAILURE, wstat=0x%04x\n",$cmdstatus),
908 " converting '$opts->{inpath}' to $opts->{cvt_to}\n",
909 0 " Command was: ",join(" ",map{qsh} @cmd);
910 }
911  
912 0 my @result_files = path($tdir)->children;
913 0 ▶ btw dvis '>> @result_files' if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
914 0 ▶ if (@result_files == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@result_files == 0) 0 0 0
915 #####
916 #sleep(2);
917 #my @rf = path($tdir)->children;
918 #croak "!?!? initially no output files, but now find @rf !?!?" if @rf;
919 #####
920  
921 0 ▶ croak qsh($src)." is missing or unreadable\n", "cmd: ",qshlist(@cmd),"\n"
Branch: 1 branch
BranchTrueFalseTotal
unless -r $src 0 0 0
922 unless -r $src;
923 croak "Something went wrong, ",path($prog)->basename," produced no output\n",
924 0 "cmd: ", (map{" $_=".qsh($ENV{$_})}
925 0 grep {/User|LIBRE/i} keys %ENV)," ", qshlist(@cmd), "\n",
926 0 "opts: ${\vis($opts)}\n",
927 }
928  
929 0 ▶ if ($opts->{allsheets}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$opts{"allsheets"}) 0 0 0
930 # Rename files to match our API (omit the spreadsheetbasename- prefix)
931 0 foreach (@result_files) {
932 0 my $dir = $_->parent; # Like dirname but including Volume:
933 0 my $base = $_->basename;
934 0 ▶ (my $newbase = $base) =~ s/^\Q$opts->{ifbase}\E-// or oops dvis '$base $opts';
Branch: 1 branch
BranchTrueFalseTotal
(my $newbase = $base) =~ s/^\Q$$opts{"ifbase"}\E-// or &oops(&dvis("\$base \$opts")) 0 0 0
935 0 my $newpath = $dir->child($newbase)->canonpath;
936 0 my $oldpath = path($_)->canonpath;
937 0 ▶ btw ">> Renaming $oldpath -> $newbase\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
938 0 ▶ rename ($oldpath, $newpath) or oops "$!";
Branch: 1 branch
BranchTrueFalseTotal
rename $oldpath, $newpath or &oops("$!") 0 0 0
939 0 $_ = $newpath; # update @result_files
940 }
941 }
942  
943 # Move the results to $dst
944 0 ▶ if (-e $dst) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $dst) { } 0 0 0
945 0 ▶ croak "$dst must be a directory if it pre-exists\n" unless -d $dst;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dst 0 0 0
946 0 ▶ btw ">> Moving results -> $dst\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
947 0 foreach (@result_files) {
948 0 ▶ btw ">>> move $_ -> $dst" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
949 0 File::Copy::move($_, $dst)
950 }
951 0 ▶ btw ">> Now $dst contains: ",avis($dst->children) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
952 } else {
953 0 ▶ if ($opts->{allsheets}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$opts{'allsheets'}) { } 0 0 0
954 0 ▶ btw ">> dirmove $tdir -> $dst\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
955 # BUG HERE? Do we _know_ the auto-remove mechanism uses only the
956 # pathname and won't remove the directory even after being renamed?
957 # (we could copy the directory and contents, but would be slower...)
958 0 ▶ rename($tdir, $dst) or File::Copy::Recursive::dirmove($tdir, $dst);
Branch: 1 branch
BranchTrueFalseTotal
rename $tdir, $dst or File::Copy::Recursive::dirmove($tdir, $dst) 0 0 0
959 } else {
960 0 ▶ croak "Expecting only one result file, not @result_files"
Branch: 1 branch
BranchTrueFalseTotal
if @result_files > 1 0 0 0
961 if @result_files > 1;
962 0 ▶ btw ">> move $result_files[0] -> $dst\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
963 0 File::Copy::move($result_files[0], $dst);
964 }
965 }
966 }#_convert_using_openlibre
967  
968 0 ▶sub _convert_using_ssconvert($$$) {
Subroutine _convert_using_ssconvert: not called CC=1 SCAR=6.9
969 0 my ($opts, $src, $dst) = @_;
970 0 confess "Deprecated with extreme prejudice"; # no longer supported
971 ##
972 ## foreach (qw/inpath cvt_to /)
973 ## { oops "missing opts->{$_}" unless exists $opts->{$_} }
974 ##
975 ## my $eff_outpath = $opts->{outpath};
976 ## if (my $prog=which("ssconvert")) {
977 ## my $enc = _get_encodings_from_opts($opts);
978 ## $enc //= "UTF-8"; # default
979 ## my @options;
980 ## if ($opts->{cvt_to} eq "csv") {
981 ## push @options, '--export-type=Gnumeric_stf:stf_assistant';
982 ## my @dashO_terms = ("format=preserve", "transliterate-mode=escape");
983 ## push @dashO_terms, "charset='${enc}'" if defined($enc);
984 ## if ($opts->{sheetname}) {
985 ## push @dashO_terms, "sheet='$opts->{sheetname}'";
986 ## }
987 ## if ($opts->{allsheets}) {
988 ## #If both {allsheets} and {sheetname} are specified, only a single
989 ## # .csv file will be in the output directory
990 ## croak "'allsheets' option: 'outpath' must specify an existing directory"
991 ## unless -d $eff_outpath;
992 ## $eff_outpath = catfile($eff_outpath, "%s.csv");
993 ## push @options, "--export-file-per-sheet";
994 ## }
995 ## elsif ($opts->{sheetname}) {
996 ## # handled above
997 ## }
998 ## else {
999 ## # A backwards-incompatible change to ssconvert stopped extracting
1000 ## # the "current" sheet by default; now all sheets are concatenated!
1001 ## # See https://gitlab.gnome.org/GNOME/gnumeric/issues/461
1002 ## # ssconvert verison 1.12.45 supports a new "-O active-sheet=y" option
1003 ## ## PORTABILITY BUG: Redirection syntax will not work on windows
1004 ## my ($ssver) = (qx/ssconvert --version 2>&1/ =~ /ssconvert version '?(\d[\d\.]*)/);
1005 ## if (version::is_lax($ssver) && version->parse($ssver) >= v1.12.45) {
1006 ## push @dashO_terms, "active-sheet=y";
1007 ## } else {
1008 ## croak("Due to an ssconvert bug, a sheetname must be given.\n",
1009 ## "(for more information, see comment at ",__FILE__,
1010 ## " near line ", (__LINE__-10), ")\n");
1011 ## }
1012 ## }
1013 ## push @options, '-O', join(" ",@dashO_terms);
1014 ## }
1015 ## elsif ($opts->{cvt_to} eq 'xlsx') {
1016 ## @options = ('--export-type=Gnumeric_Excel:xlsx2');
1017 ## }
1018 ## elsif ($opts->{cvt_to} eq 'xls') {
1019 ## @options = ('--export-type=Gnumeric_Excel:excel_biff8'); # M'soft Excel 97/2000/XP
1020 ## }
1021 ## elsif ($opts->{cvt_to} =~ /^od/) {
1022 ## @options = ('--export-type=Gnumeric_OpenCalc:odf');
1023 ## }
1024 ## elsif ($eff_outpath =~ /\.[a-z]{3,4}$/) {
1025 ## # let ssconvert choose based on the output file suffix
1026 ## }
1027 ## else {
1028 ## croak "unrecognized cvt_to='".u($opts->{cvt_to})."' and no outpath suffix";
1029 ## }
1030 ##
1031 ## my $eff_inpath = $opts->{inpath};
1032 ## if ($opts->{sheetname} && $opts->{inpath} =~ /.csv$/i) {
1033 ## # Control generated sheet name by using a symlink to the input file
1034 ## # See http://stackoverflow.com/questions/22550050/how-to-convert-csv-to-xls-with-ssconvert
1035 ## my $td = catdir($proctempdir // oops, "Gnumeric");
1036 ## remove_tree($td); mkdir($td) or die $!;
1037 ## $eff_inpath = catfile($td, $opts->{sheetname});
1038 ## symlink $opts->{inpath}, $eff_inpath or die $!;
1039 ## fixme: handle unimplmented or no-perms symlink failures
1040 ## }
1041 ## my @cmd = ($prog, @options, $eff_inpath, $eff_outpath);
1042 ##
1043 ## my $suppress_stderr = !$opts->{debug};
1044 ## if (0 != _runcmd({%$opts, suppress_stderr => $suppress_stderr}, @cmd)) {
1045 ## # Before showing a complicated ssconvert failure with backtrace,
1046 ## # check to see if the problem is just a non-existent input file
1047 ## { open my $dummy_fh, "<", $eff_inpath or croak "$eff_inpath : $!"; }
1048 ## my $failmsg = "($$) Conversion of '$opts->{inpath}' to $eff_outpath failed\n"."cmd: ".qshlist(@cmd)."\n";
1049 ## if ($suppress_stderr) { # repeat showing all output
1050 ## if (0 == _runcmd({%$opts, suppress_stderr => 0}, @cmd)) {
1051 ## warn "Surprise! Command failed the first time but succeeded on 2nd try!\n";
1052 ## }
1053 ## croak $failmsg;
1054 ## }
1055 ## }
1056 ## elsif (! -e $opts->{outpath}) {
1057 ## croak "($$) Conversion SILENTLY failed\n(using $prog)\n",
1058 ## " cmd: ",qshlist(@cmd),"\n"
1059 ## ;
1060 ## }
1061 ## return ($enc)
1062 ## }
1063 ## else {
1064 ## croak "Can not find ssconvert to convert '$opts->{inpath}' to $opts->{cvt_to}\n",
1065 ## "To install ssconvert: sudo apt-get install gnumeric\n";
1066 ## }
1067 }
1068  
1069 # Extracts |||SHEETNAME or !SHEETNAME or [SHEETNAME] from a path+sheet
1070 # specification, if present.
1071 # (Lots of historical compatibility issues...)
1072 # In scalar context, returns SHEETNAME or undef.
1073 # INTERNAL USE ONLY: In array context, returns (filepath, SHEETNAME or undef)
1074 273 ▶sub sheetname_from_spec($) {
Subroutine sheetname_from_spec: called CC=4 SCAR=14.0
Pod documented
1075 273 my $spec = shift;
1076 273 ▶ return undef if $spec eq ""; #caller will hopefully diag bogus path
Branch: 1 branch
BranchTrueFalseTotal
if $spec eq "" 0 273 273
1077 273 local $_;
1078 273 my $p = path($spec);
1079 273 my $parent = $p->parent;
1080 273 my ($base,$sn) = ($p->basename =~ /^(.*) (?| \|\|\|([^\!\[\|]+)$
1081 | \!([^\!\[\|]+)$
1082 | \[([^\[\]]+)\]$
1083 )/x);
1084 273 ▶ wantarray ? ($parent->child($base//$p->basename)->stringify, $sn) : $sn
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 206 67 273
Condition66%2/3
$base // $p->basename
l!l&&r!l&&!r
22040
MC/DC0%0/2
$base // $p->basename
$base $p->basename
Truth table66%2/3
A // B
A= $baseB= $p->basename
ABresult
1X1
011
000
1085 }
1086 67 ▶sub filepath_from_spec($) {
Subroutine filepath_from_spec: called CC=1 SCAR=0.0
Pod documented
1087 67 my ($path, undef) = sheetname_from_spec($_[0]);
1088 67 $path
1089 }
1090 #Tester
1091 #foreach ("", "/a!b/c", "/a!b/c!sheet1", "/a/b/c[sheet2]", "/a/b/c[bozo]d.xls",
1092 # ) {
1093 # foreach($_, basename($_)) {
1094 # my ($fp,$sn) = sheetname_from_spec($_);
1095 # use open ':std', ':locale';
1096 # warn ivis '# $_ → $fp $sn\n';
1097 # my $sn2 = sheetname_from_spec($_);
1098 # die "bug" unless u($sn) eq u($sn2);
1099 # }
1100 #}
1101 #die "TEX";
1102  
1103 # Construct a file + sheetname spec in the preferred form for humans to read
1104 # If sheetname is undef, just return the file path
1105 67 ▶sub form_spec_with_sheetname($$) {
Subroutine form_spec_with_sheetname: called CC=6 SCAR=21.6
Pod documented
1106 67 my ($filespec, $sheetname) = @_;
1107 67 my $embedded_sheetname = sheetname_from_spec($filespec);
1108 67 ▶ croak "conflicting embedded and separate sheetnames given"
Branch: 1 branch
BranchTrueFalseTotal
if $embedded_sheetname and $sheetname and $embedded_sheetname ne $sheetname 0 67 67
Condition33%2/6
$embedded_sheetname and $sheetname and $embedded_sheetname ne $sheetname
!ll&&!rl&&r
6700
$embedded_sheetname and $sheetname
!ll&&!rl&&r
6700
MC/DC0%0/3
$embedded_sheetname and $sheetname and $embedded_sheetname ne $sheetname
$embedded_sheetname $sheetname $embedded_sheetname ne $sheetname
Truth table25%1/4
A and B and C
A= $embedded_sheetnameB= $sheetnameC= $embedded_sheetname ne $sheetname
ABCresult
0XX0
10X0
1100
1111
1109 if $embedded_sheetname && $sheetname && $embedded_sheetname ne $sheetname;
1110 67 ▶ $sheetname //= $embedded_sheetname;
Condition33%1/3
$sheetname //= $embedded_sheetname
l!l&&r!l&&!r
0067
MC/DC0%0/2
$sheetname //= $embedded_sheetname
$sheetname $embedded_sheetname
Truth table33%1/3
A //= B
A= $sheetnameB= $embedded_sheetname
ABresult
1X1
011
000
1111 67 my $filepath = filepath_from_spec($filespec);
1112 67 ▶ $sheetname ? "${filepath}[${sheetname}]" : $filepath
Branch: 1 branch
BranchTrueFalseTotal
$sheetname ? : 0 67 67
1113 #$sheetname ? "${filepath}|||${sheetname}" : $filepath
1114 }
1115  
1116 11 our $default_output_binmode= ":raw:encoding(UTF-8):crlf";
1117 11 our $default_input_encodings = "UTF-8,windows-1252,UTF-16BE,UTF-16LE";
1118  
1119 # Return digested %opts setting
1120 # sheetname, inpath_sans_sheet (as Path::Tiny), encoding or default
1121 133 ▶sub _process_args($;@) {
Subroutine _process_args: called CC=22 SCAR=37.3
1122 133 ▶ confess "fix obsolete call to pass linearized options"
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] eq "HASH" 0 133 133
1123 if ref($_[0]) eq "HASH";
1124 133 ▶ my $leading_inpath = ( scalar(@_) % 2 == 1 ? shift(@_) : undef );
Branch: 1 branch
BranchTrueFalseTotal
scalar(@_) % 2 == 1 ? : 93 40 133
1125 133 my %opts = (
1126 cvt_from => "",
1127 cvt_to => "",
1128 @_,
1129 );
1130 133 ▶ if (defined $opts{inpath}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{'inpath'}) { } 40 93 133
1131 40 ▶ croak "Initial INPATH arg specified as well as inpath => ... in options"
Branch: 1 branch
BranchTrueFalseTotal
if defined $leading_inpath 0 40 40
1132 if defined $leading_inpath;
1133 } else {
1134 93 ▶ $opts{inpath} = $leading_inpath // croak "No inpath was specified";
Condition50%1/2
$leading_inpath // croak("No inpath was specified")
l!l
930
MC/DC0%0/1
$leading_inpath // croak("No inpath was specified")
$leading_inpath
Truth table50%1/2
A
A= $leading_inpath
Aresult
11
00
1135 }
1136 133 ▶ croak "Input path is \"\"\n" if $opts{inpath} eq "";
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"inpath"} eq "" 0 133 133
1137 133 ▶ $opts{verbose}=1 if $opts{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"debug"} 0 133 133
1138  
1139 # inpath or outpath may have "!sheetname" appended (or alternate syntaxes),
1140 # but may exist only if a separate 'sheetname' option is not specified.
1141 # Input and output can not both be spreadsheets; one must be a CSV.
1142 133 ▶ if (exists($opts{sheet})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{"sheet"}) 0 133 133
1143 0 carp "WARNING: Deprecated 'sheet' option found (use 'sheetname' instead)\n";
1144 0 ▶ croak "Both {sheet} and {sheetname} specified" if exists $opts{sheetname};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts{"sheetname"} 0 0 0
1145 0 $opts{sheetname} = delete $opts{sheet};
1146 }
1147 133 { my ($path_sans_sheet, $sheetname, $key);
1148 133 for my $thiskey ('inpath', 'outpath') {
1149 266 ▶ my $spec = $opts{$thiskey} || next;
Condition100%2/2
$opts{$thiskey} || (next)
l!l
139127
MC/DC100%1/1
$opts{$thiskey} || (next)
$opts{$thiskey}
Truth table100%2/2
A
A= $opts{$thiskey}
Aresult
11
00
1150 139 my ($pssn, $sn) = sheetname_from_spec($spec);
1151 139 ▶ if (defined $sn) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sn) 2 137 139
1152 2 ▶ croak "A sheetname is embeeded in both ",
Branch: 1 branch
BranchTrueFalseTotal
if $sheetname 0 2 2
1153 "'$thiskey' ($opts{$thiskey}) and '$key' ($opts{$key})\n"
1154 if $sheetname;
1155 2 ($path_sans_sheet, $sheetname, $key) = ($pssn, $sn, $thiskey);
1156 }
1157 }
1158 133 ▶ if ($opts{sheetname}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($opts{'sheetname'}) { } 2 131 133
elsif (defined $sheetname) { } 0 131 131
1159 croak "'sheetname' option conflicts with embedded sheet name\n",
1160 " sheetname => ", qsh($opts{sheetname}),"\n",
1161 " $key => ", qsh($opts{$key}),"\n"
1162 2 ▶ if defined($sheetname) && $sheetname ne $opts{sheetname};
Branch: 1 branch
BranchTrueFalseTotal
if defined $sheetname and $sheetname ne $opts{"sheetname"} 2 0 2
Condition33%1/3
defined $sheetname and $sheetname ne $opts{"sheetname"}
!ll&&!rl&&r
002
MC/DC0%0/2
defined $sheetname and $sheetname ne $opts{"sheetname"}
defined $sheetname $sheetname ne $opts{"sheetname"}
Truth table33%1/3
A and B
A= defined $sheetnameB= $sheetname ne $opts{"sheetname"}
ABresult
0X0
100
111
1163 }
1164 elsif (defined $sheetname) {
1165 btw "(extracted sheet name \"$sheetname\" from $key)\n"
1166 0 ▶ if $opts{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"verbose"} 0 0 0
1167 0 $opts{sheetname} = $sheetname;
1168 }
1169 $opts{inpath_sans_sheet} = path(
1170 ($key && $key eq 'inpath') ? $path_sans_sheet : $opts{inpath}
1171 131 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
$key && $key eq 'inpath' ? : 0 131 131
Condition33%1/3
$key and $key eq "inpath"
!ll&&!rl&&r
13100
MC/DC0%0/2
$key and $key eq "inpath"
$key $key eq "inpath"
Truth table33%1/3
A and B
A= $keyB= $key eq "inpath"
ABresult
0X0
100
111
1172 croak qsh($opts{inpath_sans_sheet})," does not exist!\n"
1173 131 ▶ unless $opts{inpath_sans_sheet}->exists;
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"inpath_sans_sheet"}->exists 0 131 131
1174 }
1175 # Input file basename sans any .suffix
1176 131 $opts{ifbase} = $opts{inpath_sans_sheet}->basename(qr/\.[^.]+/);
1177  
1178 131 %opts
1179 }#_process_args
1180  
1181 121 ▶sub _binmode_slurp_and_log($$$) { # *without trying to seek*
Subroutine _binmode_slurp_and_log: called CC=4 SCAR=15.2
1182 121 my ($fh, $ref2octets, $debug) = @_;
1183 121 binmode($fh);
1184 121 local $/ = undef;
1185 #$$ref2octets = <$fh>//""; # Now known to not have a BOM. <<< IS THIS REALLY TRUE???
1186 121 $$ref2octets = <$fh>;
1187 121 ▶ btwN \3,"Raw slurp-from-0 fh=$fh: ",(defined($$ref2octets) ? visO(substr($$ref2octets,0,300)) : "*undef*")
Branch: 2 branches
BranchTrueFalseTotal
if $debug 0 121 121
defined $$ref2octets ? : 0 0 0
1188 if $debug;
1189 121 ▶ $$ref2octets //= "";
Condition50%1/2
$$ref2octets //= ""
l!l
1210
MC/DC0%0/1
$$ref2octets //= ""
$$ref2octets
Truth table50%1/2
A
A= $$ref2octets
Aresult
11
00
1190 }
1191 151 ▶sub _slurp_ifnotslurped($$$) {
Subroutine _slurp_ifnotslurped: called CC=3 SCAR=11.0
1192 151 my ($fh, $ref2octets, $debug) = @_;
1193 151 ▶ return if defined $$ref2octets;
Branch: 1 branch
BranchTrueFalseTotal
if defined $$ref2octets 30 121 151
1194 121 ▶ seek($fh, 0, SEEK_SET) or die $!;
Branch: 1 branch
BranchTrueFalseTotal
seek $fh, 0, 0 or die $! 121 0 121
1195 121 _binmode_slurp_and_log($fh, $ref2octets, $debug);
1196 }
1197 157 ▶sub _decode_slurped_data($$$;$) {
Subroutine _decode_slurped_data: called CC=4 SCAR=13.9
1198 157 my ($enc, $ref2octets, $start_pos, $check) = @_;
1199 157 ▶ oops unless defined $$ref2octets;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$ref2octets 0 157 157
1200 # More attempts to find cause of mysterious failures on Windows smokers...
1201 157 my $chars = eval {
1202 157 ▶ decode($enc, substr($$ref2octets, $start_pos),
Condition100%2/2
$check // 9
l!l
56101
MC/DC100%1/1
$check // 9
$check
Truth table100%2/2
A
A= $check
Aresult
11
00
1203 $check // (Encode::FB_CROAK|Encode::LEAVE_SRC) )
1204 };
1205 157 ▶ die($@,visnew->Useqq(0)->dvis('\n$enc $start_pos $ref2octets')) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 6 151 157
1206 151 $chars
1207 }
1208  
1209  
1210 # Detect cvt_from and cvt_to from filename suffixes or by peeking at the data.
1211 # If input is CSV, detect encoding (removing a BOM if present),
1212 # detect separator and quote characters, and generate default
1213 # {col_formats} which, e.g. reads items with leading zeroes
1214 # (e.g. Zip codes) as character data and not numbers.
1215 #
1216 # Unless the input is usable as-is it will be slurped into memory and the
1217 # buffer returned by reference at $$ref2octets. If slurped data is present
1218 # the caller should use that instead of reading the original input;
1219 # either way {input_encoding} should be used to decode.
1220 #
1221 # (If $$ref2octets was already defined it is assumed to contain
1222 # previously-slurped data which is used instead of the the input file.)
1223 #
1224 131 ▶sub _preprocess($$) {
Subroutine _preprocess: called CC=28 SCAR=43.9
1225 131 my ($opts, $ref2octets) = @_;
1226 131 my $debug = $opts->{debug};
1227 131 my ($fh, $start_pos);
1228 # Skip to ==BODY== below
1229  
1230 # my sub _dump_fh($) {
1231 # return unless $debug;
1232 # my $tag = shift;
1233 # confess dvis "($tag) fh is not open \$start_pos" unless $fh;
1234 # my @layers = PerlIO::get_layers($fh);
1235 # my $cur_pos = tell($fh);
1236 # if (! seek($fh, $start_pos//0, SEEK_SET)) {
1237 # btwN 1,dvis "($tag) **NOT SEEKABLE** \$cur_pos \@layers \$start_pos";
1238 # } else {
1239 # if ((my $n = read $fh, my $buffer, 128) != 0) {
1240 # btwN 1,dvis "($tag) \$cur_pos \$start_pos \@layers First $n items are:\n", Hexify($buffer), "\n";
1241 # } else {
1242 # btwN 1,dvis "($tag) \$cur_pos \$start_pos \@layers **APPEARS EMPTY**";
1243 # }
1244 # seek($fh, $cur_pos, SEEK_SET) or die "re-seek to $cur_pos: $!";
1245 # }
1246 # }
1247 131 ▶ my sub set_fh_encoding() { # returns true if set
Subroutine set_fh_encoding: called CC=3 SCAR=12.7
1248 131 my $enc = $opts->{input_encoding}; # must already be resolved!
1249 131 my $bmode = ":raw:encoding($enc):crlf";
1250 131 ▶ binmode $fh, $bmode or die "binmode '$bmode' : $!";
Branch: 1 branch
BranchTrueFalseTotal
binmode $fh, $bmode or die "binmode '${bmode}' : $!" 131 0 131
1251 131 ▶ if ($debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 131 131
1252 0 my @layers = PerlIO::get_layers($fh);
1253 0 warn dvis 'set_fh_encoding: $fh @layers\n';
1254 }
1255 }
1256 131 ▶ my sub open_input() {
Subroutine open_input: called CC=12 SCAR=32.0
1257 131 ▶ oops if defined $fh;
Branch: 1 branch
BranchTrueFalseTotal
if defined $fh 0 131 131
1258 131 ▶ if (defined $$ref2octets) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $$ref2octets) { } 0 131 131
1259 0 ▶ open $fh, "<:raw", $ref2octets or confess "BUG:in-mem open:$!";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<:raw", $ref2octets or confess("BUG:in-mem open:$!") 0 0 0
1260 #_dump_fh("open_input TO PREVIOUSLY SLURPED");
1261 } else {
1262 131 my $path = $opts->{inpath_sans_sheet};
1263 131 $fh = openhandle($path); # undef unless $path is a file handle
1264 131 ▶ unless ($fh) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh) 131 0 131
1265 131 ▶ open $fh, "<", $path or die "$path : $!";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<", $path or die "$path : $!" 131 0 131
1266 }
1267 131 binmode($fh);
1268 #_dump_fh("AAA $path open_input raw");
1269 }
1270 131 ▶ if (! seek($fh, 0, SEEK_SET)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (seek $fh, 0, 0) 0 131 131
1271 0 ▶ oops if defined $$ref2octets;
Branch: 1 branch
BranchTrueFalseTotal
if defined $$ref2octets 0 0 0
1272 0 _binmode_slurp_and_log($fh, $ref2octets, $debug);
1273 0 close $fh;
1274 0 $fh = undef;
1275 0 ▶ open $fh, "<:raw", $ref2octets or confess "BUG:in-mem open:$!";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<:raw", $ref2octets or confess("BUG:in-mem open:$!") 0 0 0
1276 #_dump_fh("BBB unseekable, slurped");
1277 }
1278 131 my $bomenc = File::BOM::get_encoding_from_filehandle($fh);
1279 131 $start_pos = tell($fh);
1280 131 ▶ if ($bomenc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bomenc) 19 112 131
1281 19 ▶ btw dvis 'Input has BOM, $bomenc $start_pos' if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 19 19
1282 19 $opts->{input_encoding} = $bomenc;
1283 19 binmode($fh); # unnecessary???
1284 19 ▶ binmode($fh, ":raw:encoding($bomenc):crlf") or die "binmode: $!";
Branch: 1 branch
BranchTrueFalseTotal
binmode $fh, ":raw:encoding($bomenc):crlf" or die "binmode: $!" 19 0 19
1285 }
1286 #_dump_fh("CCC final");
1287 }
1288 131 ▶ my sub determine_input_encoding() {
Subroutine determine_input_encoding: called CC=8 SCAR=21.0
1289 # If one encoding was specified by the user or implied by a BOM, use it;
1290 # otherwise try multiple encodings specified by the user or defaulted
1291 # until one seems to work.
1292 131 ▶ $opts->{input_encoding} //= $default_input_encodings;
Condition66%2/3
$$opts{"input_encoding"} //= $Spreadsheet::Edit::IO::default_input_encodings
l!l&&r!l&&!r
60710
MC/DC0%0/2
$$opts{"input_encoding"} //= $Spreadsheet::Edit::IO::default_input_encodings
$$opts{"input_encoding"} $Spreadsheet::Edit::IO::default_input_encodings
Truth table66%2/3
A //= B
A= $$opts{"input_encoding"}B= $Spreadsheet::Edit::IO::default_input_encodings
ABresult
1X1
011
000
1293 131 my @enclist = split m#,#, $opts->{input_encoding};
1294 return
1295 131 ▶ if @enclist == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @enclist == 1 36 95 131
1296 95 _slurp_ifnotslurped($fh, $ref2octets, $debug);
1297 95 for my $enc (@enclist) {
1298 101 eval { _decode_slurped_data($enc, $ref2octets, $start_pos) };
1299  
1300 101 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 6 95 101
1301 6 ▶ btw "Input encoding '$enc' did not work...($@)\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 6 6
1302 6 next;
1303 }
1304 95 ▶ btw "Input encoding '$enc' seems to work.\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 95 95
1305 95 @enclist = ($enc);
1306 last
1307 95 }
1308 95 ▶ confess "Could not detect encoding of $opts->{inpath_sans_sheet}\n"
Branch: 1 branch
BranchTrueFalseTotal
if @enclist > 1 0 95 95
1309 if @enclist > 1;
1310 95 $opts->{input_encoding} = $enclist[0];
1311 } #determine_input_encoding
1312  
1313 131 ▶ my sub readparse_csv(@) {
Subroutine readparse_csv: called CC=6 SCAR=18.6
1314 my %csvopts = (
1315 @sane_CSV_read_options,
1316 defined($opts->{quote_char}) ? (quote_char=>$opts->{quote_char}) : (),
1317 131 ▶ defined($opts->{sep_char}) ? (sep_char=>$opts->{sep_char}) : (),
Branch: 2 branches
BranchTrueFalseTotal
defined $$opts{'quote_char'} ? : 0 131 131
defined $$opts{'sep_char'} ? : 0 131 131
1318 auto_diag => 2, # throw on error
1319 @_
1320 );
1321 131 $csvopts{escape_char} = $csvopts{quote_char}; # must always be the same
1322  
1323 131 ▶ my $csv = Text::CSV->new (\%csvopts)
Branch: 1 branch
BranchTrueFalseTotal
my $csv = "Text::CSV"->new(\%csvopts) or croak("Text::CSV->new: ", "Text::CSV"->error_diag, &dvis("\\n## %csvopts\\n")) 131 0 131
1324 or croak "Text::CSV->new: ", Text::CSV->error_diag(),
1325 dvis('\n## %csvopts\n');
1326 131 ▶ seek($fh, $start_pos, SEEK_SET) or die $!; # skip over possible BOM
Branch: 1 branch
BranchTrueFalseTotal
seek $fh, $start_pos, 0 or die $! 131 0 131
1327 131 my $rows;
1328 131 ▶ while (my $F = $csv->getline( $fh )) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $F = $csv->getline($fh)) 698 131 829
1329 698 push(@$rows, $F);
1330 }
1331 $rows
1332 131 }
1333  
1334 84 ▶ my sub determine_csv_q_sep($) {
Subroutine determine_csv_q_sep: called CC=20 SCAR=41.2
1335 84 my ($r2rows) = @_;
1336 return
1337 84 ▶ if defined($opts->{quote_char}) && defined($opts->{sep_char});
Branch: 1 branch
BranchTrueFalseTotal
if defined $$opts{"quote_char"} and defined $$opts{"sep_char"} 0 84 84
Condition33%1/3
defined $$opts{"quote_char"} and defined $$opts{"sep_char"}
!ll&&!rl&&r
8400
MC/DC0%0/2
defined $$opts{"quote_char"} and defined $$opts{"sep_char"}
defined $$opts{"quote_char"} defined $$opts{"sep_char"}
Truth table33%1/3
A and B
A= defined $$opts{"quote_char"}B= defined $$opts{"sep_char"}
ABresult
0X0
100
111
1338  
1339 # Try combinations starting with the most-common '"' and ',' while
1340 # parsing the file for unsafe unquoted values (throws on syntax error).
1341 # The expectation is that the first try usually succeeds
1342 Q:
1343 84 ▶ for my $q (defined($opts->{quote_char})
Branch: 1 branch
BranchTrueFalseTotal
defined $$opts{'quote_char'} ? : 0 84 84
1344 ? ($opts->{quote_char}) : ("\"", "'")) {
1345 84 my $found_q;
1346 SEP:
1347 84 ▶ for my $sep (defined($opts->{sep_char})
Branch: 1 branch
BranchTrueFalseTotal
defined $$opts{'sep_char'} ? : 0 84 84
1348 ? ($opts->{sep_char}) : (",","\t")) {
1349 84 ▶ btw dvisq '--- TRYING $q $sep ---' if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 84 84
1350  
1351 # # Preliminary check for an illegal use of the quote char
1352 # if (defined($chars)
1353 # && $chars =~ /[^${q}${sep}\x{0D}\x{0A}]
1354 # ${q}
1355 # (?=[^${q}${sep}\x{0D}\x{0A}] | \z)/gx) {
1356 # btw ivis '>>>quote_char CAN NOT BE $q with sep=$sep because q exists mid-field before pos ${\(pos($chars))}'
1357 # if $debug;
1358 # next SEP
1359 # }
1360 84 $$r2rows = eval{ readparse_csv(quote_char=>$q, sep_char=>$sep) };
1361 84 ▶ if ($@ eq "") {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ eq "") 84 0 84
1362 84 ▶ warn ivis '>> Detected quote_char=$q sep_char=$sep\n' if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 84 84
1363 84 $opts->{quote_char} = $q;
1364 84 $opts->{sep_char} = $sep;
1365 84 last Q;
1366 }
1367 0 ▶ warn dvis('$q sep=$sep did not work...\n'),vis($@),"\n"
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
1368 if $debug;
1369 0 ▶ _dump_fd("$q sep=$sep did not work") if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
1370 0 $$r2rows = undef;
1371 }
1372 }
1373 84 ▶ unless (defined($$r2rows)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $$r2rows) { } 42 42 84
1374 #confess "Input file is not valid CSV (or we have a bug)\n"
1375 42 ▶ seek($fh, $start_pos, SEEK_SET) or die $!; # skip over possible BOM
Branch: 1 branch
BranchTrueFalseTotal
seek $fh, $start_pos, 0 or die $! 42 0 42
1376 42 my $n = read($fh, my $somechars, 100);
1377 42 ▶ croak "ERROR READING input: $!" unless defined($n);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n 0 42 42
1378 42 ▶ if ($n == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n == 0) { } 42 0 42
1379 42 ▶ warn "File has NO CONTENT. Treating it like an (empty) CSV\n"
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 42 42
1380 if $debug;
1381 42 $$r2rows = [];
1382 } else {
1383 0 my @layers = PerlIO::get_layers($fh);
1384 0 warn dvis '$start_pos @layers $somechars\n';
1385 0 ▶ if (open my $fh99, "<:raw", $opts->{inpath}) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh99, "<:raw", $$opts{"inpath"}) 0 0 0
1386 0 ▶ if ((my $nb = read $fh99, my $octets, 128) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $nb = read($fh99, my $octets, 128)) != 0) { } 0 0 0
1387 0 warn "First $nb octets are as follows:\n", Hexify($octets), "\n";
1388 } else {
1389 0 oops
1390 }
1391 }
1392 0 confess "Input file is not valid CSV (or we have a bug)\n"
1393 }
1394 }
1395 else {
1396 42 ▶ warn dvis '#### CSV DETECTED $opts->{quote_char} $opts->{sep_char} $start_pos'
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 42 42
1397 if $debug;
1398 }
1399 }#determine_csv_q_sep
1400  
1401 131 ▶ my sub determine_csv_col_formats($) {
Subroutine determine_csv_col_formats: called CC=30 SCAR=50.4
1402 131 my ($r2rows) = @_;
1403 return
1404 131 ▶ if defined $opts->{col_formats};
Branch: 1 branch
BranchTrueFalseTotal
if defined $$opts{"col_formats"} 0 131 131
1405 131 ▶ $$r2rows //= readparse_csv();
Condition100%3/3
$$r2rows //= readparse_csv()
l!l&&r!l&&!r
84443
MC/DC100%2/2
$$r2rows //= readparse_csv()
$$r2rows readparse_csv()
Truth table100%3/3
A //= B
A= $$r2rowsB= readparse_csv()
ABresult
1X1
011
000
1406 131 my $max_cols = 0;
1407 131 ▶ for my $row (@{ $$r2rows }) { $max_cols = @$row if $max_cols < @$row }
Branch: 1 branch
BranchTrueFalseTotal
if $max_cols < @$row 159 539 698
1408 131 state $curr_yy = (localtime(time))[5];
1409 131 my @col_formats;
1410 48 ▶ my sub recognized($$$$;$) {
Subroutine recognized: called CC=4 SCAR=22.3
1411 48 my ($cx, $rx, $thing, $format, $as_msg) = @_;
1412 48 $col_formats[$cx] = $format;
1413 48 ▶ return unless $debug;
Branch: 1 branch
BranchTrueFalseTotal
unless $debug 48 0 48
1414 0 ▶ $as_msg //= " as ".vis($col_formats[$cx])." format";
Condition0%0/2
$as_msg //= ' as ' . &vis($col_formats[$cx]) . ' format'
l!l
00
MC/DC0%0/1
$as_msg //= ' as ' . &vis($col_formats[$cx]) . ' format'
$as_msg
Truth table0%0/2
A
A= $as_msg
Aresult
11
00
1415 0 ▶ if (length($thing) > 35) { $thing = substr($thing,0,32)."..."; }
Branch: 1 branch
BranchTrueFalseTotal
if (length $thing > 35) 0 0 0
1416 0 btwN 1, "Recognized $thing in ", cxrx2sheetaddr($cx,$rx), $as_msg;
1417 }
1418 CX:
1419 131 for my $cx (0..$max_cols-1) {
1420 RX:
1421 429 for my $rx (0..$#{$$r2rows}) {
1422 3411 my $row = $$r2rows->[$rx];
1423 3411 ▶ next if $cx > $#$row; # row has fewer columns than others
Branch: 1 branch
BranchTrueFalseTotal
if $cx > $row->$#* 192 3219 3411
1424 3219 for ($row->[$cx]) {
1425 # recognize obvious Y/M/D or M/D/Y or D/M/Y date forms
1426 3219 ▶ if (m#\b(?<y>(?:[12]\d)?\d\d)/(?<m>\d\d)/(?<d>\d\d)\b#) {
Branch: 1 branch
BranchTrueFalseTotal
if (m[\b(?<y>(?:[12]\d)?\d\d)/(?<m>\d\d)/(?<d>\d\d)\b]) 0 3219 3219
1427 0 ▶ if ($+{d} > 12 && $+{d} <= 31 && $+{"m"} >= 1 && $+{"m"} <= 12
Branch: 1 branch
BranchTrueFalseTotal
if ($+{"d"} > 12 and $+{"d"} <= 31 and $+{"m"} >= 1 and $+{"m"} <= 12 and $+{"y"} < 100 || $+{"y"} >= 1000) 0 0 0
Condition0%0/15
$+{"d"} > 12 and $+{"d"} <= 31 and $+{"m"} >= 1 and $+{"m"} <= 12 and $+{"y"} < 100 || $+{"y"} >= 1000
!ll&&!rl&&r
000
$+{"d"} > 12 and $+{"d"} <= 31 and $+{"m"} >= 1 and $+{"m"} <= 12
!ll&&!rl&&r
000
$+{"d"} > 12 and $+{"d"} <= 31 and $+{"m"} >= 1
!ll&&!rl&&r
000
$+{"d"} > 12 and $+{"d"} <= 31
!ll&&!rl&&r
000
$+{"y"} < 100 or $+{"y"} >= 1000
l!l&&r!l&&!r
000
MC/DC0%0/6
$+{"d"} > 12 and $+{"d"} <= 31 and $+{"m"} >= 1 and $+{"m"} <= 12 and $+{"y"} < 100 || $+{"y"} >= 1000
$+{"d"} > 12 $+{"d"} <= 31 $+{"m"} >= 1 $+{"m"} <= 12 $+{"y"} < 100 $+{"y"} >= 1000
Truth table0%0/7
A and B and C and D and E or F
A= $+{"d"} > 12B= $+{"d"} <= 31C= $+{"m"} >= 1D= $+{"m"} <= 12E= $+{"y"} < 100F= $+{"y"} >= 1000
ABCDEFresult
0XXXXX0
10XXXX0
110XXX0
1110XX0
1111000
11111X1
1111011
1428 && ($+{"y"} < 100 || $+{"y"} >= 1000)) {
1429 0 recognized($cx,$rx,$_,"YY/MM/DD");
1430 0 next CX;
1431 }
1432 # If ambiguous YYYY/??/?? we can still assume it is a date and not text
1433 0 ▶ if (length( $+{"y"} )==4) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $+{"y"} == 4) 0 0 0
1434 #recognized($cx,$rx,$_,""," as some kind of date, fmt unknown");
1435 0 next RX;
1436 }
1437 }
1438 3219 ▶ if (m#\b(?<m>\d\d)/(?<d>\d\d)/(?<y>(?:[12]\d)?\d\d)\b#) {
Branch: 1 branch
BranchTrueFalseTotal
if (m[\b(?<m>\d\d)/(?<d>\d\d)/(?<y>(?:[12]\d)?\d\d)\b]) 135 3084 3219
1439 135 ▶ if ($+{"y"} < 100 || $+{"y"} >= 1000) {
Branch: 1 branch
BranchTrueFalseTotal
if ($+{"y"} < 100 or $+{"y"} >= 1000) 135 0 135
Condition33%1/3
$+{"y"} < 100 or $+{"y"} >= 1000
l!l&&r!l&&!r
01350
MC/DC0%0/2
$+{"y"} < 100 or $+{"y"} >= 1000
$+{"y"} < 100 $+{"y"} >= 1000
Truth table33%1/3
A or B
A= $+{"y"} < 100B= $+{"y"} >= 1000
ABresult
1X1
011
000
1440 135 ▶ if ($+{d} > 12 && $+{d} <= 31 && $+{"m"} >= 1 && $+{"m"} <= 12) {
Branch: 2 branches
BranchTrueFalseTotal
if ($+{'d'} > 12 and $+{'d'} <= 31 and $+{'m'} >= 1 and $+{'m'} <= 12) { } 0 135 135
elsif ($+{'m'} > 12 and $+{'m'} <= 31 and $+{'d'} >= 1 and $+{'d'} <= 12) { } 0 270 270
Condition33%3/9
$+{"d"} > 12 and $+{"d"} <= 31 and $+{"m"} >= 1 and $+{"m"} <= 12
!ll&&!rl&&r
13500
$+{"d"} > 12 and $+{"d"} <= 31 and $+{"m"} >= 1
!ll&&!rl&&r
13500
$+{"d"} > 12 and $+{"d"} <= 31
!ll&&!rl&&r
13500
MC/DC0%0/4
$+{"d"} > 12 and $+{"d"} <= 31 and $+{"m"} >= 1 and $+{"m"} <= 12
$+{"d"} > 12 $+{"d"} <= 31 $+{"m"} >= 1 $+{"m"} <= 12
Truth table20%1/5
A and B and C and D
A= $+{"d"} > 12B= $+{"d"} <= 31C= $+{"m"} >= 1D= $+{"m"} <= 12
ABCDresult
0XXX0
10XX0
110X0
11100
11111
1441 0 recognized($cx,$rx,$_,"MM/DD/YY");
1442 next CX
1443 0 }
1444 ▶ elsif ($+{"m"} > 12 && $+{"m"} <= 31 && $+{d} >= 1 && $+{d} <= 12) {
Condition33%3/9
$+{"m"} > 12 and $+{"m"} <= 31 and $+{"d"} >= 1 and $+{"d"} <= 12
!ll&&!rl&&r
13500
$+{"m"} > 12 and $+{"m"} <= 31 and $+{"d"} >= 1
!ll&&!rl&&r
13500
$+{"m"} > 12 and $+{"m"} <= 31
!ll&&!rl&&r
13500
MC/DC0%0/4
$+{"m"} > 12 and $+{"m"} <= 31 and $+{"d"} >= 1 and $+{"d"} <= 12
$+{"m"} > 12 $+{"m"} <= 31 $+{"d"} >= 1 $+{"d"} <= 12
Truth table20%1/5
A and B and C and D
A= $+{"m"} > 12B= $+{"m"} <= 31C= $+{"d"} >= 1D= $+{"d"} <= 12
ABCDresult
0XXX0
10XX0
110X0
11100
11111
1445 0 recognized($cx,$rx,$_,"DD/MM/YY");
1446 next CX
1447 0 }
1448 }
1449 # If ambiguous ??/??/YYYY we can still assume it is a date and not text
1450 135 ▶ if (length($+{"y"})==4) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $+{"y"} == 4) 135 0 135
1451 #recognized($cx,$rx,$_,""," as some kind of date, fmt unknown");
1452 135 next RX;
1453 }
1454 }
1455 # Things to force to be read as text fields:
1456 # 1. Leading zeroes
1457 # 2. Leading ascii minus (\x{2D}) rather than math minus \N{U+2212}.
1458 # This prevents conversion to the Unicode math minus when LO
1459 # reads the CSV. The assumption is that if the input has an ascii
1460 # minus then the original spreadsheet format was "text" not
1461 # numeric (TODO: Look for counterveiling certainly-numeric forms).
1462 3084 ▶ if (/^[\x{2D}0]/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^[\x{2D}0]/) 48 3036 3084
1463 48 recognized($cx,$rx,$_,"text");
1464 48 next CX;
1465 }
1466 }
1467 }
1468 }
1469 131 $opts->{col_formats} = \@col_formats;
1470 }#determine_csv_col_formats
1471  
1472 # ==BODY==
1473 131 ▶ unless ($opts->{cvt_to}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($$opts{"cvt_to"}) 6 125 131
1474 6 ▶ if ($opts->{outpath} && $opts->{outpath} =~ /\.([^.]+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$opts{"outpath"} and $$opts{"outpath"} =~ /\.([^.]+)$/) 6 0 6
Condition33%1/3
$$opts{"outpath"} and $$opts{"outpath"} =~ /\.([^.]+)$/
!ll&&!rl&&r
006
MC/DC0%0/2
$$opts{"outpath"} and $$opts{"outpath"} =~ /\.([^.]+)$/
$$opts{"outpath"} $$opts{"outpath"} =~ /\.([^.]+)$/
Truth table33%1/3
A and B
A= $$opts{"outpath"}B= $$opts{"outpath"} =~ /\.([^.]+)$/
ABresult
0X0
100
111
1475 6 $opts->{cvt_to} = $1;
1476 }
1477 croak "outpath ",qsh($opts->{outpath}),
1478 " has no suffix and 'cvt_to' was not specified"
1479 6 ▶ unless $opts->{cvt_to};
Branch: 1 branch
BranchTrueFalseTotal
unless $$opts{"cvt_to"} 0 6 6
1480 }
1481 131 ▶ unless ($opts->{cvt_from}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($$opts{"cvt_from"}) 131 0 131
1482 131 ▶ if ($opts->{inpath_sans_sheet} =~ /\.([^.]+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$opts{"inpath_sans_sheet"} =~ /\.([^.]+)$/) 47 84 131
1483 47 $opts->{cvt_from} = $1;
1484 }
1485 }
1486 131 ▶ if ($opts->{cvt_from}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$opts{"cvt_from"}) 47 84 131
1487 47 $opts->{cvt_from} = lc($opts->{cvt_from}); # CSV -> csv
1488 croak ivis 'cvt_from must be a suffix without a dot (not $opts->{cvt_from})'
1489 47 ▶ if $opts->{cvt_from} =~ /[^a-z]/;
Branch: 1 branch
BranchTrueFalseTotal
if $$opts{"cvt_from"} =~ /[^a-z]/ 0 47 47
1490 47 ▶ $opts->{cvt_from} = "csv" if $opts->{cvt_from} eq "txt";
Branch: 1 branch
BranchTrueFalseTotal
if $$opts{"cvt_from"} eq "txt" 0 47 47
1491 }
1492  
1493 # Detect input file format and, if CSV, encoding etc.
1494 131 my $rows;
1495 131 ▶ if (!$opts->{cvt_from} || $opts->{cvt_from} eq "csv") {
Branch: 1 branch
BranchTrueFalseTotal
if (not $$opts{"cvt_from"} or $$opts{"cvt_from"} eq "csv") 131 0 131
Condition66%2/3
not $$opts{"cvt_from"} or $$opts{"cvt_from"} eq "csv"
l!l&&r!l&&!r
84470
MC/DC0%0/2
not $$opts{"cvt_from"} or $$opts{"cvt_from"} eq "csv"
not $$opts{"cvt_from"} $$opts{"cvt_from"} eq "csv"
Truth table66%2/3
not(A) or B
A= not $$opts{"cvt_from"}B= $$opts{"cvt_from"} eq "csv"
ABresult
1X1
011
000
1496 131 open_input();
1497 131 determine_input_encoding();
1498 131 set_fh_encoding();
1499 }
1500 131 ▶ if (!$opts->{cvt_from}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($$opts{"cvt_from"}) 84 47 131
1501 # Detect the file format by looking at the data. Actually, we only
1502 # support CSV in this case, so this is just a (half-baked) sanity check.
1503 84 eval {
1504 84 determine_csv_q_sep(\$rows);
1505 84 ▶ $rows //= readparse_csv();
Condition33%1/3
$rows //= readparse_csv()
l!l&&r!l&&!r
8400
MC/DC0%0/2
$rows //= readparse_csv()
$rows readparse_csv()
Truth table33%1/3
A //= B
A= $rowsB= readparse_csv()
ABresult
1X1
011
000
1506 };
1507 84 ▶ if ($@ eq "") {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ eq '') { } 84 0 84
1508 84 ▶ warn "> Detected $opts->{inpath_sans_sheet} as a seemingly-valid CSV\n"
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 84 84
1509 if $debug;
1510 84 $opts->{cvt_from} = "csv";
1511 } else {
1512 #croak "Can not detect what kind of file ",qsh($opts->{inpath})," is\n";
1513 0 carp "Can not detect what kind of file ",qsh($opts->{inpath})," is\n",
1514 $@, "\n";
1515 0 ▶ open my $fh99, "<:raw", $opts->{inpath} or die "Can not re-open $opts->{inpath}: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh99, "<:raw", $$opts{"inpath"} or die qq[Can not re-open $$opts{"inpath"}: $!] 0 0 0
1516 0 ▶ if ((my $nb = read $fh99, my $octets, 16) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $nb = read($fh99, my $octets, 16)) != 0) { } 0 0 0
1517 0 die "First 16 octets are as follows:\n", Hexify($octets);
1518 } else {
1519 0 die "File appears to be EMPTY";
1520 }
1521 }
1522 }
1523 131 ▶ oops if defined($rows) && $opts->{cvt_from} ne "csv";
Branch: 1 branch
BranchTrueFalseTotal
if defined $rows and $$opts{"cvt_from"} ne "csv" 0 131 131
Condition66%2/3
defined $rows and $$opts{"cvt_from"} ne "csv"
!ll&&!rl&&r
47840
MC/DC0%0/2
defined $rows and $$opts{"cvt_from"} ne "csv"
defined $rows $$opts{"cvt_from"} ne "csv"
Truth table66%2/3
A and B
A= defined $rowsB= $$opts{"cvt_from"} ne "csv"
ABresult
0X0
100
111
1524  
1525 131 ▶ if ($opts->{cvt_from} eq "csv") {
Branch: 1 branch
BranchTrueFalseTotal
if ($$opts{'cvt_from'} eq 'csv') { } 131 0 131
1526 131 determine_csv_col_formats(\$rows);
1527 } else {
1528 0 ▶ oops if defined($rows);
Branch: 1 branch
BranchTrueFalseTotal
if defined $rows 0 0 0
1529 }
1530  
1531 131 ▶ if ($opts->{cvt_to} =~ /^(?:csv|txt)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$opts{"cvt_to"} =~ /^(?:csv|txt)$/) 131 0 131
1532 # 6/12/2025: csv --> txt IS NOT WORKING (truncates wide columns)
1533 131 ▶ if ($opts->{output_encoding}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$opts{'output_encoding'}) { } 30 101 131
1534 croak "Only one of {output_encoding} or {output_binmode} may be specified"
1535 30 ▶ if $opts->{output_binmode};
Branch: 1 branch
BranchTrueFalseTotal
if $$opts{"output_binmode"} 0 30 30
1536 30 ▶ ($opts->{output_binmode} = $default_output_binmode)
Branch: 1 branch
BranchTrueFalseTotal
($$opts{"output_binmode"} = $Spreadsheet::Edit::IO::default_output_binmode) =~ s/:encoding(.*?)/':encoding(' . $$opts{"output_encoding"} . ')';/e or &oops() 30 0 30
1537 30 =~ s/:encoding(.*?)/":encoding(".$opts->{output_encoding}.")"/e or oops;
1538 } else {
1539 101 ▶ $opts->{output_binmode} //= $default_output_binmode;
Condition33%1/3
$$opts{"output_binmode"} //= $Spreadsheet::Edit::IO::default_output_binmode
l!l&&r!l&&!r
01010
MC/DC0%0/2
$$opts{"output_binmode"} //= $Spreadsheet::Edit::IO::default_output_binmode
$$opts{"output_binmode"} $Spreadsheet::Edit::IO::default_output_binmode
Truth table33%1/3
A //= B
A= $$opts{"output_binmode"}B= $Spreadsheet::Edit::IO::default_output_binmode
ABresult
1X1
011
000
1540 croak "output_binmode must include ':encoding(...)'\n"
1541 101 ▶ unless $opts->{output_binmode} =~ /:encoding\(.+?\)/;
Branch: 1 branch
BranchTrueFalseTotal
unless $$opts{"output_binmode"} =~ /:encoding\(.+?\)/ 0 101 101
1542 }
1543 }
1544  
1545 131 return ($fh, $start_pos);
1546 }#_preprocess
1547  
1548 0 ▶sub _tool_extract_all_csvs($$) {
Subroutine _tool_extract_all_csvs: not called CC=3 SCAR=24.8
1549 0 my ($opts, $destdir) = @_;
1550  
1551 0 _get_exclusive_lock($opts);
1552 0 ▶ scope_guard { _release_lock($opts); };
Subroutine __ANON__: not called CC=1 SCAR=6.9
1553  
1554 0 delete local $opts->{sheetname};
1555 0 local $opts->{allsheets} = 1;
1556 0 ▶ if (_openlibre_supports_allsheets()) {
Branch: 2 branches
BranchTrueFalseTotal
if (&_openlibre_supports_allsheets()) { } 0 0 0
elsif (&_ssconvert_supports_allsheets()) { } 0 0 0
1557 0 _convert_using_openlibre($opts, $opts->{inpath_sans_sheet}, $destdir);
1558 }
1559 elsif (_ssconvert_supports_allsheets()) {
1560 0 _convert_using_ssconvert($opts, $opts->{inpath_sans_sheet}, $destdir);
1561 }
1562 0 else { confess "Can't extract 'allsheets'. Please install LibreOffice 7.2 or newer" }
1563 }
1564  
1565 0 ▶sub _tool_can_extract_csv_byname() {
Subroutine _tool_can_extract_csv_byname: not called CC=2 SCAR=17.9
1566 0 ▶ _openlibre_supports_named_sheet() || _ssconvert_supports_named_sheet()
Branch: 1 branch
BranchTrueFalseTotal
&_openlibre_supports_named_sheet() or &_ssconvert_supports_named_sheet() 0 0 0
1567 }
1568 0 ▶sub _tool_extract_one_csv($$) {
Subroutine _tool_extract_one_csv: not called CC=4 SCAR=30.0
1569 0 my ($opts, $destpath) = @_;
1570  
1571 0 _get_exclusive_lock($opts);
1572 0 ▶ scope_guard { _release_lock($opts); };
Subroutine __ANON__: not called CC=1 SCAR=6.9
1573  
1574 0 ▶ if (_openlibre_features()->{available}) {
Branch: 1 branch
BranchTrueFalseTotal
if (&_openlibre_features()->{'available'}) { } 0 0 0
1575 0 ▶ oops if $opts->{sheetname} && !_openlibre_supports_named_sheet();
Branch: 1 branch
BranchTrueFalseTotal
if $$opts{"sheetname"} and not &_openlibre_supports_named_sheet() 0 0 0
Condition0%0/3
$$opts{"sheetname"} and not &_openlibre_supports_named_sheet()
!ll&&!rl&&r
000
MC/DC0%0/2
$$opts{"sheetname"} and not &_openlibre_supports_named_sheet()
$$opts{"sheetname"} not &_openlibre_supports_named_sheet()
Truth table0%0/3
A and not(B)
A= $$opts{"sheetname"}B= not &_openlibre_supports_named_sheet()
ABresult
0X0
100
111
1576 0 _convert_using_openlibre($opts, $opts->{inpath_sans_sheet}, $destpath);
1577 } else {
1578 0 _convert_using_ssconvert($opts, $opts->{inpath_sans_sheet}, $destpath);
1579 }
1580 }
1581 0 ▶sub _tool_can_extract_current_sheet() {
Subroutine _tool_can_extract_current_sheet: not called CC=2 SCAR=17.9
1582 _openlibre_features()->{available} || _ssconvert_features()->{available}
1583 0 ▶}
Branch: 1 branch
BranchTrueFalseTotal
&_openlibre_features()->{"available"} or &_ssconvert_features()->{"available"} 0 0 0
1584  
1585 0 ▶sub _tool_write_spreadsheet($$) {
Subroutine _tool_write_spreadsheet: not called CC=6 SCAR=37.4
1586 0 my ($opts, $destpath) = @_;
1587  
1588 0 _get_exclusive_lock($opts);
1589 0 ▶ scope_guard { _release_lock($opts); };
Subroutine __ANON__: not called CC=1 SCAR=6.9
1590  
1591 # ssconvert allows specifying the sheetname when importing a csv but not LO
1592 0 ▶ if ($opts->{sheetname} && _ssconvert_supports_writing($opts->{cvt_to})) {
Branch: 3 branches
BranchTrueFalseTotal
if ($$opts{'sheetname'} and &_ssconvert_supports_writing($$opts{'cvt_to'})) { } 0 0 0
elsif (&_openlibre_supports_writing($$opts{'cvt_to'})) { } 0 0 0
elsif (&_ssconvert_supports_writing($$opts{'cvt_to'})) { } 0 0 0
Condition0%0/3
$$opts{"sheetname"} and &_ssconvert_supports_writing($$opts{"cvt_to"})
!ll&&!rl&&r
000
MC/DC0%0/2
$$opts{"sheetname"} and &_ssconvert_supports_writing($$opts{"cvt_to"})
$$opts{"sheetname"} &_ssconvert_supports_writing($$opts{"cvt_to"})
Truth table0%0/3
A and B
A= $$opts{"sheetname"}B= &_ssconvert_supports_writing($$opts{"cvt_to"})
ABresult
0X0
100
111
1593 0 _convert_using_ssconvert($opts, $opts->{inpath_sans_sheet}, $destpath);
1594 }
1595 elsif (_openlibre_supports_writing($opts->{cvt_to})) {
1596 0 ▶ if ($opts->{sheetname}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$opts{"sheetname"}) 0 0 0
1597 0 carp "WARNING: Sheet name when creating a spreadsheet will be ignored\n";
1598 0 delete $opts->{sheetname};
1599 }
1600 0 _convert_using_openlibre($opts, $opts->{inpath_sans_sheet}, $destpath);
1601 }
1602 elsif (_ssconvert_supports_writing($opts->{cvt_to})) {
1603 0 _convert_using_ssconvert($opts, $opts->{inpath_sans_sheet}, $destpath);
1604 }
1605 0 else { croak "Can't create $opts->{cvt_to} spreadsheets. Please install LibreOffice 7.2 or newer" }
1606 }
1607  
1608  
1609 # Extract CSVs for every sheet into {outpath} (setting to tmpdir if not preset).
1610 # If cached CSVs are available they are moved into {outpath}/ .
1611 0 ▶sub _extract_all_csvs($) {
Subroutine _extract_all_csvs: not called CC=1 SCAR=6.9
1612 0 my ($opts) = @_;
1613 0 my $outpath = _finalize_outpath($opts);
1614 0 $outpath->mkpath; # nop if exists, croaks if conflicts with file
1615  
1616 0 _tool_extract_all_csvs($opts, $outpath); #logs
1617 }
1618  
1619  
1620 # Extract a single sheet into a CSV at {outpath} (defaulting to temp file).
1621 # If a cached CSV is available it is moved to {outpath}.
1622 0 ▶sub _extract_one_csv($) {
Subroutine _extract_one_csv: not called CC=12 SCAR=50.5
1623 0 my ($opts) = @_;
1624 0 my $cachedirpath = _cachedir($opts);
1625  
1626 0 ▶ my sub _fill_csv_cache() {
Subroutine _fill_csv_cache: not called CC=1 SCAR=6.9
1627 0 $cachedirpath->remove_tree;
1628 0 $cachedirpath->mkpath;
1629 { #local $opts->{verbose} = 0;
1630 #local $opts->{debug} = 0;
1631 0 _tool_extract_all_csvs($opts, $cachedirpath);
1632 }
1633 }
1634  
1635 0 my $outpath = _finalize_outpath($opts);
1636 0 ▶ $outpath->remove unless -d $outpath;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $outpath 0 0 0
1637  
1638 0 ▶ if (defined($opts->{sheetname})) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $$opts{'sheetname'}) { } 0 0 0
elsif (&_tool_can_extract_current_sheet()) { } 0 0 0
1639 0 my $fname = $opts->{sheetname}.".csv";
1640 0 my $cached_path = $cachedirpath->child($fname);
1641 0 ▶ if (! -e $cached_path) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $cached_path) 0 0 0
1642 0 ▶ if (_tool_can_extract_csv_byname()) {
Branch: 1 branch
BranchTrueFalseTotal
if (&_tool_can_extract_csv_byname()) 0 0 0
1643 0 _tool_extract_one_csv($opts, $outpath); #logs
1644 return
1645 0 }
1646 warn ">>Emulating extract-by-name by extracting all csvs into cache...\n"
1647 0 ▶ if $opts->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $$opts{"debug"} 0 0 0
1648 0 _fill_csv_cache;
1649 }
1650 0 ▶ croak "Sheet '$opts->{sheetname}' does not exist in $opts->{inpath_sans_sheet}\n"
Branch: 1 branch
BranchTrueFalseTotal
unless -e $cached_path 0 0 0
1651 unless -e $cached_path;
1652 0 ▶ warn "> Moving cached $fname to $outpath\n" if $opts->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $$opts{"verbose"} 0 0 0
1653 0 File::Copy::move($cached_path, $outpath);
1654 return
1655 0 }
1656 elsif (_tool_can_extract_current_sheet()) {
1657 0 _tool_extract_one_csv($opts, $outpath); #logs
1658 return
1659 0 }
1660 else {
1661 0 _fill_csv_cache;
1662 0 my @children = $cachedirpath->children;
1663 0 ▶ if (@children == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (@children == 0) { } 0 0 0
elsif (@children == 1) { } 0 0 0
1664 0 croak "$opts->{inpath_sans_sheet} appears to have zero sheets!\n"
1665 }
1666 elsif (@children == 1) {
1667 0 my $fname = $children[0]->basename;
1668 0 my $cached_path = $cachedirpath->child($fname);
1669 0 ▶ warn "> Moving cached $fname to $outpath\n" if $opts->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $$opts{"verbose"} 0 0 0
1670 0 File::Copy::move($cached_path, $outpath);
1671 return
1672 0 }
1673 else {
1674 0 croak "$opts->{inpath_sans_sheet} contains multiple sheets; you must specify a sheetname\n"
1675 }
1676 }
1677 }#_extract_one_csv
1678 0 ▶sub _write_spreadsheet($) {
Subroutine _write_spreadsheet: not called CC=2 SCAR=17.9
1679 0 my ($opts) = @_;
1680  
1681 0 my $outpath = _finalize_outpath($opts);
1682 0 ▶ $outpath->remove unless -d $outpath;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $outpath 0 0 0
1683  
1684 0 _tool_write_spreadsheet($opts, $outpath);
1685 }
1686  
1687 # If {outpath} is not set, set it to a unique output path in $proctempdir
1688 # Always returns {outpath} as a Path::Tiny object.
1689 58 ▶sub _finalize_outpath($) {
Subroutine _finalize_outpath: called CC=3 SCAR=11.0
1690 58 my $opts = shift;
1691 58 ▶ if (defined $opts->{outpath}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $$opts{'outpath'}) { } 4 54 58
1692 4 return path($opts->{outpath});
1693 } else {
1694 54 ▶ my $suf = $opts->{allsheets} ? undef : $opts->{cvt_to};
Branch: 1 branch
BranchTrueFalseTotal
$$opts{'allsheets'} ? : 2 52 54
1695 return(
1696 54 ($opts->{outpath}=_uniqpath_under_tempdir($opts, suf=>$suf))
1697 );
1698 }
1699 }
1700  
1701 133 ▶sub convert_spreadsheet(@) {
Subroutine convert_spreadsheet: called CC=32 SCAR=42.7
Pod documented
1702 # Set inpath_sans_sheet, sheetname, ifbase, etc.
1703 133 my %opts = &_process_args;
1704 131 ▶ btw dvis('>>> convert_spreadsheet %opts\n') if $opts{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"debug"} 0 131 131
1705 131 my %input_opts = %opts;
1706  
1707 131 _create_proctempdir_ifneeded(\%opts);
1708  
1709 # intuit cvt_from & cvt_to, detect encoding, and pre-process .csv input
1710 # if needed to avoid corruption of leading zeroes.
1711 131 my $octets;
1712 131 my ($fh, $start_pos) = _preprocess(\%opts, \$octets); # sets defaults etc.
1713  
1714 131 my $input_enc = $opts{input_encoding};
1715 my ($output_enc) = $opts{output_binmode}
1716 131 ▶ && $opts{output_binmode} =~ /:encoding\((.+?)\)/ or oops;
Branch: 1 branch
BranchTrueFalseTotal
my($output_enc) = $opts{"output_binmode"} && $opts{"output_binmode"} =~ /:encoding\((.+?)\)/ or &oops() 131 0 131
Condition33%1/3
$opts{'output_binmode'} && $opts{'output_binmode'} =~ /:encoding\((.+?)\)/
!ll&&!rl&&r
00131
MC/DC0%0/2
$opts{'output_binmode'} && $opts{'output_binmode'} =~ /:encoding\((.+?)\)/
$opts{'output_binmode'} $opts{'output_binmode'} =~ /:encoding\((.+?)\)/
Truth table33%1/3
A && B
A= $opts{'output_binmode'}B= $opts{'output_binmode'} =~ /:encoding\((.+?)\)/
ABresult
0X0
100
111
1717  
1718 croak "Either input or output must be 'csv'\n"
1719 131 ▶ unless $opts{cvt_from} eq 'csv' || $opts{cvt_to} eq 'csv';
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"cvt_from"} eq "csv" or $opts{"cvt_to"} eq "csv" 0 131 131
Condition33%1/3
$opts{"cvt_from"} eq "csv" or $opts{"cvt_to"} eq "csv"
l!l&&r!l&&!r
13100
MC/DC0%0/2
$opts{"cvt_from"} eq "csv" or $opts{"cvt_to"} eq "csv"
$opts{"cvt_from"} eq "csv" $opts{"cvt_to"} eq "csv"
Truth table33%1/3
A or B
A= $opts{"cvt_from"} eq "csv"B= $opts{"cvt_to"} eq "csv"
ABresult
1X1
011
000
1720 131 ▶ if ($opts{allsheets}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"allsheets"}) 2 129 131
1721 croak "'allsheets' is allowed only with cvt_to => 'csv'"
1722 2 ▶ unless ($opts{cvt_to}//"") eq "csv";
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts{"cvt_to"} // "") eq "csv" 0 2 2
Condition50%1/2
$opts{"cvt_to"} // ""
l!l
20
MC/DC0%0/1
$opts{"cvt_to"} // ""
$opts{"cvt_to"}
Truth table50%1/2
A
A= $opts{"cvt_to"}
Aresult
11
00
1723 croak "With 'allsheets', a sheet name may not be specified\n"
1724 2 ▶ if $opts{sheetname};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"sheetname"} 0 2 2
1725 croak "With 'allsheets', 'outpath' must be a directory if it exists\n"
1726 2 ▶ if $opts{outpath} && -e $opts{outpath} && ! -d _;
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"outpath"} and -e $opts{"outpath"} and not -d _ 0 2 2
Condition33%2/6
$opts{"outpath"} and -e $opts{"outpath"} and not -d _
!ll&&!rl&&r
200
$opts{"outpath"} and -e $opts{"outpath"}
!ll&&!rl&&r
200
MC/DC0%0/3
$opts{"outpath"} and -e $opts{"outpath"} and not -d _
$opts{"outpath"} -e $opts{"outpath"} not -d _
Truth table25%1/4
A and B and not(C)
A= $opts{"outpath"}B= -e $opts{"outpath"}C= not -d _
ABCresult
0XX0
10X0
1100
1111
1727 }
1728  
1729 58 ▶ my sub get_outcsv_path() {
Subroutine get_outcsv_path: called CC=2 SCAR=6.9
1730 58 my $outpath = _finalize_outpath(\%opts);
1731 58 ▶ if ($opts{allsheets}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{'allsheets'}) { } 2 56 58
1732 2 $outpath->mkpath; # nop if exists, croaks if conflicts with file
1733 2 return $outpath->child( $opts{ifbase}.".csv" );
1734 } else {
1735 56 return $outpath;
1736 }
1737 }
1738  
1739 131 my $done;
1740 131 ▶ if ($opts{cvt_from} eq $opts{cvt_to}) { # csv to csv
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"cvt_from"} eq $opts{"cvt_to"}) 131 0 131
1741 # Special cases: in & out are both CSVs
1742 131 ▶ if ($input_enc ne $output_enc || $start_pos != 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($input_enc ne $output_enc or $start_pos != 0) { } 56 75 131
elsif (not $opts{'allsheets'}) { } 73 2 75
Condition100%3/3
$input_enc ne $output_enc or $start_pos != 0
l!l&&r!l&&!r
441275
MC/DC100%2/2
$input_enc ne $output_enc or $start_pos != 0
$input_enc ne $output_enc $start_pos != 0
Truth table100%3/3
A or B
A= $input_enc ne $output_encB= $start_pos != 0
ABresult
1X1
011
000
1743 # Special case #1: In & out are CSV but using different encodings,
1744 # or the same encoding but the input contained a BOM
1745 # (which we never want in the output).
1746 # With {allsheets} the output will be inside the {outpath} directory.
1747 56 my $dst = get_outcsv_path();
1748 warn "> Transcoding csv: $input_enc -> $output_enc into ",qsh($dst),"\n"
1749 56 ▶ if $opts{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"debug"} 0 56 56
1750 56 _slurp_ifnotslurped($fh, \$octets, $opts{debug});
1751 56 my $chars = _decode_slurped_data($input_enc, \$octets, $start_pos,
1752 Encode::FB_CROAK);
1753 56 $octets = encode($output_enc, $chars, Encode::FB_CROAK);
1754 56 $dst->spew_raw($octets);
1755 56 $done = 1;
1756 }
1757 elsif (! $opts{allsheets}) {
1758 # Special case #2: Not {allsheets} and no conversion is needed:
1759 # Just pass through the original input.
1760 73 my $src = $opts{inpath_sans_sheet};
1761 73 ▶ if (my $outpath = $opts{outpath}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $outpath = $opts{'outpath'}) { } 2 71 73
1762 warn "> No conversion needed! Copying to ", qsh($outpath),"\n"
1763 2 ▶ if $opts{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"verbose"} 0 2 2
1764 2 $src->copy($outpath);
1765 } else {
1766 warn "> No conversion needed! Returning input as {outpath}\n"
1767 71 ▶ if $opts{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"verbose"} 0 71 71
1768 71 $opts{outpath} = $src;
1769 }
1770 73 $done = 1;
1771 }
1772 else {
1773 # Special case #3: {allsheets} with CSV input, no converstion needed:
1774 # Create a symlink to the input in the {outpath} directory.
1775 2 my $src = $opts{inpath_sans_sheet};
1776 2 my $dst = get_outcsv_path();
1777 2 ▶ if ((my $s = eval{symlink($src, $dst)}) && !$@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $s = eval { do { symlink $src, $dst } } and not $@) { } 2 0 2
Condition33%1/3
my $s = eval { do { symlink $src, $dst } } and not $@
!ll&&!rl&&r
002
MC/DC0%0/2
my $s = eval { do { symlink $src, $dst } } and not $@
my $s = eval { do { symlink $src, $dst } } not $@
Truth table33%1/3
A and not(B)
A= my $s = eval { do { symlink $src, $dst } }B= not $@
ABresult
0X0
100
111
1778 warn "> No conversion needed! Left symlink at ", qsh($dst),"\n"
1779 2 ▶ if $opts{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"verbose"} 0 2 2
1780 2 $done = 1;
1781 } else {
1782 0 ▶ btw dvis 'symlink>> $@' if $opts{debug}; # unimplmented or perms?
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"debug"} 0 0 0
1783 warn "> No conversion needed! Copying into ", qsh($dst),"\n"
1784 0 ▶ if $opts{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"verbose"} 0 0 0
1785 0 $src->copy($dst);
1786 0 $done = 1;
1787 }
1788 }
1789 }
1790 131 ▶ if (! $done) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($done) 0 131 131
1791 0 ▶ if ($opts{allsheets}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{'allsheets'}) { } 0 0 0
1792 0 _extract_all_csvs(\%opts);
1793 }
1794 else {
1795 # Result will be a single file.
1796 0 ▶ if ($opts{cvt_to} eq "csv") {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{'cvt_to'} eq 'csv') { } 0 0 0
1797 0 _extract_one_csv(\%opts);
1798 } else {
1799 0 _write_spreadsheet(\%opts);
1800 }
1801 }
1802 }
1803 my $result = {
1804 defined($output_enc) ? (encoding => $output_enc):(),
1805 524 (map{ my $v = $opts{$_};
1806 524 ▶ ($_ => (blessed($v) ? $v->stringify : $v))
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($v) ? : 262 262 524
1807 131 ▶ } grep{ defined $opts{$_} }
Branch: 1 branch
BranchTrueFalseTotal
defined $output_enc ? : 131 0 131
1808 qw/inpath_sans_sheet outpath cvt_from cvt_to sheetname/)
1809 };
1810 log_call [\%input_opts], [$result, \_fmt_outpath_contents($result)]
1811 131 ▶ if $opts{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"verbose"} 0 131 131
1812  
1813 131 $result;
1814 }#convert_spreadsheet
1815  
1816 # Open as a CSV, intuiting input encoding, converting from spreadsheet if
1817 # necessary.
1818 #
1819 # :crlf translation is enabled on the resulting file handle, which converts
1820 # DOS CR,LF to \n while passing *nix bare LF through unmolested.
1821 #
1822 # Input argument(s) are the same as for convert_spreadsheet (except
1823 # outpath may not be specified).
1824 #
1825 # Returns a hash containing the file handle and other information.
1826 93 ▶sub OpenAsCsv {
Subroutine OpenAsCsv: called CC=14 SCAR=30.6
Pod documented
1827 93 ▶ my %opts = (
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 0 93 93
1828 (@_ == 1 ? (inpath => $_[0]) : (@_)),
1829 cvt_to => 'csv',
1830 );
1831 93 ▶btw dvis('>>> OpenAsCsv %opts\n') if $opts{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"debug"} 0 93 93
1832 # TODO: Rename {path} to {inpath} in all usages and rm this cruft;
1833 carp "Obsolete OpenAsCsv usage: Change path to inpath\n"
1834 93 ▶ if exists($opts{path}) and !$opts{silent};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts{"path"} and not $opts{"silent"} 0 93 93
Condition33%1/3
exists $opts{"path"} and not $opts{"silent"}
!ll&&!rl&&r
9300
MC/DC0%0/2
exists $opts{"path"} and not $opts{"silent"}
exists $opts{"path"} not $opts{"silent"}
Truth table33%1/3
A and not(B)
A= exists $opts{"path"}B= not $opts{"silent"}
ABresult
0X0
100
111
1835 93 ▶ $opts{inpath} //= delete $opts{path}; # be compatible with old API
Condition33%1/3
$opts{"inpath"} //= delete $opts{"path"}
l!l&&r!l&&!r
9300
MC/DC0%0/2
$opts{"inpath"} //= delete $opts{"path"}
$opts{"inpath"} delete $opts{"path"}
Truth table33%1/3
A //= B
A= $opts{"inpath"}B= delete $opts{"path"}
ABresult
1X1
011
000
1836  
1837 93 my $inpath = delete $opts{inpath};
1838 93 ▶ croak "OpenAsCsv: missing 'inpath' option\n" unless $inpath;
Branch: 1 branch
BranchTrueFalseTotal
unless $inpath 0 93 93
1839 93 ▶ croak "OpenAsCsv: outpath may not be specified\n" if $opts{outpath};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"outpath"} 0 93 93
1840  
1841 93 my $h = convert_spreadsheet($inpath, %opts, verbose => $opts{debug});
1842 91 ▶ oops "sheetname key bug" if exists $h->{sheet};
Branch: 1 branch
BranchTrueFalseTotal
if exists $$h{"sheet"} 0 91 91
1843  
1844 91 ▶ my $csvpath = $h->{outpath} // oops; # same as {inpath} if already a CSV
Condition33%1/3
$$h{"outpath"} // &oops()
l!l&&r!l&&!r
9100
MC/DC0%0/2
$$h{"outpath"} // &oops()
$$h{"outpath"} &oops()
Truth table33%1/3
A // B
A= $$h{"outpath"}B= &oops()
ABresult
1X1
011
000
1845  
1846 91 ▶ open my $fh, "<", $csvpath or croak "$csvpath : $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $csvpath or croak("$csvpath : $!\n") 91 0 91
1847 91 ▶ binmode $fh, ":raw:encoding(".$h->{encoding}."):crlf" or die "binmode:$!";
Branch: 1 branch
BranchTrueFalseTotal
binmode $fh, ':raw:encoding(' . $$h{"encoding"} . '):crlf' or die "binmode:$!" 91 0 91
1848 91 ▶ seek($fh, 0, SEEK_SET) or oops; #"should be seekable
Branch: 1 branch
BranchTrueFalseTotal
seek $fh, 0, 0 or &oops() 91 0 91
1849  
1850 my $r = {
1851 fh => $fh,
1852 csvpath => $csvpath,
1853 inpath => $inpath,
1854 91 ▶ (map{ exists($h->{$_}) ? ($_ => $h->{$_}) : () }
Branch: 1 branch
BranchTrueFalseTotal
exists $$h{$_} ? : 182 273 455
1855 qw/inpath_sans_sheet sheetname encoding tempdir raw_values/),
1856 };
1857  
1858 91 return $r;
1859 }
1860  
1861 11 1;
1862 __END__
↑ Summary Edit/Log.pm »