blib/lib/Script/Toolbox/Util.pm

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