blib/lib/Script/Toolbox/Util.pm

filter: Statementstmt 50.0% 294 / 588 Branchbran 34.2% 91 / 266 Conditioncond 47.0% 16 / 34 MC/DCmcdc 19.0% 4 / 21 Subroutinesub 53.7% 43 / 80 Podpod 0.0% 0 / 19 totaltotal 44.4% 448 / 1008 SCAR 84.0
CC
161
Cov
45%
_normMenueOpts
56.0
_getNumber
56.0
_hashHandler
42.0
CRAP 4436.6
↑ Summary blib/lib/Script/Toolbox/Util/Menues.pm »
1 package Script::Toolbox::Util;
2  
3 21 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 21 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 21 ▶use Script::Toolbox::Util::Opt;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 21 ▶use Script::Toolbox::Util::Formatter;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 21 ▶use Script::Toolbox::Util::Menues;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 21 ▶use Script::Toolbox::TableO;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 21 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 21 ▶use IO::Dir;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 21 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 21 ▶use Fatal qw(open close);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 21 ▶use POSIX qw(strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 21 ▶use Time::ParseDate;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 21 require Exporter;
17  
18 21 our @ISA = qw(Exporter);
19  
20 # Items to export into callers namespace by default. Note: do not export
21 # names by default without a very good reason. Use EXPORT_OK instead.
22 # Do not simply export all your public functions/methods/constants.
23  
24 # This allows declaration use Script::Toolbox::Util ':all';
25 # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
26 # will save memory.
27 21 our %EXPORT_TAGS = ( 'all' => [ qw(Open Log Exit Table Usage Dir File FileC System Now Menue KeyMap Stat TmpFile DataMenue) ] );
28  
29 21 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
30  
31 21 our @EXPORT = qw(
32
33 );
34  
35 #our $VERSION = '0.50';
36  
37 # Preloaded methods go here.
38 sub _getKV(@);
39 sub _getCSV(@);
40  
41 #------------------------------------------------------------------------------
42 #------------------------------------------------------------------------------
43 sub new
44 {
45 19 ▶ my $classname = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
46 19 my $optDef = shift; # options definition
47 19 my $self = {};
48 19 bless( $self, $classname );
49  
50 19 @Script::Toolbox::Util::caller = caller();
51 19 $self->_init( $optDef, \@Script::Toolbox::Util::caller, @_ );
52  
53 15 return $self;
54 }
55  
56 #------------------------------------------------------------------------------
57 #------------------------------------------------------------------------------
58 sub _init($)
59 {
60 19 ▶ my ($self,$ops,$caller,$args) = @_;
Subroutine _init: called CC=4 SCAR=13.9
61  
62 19 my $log = $caller->[1];
63 19 $log =~ s|^.*/||;
64 19 $log =~ s/[.].*$//;
65 19 $Script::Toolbox::Util{'_logFH'} = undef; # use default STDERR
66  
67 # Install signal handler
68 19 $self->_installSigHandlers();
69  
70 # install options
71 19 $self->_installOps( $ops );
72 Exit( 1, "Invalid option definition, 'opsDef' => {} invalid." )
73 18 ▶ if( defined $ops && !defined $self->{'ops'});
Branch: 1 branch
BranchTrueFalseTotal
if defined $ops and not defined $self->{"ops"} 3 15 18
Condition100%3/3
defined $ops and not defined $self->{"ops"}
!ll&&!rl&&r
783
MC/DC100%2/2
defined $ops and not defined $self->{"ops"}
defined $ops not defined $self->{"ops"}
Truth table100%3/3
A and not(B)
A= defined $opsB= not defined $self->{"ops"}
ABresult
0X0
100
111
74  
75 # init log file
76 15 my $logdir = $self->GetOpt('logdir');
77 15 ▶ if( defined $logdir )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $logdir) 4 11 15
78 {
79 4 system( "mkdir -p $logdir" );
80 4 $Script::Toolbox::Util{'_logFH'} = Open( ">> $logdir/$log.log" );
81 4 $Script::Toolbox::Util{'_logFH'}->autoflush();
82 }
83 }
84  
85 #------------------------------------------------------------------------------
86 #------------------------------------------------------------------------------
87 sub _installOps($)
88 {
89 19 ▶ my ($self, $opsDef) = @_;
Subroutine _installOps: called CC=4 SCAR=14.6
90  
91 19 $self->{'ops'} = Script::Toolbox::Util::Opt->new( $opsDef, \@Script::Toolbox::Util::caller );
92 18 ▶ return if( !defined $self->{'ops'} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"ops"} 3 15 18
93  
94 15 foreach my $key ( keys %{$self->{'ops'}} )
95 {
96 52 ▶ if( defined $self->{$key} )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{$key}) 0 52 52
97 {
98 0 print STDERR "Script::Toolbox internal error. ";
99 0 print STDERR "Can't use command line option $key (internal used)\n";
100 0 next;
101 }
102 52 $self->{$key} = $self->{'ops'}->get($key);
103 }
104 15 return;
105 }
106  
107 #------------------------------------------------------------------------------
108 # Signal handler.
109 #------------------------------------------------------------------------------
110 sub _sigExit($)
111 {
112 0 ▶ my ($sig) = @_;
Subroutine _sigExit: not called CC=1 SCAR=6.9
113 0 Exit( 1, "program aborted by signal SIG$sig." );
114 }
115  
116 #------------------------------------------------------------------------------
117 #------------------------------------------------------------------------------
118 sub _installSigHandlers()
119 {
120 19 ▶ my ($self) = @_;
Subroutine _installSigHandlers: called CC=1 SCAR=0.0
121 19 $SIG{'INT'} = \&_sigExit;
122 19 $SIG{'HUP'} = \&_sigExit;
123 19 $SIG{'QUIT'}= \&_sigExit;
124 19 $SIG{'TERM'}= \&_sigExit;
125 }
126  
127  
128 #------------------------------------------------------------------------------
129 # Log a message and exit the programm with the given error code.
130 #------------------------------------------------------------------------------
131 sub Exit($$)
132 {
133 4 ▶ my ($exitCode, $message) = _getParam(@_);
Subroutine Exit: called CC=1 SCAR=0.0
Pod undocumented
134  
135 4 Log( $message );
136 4 exit $exitCode;
137 }
138  
139 #------------------------------------------------------------------------------
140 # Write 'die' messages via Log().
141 #------------------------------------------------------------------------------
142 sub _dieHook
143 {
144 0 ▶ die @_ if $^S;
Subroutine _dieHook: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if $^S 0 0 0
145  
146 0 my @y = split /\n/, $_[0];
147 0 map { Log( $_ ); } @y;
148 };
149 21 $main::SIG{'__DIE__'} = \&_dieHook; # install die hook
150  
151  
152 #------------------------------------------------------------------------------
153 # Write a log message with time stamp to a channel.
154 # $severity, $logtag only required for syslog.
155 #------------------------------------------------------------------------------
156 sub Log(@)
157 {
158 8 ▶ my ($message, $canal, $severity, $logtag) = _getParam(@_);
Subroutine Log: called CC=7 SCAR=20.1
Pod undocumented
159  
160 8 my $prog = $0;
161 8 $prog =~ s|^.*/||;
162 8 my $msg = sprintf "%s: %s: %s\n", $prog, scalar localtime(), $message;
163  
164 8 my $fh;
165 8 my $can = *STDERR;
166
167 8 ▶ if ( !defined $canal )
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $canal) { } 6 2 8
168 {
169 6 ▶ if( defined $Script::Toolbox::Util{'_logFH'}) { $can = $Script::Toolbox::Util{'_logFH'}; }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $Script::Toolbox::Util{"_logFH"}) 1 5 6
170 }else{
171 # canel is defined here
172 2 ▶ if ( ref($canal) eq 'IO::File' ){ $can = $canal; }
Branch: 4 branches
BranchTrueFalseTotal
if (ref $canal eq 'IO::File') { } 0 2 2
elsif ($canal eq 'STDERR') { } 0 2 2
elsif ($canal eq 'STDOUT') { } 1 1 2
elsif ($canal eq 'syslog') { } 0 1 1
173 0 elsif ( $canal eq 'STDERR') { $can = *STDERR; }
174 1 elsif ( $canal eq 'STDOUT') { $can = *STDOUT; }
175 0 elsif ( $canal eq 'syslog') { $can = new IO::File "| logger -p '$severity' -t '$logtag'"; }
176 1 else { $can = _openFromString($canal); }
177 }
178 8 print $can $msg;
179 8 return $msg;
180 }
181  
182 #------------------------------------------------------------------------------
183 # We got a string like "/tmp/x", ">> /tmp/x" or "| someProgram".
184 # Try to open it as a log canal. If it fails open STDERR instead.
185 #------------------------------------------------------------------------------
186 sub _openFromString($)
187 {
188 1 ▶ my ($canal) = @_;
Subroutine _openFromString: called CC=4 SCAR=15.5
189  
190 1 ▶ if( $canal !~ /^\s*>/ && $canal !~ /^\s*[|]/ ) { $canal = '>>' . $canal; }
Branch: 1 branch
BranchTrueFalseTotal
unless ($canal =~ /^\s*>/ or $canal =~ /^\s*[|]/) 1 0 1
Condition33%1/3
$canal =~ /^\s*>/ or $canal =~ /^\s*[|]/
l!l&&r!l&&!r
001
MC/DC0%0/2
$canal =~ /^\s*>/ or $canal =~ /^\s*[|]/
$canal =~ /^\s*>/ $canal =~ /^\s*[|]/
Truth table33%1/3
A or B
A= $canal =~ /^\s*>/B= $canal =~ /^\s*[|]/
ABresult
1X1
011
000
191  
192 1 my $can;
193 1 my $fh = new IO::File "$canal";
194 1 ▶ if( !defined $fh )
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $fh) { } 0 1 1
195 {
196 0 $can = *STDERR;
197 0 printf $can "%s: %s: %s %s\n",
198 $0, scalar localtime(), "WARNING: can't write to", $canal;
199 }else{
200 1 $can = $fh;
201 }
202 1 return $can;
203 }
204  
205 #------------------------------------------------------------------------------
206 # Open a file via IO::File with Fatal handling
207 #------------------------------------------------------------------------------
208 sub Open(@)
209 {
210 20 ▶ my ($file, $iolayer) = _getParam(@_);
Subroutine Open: called CC=1 SCAR=0.0
Pod undocumented
211 20 my $fh = new IO::File;
212 20 $fh->open( "$file" );
213 #$fh->open( "$file" ) || return undef;
214 20 return $fh;
215 }
216 21 ▶use Fatal qw(IO::File::open);
Subroutine BEGIN: called CC=1 SCAR=0.0
217  
218  
219 #------------------------------------------------------------------------------
220 # Format $param as table and return a reference to the result array.
221 #------------------------------------------------------------------------------
222 sub Table(@)
223 {
224 10 ▶ my ($self, $param, $separator) = @_;
Subroutine Table: called CC=2 SCAR=8.1
Pod undocumented
225 10 ▶ if( ref $self ne 'Script::Toolbox') { # only for compatibility
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self ne "Script::Toolbox") 0 10 10
226 0 $separator = $param; # to versions befor 0.50
227 0 $param = $self;
228 0 $self = undef;
229 }
230  
231 10 my $t = Script::Toolbox::TableO->new($param, $separator);
232 10 my @T = $t->asArray();
233 10 return \@T;
234 }
235  
236 #------------------------------------------------------------------------------
237 # $param must be a hash reference. This Hash must have a key "data".
238 # This key may point to:
239 # arrayref
240 # hashref
241 #------------------------------------------------------------------------------
242 sub _noData($)
243 {
244 0 ▶ my ($param) = @_;
Subroutine _noData: not called CC=5 SCAR=34.0
245  
246 0 ▶ return 0 if( ref $param ne 'HASH' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $param ne "HASH" 0 0 0
247 0 ▶ return 0 if( ref $param->{'data'} eq 'HASH' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $param->{"data"} eq "HASH" 0 0 0
248 0 ▶ return 0 if( ref $param->{'data'} eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
if ref $param->{"data"} eq "ARRAY" 0 0 0
249  
250 0 ▶ if( !defined $param->{'data'}[0] )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $param->{"data"}[0]) 0 0 0
251 {
252 0 Log( "WARNING: no input data for Table()." );
253 0 return 1;
254 }
255 0 return 0;
256 }
257  
258 #------------------------------------------------------------------------------
259 # Valid Calls:
260 # [ "csvString", "csvString",...], undef
261 # [ "csvString", "csvString",...], separatorString
262 # [ "TitelString", [headArray], [dataArray],...], undef
263 # [ [dataArray],...], undef
264 # {title=>"", head=>[], data=>[[],[],...] }, undef
265 # {title=>"", head=>[], data=>[{},{},...] }, undef
266 # {title=>"", head=>[], data=>{r1=>{c1=>,c2=>,},r2=>{c1=>,c2=>,},}, undef
267 #------------------------------------------------------------------------------
268 sub _normParam($$)
269 {
270 0 ▶ my ($param, $separator) = @_;
Subroutine _normParam: not called CC=4 SCAR=30.0
271  
272 0 ▶ if( ref $param eq 'HASH' )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $param eq "HASH") 0 0 0
273 {
274 # keine Ahnung wozu: return _sepHash($param, $separator) if( _isCSV($param->{'data'}) );
275 0 return $param;
276 }
277 0 ▶ return _sepTitleHead($param) if( _isTitleHead($param) );
Branch: 1 branch
BranchTrueFalseTotal
if &_isTitleHead($param) 0 0 0
278 0 ▶ return _sepCSV($param, $separator) if( _isCSV($param, $separator) );
Branch: 1 branch
BranchTrueFalseTotal
if &_isCSV($param, $separator) 0 0 0
279 0 return { 'data' => $param };
280 }
281  
282 #------------------------------------------------------------------------------
283 #------------------------------------------------------------------------------
284 sub _sepHash($$)
285 {
286 0 ▶ my ($param,$separator) = @_;
Subroutine _sepHash: not called CC=1 SCAR=6.9
287  
288 0 my $d = _sepCSV($param->{'data'}, $separator);
289 0 $param->{'data'} = $d->{'data'};
290 0 return $param;
291 }
292  
293 # ------------------------------------------------------------------------------
294 # Check if we found the special data array format.
295 # ["TitleString", [headString,headString,...],[data,...],...]
296 #------------------------------------------------------------------------------
297 sub _isTitleHead($)
298 {
299 0 ▶ my ($param) = @_;
Subroutine _isTitleHead: not called CC=3 SCAR=24.8
300  
301 0 ▶ return 1 if( ref \$param->[0] eq 'SCALAR' && ref $param->[1] eq 'ARRAY' );
Branch: 1 branch
BranchTrueFalseTotal
if ref \$param->[0] eq "SCALAR" and ref $param->[1] eq "ARRAY" 0 0 0
Condition0%0/3
ref \$param->[0] eq "SCALAR" and ref $param->[1] eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
ref \$param->[0] eq "SCALAR" and ref $param->[1] eq "ARRAY"
ref \$param->[0] eq "SCALAR" ref $param->[1] eq "ARRAY"
Truth table0%0/3
A and B
A= ref \$param->[0] eq "SCALAR"B= ref $param->[1] eq "ARRAY"
ABresult
0X0
100
111
302 0 return 0;
303 }
304  
305 #------------------------------------------------------------------------------
306 # Transform the special data array
307 # ["TitleString", [headString,headString,...],[data,...],...]
308 # into hash format.
309 #------------------------------------------------------------------------------
310 sub _sepTitleHead($)
311 {
312 0 ▶ my ($param) = @_;
Subroutine _sepTitleHead: not called CC=1 SCAR=6.9
313  
314 0 my $title= splice @{$param}, 0,1;
315 0 my $head = splice @{$param}, 0,1;
316  
317 return {
318 0 'title' => $title,
319 'head' => $head,
320 'data' => $param
321 };
322 }
323  
324  
325 #------------------------------------------------------------------------------
326 # [[],[],...]
327 # [{},{},...]
328 # {r1=>{c1=>,c2=>,},r2=>{c1=>,c2=>,},}
329 #------------------------------------------------------------------------------
330 sub _isCSV($$)
331 {
332 0 ▶ my ($param, $separator) = @_;
Subroutine _isCSV: not called CC=4 SCAR=30.0
333  
334 0 ▶ return 0 if( ref $param ne 'ARRAY' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $param ne "ARRAY" 0 0 0
335  
336 0 ▶ $separator = ';' unless defined $separator; #FIXME default sep
Branch: 1 branch
BranchTrueFalseTotal
unless defined $separator 0 0 0
337 0 ▶ return 1 if( $param->[0] =~ /$separator/ ); #assume it's a CSV record
Branch: 1 branch
BranchTrueFalseTotal
if $param->[0] =~ /$separator/ 0 0 0
338 0 return 0;
339 }
340  
341 #------------------------------------------------------------------------------
342 # Convert an array of CSV strings into an array of arrays.
343 #
344 # [ "a;b","c,d"] becomes
345 # [[a,b], [c,d]]
346 #------------------------------------------------------------------------------
347 sub _sepCSV($$)
348 {
349 0 ▶ my ($param, $separator) = @_;
Subroutine _sepCSV: not called CC=3 SCAR=24.8
350  
351 0 ▶ $separator = ';' if( !defined $separator);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $separator 0 0 0
352 0 my @R;
353 0 foreach my $l ( @{$param} )
354 {
355 0 my @r = split /$separator/, $l;
356 0 push @R, \@r;
357 }
358  
359 0 return { 'data' => \@R };
360 }
361  
362 #------------------------------------------------------------------------------
363 #------------------------------------------------------------------------------
364 sub SetOpsDef($)
365 {
366 1 ▶ my ($self,$opsDef) = @_;
Subroutine SetOpsDef: called CC=1 SCAR=0.0
Pod undocumented
367 1 my $old = $self->{'ops'};
368 1 $self->{'ops'} = Script::Toolbox::Util::Opt->new( $opsDef );
369 1 return ($self->{'ops'}, $old);
370 }
371  
372 #------------------------------------------------------------------------------
373 #------------------------------------------------------------------------------
374 sub GetOpt($)
375 {
376 19 ▶ my ($self,$opt) = @_;
Subroutine GetOpt: called CC=2 SCAR=7.1
Pod undocumented
377 19 ▶ return undef if( ! defined $self->{'ops'} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"ops"} 0 19 19
378 19 return $self->{'ops'}->get($opt);
379 }
380  
381 #------------------------------------------------------------------------------
382 # Read the entire file into an array.
383 # File can be a file name or an IO::File handle.
384 # Chomp all array elements.
385 #------------------------------------------------------------------------------
386 sub FileC(@)
387 {
388 0 ▶ my ($filename,$newContent) = _getParam(@_);
Subroutine FileC: not called CC=4 SCAR=30.0
Pod undocumented
389  
390 0 my $f = undef;
391 0 ▶ if( !defined $newContent) { $f = _ReadFile($filename); }
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $newContent) { } 0 0 0
elsif (ref $newContent eq 'CODE') { } 0 0 0
392 0 elsif( ref $newContent eq 'CODE'){ $f = _ReadFile($filename,$newContent);}
393 0 else { return undef; }
394  
395 0 ▶ chomp @{$f} if( defined $f );
Branch: 1 branch
BranchTrueFalseTotal
if defined $f 0 0 0
396 0 return $f;
397 }
398  
399 #------------------------------------------------------------------------------
400 # Read the entire file into an array or write the new content to the file.
401 # File can be a file name or an IO::File handle.
402 # Newcontent may be a SCALAR value, an ARRAY reference, a HASH reference or
403 # a reference to a callback function.
404 #------------------------------------------------------------------------------
405 sub File(@)
406 {
407 14 ▶ my ($filename,$newContent,$recSep,$fieldSep) = _getParam(@_);
Subroutine File: called CC=3 SCAR=11.0
Pod undocumented
408  
409 14 ▶ if( !defined $newContent) { return _ReadFile($filename); }
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $newContent) { } 4 10 14
elsif (ref $newContent eq 'CODE') { } 1 9 10
410 1 elsif( ref $newContent eq 'CODE'){ return _ReadFile($filename,$newContent);}
411 9 else { _WriteFile($filename,$newContent,$recSep,$fieldSep); }
412 }
413  
414 #------------------------------------------------------------------------------
415 # Read the entire file into a hash or write the new content to the file.
416 # File can be a file name or an IO::File handle.
417 # Newcontent may be a reference to a keyMap HASH or a reference to a callback
418 # function.
419 # The Hash looks like:
420 # keyA1 => keyB1 ... =>keyN1 => value1
421 # keyA2 => keyB2 ... =>keyN2 => value2
422 #------------------------------------------------------------------------------
423 sub KeyMap(@)
424 {
425 2 ▶ my ($filename,$fieldSep,$newContent) = _getParam(@_);
Subroutine KeyMap: called CC=3 SCAR=15.1
Pod undocumented
426  
427 2 ▶ if( !defined $newContent)
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $newContent) { } 2 0 2
elsif (ref $newContent eq 'CODE') { } 0 0 0
428 2 { return _ReadKeyMap($filename, $fieldSep); }
429 elsif( ref $newContent eq 'CODE' )
430 0 { return _ReadKeyMap($filename, $fieldSep, $newContent); }
431 0 else { _WriteKeyMap($filename,$fieldSep,$newContent); }
432 }
433  
434 #------------------------------------------------------------------------------
435 # The Hash looks like:
436 # keyA1 => keyB1 ... =>keyN1 => value1
437 # keyA2 => keyB2 ... =>keyN2 => value2
438 #------------------------------------------------------------------------------
439 sub _WriteKeyMap($$$)
440 {
441 0 ▶ my ($filename,$fieldSep,$newContent) = @_;
Subroutine _WriteKeyMap: not called CC=2 SCAR=17.9
442  
443 0 ▶ $fieldSep = ',' if( !defined $fieldSep );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fieldSep 0 0 0
444  
445 0 my $TXT = '';
446 0 _getCSV( \$TXT, '', $newContent, $fieldSep );
447  
448 0 File( "> $filename", $TXT );
449 }
450  
451 #------------------------------------------------------------------------------
452 # Write a KeyMap (HASH) to a file.
453 #
454 # The Hash looks like:
455 # keyA1 => keyB1 ... =>keyN1 => value1
456 # keyA2 => keyB2 ... =>keyN2 => value2
457 #------------------------------------------------------------------------------
458 sub _getCSV(@)
459 {
460 0 ▶ my ($txt, $prev, $newContent,$fieldSep) = @_;
Subroutine _getCSV: not called CC=3 SCAR=24.8
461  
462 0 my $prefix = '';
463 0 foreach my $k ( sort keys %{$newContent} )
464 {
465 0 $$txt .= $prefix .$k . $fieldSep;
466 0 ▶ if( ref $newContent->{$k} ne 'HASH' )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $newContent->{$k} ne "HASH") 0 0 0
467 {
468 0 $$txt .= $newContent->{$k} . "\n";
469 0 $prefix = $prev;
470 0 next;
471 }
472 0 _getCSV($txt, "$prev$k$fieldSep", $newContent->{$k}, $fieldSep);
473 }
474 }
475  
476 #------------------------------------------------------------------------------
477 #
478 #------------------------------------------------------------------------------
479 sub _checkParam($$)
480 {
481 2 ▶ my ( $fieldSep, $callBack) = @_;
Subroutine _checkParam: called CC=11 SCAR=26.5
482  
483 2 my $def=',';
484 2 ▶ $$fieldSep = $def if( !defined $$fieldSep );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$fieldSep 1 1 2
485  
486 2 my ( $fs, $cb ) = ( $$fieldSep, $$callBack);
487 2 my $rfs = ref $fs; my $rcb = ref $cb;
488
489 2 ▶ my $scalar_code = ref $fs eq '' && ref $cb eq 'CODE';
Condition66%2/3
ref $fs eq '' && ref $cb eq 'CODE'
!ll&&!rl&&r
110
MC/DC0%0/2
ref $fs eq '' && ref $cb eq 'CODE'
ref $fs eq '' ref $cb eq 'CODE'
Truth table66%2/3
A && B
A= ref $fs eq ''B= ref $cb eq 'CODE'
ABresult
0X0
100
111
490 2 ▶ my $scalar_undef= ref $fs eq '' && !defined $cb;
Condition66%2/3
ref $fs eq '' && !defined($cb)
!ll&&!rl&&r
101
MC/DC50%1/2
ref $fs eq '' && !defined($cb)
ref $fs eq '' !defined($cb)
Truth table66%2/3
A && not(B)
A= ref $fs eq ''B= !defined($cb)
ABresult
0X0
100
111
491 2 ▶ my $code_scalar = ref $fs eq 'CODE' && ref $cb eq '' && defined $cb;
Condition66%4/6
ref $fs eq 'CODE' && ref $cb eq '' && defined $cb
!ll&&!rl&&r
110
ref $fs eq "CODE" and ref $cb eq ""
!ll&&!rl&&r
101
MC/DC0%0/3
ref $fs eq 'CODE' && ref $cb eq '' && defined $cb
ref $fs eq "CODE" ref $cb eq "" defined $cb
Truth table50%2/4
A and B && C
A= ref $fs eq "CODE"B= ref $cb eq ""C= defined $cb
ABCresult
0XX0
10X0
1100
1111
492 2 ▶ my $code_undef = ref $fs eq 'CODE' && !defined $cb;
Condition66%2/3
ref $fs eq 'CODE' && !defined($cb)
!ll&&!rl&&r
101
MC/DC50%1/2
ref $fs eq 'CODE' && !defined($cb)
ref $fs eq 'CODE' !defined($cb)
Truth table66%2/3
A && not(B)
A= ref $fs eq 'CODE'B= !defined($cb)
ABresult
0X0
100
111
493  
494 2 ▶ if ( $scalar_code ){return;}
Branch: 4 branches
BranchTrueFalseTotal
if ($scalar_code) { } 0 2 2
elsif ($scalar_undef) { } 1 1 2
elsif ($code_scalar) { } 0 1 1
elsif ($code_undef) { } 1 0 1
495 1 elsif( $scalar_undef){return;}
496 0 elsif( $code_scalar ){$$fieldSep = $cb; $$callBack = $fs;}
497 1 elsif( $code_undef ){$$fieldSep = $def;$$callBack = $fs;}
498 0 else { $$fieldSep = $def; $$callBack = undef;}
499 }
500  
501 #------------------------------------------------------------------------------
502 # Read a CSV file into a hash. The lines of the CSV files are "\n" separated.
503 # Default field separator is ",".
504 # The Hash looks like:
505 # keyA1 => keyB1 ... =>keyN1 => value1
506 # keyA2 => keyB2 ... =>keyN2 => value2
507 #------------------------------------------------------------------------------
508 sub _ReadKeyMap($$$)
509 {
510 2 ▶ my ($file, $fieldSep, $callBack) = @_;
Subroutine _ReadKeyMap: called CC=3 SCAR=11.0
511
512 2 _checkParam(\$fieldSep, \$callBack);
513  
514 2 my $f;
515 2 ▶ if( defined $callBack ) { $f = File( $file,$callBack, $fieldSep ); }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $callBack) { } 1 1 2
516 1 else { $f = File( $file ); }
517 2 chomp( @{$f} );
518  
519 2 my %P;
520 2 foreach my $line ( @{$f} )
521 {
522 4 my @L = split /$fieldSep/, $line;
523 4 _getKV( \%P, @L );
524 }
525 2 return \%P;
526 }
527  
528 #------------------------------------------------------------------------------
529 # Add one line (from @_ array) to the hash. Hash looks like:
530 # key1 => key2 ... =>keyN => value1
531 # key1 => key2 ... =>keyX => value2
532 #------------------------------------------------------------------------------
533 sub _getKV(@)
534 {
535 8 ▶ my ($P, $k, @v) = @_;
Subroutine _getKV: called CC=4 SCAR=13.9
536
537 8 ▶ return if( ! defined $k );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $k 0 8 8
538 8 ▶ if( ref $P->{$k} eq 'HASH' ){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $P->{$k} eq "HASH") 2 6 8
539 2 _getKV( $P->{$k}, @v );
540 2 return;
541 }
542 6 ▶ if( @v == 1 ){
Branch: 1 branch
BranchTrueFalseTotal
if (@v == 1) { } 4 2 6
543 4 $P->{$k} = $v[0];
544 4 return;
545 }else{
546 2 my $x = {};
547 2 $P->{$k} = $x;
548 2 _getKV( $x, @v );
549 }
550 }
551  
552  
553 #------------------------------------------------------------------------------
554 # Open the file in required write mode (default append mode) and write the new
555 # content to the file.
556 # Newcontent can be any kind of data structure.
557 #------------------------------------------------------------------------------
558 sub _WriteFile($$)
559 {
560 9 ▶ my($file,$newContent,$recSep,$fieldSep) =@_;
Subroutine _WriteFile: called CC=7 SCAR=19.6
561  
562 9 my $fh;
563 9 ▶ if( ref $file eq 'IO::File' )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $file eq 'IO::File') { } 0 9 9
564 {
565 0 $fh = $file;
566 }else{
567 9 $file =~ s/^\s+//;
568 9 $file =~ s/^<+//; # write mode only
569 9 ▶ $file = '>>' . $file if( $file !~ /^[|>]/ );
Branch: 1 branch
BranchTrueFalseTotal
unless $file =~ /^[|>]/ 7 2 9
570 9 ▶ $fh = Open( $file ) || return undef;
Condition50%1/2
&Open($file) || (return undef)
l!l
90
MC/DC0%0/1
&Open($file) || (return undef)
&Open($file)
Truth table50%1/2
A
A= &Open($file)
Aresult
11
00
571 }
572 9 ▶ if( ref $newContent eq '' ) {print $fh $newContent;}
Branch: 3 branches
BranchTrueFalseTotal
if (ref $newContent eq '') { } 6 3 9
elsif (&_simpleArray($newContent)) { } 1 2 3
elsif (&_simpleHash($newContent)) { } 1 1 2
573 elsif( _simpleArray( $newContent))
574 1 { _printSimpleArray($newContent, $fh, $recSep)}
575 elsif( _simpleHash( $newContent ))
576 1 { _printSimpleHash($newContent, $fh, $recSep,$fieldSep)}
577 1 else { print $fh Dumper $newContent; }
578 }
579  
580 #------------------------------------------------------------------------------
581 #------------------------------------------------------------------------------
582 sub _printSimpleArray($$$)
583 {
584 1 ▶ my ($content,$fh,$recSep) = @_;
Subroutine _printSimpleArray: called CC=2 SCAR=7.0
585  
586 map
587 {
588 3 ▶ my $rs = defined $recSep ? $recSep : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $recSep ? : 0 3 3
589 3 print $fh "$_$rs";
590 1 } @{$content};
591 }
592  
593 #------------------------------------------------------------------------------
594 #------------------------------------------------------------------------------
595 sub _printSimpleHash($$$$)
596 {
597 1 ▶ my ($content,$fh,$recSep,$fieldSep) = @_;
Subroutine _printSimpleHash: called CC=4 SCAR=14.1
598 1 foreach my $key (sort keys %{$content})
599 {
600 3 ▶ my $rs = defined $recSep ? $recSep : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $recSep ? : 0 3 3
601 3 ▶ my $fs = defined $fieldSep ? $fieldSep : ':';
Branch: 1 branch
BranchTrueFalseTotal
defined $fieldSep ? : 0 3 3
602 3 printf $fh "%s%s%s%s", $key, $fs, $content->{$key},$rs;
603 }
604 1 return;
605 }
606  
607 #------------------------------------------------------------------------------
608 #------------------------------------------------------------------------------
609 sub _simpleHash($)
610 {
611 2 ▶ my ($content) = @_;
Subroutine _simpleHash: called CC=4 SCAR=13.9
612  
613 2 ▶ return 0 if( ref $content ne 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
if ref $content ne "HASH" 0 2 2
614 2 foreach my $key ( keys %{$content} )
615 {
616 6 ▶ return 0 if( ref $content->{$key} ne '' ); # scalar estimated
Branch: 1 branch
BranchTrueFalseTotal
if ref $content->{$key} ne "" 1 5 6
617 }
618 1 return 1;
619 }
620  
621 #------------------------------------------------------------------------------
622 #------------------------------------------------------------------------------
623 sub _simpleArray($)
624 {
625 3 ▶ my ($content) = @_;
Subroutine _simpleArray: called CC=4 SCAR=13.9
626  
627 3 ▶ return 0 if( ref $content ne 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
if ref $content ne "ARRAY" 2 1 3
628 1 foreach my $line ( @{$content} )
629 {
630 3 ▶ return 0 if( ref $line ne '' ); # scalar estimated
Branch: 1 branch
BranchTrueFalseTotal
if ref $line ne "" 0 3 3
631 }
632 1 return 1;
633 }
634  
635 #------------------------------------------------------------------------------
636 # Read the file content into an array and return a referenz to this array.
637 # Return undef if the file isn't readable.
638 # File can be a file name or an IO::File handle.
639 #------------------------------------------------------------------------------
640 sub _ReadFile($$)
641 {
642 5 ▶ my($file,$callBack) =@_;
Subroutine _ReadFile: called CC=5 SCAR=16.5
643  
644 5 my ($fh,@F);
645 5 ▶ if( ref $file eq 'IO::File' )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $file eq 'IO::File') { } 0 5 5
646 {
647 0 $fh = $file;
648 }else{
649 5 $file =~ s/^\s*>+\s*//; # read only mode
650 5 ▶ $fh= Open( $file ) || return undef;
Condition50%1/2
&Open($file) || (return undef)
l!l
50
MC/DC0%0/1
&Open($file) || (return undef)
&Open($file)
Truth table50%1/2
A
A= &Open($file)
Aresult
11
00
651 }
652 5 @F = <$fh>; my $rf = \@F;
653 5 ▶ $rf = &{$callBack}( \@F ) if( defined $callBack );
Branch: 1 branch
BranchTrueFalseTotal
if defined $callBack 1 4 5
654 5 ▶ $rf = \@F if(!defined $rf );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rf 0 5 5
655 5 return $rf;
656 }
657  
658 #------------------------------------------------------------------------------
659 # Without an input argument TmpFile() returns an file handle to an new
660 # temporary file.
661 # Otherwise read the tempfile into an array and return a reference to it.
662 #------------------------------------------------------------------------------
663 sub TmpFile(@)
664 {
665 2 ▶ my ($file) = _getParam(@_);
Subroutine TmpFile: called CC=2 SCAR=6.9
Pod undocumented
666  
667 2 my ($f,@F);
668 2 ▶ if( ref $file eq 'IO::File' ) { $file->seek(0,0); @F = <$file>; $f=\@F; }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $file eq 'IO::File') { } 1 1 2
669 1 else { $f = IO::File::new_tmpfile; }
670 2 return $f;
671 }
672  
673  
674 #------------------------------------------------------------------------------
675 # Return the filenames of a directory as array reference.
676 # Skip '.','..' and all filenames not matching search pattern if a search
677 # pattern is defined.
678 #------------------------------------------------------------------------------
679 sub Dir(@)
680 {
681 6 ▶ my ($dirPath,$searchPattern) = _getParam(@_);
Subroutine Dir: called CC=4 SCAR=13.9
Pod undocumented
682  
683 6 my $d = IO::Dir->new($dirPath);
684 6 ▶ return undef if( !defined $d );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $d 0 6 6
685
686 6 my @D;
687 6 ▶ while( defined($_ = $d->read))
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = $d->read)) 54 6 60
688 {
689 54 ▶ next if( _toSkip( $_, $searchPattern ));
Branch: 1 branch
BranchTrueFalseTotal
if &_toSkip($_, $searchPattern) 28 26 54
690 26 push @D, $_;
691 }
692 6 @D = sort @D;
693 6 return \@D;
694 }
695  
696 #------------------------------------------------------------------------------
697 #------------------------------------------------------------------------------
698 sub _toSkip($$)
699 {
700 54 ▶ my ($line,$pattern) = @_;
Subroutine _toSkip: called CC=6 SCAR=17.9
701  
702 54 ▶ return 1 if( $line =~ /^[.]{1,2}$/ );
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^[.]{1,2}$/ 12 42 54
703 42 ▶ return 0 if( !defined $pattern );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pattern 20 22 42
704  
705 22 ▶ if( $pattern =~ /^\s*!/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($pattern =~ /^\s*!/) { } 4 18 22
706 {
707 4 $pattern = substr($pattern, 1 );
708 4 ▶ return 1 if( $line =~ /$pattern/ );
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /$pattern/ 2 2 4
709 }else{
710 18 ▶ return 1 if( $line !~ /$pattern/ );
Branch: 1 branch
BranchTrueFalseTotal
if not $line =~ /$pattern/ 14 4 18
711 }
712 }
713  
714 #------------------------------------------------------------------------------
715 #------------------------------------------------------------------------------
716 sub Usage($$)
717 {
718 0 ▶ my ($self, $add) = @_;
Subroutine Usage: not called CC=1 SCAR=6.9
Pod undocumented
719  
720 0 return $self->{'ops'}->usage($add);
721 }
722  
723 #------------------------------------------------------------------------------
724 #------------------------------------------------------------------------------
725 sub SetOpt($$$)
726 {
727 1 ▶ my ($self,$opt,$value) = @_;
Subroutine SetOpt: called CC=3 SCAR=11.2
Pod undocumented
728 1 ▶ return undef unless defined $self->{'ops'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"ops"} 0 1 1
729 1 ▶ return undef unless ref($self->{'ops'}) eq 'Script::Toolbox::Util::Opt';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->{"ops"} eq "Script::Toolbox::Util::Opt" 0 1 1
730 1 my $old = $self->{'ops'}->set($opt,$value);
731 1 $self->{$opt} = $value;
732 1 return $old;
733 }
734  
735 #------------------------------------------------------------------------------
736 #------------------------------------------------------------------------------
737 sub _getParam(@)
738 {
739 #if( isa( $_[0], "Script::Toolbox::Util" ))
740 65 ▶ my $x = ref $_[0];
Subroutine _getParam: called CC=3 SCAR=11.0
741 65 ▶ if( $x =~ /Script::Toolbox/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($x =~ /Script::Toolbox/) 25 40 65
742 {
743 25 ▶ shift @_ if( $_[0]->isa("Script::Toolbox::Util" ));
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]->isa("Script::Toolbox::Util") 25 0 25
744 }
745 65 return @_;
746 }
747  
748 #------------------------------------------------------------------------------
749 # Start a shell command with logging.
750 # Return 0 if shell command failed otherwise 1.
751 #------------------------------------------------------------------------------
752 sub System($)
753 {
754 0 ▶ my( $cmd ) = _getParam(@_);
Subroutine System: not called CC=5 SCAR=34.0
Pod undocumented
755  
756 0 my $fh = new IO::File;
757 0 my $pid = $fh->open("$cmd ". '2>&1; echo __RC__$? |' );
758  
759 0 my $rc;
760 0 ▶ while( <$fh> )
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 0 0 0
761 {
762 0 chomp;
763 0 ▶ $rc = $_, next if( /^__RC__/ );
Branch: 1 branch
BranchTrueFalseTotal
if /^__RC__/ 0 0 0
764 0 ▶ next if( /^\s*$/ );
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*$/ 0 0 0
765 0 Log( " $_" );
766 }
767  
768 0 $rc =~ s/__RC__//;
769  
770 0 ▶ return 1 if( $rc == 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $rc == 0 0 0 0
771 0 return 0;
772 }
773  
774 #------------------------------------------------------------------------------
775 # Compute the difference between NOW[+offset] and the time value given as
776 # second parameter. Return a hash reference holding the difference in seconds,
777 # minutes, hours and days. Every value as a floating point number.
778 #
779 # The referenz time (rtime) may be an epoch value or any string parsable by
780 # Time::ParseDate.
781 #------------------------------------------------------------------------------
782 sub _nowDiff($$)
783 {
784 3 ▶ my ($now,$rtime) = @_;
Subroutine _nowDiff: called CC=2 SCAR=6.9
785  
786 3 ▶ $rtime = parsedate( $rtime ) if( $rtime !~ /^[0-9]+$/ );
Branch: 1 branch
BranchTrueFalseTotal
unless $rtime =~ /^[0-9]+$/ 1 2 3
787  
788 3 my $secDiff= $now - $rtime;
789 3 my $D = int $secDiff / 86400; my $x = $secDiff % 86400;
790 3 my $H = int $x / 3600; $x = $x % 3600;
791 3 my $M = int $x / 60; $x = $x % 60;
792 3 my $S = $x;
793  
794 3 my %R;
795 3 $R{seconds}= $secDiff;
796 3 $R{minutes}= $R{seconds} / 60.0;
797 3 $R{hours} = $R{seconds} / 3600.0;
798 3 $R{days} = $R{seconds} / 86400.0;
799 3 $R{DHMS} = sprintf "%dd %.2d:%.2d:%.2d", $D,$H,$M,$S;
800 3 return \%R;
801 }
802  
803  
804 #------------------------------------------------------------------------------
805 # Return the actual date and time. If $format is undef the result is a hash
806 # ref with keys sec,min,hour,mday,mon,year,wday,yday,isdst,epoch.
807 # Mon and year are corrected. Epoch is the time in seconds since 1.1.1970.
808 # If $format is not undef it must be a strftime() format string. The result
809 # of Now() is then the strftime() formated string.
810 # $opt may be {format=><'strftime-format'>, offset=><+-seconds>, diff=><time>}
811 #------------------------------------------------------------------------------
812 sub Now(@)
813 {
814 7 ▶ my( $opt ) = _getParam(@_);
Subroutine Now: called CC=4 SCAR=13.9
Pod undocumented
815  
816 7 ▶ my $offset = defined $opt->{offset} ? $opt->{offset}+0 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $opt->{'offset'} ? : 2 5 7
817 7 my $epoch = time+$offset;
818  
819 7 ▶ return _nowDiff( $epoch, $opt->{diff} ) if( $opt->{diff} );
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"diff"} 3 4 7
820  
821 4 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($epoch);
822  
823 return strftime $opt->{format},
824 $sec,$min,$hour,$mday,$mon, $year,$wday,$yday,$isdst
825 4 ▶ if( defined $opt->{format} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt->{"format"} 2 2 4
826  
827 2 $mon++;
828 2 $year+=1900;
829 2 return {sec=>$sec,min=>$min,hour=>$hour, mday=>$mday,mon=>$mon,year=>$year,
830 wday=>$wday,yday=>$yday,isdst=>$isdst,epoch=>$epoch};
831 }
832  
833 #------------------------------------------------------------------------------
834 #------------------------------------------------------------------------------
835 sub _printMenueHeader($) {
836 0 ▶ my ($head) = @_;
Subroutine _printMenueHeader: not called CC=2 SCAR=17.9
837 0 ▶ return if( ! defined $head );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $head 0 0 0
838 0 printf "%s", $head;
839 }
840  
841 #------------------------------------------------------------------------------
842 #------------------------------------------------------------------------------
843 sub _printMenueFooter($) {
844 0 ▶ my ($foot) = @_;
Subroutine _printMenueFooter: not called CC=2 SCAR=17.9
845 0 ▶ return if( ! defined $foot );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $foot 0 0 0
846 0 printf "%s", $foot;
847 }
848  
849 #------------------------------------------------------------------------------
850 # Compute real index if we have NON-Lable lines in the opts-array.
851 #------------------------------------------------------------------------------
852 sub _getRealIndex($$) {
853 0 ▶ my ($o, $opts) = @_;
Subroutine _getRealIndex: not called CC=4 SCAR=30.0
854  
855 0 my $real = 0;
856 0 my $curr = 0;
857 0 foreach my $op ( @{$opts} )
858 {
859 0 ▶ $real++ if( ! $op->{'label'} );
Branch: 1 branch
BranchTrueFalseTotal
unless $op->{"label"} 0 0 0
860 0 ▶ return $real if( $o == $curr );
Branch: 1 branch
BranchTrueFalseTotal
if $o == $curr 0 0 0
861 0 $real++;
862 0 $curr++;
863 }
864 0 return $real;
865 }
866  
867 #------------------------------------------------------------------------------
868 # Concatenate several header/footer options into header/footer strings.
869 # Return original options array-ref and header/footer strings.
870 #------------------------------------------------------------------------------
871 sub _normMenueOpts($){
872 0 ▶ my ($opts) = @_;
Subroutine _normMenueOpts: not called CC=7 SCAR=40.3
873  
874 0 my $head = '';
875 0 my $foot = '';
876 0 my @OPTS;
877 0 foreach my $l ( @{$opts} ){
878 0 ▶ $head .= sprintf "%s\n", $l->{'header'} if( defined $l->{'header'} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $l->{"header"} 0 0 0
879 0 ▶ $foot .= sprintf "%s\n", $l->{'footer'} if( defined $l->{'footer'} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $l->{"footer"} 0 0 0
880 0 ▶ push @OPTS, $l if( defined $l->{'label'} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $l->{"label"} 0 0 0
881 }
882 0 ▶ $head = $head eq '' ? undef : $head;
Branch: 1 branch
BranchTrueFalseTotal
$head eq '' ? : 0 0 0
883 0 ▶ $foot = $foot eq '' ? undef : $foot;
Branch: 1 branch
BranchTrueFalseTotal
$foot eq '' ? : 0 0 0
884  
885 0 return \@OPTS, $head, $foot;
886 }
887  
888 #------------------------------------------------------------------------------
889 # Display a menue, return the selected index number and the menue data structure.
890 # If a VALUE or DEFAULT key of a menue option points to a value this value can
891 # be changed.
892 # If a jump target is defined, the corresponding function will be called with
893 # argv=> as arguments.
894 # Data structure: [{label=>,value=>,jump=>,argv=>},...]
895 # - label=> must be defined all other keys are optinal
896 # - jump=> must point to a subroutine if set
897 # - argv=> arguments for the subroutine jump points to
898 # - header=> an optional head line
899 # - footer=> an optional footer line
900 #------------------------------------------------------------------------------
901 sub Menue($)
902 {
903 0 ▶ my ($OPTS) = @_;
Subroutine Menue: not called CC=4 SCAR=30.0
Pod undocumented
904  
905 0 my ($opts,$head,$foot) = _normMenueOpts($OPTS);
906 0 my ($i,$o) = (0,0);
907 0 my $maxLen = _maxLabelLength($opts);
908 0 my $form1 = "%3d %-${maxLen}s ";
909 0 system("clear");
910 0 _printMenueHeader($head);
911 0 ($i,$o) = (0,0);
912 0 foreach my $op ( @{$opts} )
913 {
914 0 ▶ next if( ! $op->{'label'} );
Branch: 1 branch
BranchTrueFalseTotal
unless $op->{"label"} 0 0 0
915 0 my ($def,$form)=_getDefForm($form1,$op);
916 0 ▶ if( defined $def ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $def) { } 0 0 0
917 0 printf $form, $i++,$op->{'label'},$def;
918 }else{
919 0 printf $form, $i++,$op->{'label'};
920 }
921 }
922 0 _printMenueFooter($foot);
923 0 printf "\nSelect: ";
924 0 $o = _getNumber( $i-1);
925 0 _setValue($o, $opts);
926 0 _jump($o, $opts); # jump to callback if defined
927 0 return $o,$opts;
928 }
929 #------------------------------------------------------------------------------
930 # Prepare input for Data Menue. Allowed input formats:
931 # INPUT2: [{'label'=>'Max','value'=>'foo'},{'label'=>'Tim','value'=>99}]
932 #------------------------------------------------------------------------------
933 sub _arrayHandler($$) {
934 0 ▶ my ($dataMenue,$opts) = @_;
Subroutine _arrayHandler: not called CC=5 SCAR=34.0
935  
936 0 foreach my $line ( @{$opts} ) {
937 0 ▶ next if( ref $line ne 'HASH' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $line ne "HASH" 0 0 0
938 0 ▶ next if( ! defined $line-{'label'} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line - {"label"} 0 0 0
939 0 ▶ next if( ! defined $line-{'value'} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line - {"value"} 0 0 0
940 0 push @{$dataMenue}, $line,
941 }
942 0 return 'ARRAY';
943 }
944  
945 #------------------------------------------------------------------------------
946 # Prepare input for Data Menue. Allowed input formats:
947 # INPUT3: {}[, {'header'=>'','footer'=>''}]
948 #------------------------------------------------------------------------------
949 sub _hashHandler($$$) {
950 0 ▶ my ($dataMenue,$opts,$frame) = @_;
Subroutine _hashHandler: not called CC=6 SCAR=37.4
951  
952 0 foreach my $l ( sort keys %{$opts} ) {
953 0 ▶ next if( ref $opts->{$l} ne '' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $opts->{$l} ne "" 0 0 0
954 0 my $line = {'label' => $l, 'value' => $opts->{$l}};
955 0 push @{$dataMenue}, $line,
956 }
957 0 ▶ return 'HASH' if( ! defined $frame );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $frame 0 0 0
958  
959 0 ▶ push @{$dataMenue}, {'header' => $frame->{'header'}} if( defined $frame->{'header'});
Branch: 1 branch
BranchTrueFalseTotal
if defined $frame->{"header"} 0 0 0
960 0 ▶ push @{$dataMenue}, {'footer' => $frame->{'footer'}} if( defined $frame->{'footer'});
Branch: 1 branch
BranchTrueFalseTotal
if defined $frame->{"footer"} 0 0 0
961 0 return 'HASH';
962 }
963  
964  
965 #------------------------------------------------------------------------------
966 # Prepare input for Data Menue. Allowed input formats:
967 # INPUT1: 'value1 value2 ..'[, {'header'=>'','footer'=>''}]
968 #------------------------------------------------------------------------------
969 sub _scalarHandler($$$) {
970 0 ▶ my ($dataMenue,$opts,$frame) = @_;
Subroutine _scalarHandler: not called CC=5 SCAR=34.0
971  
972 0 my $i=1;
973 0 foreach my $l ( split /\s+/, $opts ) {
974 0 my $line = {'label' => 'V'.$i++, 'value' => $l};
975 0 push @{$dataMenue}, $line,
976 }
977 0 ▶ return 'SCALAR' if( ! defined $frame );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $frame 0 0 0
978 0 ▶ push @{$dataMenue}, {'header' => $frame->{'header'}} if( defined $frame->{'header'});
Branch: 1 branch
BranchTrueFalseTotal
if defined $frame->{"header"} 0 0 0
979 0 ▶ push @{$dataMenue}, {'footer' => $frame->{'footer'}} if( defined $frame->{'footer'});
Branch: 1 branch
BranchTrueFalseTotal
if defined $frame->{"footer"} 0 0 0
980 0 return 'SCALAR';
981 }
982  
983 #------------------------------------------------------------------------------
984 # Prepare input for Data Menue. Allowed input formats:
985 # INPUT1: 'value1 value2 ..'[, {'header'=>'','footer'=>''}]
986 # INPUT2: [{'label'=>'Max','value'=>'foo'},{'label'=>'Tim','value'=>99}]
987 # INPUT3: {}[, {'header'=>'','footer'=>''}]
988 #------------------------------------------------------------------------------
989 sub _addData($$$)
990 {
991 0 ▶ my ($dataMenue,$opts,$frame) = @_;
Subroutine _addData: not called CC=4 SCAR=30.0
992  
993 0 ▶ return _arrayHandler ($dataMenue,$opts) if(ref $opts eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
if ref $opts eq "ARRAY" 0 0 0
994 0 ▶ return _hashHandler ($dataMenue,$opts,$frame) if(ref $opts eq 'HASH' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $opts eq "HASH" 0 0 0
995 0 ▶ return _scalarHandler($dataMenue,$opts,$frame) if(ref $opts eq '' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $opts eq "" 0 0 0
996 }
997  
998 #------------------------------------------------------------------------------
999 # Remove {label=>"EXIT"} line.
1000 #------------------------------------------------------------------------------
1001 sub _returnArray($) {
1002 0 ▶ my ($dataMenue) = @_;
Subroutine _returnArray: not called CC=1 SCAR=6.9
1003 0 splice @{$dataMenue},0,1;
1004 0 return $dataMenue;
1005 }
1006  
1007 #------------------------------------------------------------------------------
1008 # Remove {label=>"EXIT"} line. Return values as white space separated string.
1009 #------------------------------------------------------------------------------
1010 sub _returnScalar($) {
1011 0 ▶ my ($dataMenue) = @_;
Subroutine _returnScalar: not called CC=2 SCAR=17.9
1012  
1013 0 splice @{$dataMenue},0,1;
1014 0 my $data='';
1015 0 ▶ map { $data .= $_->{'value'} .' ' if( defined $_->{'value'})} @{$dataMenue};
Branch: 1 branch
BranchTrueFalseTotal
if defined $_->{"value"} 0 0 0
1016 0 chop $data;
1017 0 return $data;
1018 }
1019  
1020 #------------------------------------------------------------------------------
1021 # Remove {label=>"EXIT"}, header and footer lines.
1022 # Return values as white hash ref with 'label' as key and 'value' as value.
1023 #------------------------------------------------------------------------------
1024 sub _returnHash($) {
1025 0 ▶ my ($dataMenue) = @_;
Subroutine _returnHash: not called CC=3 SCAR=24.8
1026  
1027 0 splice @{$dataMenue},0,1;
1028 0 my %data;
1029 0 ▶ map { if( defined $_->{'value'} && defined $_->{'label'} ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_->{"value"} and defined $_->{"label"}) 0 0 0
Condition0%0/3
defined $_->{"value"} and defined $_->{"label"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_->{"value"} and defined $_->{"label"}
defined $_->{"value"} defined $_->{"label"}
Truth table0%0/3
A and B
A= defined $_->{"value"}B= defined $_->{"label"}
ABresult
0X0
100
111
1030 0 my $k = $_->{'label'};
1031 0 my $v = $_->{'value'};
1032 0 $data{$k} = $v;
1033 }
1034 0 } @{$dataMenue};
1035 0 return \%data;
1036 }
1037  
1038 #------------------------------------------------------------------------------
1039 # Use Menue to edit small data sets. Two input formats allowed.
1040 # INPUT1: 'value1 value2 ..'
1041 # INPUT2: [{'label'=>'Max','value'=>'foo'},{'label'=>'Tim','value'=>99}]
1042 #------------------------------------------------------------------------------
1043 sub DataMenue(@) {
1044 0 ▶ my ($opts,$head) = @_;
Subroutine DataMenue: not called CC=5 SCAR=34.0
Pod undocumented
1045  
1046 0 my $dataMenue = [{label=>"EXIT"}];
1047 0 my $format = _addData($dataMenue, $opts,$head);
1048  
1049 0 while( 1 ) {
1050 0 my ($o,$dataMenue) = Menue($dataMenue);
1051 0 ▶ last if( $o == 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $o == 0 0 0 0
1052 }
1053 0 ▶ return _returnArray ( $dataMenue) if( $format eq 'ARRAY' );
Branch: 1 branch
BranchTrueFalseTotal
if $format eq "ARRAY" 0 0 0
1054 0 ▶ return _returnScalar($dataMenue) if( $format eq 'SCALAR');
Branch: 1 branch
BranchTrueFalseTotal
if $format eq "SCALAR" 0 0 0
1055 0 ▶ return _returnHash ($dataMenue) if( $format eq 'HASH' );
Branch: 1 branch
BranchTrueFalseTotal
if $format eq "HASH" 0 0 0
1056 }
1057 #------------------------------------------------------------------------------
1058 # Read a directory and return a hash with filenames stat() structure infos
1059 # for every file. An optional pattern (regexp) may be used for selecting files.
1060 #------------------------------------------------------------------------------
1061 sub Stat($$)
1062 {
1063 2 ▶ my ($path,$patt) = _getParam( @_ );
Subroutine Stat: called CC=2 SCAR=6.9
Pod undocumented
1064  
1065 2 my $dir = Dir($path,$patt);
1066  
1067 2 my $stat;
1068 2 foreach my $f ( @{$dir} )
1069 {
1070 17 my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
1071 $atime,$mtime,$ctime,$blksize,$blocks) = CORE::stat("$path/$f");
1072  
1073 17 $stat->{$f}{'dev'} = $dev;
1074 17 $stat->{$f}{'ino'} = $ino;
1075 17 $stat->{$f}{'mode'} = $mode;
1076 17 $stat->{$f}{'nlink'}= $nlink;
1077 17 $stat->{$f}{'uid'} = $uid;
1078 17 $stat->{$f}{'gid'} = $gid;
1079 17 $stat->{$f}{'rdev'} = $rdev;
1080 17 $stat->{$f}{'size'} = $size;
1081 17 $stat->{$f}{'atime'}= $atime;
1082 17 $stat->{$f}{'mtime'}= $mtime;
1083 17 $stat->{$f}{'ctime'}= $ctime;
1084 17 $stat->{$f}{'blksize'} = $blksize;
1085 17 $stat->{$f}{'blocks'} = $blocks;
1086 }
1087 2 return $stat;
1088 }
1089  
1090 #------------------------------------------------------------------------------
1091 # Jump to a callback function of a menue option.
1092 #------------------------------------------------------------------------------
1093 sub _jump($$)
1094 {
1095 0 ▶ my ($o,$menue) = @_;
Subroutine _jump: not called CC=4 SCAR=30.0
1096  
1097 0 ▶ return if( !defined $menue->[$o]->{'jump'} ); #option has no callback
Branch: 1 branch
BranchTrueFalseTotal
unless defined $menue->[$o]{"jump"} 0 0 0
1098  
1099 0 my $call = $menue->[$o]->{'jump'};
1100 0 ▶ my $args = defined $menue->[$o]->{'argv'} ? $menue->[$o]->{'argv'} : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $menue->[$o]{'argv'} ? : 0 0 0
1101  
1102 0 ▶ if( ref $call eq 'CODE' ) { $call->($args); return; }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $call eq "CODE") 0 0 0
1103 0 Log("\nERROR: Can't call function $call(). It's not a code reference.");
1104 0 sleep 5;
1105 }
1106  
1107 #------------------------------------------------------------------------------
1108 # Compute the maximum length of all labels found in the menu array @{$opts}.
1109 #------------------------------------------------------------------------------
1110 sub _maxLabelLength($)
1111 {
1112 0 ▶ my ($opts) = @_;
Subroutine _maxLabelLength: not called CC=4 SCAR=30.0
1113 0 my $len=0;
1114 0 foreach my $op ( @{$opts} )
1115 {
1116 0 ▶ next if( ! defined $op->{'label'});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $op->{"label"} 0 0 0
1117 0 my $l = length($op->{'label'});
1118 0 ▶ $len = $len < $l ? $l : $len;
Branch: 1 branch
BranchTrueFalseTotal
$len < $l ? : 0 0 0
1119 }
1120 0 return $len;
1121 }
1122  
1123 #------------------------------------------------------------------------------
1124 # Compute the default value and the format string.
1125 #------------------------------------------------------------------------------
1126 sub _getDefForm($$)
1127 {
1128 0 ▶ my ($form1,$op) = @_;
Subroutine _getDefForm: not called CC=3 SCAR=24.8
1129  
1130 0 my $def;
1131 0 ▶ $def = $op->{'value'} if( defined $op->{'value'} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $op->{"value"} 0 0 0
1132 0 ▶ my $form = defined $def ? "$form1 [%s]" : $form1;
Branch: 1 branch
BranchTrueFalseTotal
defined $def ? : 0 0 0
1133  
1134 0 return $def,"$form\n";
1135 }
1136  
1137 #------------------------------------------------------------------------------
1138 # Remove last entered character from input. Move cursor one position backward.
1139 #------------------------------------------------------------------------------
1140 sub _delLastChar(){
1141 0 ▶ printf "%c %c", 0x08, 0x08; # backspace,blank,backspace
Subroutine _delLastChar: not called CC=1 SCAR=6.9
1142 }
1143  
1144 #------------------------------------------------------------------------------
1145 # Print one character to STDOUT without buffering.
1146 #------------------------------------------------------------------------------
1147 sub _flushChar($){
1148 0 ▶ my ($char) = @_;
Subroutine _flushChar: not called CC=1 SCAR=6.9
1149 0 my $old = $|;
1150 0 $| = 1;
1151 0 printf "%s", $char;
1152 0 $| = $old;
1153 0 return;
1154 }
1155  
1156 #------------------------------------------------------------------------------
1157 # Check if first digit exeeds the range if used on ten-spot position. In this
1158 # case we got a valid number in the range of 0-9.
1159 #------------------------------------------------------------------------------
1160 sub _unique($$){
1161 0 ▶ my ($o,$maxNum) = @_;
Subroutine _unique: not called CC=3 SCAR=24.8
1162 0 ▶ return 1 if( $$o =~ s/^\n$/0/ ); # set default option 0 if we got ENTER
Branch: 1 branch
BranchTrueFalseTotal
if $$o =~ s/^\n$/0/ 0 0 0
1163 0 ▶ return 1 if( $$o*10 > $maxNum);
Branch: 1 branch
BranchTrueFalseTotal
if $$o * 10 > $maxNum 0 0 0
1164 }
1165  
1166 #------------------------------------------------------------------------------
1167 # Check if first digit is in range. \n is valid for special handling later.
1168 #------------------------------------------------------------------------------
1169 sub _invalid($$){
1170 0 ▶ my ($o,$maxNum) = @_;
Subroutine _invalid: not called CC=3 SCAR=24.8
1171 0 ▶ return 0 if( $o =~ /^\n$/ );
Branch: 1 branch
BranchTrueFalseTotal
if $o =~ /^\n$/ 0 0 0
1172 0 ▶ return 0 if( $o <= $maxNum);
Branch: 1 branch
BranchTrueFalseTotal
if $o <= $maxNum 0 0 0
1173 0 return 1;
1174 }
1175  
1176 #------------------------------------------------------------------------------
1177 # Read the next number from STDIN. Return 0 if given character is not a digit.
1178 # Read two characters if max option number is greater than 9.
1179 # Valid option numbers are 0...99.
1180 #------------------------------------------------------------------------------
1181 sub _getNumber($) {
1182 0 ▶ my ($maxNum) = @_;
Subroutine _getNumber: not called CC=7 SCAR=40.3
1183  
1184 0 my $o;
1185 0 while( 1 ) {
1186 0 $o=_getChar('^\d+$');
1187 0 ▶ next if( _invalid($o,$maxNum) );
Branch: 1 branch
BranchTrueFalseTotal
if &_invalid($o, $maxNum) 0 0 0
1188 0 ▶ last if( _unique(\$o,$maxNum) );
Branch: 1 branch
BranchTrueFalseTotal
if &_unique(\$o, $maxNum) 0 0 0
1189  
1190 0 ▶ if( $maxNum > 9 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($maxNum > 9) 0 0 0
1191 0 _flushChar($o);
1192 0 my $oo=_getChar('^\d+$');
1193 0 ▶ last if( $oo eq "\n" );
Branch: 1 branch
BranchTrueFalseTotal
if $oo eq "\n" 0 0 0
1194 0 $o=10*$o+$oo;
1195 }
1196 0 ▶ last if( $o <= $maxNum && $o > -1 );
Branch: 1 branch
BranchTrueFalseTotal
if $o <= $maxNum and $o > -1 0 0 0
Condition0%0/3
$o <= $maxNum and $o > -1
!ll&&!rl&&r
000
MC/DC0%0/2
$o <= $maxNum and $o > -1
$o <= $maxNum $o > -1
Truth table0%0/3
A and B
A= $o <= $maxNumB= $o > -1
ABresult
0X0
100
111
1197 0 _delLastChar();
1198 }
1199 0 _delLastChar();
1200 0 _flushChar($o);
1201 0 return $o;
1202 }
1203 #------------------------------------------------------------------------------
1204 # Read one character from STDIN. FIXME: stty method is not portable
1205 #------------------------------------------------------------------------------
1206 sub _getChar($)
1207 {
1208 0 ▶ my ($patt) = @_;
Subroutine _getChar: not called CC=3 SCAR=24.8
1209  
1210 0 while( 1 ) {
1211 0 system "stty", '-echo', '-icanon', 'eol', "\001";
1212 0 my $key = getc(STDIN);
1213 0 system "stty", 'echo', 'icanon', 'eol', '^@'; # ASCII null
1214 0 ▶ return $key if( $key =~ /$patt/ );
Branch: 1 branch
BranchTrueFalseTotal
if $key =~ /$patt/ 0 0 0
1215 0 ▶ return $key if( $key =~ /\n/ ); # use default
Branch: 1 branch
BranchTrueFalseTotal
if $key =~ /\n/ 0 0 0
1216 }
1217 }
1218  
1219 #------------------------------------------------------------------------------
1220 # Read a line from STDIN and assign it to the "value" key of an menue option.
1221 # Data structure: [{label=>,value=>,readOnly=>,jump=>,argv=>},...]
1222 # After this function value=> has one of the following values:
1223 # - the read line if not empty
1224 # - the old value if read an empty line and an old value exists
1225 # - the value is not changeable if the key readOnly is defined
1226 #------------------------------------------------------------------------------
1227 sub _setValue($)
1228 {
1229 0 ▶ my ($o,$opts) = @_;
Subroutine _setValue: not called CC=5 SCAR=34.0
1230  
1231 0 ▶ return undef if( !defined $opts->[$o]{'value'} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts->[$o]{"value"} 0 0 0
1232  
1233 0 my $op = $opts->[$o];
1234 0 ▶ my $def = defined $op->{'value'} ? $op->{'value'} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $op->{'value'} ? : 0 0 0
1235  
1236 0 printf "\n%s [%s]:", $op->{'label'}, $def;
1237 0 ▶ return $def if( defined $op->{'readOnly'} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $op->{"readOnly"} 0 0 0
1238  
1239 0 my $resp = <STDIN>;
1240 0 chomp $resp;
1241  
1242 0 ▶ $resp = $def if( $resp eq '' );
Branch: 1 branch
BranchTrueFalseTotal
if $resp eq "" 0 0 0
1243 0 $op->{'value'} = $resp;
1244 0 return $resp;
1245 }
1246  
1247 21 1;
1248 __END__
↑ Summary blib/lib/Script/Toolbox/Util/Menues.pm »