Firemen/Load.pm

filter: Statementstmt 15.9% 19 / 119 Branchbran 0.0% 0 / 48 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 55.5% 5 / 9 Podpod 100% 3 / 3 totaltotal 13.5% 27 / 199 CC 31 SCAR 44.1
CC
31
Cov
11%
loadDatabase
132.0
loadConfig
110.0
loadCDB
56.0
CRAP 82.1
« Firemen/Change.pm ↑ Summary Firemen/Option/Check.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 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 package CAD::Firemen::Load;
28 {
29 6 $CAD::Firemen::Load::VERSION = '0.7.1';
30 }
31 6 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
32 6 our @EXPORT_OK = qw(loadConfig loadCDB loadDatabase);
33 6 ▶use POSIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 6 ▶use CAD::Firemen::Common qw(strip printColored);
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36  
37 sub loadConfig {
38 0 ▶ my $cfgUrl = shift;
Subroutine loadConfig: not called CC=10 SCAR=47.0
Pod documented
39 0 my $CFG;
40 0 my %options= ();
41 0 my %errors = ();
42  
43 0 ▶ if(!defined($cfgUrl) || $cfgUrl eq ""){
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $cfgUrl or $cfgUrl eq "") 0 0 0
Condition0%0/3
not defined $cfgUrl or $cfgUrl eq ""
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $cfgUrl or $cfgUrl eq ""
not defined $cfgUrl $cfgUrl eq ""
Truth table0%0/3
not(A) or B
A= not defined $cfgUrlB= $cfgUrl eq ""
ABresult
1X1
011
000
44 0 $errors{0} = "No URL given";
45 0 return (\%options, \%errors, 0);
46 }
47  
48 # Get all possible options listed within the cdb file
49 0 ▶ if(!open($CFG, "<", $cfgUrl)){
Branch: 1 branch
BranchTrueFalseTotal
unless (open $CFG, "<", $cfgUrl) 0 0 0
50 0 $errors{0} = "Could not open file! (". $cfgUrl .")";
51 0 return (\%options, \%errors, 0);
52 }
53  
54 0 my $i = 0;
55 0 ▶ while(<$CFG>){
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $CFG)) 0 0 0
56 0 my $line = strip($_);
57 0 $i++;
58 # skip comments and mapkeys
59 0 ▶ if($line !~ m/^\s*!/){
Branch: 1 branch
BranchTrueFalseTotal
unless ($line =~ /^\s*!/) 0 0 0
60 0 ▶ if($line =~ m/^\s*mapkey/){
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*mapkey/) { } 0 0 0
61 # handle mapkeys here
62 }
63 else{
64 0 ▶ if($line =~ m/^([^\s]+)\s+([^!]+)/){
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^([^\s]+)\s+([^!]+)/) { } 0 0 0
65 0 my $opt = uc($1);
66 0 ▶ if(!exists($options{$opt})){
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $options{$opt}) 0 0 0
67 0 $options{$opt} = {};
68 }
69 0 $options{$opt}->{strip($i)} = strip($2);
70 }
71 else{
72 0 ▶ if($line ne ""){
Branch: 1 branch
BranchTrueFalseTotal
if ($line ne "") 0 0 0
73 0 $errors{$i} = "Detected uncommented line without an option";
74 }
75 }
76 }
77 }
78 }
79  
80 0 close($CFG);
81  
82 0 return (\%options, \%errors, $i);
83 }
84  
85 sub loadCDB {
86 0 ▶ my $cdbUrl = shift;
Subroutine loadCDB: not called CC=7 SCAR=40.3
Pod documented
87 0 my $CDB;
88 0 my %results = ();
89 0 my %errors = ();
90  
91 0 ▶ if(!defined($cdbUrl) || $cdbUrl eq ""){
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $cdbUrl or $cdbUrl eq "") 0 0 0
Condition0%0/3
not defined $cdbUrl or $cdbUrl eq ""
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $cdbUrl or $cdbUrl eq ""
not defined $cdbUrl $cdbUrl eq ""
Truth table0%0/3
not(A) or B
A= not defined $cdbUrlB= $cdbUrl eq ""
ABresult
1X1
011
000
92 0 $errors{0} = "No URL given";
93 0 return (\%results, \%errors);
94 }
95  
96 # Get all possible options listed within the cdb file
97 0 ▶ if(!open($CDB, "<", $cdbUrl)){
Branch: 1 branch
BranchTrueFalseTotal
unless (open $CDB, "<", $cdbUrl) 0 0 0
98 0 $errors{0} = "Could not open file! (". $cdbUrl .")";
99 0 return (\%results, \%errors);
100 }
101  
102 0 my $i = 1;
103 0 ▶ while(<$CDB>){
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $CDB)) 0 0 0
104 0 my $line = strip($_);
105 0 ▶ if($line =~ m/^([^\s]+)\s{0,}\( -[\w\d]+\s{0,}\)/){
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^([^\s]+)\s{0,}\( -[\w\d]+\s{0,}\)/) 0 0 0
106 0 my $ref;
107 0 my $error = "";
108 0 my $lineNumber = $i;
109 0 ($ref, $i, $error) = _extractParameters($CDB, $i);
110 0 $results{uc($1)} = { $lineNumber => $ref };
111 0 ▶ if($error ne ""){
Branch: 1 branch
BranchTrueFalseTotal
if ($error ne "") 0 0 0
112 0 $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 0 $i++;
120 }
121  
122 0 close($CDB);
123  
124 0 return (\%results, \%errors);
125 }
126  
127 sub loadDatabase {
128 0 ▶ my $dbh = shift;
Subroutine loadDatabase: not called CC=11 SCAR=48.8
Pod documented
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 sub _extractParameters {
177 0 ▶ my $CDB = shift;
Subroutine _extractParameters: not called CC=6 SCAR=37.4
178 # the line number
179 0 my $i = shift;
180 0 my $start = 0;
181 0 my %values = ();
182 0 ▶ while(<$CDB>){
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $CDB)) 0 0 0
183 # raise it here, because we have read a new line already
184 0 $i++;
185 0 my $line = strip($_);
186 0 ▶ if($line eq "{"){
Branch: 3 branches
BranchTrueFalseTotal
if ($line eq '{') { } 0 0 0
elsif ($line eq '}') { } 0 0 0
elsif ($start) { } 0 0 0
187 0 $start = 1;
188 }
189 elsif($line eq "}"){
190 0 ▶ if(!$start){
Branch: 1 branch
BranchTrueFalseTotal
unless ($start) 0 0 0
191 # we found an end, without a beginning,
192 # therefore it was an error
193 0 return (\%values, $i, "No opening bracket found");
194 }
195 0 return (\%values, $i, "");
196 }
197 elsif($start){
198 0 $values{$line} = 0;
199 }
200 }
201 0 my %tmp = ();
202 0 return (\%tmp, $i, "No opening or closing bracket found");
203 }
204  
205 6 1;
206  
207 __END__
« Firemen/Change.pm ↑ Summary Firemen/Option/Check.pm »