blib/lib/Script/Toolbox/Util.pm

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