blib/lib/Text/TreeFile.pm

filter: Statementstmt 87.7% 93 / 106 Branchbran 80.0% 48 / 60 Conditioncond 66.6% 20 / 30 MC/DCmcdc 41.1% 7 / 17 Subroutinesub 100% 6 / 6 Podpod 0.0% 0 / 1 totaltotal 79.0% 174 / 220 CC 41 SCAR 28.3
CC
41
Cov
82%
_readspec
20.6
_loadtree
19.6
new
8.0
CRAP 17.0
↑ Summary
1 # file: Text/TreeFile.pm
2 #
3 # Copyright (c) 2000 John Kirk. All rights reserved.
4 # This program is free software; you can redistribute it and/or
5 # modify it under the same terms as Perl itself.
6 #
7 # The latest version of this module should always be available
8 # at any CPAN (http://www.cpan.org) mirror, or from the author:
9 # http://perl.dystanhays.com/jnk
10  
11 package Text::TreeFile;
12  
13 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use vars qw($VERSION @ISA @EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 2 require Exporter;
17 2 require AutoLoader;
18  
19 2 $VERSION=0.39;
20 2 @ISA=qw(Exporter AutoLoader);
21 2 @EXPORT_OK=qw(showlines showglobals);
22  
23 2 require 5.002;
24 2 ▶use FileHandle; # nice to easily have filehandles as plain variables
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 2 my %proto=( 'endq'=>undef,'top'=>undef,'idir'=>undef,'iname'=>undef,
27 'nest'=>undef,'level'=>undef,'line'=>undef,'lev'=>undef, );
28  
29 sub _loadtree;
30 sub _readspec;
31 sub showlines;
32 sub showglobals;
33  
34 3 ▶sub new { my ($that,$iname,$endq)=@_;
Subroutine new: called CC=7 SCAR=20.7
Pod undocumented
35 3 ▶ my $class=ref($that)||$that;
Condition33%1/3
ref $that || $that
l!l&&r!l&&!r
030
MC/DC0%0/2
ref $that || $that
ref $that $that
Truth table33%1/3
A || B
A= ref $thatB= $that
ABresult
1X1
011
000
36 3 my $me={_prop=>\%proto,%proto,};
37 3 bless $me,$class;
38 3 ▶ if(defined $endq) { $$me{endq}=$endq; }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $endq) 1 2 3
39 3 ▶ if(defined $iname) { $$me{iname}=$iname;
Branch: 1 branch
BranchTrueFalseTotal
if (defined $iname) 3 0 3
40 3 $iname=~/^(.*\/)([^\/]*)$/;
41 3 ▶ $$me{idir}=(defined $1 and $1 ne '')?$1:'';
Branch: 1 branch
BranchTrueFalseTotal
defined $1 && $1 ne '' ? : 3 0 3
Condition33%1/3
defined $1 and $1 ne ""
!ll&&!rl&&r
003
MC/DC0%0/2
defined $1 and $1 ne ""
defined $1 $1 ne ""
Truth table33%1/3
A and B
A= defined $1B= $1 ne ""
ABresult
0X0
100
111
42 3 ▶ $$me{top}=_loadtree $me or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$me->{"top"} = _loadtree($me) or return undef 3 0 3
43 }
44 3 return $me;
45 }
46  
47 28 ▶sub _loadtree { my $me=shift;
Subroutine _loadtree: called CC=18 SCAR=29.7
48 28 my ($spec,$cnt);
49 28 ▶ if(!defined $$me{nest}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $me->{"nest"}) 3 25 28
50 3 $$me{nest}=0;
51 3 $$me{lev}=[{'cum'=>0,'lev'=>0,'ifn'=>undef,'ifh'=>undef,'ifl'=>undef,}];
52 3 ▶ if(defined $$me{endq}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $me->{"endq"}) 1 2 3
53 1 $spec=[];
54 1 $cnt=0;
55 }
56 }
57 28 my $lp=$$me{lev}[$$me{nest}];
58 28 ▶ if((!defined $$lp{ifh})&&($$me{nest}==0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $lp->{"ifh"} and $me->{"nest"} == 0) 3 25 28
Condition100%3/3
not defined $lp->{"ifh"} and $me->{"nest"} == 0
!ll&&!rl&&r
2413
MC/DC100%2/2
not defined $lp->{"ifh"} and $me->{"nest"} == 0
not defined $lp->{"ifh"} $me->{"nest"} == 0
Truth table100%3/3
not(A) and B
A= not defined $lp->{"ifh"}B= $me->{"nest"} == 0
ABresult
0X0
100
111
59 3 ▶ if(exists $$me{iname} and defined $$me{iname}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $me->{'iname'} and defined $me->{'iname'}) { } 3 0 3
Condition33%1/3
exists $me->{"iname"} and defined $me->{"iname"}
!ll&&!rl&&r
003
MC/DC0%0/2
exists $me->{"iname"} and defined $me->{"iname"}
exists $me->{"iname"} defined $me->{"iname"}
Truth table33%1/3
A and B
A= exists $me->{"iname"}B= defined $me->{"iname"}
ABresult
0X0
100
111
60 3 $$lp{ifn}=$$me{iname};
61 } else {
62 0 print "_loadtree(): wasn't given a filename at the top file nesting level\n";
63 0 return undef;
64 }
65 }
66 28 ▶ if(!defined $$lp{ifn}) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $lp->{'ifn'}) { } 0 28 28
elsif (not defined $lp->{'ifh'}) { } 4 24 28
67 0 print "_loadtree() got no input filename at file nesting level $$me{nest}\n";
68 0 return undef;
69 } elsif(!defined $$lp{ifh}) {
70 4 $$lp{ifh}=new FileHandle "<$$lp{ifn}";
71 4 $$me{line}=undef;
72 }
73 28 ▶ if(!defined $$lp{ifh}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $lp->{"ifh"}) 0 28 28
74 0 print "_loadtree() couldn't open ifile: $$lp{ifn}\n";
75 0 return undef;
76 }
77 28 ▶ if(!defined $$me{line}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $me->{"line"}) 4 24 28
78 4 ($$me{level},$$me{line})=_readspec $$lp{ifh},$$lp{ifl};
79 }
80 28 ▶ while(defined $$me{line}) { # loop on top-level specs; test eof from readspec
Branch: 1 branch
BranchTrueFalseTotal
if (defined $me->{"line"}) 29 1 30
81 29 ▶ if($$me{line}=~/^include\s+(\S+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($me->{'line'} =~ /^include\s+(\S+)/) { } 1 28 29
82 1 my $iname="$$me{idir}$1";
83 1 my ($cum,$lev)=($$me{lev}[$$me{nest}]{cum},$$me{lev}[$$me{nest}]{lev});
84 1 ++$$me{nest};
85 1 $$me{lev}[$$me{nest}]={
86 'cum'=>$cum+$lev,'lev'=>0,'ifn'=>$iname,'ifh'=>undef,'ifl'=>undef,
87 };
88 0 ▶ if(defined $cnt) { $$spec[$cnt++]=_loadtree($me); }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cnt) { } 0 1 1
89 1 else { $spec =_loadtree($me); }
90  
91 1 --$$me{nest};
92 1 ($$me{level},$$me{line})=_readspec $$lp{ifh},$$lp{ifl};
93 } else {
94 28 my @specs=();
95 28 ▶ if(defined $cnt) { $$spec[$cnt++]=[$$me{line},\@specs]; }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cnt) { } 2 26 28
96 26 else { $spec =[$$me{line},\@specs]; }
97 28 ($$me{level},$$me{line})=_readspec $$lp{ifh},$$lp{ifl};
98 28 my $sublevel=(++$$lp{lev});
99 28 ▶ while((defined $$me{level}) && ($$me{level}==$sublevel)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $me->{"level"} and $me->{"level"} == $sublevel) 24 28 52
Condition66%2/3
defined $me->{"level"} and $me->{"level"} == $sublevel
!ll&&!rl&&r
02824
MC/DC50%1/2
defined $me->{"level"} and $me->{"level"} == $sublevel
defined $me->{"level"} $me->{"level"} == $sublevel
Truth table66%2/3
A and B
A= defined $me->{"level"}B= $me->{"level"} == $sublevel
ABresult
0X0
100
111
100 24 push @specs,_loadtree($me);
101 }
102 28 --$$lp{lev};
103 }
104 29 ▶ last if(!defined $cnt);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cnt 27 2 29
105 }
106 28 return $spec;
107 }
108  
109 33 ▶sub _readspec { my $ifh=shift;
Subroutine _readspec: called CC=18 SCAR=30.2
110 33 ▶ if(!defined $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $_[0]) 4 29 33
111 4 ▶ while(<$ifh>) { # this is the first line of a file
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $ifh)) 10 0 10
112 10 ▶ next if(/^exec\s/ or /^[#;\/]/ or /^\s*\.\.\./ or /^\s*$/);
Branch: 1 branch
BranchTrueFalseTotal
if /^exec\s/ or m[^[#;/]] or /^\s*\.\.\./ or /^\s*$/ 6 4 10
Condition77%7/9
/^exec\s/ or m[^[#;/]] or /^\s*\.\.\./ or /^\s*$/
l!l&&r!l&&!r
424
/^exec\s/ or m[^[#;/]] or /^\s*\.\.\./
l!l&&r!l&&!r
406
/^exec\s/ or m[^[#;/]]
l!l&&r!l&&!r
046
MC/DC50%2/4
/^exec\s/ or m[^[#;/]] or /^\s*\.\.\./ or /^\s*$/
/^exec\s/ m[^[#;/]] /^\s*\.\.\./ /^\s*$/
Truth table60%3/5
A or B or C or D
A= /^exec\s/B= m[^[#;/]]C= /^\s*\.\.\./D= /^\s*$/
ABCDresult
1XXX1
01XX1
001X1
00011
00000
113 4 last;
114 }
115 4 ▶ if(eof($ifh)) {
Branch: 1 branch
BranchTrueFalseTotal
if (eof $ifh) 0 4 4
116 0 $_[0]='';
117 0 return (0,undef);
118 }
119 4 chop;
120 4 $_[0]=$_;
121 }
122 33 ▶ if(!defined $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $_[0]) 0 33 33
123 0 return (undef,undef);
124 }
125 33 ▶ if($_[0] eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] eq "") 3 30 33
126 3 return (0,undef);
127 }
128 30 my ($indent,$line,$str);
129 30 ($indent,$line)=$_[0]=~/^([ ]*)(.*)$/;
130 30 my $level=length($indent)/2;
131 30 ▶ die "indent. not a mult. of two spaces\n" if($level*2!=length($indent));
Branch: 1 branch
BranchTrueFalseTotal
if $level * 2 != length $indent 0 30 30
132 30 ▶ while(<$ifh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $ifh)) 31 3 34
133 31 ▶ next if(/^[#;]/ or /^\s*\.\.\.\s*$/ or /^\s*$/);
Branch: 1 branch
BranchTrueFalseTotal
if /^[#;]/ or /^\s*\.\.\.\s*$/ or /^\s*$/ 4 27 31
Condition83%5/6
/^[#;]/ or /^\s*\.\.\.\s*$/ or /^\s*$/
l!l&&r!l&&!r
2227
/^[#;]/ or /^\s*\.\.\.\s*$/
l!l&&r!l&&!r
2029
MC/DC66%2/3
/^[#;]/ or /^\s*\.\.\.\s*$/ or /^\s*$/
/^[#;]/ /^\s*\.\.\.\s*$/ /^\s*$/
Truth table75%3/4
A or B or C
A= /^[#;]/B= /^\s*\.\.\.\s*$/C= /^\s*$/
ABCresult
1XX1
01X1
0011
0000
134 27 chop;
135 27 ($indent,$str)=/^(\s*)\.\.\.(.*)$/;
136 27 ▶ if(defined $str) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $str) 0 27 27
137 0 $str=~s/^\s+/\ /;
138 0 $line.=$str;
139 0 next;
140 }
141 27 $_[0]=$_;
142 27 last;
143 }
144 30 ▶ if(eof($ifh)) {
Branch: 1 branch
BranchTrueFalseTotal
if (eof $ifh) 6 24 30
145 6 ▶ $_[0]='' if(!defined $_);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 3 3 6
146 }
147 30 return ($level,$line);
148 }
149  
150 2 1;
151  
152 __END__
↑ Summary