Firemen/Load.pm

filter: Statementstmt 68.0% 81 / 119 Branchbran 72.9% 35 / 48 Conditioncond 50.0% 6 / 12 MC/DCmcdc 50.0% 4 / 8 Subroutinesub 88.8% 8 / 9 Podpod 100% 3 / 3 totaltotal 68.8% 137 / 199 CC 31 SCAR 37.4
CC
31
Cov
68%
loadDatabase
132.0
loadConfig
10.0
loadCDB
7.0
CRAP 42.1
« Firemen/Common.pm ↑ Summary Firemen/Change.pm »
1 #!/usr/bin/perl
2 ######################
3 #
4 # Copyright (C) 2011 TU Clausthal, Institut fuer Maschinenwesen, Joachim Langenbach
5 #
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #
19 ######################
20  
21 # Pod::Weaver infos
22 # ABSTRACT: Module to parse files from Firemen (like pro and cdb files)
23  
24 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 package CAD::Firemen::Load;
28 {
29 12 $CAD::Firemen::Load::VERSION = '0.7.2';
30 }
31 12 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
32 12 our @EXPORT_OK = qw(loadConfig loadCDB loadDatabase);
33 12 ▶use POSIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 12 ▶use CAD::Firemen::Common qw(strip printColored);
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36  
37 22 ▶sub loadConfig {
Subroutine loadConfig: called CC=10 SCAR=23.0
Pod documented
38 22 my $cfgUrl = shift;
39 22 my $CFG;
40 22 my %options= ();
41 22 my %errors = ();
42  
43 22 ▶ if(!defined($cfgUrl) || $cfgUrl eq ""){
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $cfgUrl or $cfgUrl eq "") 2 20 22
Condition100%3/3
not defined $cfgUrl or $cfgUrl eq ""
l!l&&r!l&&!r
1120
MC/DC100%2/2
not defined $cfgUrl or $cfgUrl eq ""
not defined $cfgUrl $cfgUrl eq ""
Truth table100%3/3
not(A) or B
A= not defined $cfgUrlB= $cfgUrl eq ""
ABresult
1X1
011
000
44 2 $errors{0} = "No URL given";
45 2 return (\%options, \%errors, 0);
46 }
47  
48 # Get all possible options listed within the cdb file
49 20 ▶ if(!open($CFG, "<", $cfgUrl)){
Branch: 1 branch
BranchTrueFalseTotal
unless (open $CFG, "<", $cfgUrl) 1 19 20
50 1 $errors{0} = "Could not open file! (". $cfgUrl .")";
51 1 return (\%options, \%errors, 0);
52 }
53  
54 19 my $i = 0;
55 19 ▶ while(<$CFG>){
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $CFG)) 2056 19 2075
56 2056 my $line = strip($_);
57 2056 $i++;
58 # skip comments and mapkeys
59 2056 ▶ if($line !~ m/^\s*!/){
Branch: 1 branch
BranchTrueFalseTotal
unless ($line =~ /^\s*!/) 947 1109 2056
60 947 ▶ if($line =~ m/^\s*mapkey/){
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*mapkey/) { } 4 943 947
61 # handle mapkeys here
62 }
63 else{
64 943 ▶ if($line =~ m/^([^\s]+)\s+([^!]+)/){
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^([^\s]+)\s+([^!]+)/) { } 506 437 943
65 506 my $opt = uc($1);
66 506 ▶ if(!exists($options{$opt})){
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $options{$opt}) 503 3 506
67 503 $options{$opt} = {};
68 }
69 506 $options{$opt}->{strip($i)} = strip($2);
70 }
71 else{
72 437 ▶ if($line ne ""){
Branch: 1 branch
BranchTrueFalseTotal
if ($line ne "") 7 430 437
73 7 $errors{$i} = "Detected uncommented line without an option";
74 }
75 }
76 }
77 }
78 }
79  
80 19 close($CFG);
81  
82 19 return (\%options, \%errors, $i);
83 }
84  
85 24 ▶sub loadCDB {
Subroutine loadCDB: called CC=7 SCAR=19.5
Pod documented
86 24 my $cdbUrl = shift;
87 24 my $CDB;
88 24 my %results = ();
89 24 my %errors = ();
90  
91 24 ▶ if(!defined($cdbUrl) || $cdbUrl eq ""){
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $cdbUrl or $cdbUrl eq "") 2 22 24
Condition100%3/3
not defined $cdbUrl or $cdbUrl eq ""
l!l&&r!l&&!r
1122
MC/DC100%2/2
not defined $cdbUrl or $cdbUrl eq ""
not defined $cdbUrl $cdbUrl eq ""
Truth table100%3/3
not(A) or B
A= not defined $cdbUrlB= $cdbUrl eq ""
ABresult
1X1
011
000
92 2 $errors{0} = "No URL given";
93 2 return (\%results, \%errors);
94 }
95  
96 # Get all possible options listed within the cdb file
97 22 ▶ if(!open($CDB, "<", $cdbUrl)){
Branch: 1 branch
BranchTrueFalseTotal
unless (open $CDB, "<", $cdbUrl) 4 18 22
98 4 $errors{0} = "Could not open file! (". $cdbUrl .")";
99 4 return (\%results, \%errors);
100 }
101  
102 18 my $i = 1;
103 18 ▶ while(<$CDB>){
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $CDB)) 14953 18 14971
104 14953 my $line = strip($_);
105 14953 ▶ if($line =~ m/^([^\s]+)\s{0,}\( -[\w\d]+\s{0,}\)/){
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^([^\s]+)\s{0,}\( -[\w\d]+\s{0,}\)/) 14881 72 14953
106 14881 my $ref;
107 14881 my $error = "";
108 14881 my $lineNumber = $i;
109 14881 ($ref, $i, $error) = _extractParameters($CDB, $i);
110 14881 $results{uc($1)} = { $lineNumber => $ref };
111 14881 ▶ if($error ne ""){
Branch: 1 branch
BranchTrueFalseTotal
if ($error ne "") 1 14880 14881
112 1 $errors{$i} = $error;
113 }
114 # this can not be treated as an error, because many options does not have values supplied
115 #if(scalar(@opts) < 1){
116 # $errors{$i} = "WARNING: Could not get a list of parameters for option ". $1;
117 #}
118 }
119 14953 $i++;
120 }
121  
122 18 close($CDB);
123  
124 18 return (\%results, \%errors);
125 }
126  
127 0 ▶sub loadDatabase {
Subroutine loadDatabase: not called CC=11 SCAR=48.8
Pod documented
128 0 my $dbh = shift;
129 0 my $sqlQuery = shift;
130 0 my $verbose = shift;
131  
132 0 my %options = ();
133 0 my %errors = ();
134 0 my %descriptions = ();
135  
136 0 ▶ if(!defined($verbose)){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $verbose) 0 0 0
137 0 $verbose = 0;
138 }
139 0 ▶ if(!$dbh){
Branch: 1 branch
BranchTrueFalseTotal
unless ($dbh) 0 0 0
140 0 $errors{0} = "No database handle given";
141 0 return (\%options, \%errors, \%descriptions);
142 }
143 0 ▶ if(!defined($sqlQuery) || ($sqlQuery eq "")){
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $sqlQuery or $sqlQuery eq "") 0 0 0
Condition0%0/3
not defined $sqlQuery or $sqlQuery eq ""
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $sqlQuery or $sqlQuery eq ""
not defined $sqlQuery $sqlQuery eq ""
Truth table0%0/3
not(A) or B
A= not defined $sqlQueryB= $sqlQuery eq ""
ABresult
1X1
011
000
144 0 $errors{0} = "No SQL Query string given";
145 0 return (\%options, \%errors, \%descriptions);
146 }
147 0 my $ref = $dbh->selectall_hashref($sqlQuery, 'name');
148 0 ▶ if(!$ref){
Branch: 1 branch
BranchTrueFalseTotal
unless ($ref) 0 0 0
149 0 $errors{0} = "Error ". $dbh->err ." (". $dbh->errstr .")";
150 0 return (\%options, \%errors, \%descriptions);
151 }
152 0 my %results = %{$ref};
153 0 foreach my $name (keys(%results)){
154 0 $sqlQuery = "SELECT vals.id as id, vals.name as name FROM options_values as vals, options_has_values opthasvals";
155 0 $sqlQuery .= " WHERE vals.id=opthasvals.valuesId";
156 0 $sqlQuery .= " AND opthasvals.optionsId=". $results{$name}->{"id"};
157 0 $ref = $dbh->selectall_hashref($sqlQuery, "id");
158 0 ▶ if(!defined($ref)){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ref) 0 0 0
159 0 next;
160 }
161 0 my %tmpValues = ();
162 0 foreach my $valId (keys(%{$ref})){
163 0 $tmpValues{$ref->{$valId}->{"name"}} = 0;
164 0 ▶ if(defined($results{$name}->{"defaultValueId"}) && ($valId == $results{$name}->{"defaultValueId"})){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $results{$name}{"defaultValueId"} and $valId == $results{$name}{"defaultValueId"}) 0 0 0
Condition0%0/3
defined $results{$name}{"defaultValueId"} and $valId == $results{$name}{"defaultValueId"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $results{$name}{"defaultValueId"} and $valId == $results{$name}{"defaultValueId"}
defined $results{$name}{"defaultValueId"} $valId == $results{$name}{"defaultValueId"}
Truth table0%0/3
A and B
A= defined $results{$name}{"defaultValueId"}B= $valId == $results{$name}{"defaultValueId"}
ABresult
0X0
100
111
165 0 $tmpValues{$ref->{$valId}->{"name"}} = 1;
166 }
167 }
168 # we use line 0 here, because we do not know the line
169 0 $options{$name} = { 0 => \%tmpValues};
170 0 $descriptions{$name} = $results{$name}->{"description"};
171 }
172  
173 0 return (\%options, \%errors, \%descriptions);
174 }
175  
176 14881 ▶sub _extractParameters {
Subroutine _extractParameters: called CC=6 SCAR=18.0
177 14881 my $CDB = shift;
178 # the line number
179 14881 my $i = shift;
180 14881 my $start = 0;
181 14881 my %values = ();
182 14881 ▶ while(<$CDB>){
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $CDB)) 59087 0 59087
183 # raise it here, because we have read a new line already
184 59087 $i++;
185 59087 my $line = strip($_);
186 59087 ▶ if($line eq "{"){
Branch: 3 branches
BranchTrueFalseTotal
if ($line eq '{') { } 14880 44207 59087
elsif ($line eq '}') { } 14881 29326 44207
elsif ($start) { } 29323 3 29326
187 14880 $start = 1;
188 }
189 elsif($line eq "}"){
190 14881 ▶ if(!$start){
Branch: 1 branch
BranchTrueFalseTotal
unless ($start) 1 14880 14881
191 # we found an end, without a beginning,
192 # therefore it was an error
193 1 return (\%values, $i, "No opening bracket found");
194 }
195 14880 return (\%values, $i, "");
196 }
197 elsif($start){
198 29323 $values{$line} = 0;
199 }
200 }
201 0 my %tmp = ();
202 0 return (\%tmp, $i, "No opening or closing bracket found");
203 }
204  
205 12 1;
206  
207 __END__
« Firemen/Common.pm ↑ Summary Firemen/Change.pm »