blib/lib/C/Utility.pm

filter: Statementstmt 42.4% 73 / 172 Branchbran 15.1% 10 / 66 Conditioncond 13.3% 2 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 50.0% 15 / 30 Podpod 90.9% 20 / 22 totaltotal 38.2% 120 / 314 CC 40 SCAR 31.1
CC
40
Cov
34%
hash_to_c_file
90.0
brute_force_line
42.0
add_lines
30.0
CRAP 22.4
↑ Summary
1 package C::Utility;
2 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use File::Versions 'make_backup';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use File::Slurper qw/read_text write_text/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use C::Tokenize qw/$comment_re $include $reserved_re/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Text::LineNumber;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 require Exporter;
12  
13 3 our @ISA = qw(Exporter);
14  
15 3 our @EXPORT_OK = qw/
16 add_lines
17 brute_force_line
18 c_string
19 c_to_h_name
20 ch_files
21 convert_to_c_string
22 convert_to_c_string_pc
23 escape_string
24 hash_to_c_file
25 line_directive
26 linein
27 lineout
28 print_bottom_h_wrapper
29 print_top_h_wrapper
30 read_includes
31 remove_quotes
32 stamp_file
33 valid_c_variable
34 /;
35  
36 3 our %EXPORT_TAGS = (
37 'all' => \@EXPORT_OK,
38 );
39  
40 3 our $VERSION = '0.012';
41  
42 sub convert_to_c_string
43 2 ▶{
Subroutine convert_to_c_string: called CC=3 SCAR=11.1
Pod documented
44 2 my ($text) = @_;
45 2 ▶ if (length ($text) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $text == 0) 0 2 2
46 0 return "\"\"";
47 }
48 # Convert backslashes to double backslashes.
49 2 $text =~ s/\\/\\\\/g;
50 # Escape double quotes
51 2 $text = escape_string ($text);
52 # If there was a backslash before a quote, as in \", the first
53 # regex above converted it to \\", and then escape_string
54 # converted that to \\\".
55 2 $text =~ s/\\\\"/\\"/g;
56 # Remove backslashes from before the @ symbol.
57 2 $text =~ s/\\\@/@/g;
58 # Turn each line into a string
59 2 $text =~ s/(.*)\n/"$1\\n"\n/gm;
60 # Catch a final line without any \n at its end.
61 2 ▶ if ($text !~ /\\n\"$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($text =~ /\\n\"$/) 1 1 2
62 1 $text =~ s/(.+)$/"$1"/g;
63 }
64 2 return $text;
65 }
66  
67 sub c_string
68 0 ▶{
Subroutine c_string: not called CC=1 SCAR=6.9
Pod documented
69 0 goto & convert_to_c_string;
70 }
71  
72 sub ch_files
73 0 ▶{
Subroutine ch_files: not called CC=4 SCAR=30.0
Pod documented
74 0 my ($c_file_name) = @_;
75 0 ▶ if ($c_file_name !~ /\.c/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($c_file_name =~ /\.c/) 0 0 0
76 0 die "$c_file_name is not a C file name";
77 }
78 0 my $h_file_name = $c_file_name;
79 0 $h_file_name =~ s/\.c$/\.h/;
80 0 ▶ if (-f $c_file_name) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $c_file_name) 0 0 0
81 0 make_backup ($c_file_name);
82 }
83 0 ▶ if (-f $h_file_name) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $h_file_name) 0 0 0
84 0 make_backup ($h_file_name);
85 }
86 0 return $h_file_name;
87 }
88  
89 sub convert_to_c_string_pc
90 1 ▶{
Subroutine convert_to_c_string_pc: called CC=1 SCAR=0.0
Pod documented
91 1 my ($text) = @_;
92 1 $text =~ s/%/%%/g;
93 1 return convert_to_c_string ($text);
94 }
95  
96 sub escape_string
97 2 ▶{
Subroutine escape_string: called CC=1 SCAR=0.0
Pod documented
98 2 my ($text) = @_;
99 2 $text =~ s/\"/\\\"/g;
100 2 return $text;
101 }
102  
103 sub c_to_h_name
104 0 ▶{
Subroutine c_to_h_name: not called CC=2 SCAR=17.9
Pod documented
105 0 my ($c_file_name) = @_;
106 0 ▶ if ($c_file_name !~ /\.c/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($c_file_name =~ /\.c/) 0 0 0
107 0 die "$c_file_name is not a C file name";
108 }
109 0 my $h_file_name = $c_file_name;
110 0 $h_file_name =~ s/\.c$/\.h/;
111 0 return $h_file_name;
112 }
113  
114 sub valid_c_variable
115 0 ▶{
Subroutine valid_c_variable: not called CC=3 SCAR=24.8
Pod documented
116 0 my ($variable_name) = @_;
117 0 ▶ if ($variable_name !~ /^[A-Za-z_][A-Za-z_0-9]+$/ ||
Branch: 1 branch
BranchTrueFalseTotal
if (not $variable_name =~ /^[A-Za-z_][A-Za-z_0-9]+$/ or $variable_name =~ /^(?:$C::Utility::reserved_re)$/) 0 0 0
Condition0%0/3
not $variable_name =~ /^[A-Za-z_][A-Za-z_0-9]+$/ or $variable_name =~ /^(?:$C::Utility::reserved_re)$/
l!l&&r!l&&!r
000
MC/DC0%0/2
not $variable_name =~ /^[A-Za-z_][A-Za-z_0-9]+$/ or $variable_name =~ /^(?:$C::Utility::reserved_re)$/
not $variable_name =~ /^[A-Za-z_][A-Za-z_0-9]+$/ $variable_name =~ /^(?:$C::Utility::reserved_re)$/
Truth table0%0/3
not(A) or B
A= not $variable_name =~ /^[A-Za-z_][A-Za-z_0-9]+$/B= $variable_name =~ /^(?:$C::Utility::reserved_re)$/
ABresult
1X1
011
000
118 $variable_name =~ /^(?:$reserved_re)$/) {
119 0 return;
120 }
121 0 return 1;
122 }
123  
124 # Wrapper name
125 # BKB 2009-10-05 14:09:41
126  
127 sub wrapper_name
128 0 ▶{
Subroutine wrapper_name: not called CC=2 SCAR=17.9
Pod documented
129 0 my ($string) = @_;
130 0 $string =~ s/[.-]/_/g;
131 0 ▶ if (! valid_c_variable ($string)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (valid_c_variable($string)) 0 0 0
132 0 croak "Bad string for wrapper '$string'";
133 }
134 0 my $wrapper_name = uc $string;
135 0 return $wrapper_name;
136 }
137  
138 sub print_top_h_wrapper
139 0 ▶{
Subroutine print_top_h_wrapper: not called CC=1 SCAR=6.9
Pod documented
140 0 my ($fh, $file_name) = @_;
141
142 0 my $wrapper_name = wrapper_name ($file_name);
143 0 my $wrapper = <<EOF;
144 #ifndef $wrapper_name
145 #define $wrapper_name
146 EOF
147 0 print_out ($fh, $wrapper);
148 }
149  
150 sub print_out
151 0 ▶{
Subroutine print_out: not called CC=3 SCAR=24.8
Pod undocumented
152 0 my ($fh, $wrapper) = @_;
153 0 ▶ if (ref $fh && ref $fh eq 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $fh and ref $fh eq 'SCALAR') { } 0 0 0
Condition0%0/3
ref $fh and ref $fh eq "SCALAR"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $fh and ref $fh eq "SCALAR"
ref $fh ref $fh eq "SCALAR"
Truth table0%0/3
A and B
A= ref $fhB= ref $fh eq "SCALAR"
ABresult
0X0
100
111
154 0 ${$fh} .= $wrapper;
155 }
156 else {
157 0 print $fh $wrapper;
158 }
159 }
160  
161 sub print_bottom_h_wrapper
162 0 ▶{
Subroutine print_bottom_h_wrapper: not called CC=1 SCAR=6.9
Pod documented
163 0 my ($fh, $file_name) = @_;
164 0 my $wrapper_name = wrapper_name ($file_name);
165 0 my $wrapper = <<EOF;
166 #endif /* $wrapper_name */
167 EOF
168 0 print_out ($fh, $wrapper);
169 }
170  
171 sub print_include
172 0 ▶{
Subroutine print_include: not called CC=1 SCAR=6.9
Pod documented
173 0 my ($fh, $h_file_name) = @_;
174 0 print $fh <<EOF;
175 #include "$h_file_name"
176 EOF
177 }
178  
179 sub hash_to_c_file
180 0 ▶{
Subroutine hash_to_c_file: not called CC=9 SCAR=45.0
Pod documented
181 # $prefix is an optional prefix applied to all variables.
182 0 my ($c_file_name, $hash_ref, $prefix) = @_;
183 0 my $h_file_name = ch_files ($c_file_name);
184 0 ▶ die "Not a hash ref" unless ref $hash_ref eq "HASH";
Branch: 1 branch
BranchTrueFalseTotal
unless ref $hash_ref eq "HASH" 0 0 0
185 0 ▶ $prefix = "" unless $prefix;
Branch: 1 branch
BranchTrueFalseTotal
unless $prefix 0 0 0
186 0 ▶ open my $c_out, ">:utf8", $c_file_name or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $c_out, ">:utf8", $c_file_name or die $! 0 0 0
187 0 my (undef, undef, $h_file) = File::Spec->splitpath ($h_file_name);
188 0 print_include ($c_out, $h_file);
189 0 ▶ open my $h_out, ">:utf8", $h_file_name or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $h_out, ">:utf8", $h_file_name or die $! 0 0 0
190 0 print_top_h_wrapper ($h_out, $h_file);
191 0 for my $variable (sort keys %$hash_ref) {
192 0 ▶ if (! valid_c_variable ($variable)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (valid_c_variable($variable)) 0 0 0
193 0 croak "key '$variable' is not a valid C variable";
194 }
195 0 my $value = $hash_ref->{$variable};
196 0 $value = convert_to_c_string ($value);
197 0 print $c_out "const char * $prefix$variable = $value;\n";
198 0 print $h_out "extern const char * $prefix$variable; /* $value */\n";
199 }
200 0 ▶ close $c_out or die $!;
Branch: 1 branch
BranchTrueFalseTotal
close $c_out or die $! 0 0 0
201 0 print_bottom_h_wrapper ($h_out, $h_file);
202 0 ▶ close $h_out or die $!;
Branch: 1 branch
BranchTrueFalseTotal
close $h_out or die $! 0 0 0
203 0 return $h_file_name;
204 }
205  
206 sub line_directive
207 0 ▶{
Subroutine line_directive: not called CC=3 SCAR=24.8
Pod documented
208 0 my ($output, $line_number, $file_name) = @_;
209 0 ▶ die "$line_number is not a positive integer number"
Branch: 1 branch
BranchTrueFalseTotal
unless $line_number =~ /^[0-9]+$/ and $line_number > 0 0 0 0
Condition0%0/3
$line_number =~ /^[0-9]+$/ and $line_number > 0
!ll&&!rl&&r
000
MC/DC0%0/2
$line_number =~ /^[0-9]+$/ and $line_number > 0
$line_number =~ /^[0-9]+$/ $line_number > 0
Truth table0%0/3
A and B
A= $line_number =~ /^[0-9]+$/B= $line_number > 0
ABresult
0X0
100
111
210 unless $line_number =~ /^[0-9]+$/ && $line_number > 0;
211 0 print_out ($output, "#line $line_number \"$file_name\"\n");
212 }
213  
214 sub brute_force_line
215 0 ▶{
Subroutine brute_force_line: not called CC=6 SCAR=37.4
Pod documented
216 0 my ($input_file, $output_file) = @_;
217 0 ▶ open my $input, "<:encoding(utf8)", $input_file or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $input, "<:encoding(utf8)", $input_file or die $! 0 0 0
218 0 ▶ open my $output, ">:encoding(utf8)", $output_file or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $output, ">:encoding(utf8)", $output_file or die $! 0 0 0
219 0 ▶ while (<$input>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $input)) 0 0 0
220 0 print $output "#line $. \"$input_file\"\n";
221 0 print $output $_;
222 }
223 0 ▶ close $input or die $!;
Branch: 1 branch
BranchTrueFalseTotal
close $input or die $! 0 0 0
224 0 ▶ close $output or die $!;
Branch: 1 branch
BranchTrueFalseTotal
close $output or die $! 0 0 0
225 }
226  
227 sub add_lines
228 0 ▶{
Subroutine add_lines: not called CC=5 SCAR=34.0
Pod documented
229 0 my ($input_file) = @_;
230 0 my $full_name = File::Spec->rel2abs ($input_file);
231 0 my $text = '';
232 0 ▶ open my $input, "<:encoding(utf8)", $input_file or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $input, "<:encoding(utf8)", $input_file or die $! 0 0 0
233 0 ▶ while (<$input>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $input)) 0 0 0
234 0 ▶ if (/^#line/) {
Branch: 2 branches
BranchTrueFalseTotal
if (/^#line/) { } 0 0 0
elsif ($. == 1) { } 0 0 0
235 0 my $line_no = $. + 1;
236 0 $text .= "#line $line_no \"$full_name\"\n";
237 }
238 elsif ($. == 1) {
239 0 $text .= "#line 1 \"$full_name\"\n";
240 0 $text .= $_;
241 }
242 else {
243 0 $text .= $_;
244 }
245 }
246 0 return $text;
247 }
248  
249 sub remove_quotes
250 3 ▶{
Subroutine remove_quotes: called CC=1 SCAR=0.0
Pod documented
251 3 my ($string) = @_;
252 3 $string =~ s/^"|"$|"\s*"//g;
253 3 return $string;
254 }
255 #use Data::Dumper;
256 sub linedirective
257 2 ▶{
Subroutine linedirective: called CC=8 SCAR=21.5
Pod undocumented
258 2 my ($intext, $file, $directive) = @_;
259 2 ▶ die unless $intext && $file && $directive;
Branch: 1 branch
BranchTrueFalseTotal
unless $intext and $file and $directive 0 2 2
Condition33%2/6
$intext and $file and $directive
!ll&&!rl&&r
002
$intext and $file
!ll&&!rl&&r
002
MC/DC0%0/3
$intext and $file and $directive
$intext $file $directive
Truth table25%1/4
A and B and C
A= $intextB= $fileC= $directive
ABCresult
0XX0
10X0
1100
1111
260 2 my %renumbered;
261 # Uniquifier for the lines.
262 2 my $count = 0;
263 # This is unlikely to occur.
264 2 my $tag = 'ABRACADABRA';
265 # Watch for blue-moon occurences
266 2 ▶ die if $intext =~ /$tag\d+/;
Branch: 1 branch
BranchTrueFalseTotal
if $intext =~ /$tag\d+/ 0 2 2
267 2 ▶ while ($intext =~ s/^\Q$directive/$tag$count$tag/sm) {
Branch: 1 branch
BranchTrueFalseTotal
if ($intext =~ s/^\Q$directive\E/$tag$count$tag/ms) 3 2 5
268 3 $count++;
269 }
270 # Make the line numbering only after the above substitution, or we
271 # get problems due to changed offsets after the substitution
272 # above.
273 2 my $tln = Text::LineNumber->new ($intext);
274 # "pos" doesn't work well with s///g, so now we need to match the tags
275 # one by one.
276 2 ▶ while ($intext =~ /($tag\d+$tag)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($intext =~ /($tag\d+$tag)/g) 3 2 5
277 3 my $key = $1;
278 3 my $pos = pos ($intext);
279 3 my $line = $tln->off2lnr ($pos);
280 # print "Position $pos in $file = line $line.\n";
281  
282 # The actual line the line directive sends us to is one after
283 # the value the line contains, e.g. on the first line we
284 # should have "#line 2", so we need to add one to the value.
285  
286 3 $renumbered{$key} = $line + 1;
287 }
288 # print Dumper (\%renumbered);
289 2 $intext =~ s/($tag\d+$tag)/#line $renumbered{$1} "$file"/g;
290 # Check for failures. We already checked this doesn't occur
291 # naturally in the file above.
292 2 ▶ die if $intext =~ /$tag\d+$tag/;
Branch: 1 branch
BranchTrueFalseTotal
if $intext =~ /$tag\d+$tag/ 0 2 2
293 2 return $intext;
294 }
295  
296 sub linein
297 1 ▶{
Subroutine linein: called CC=1 SCAR=0.0
Pod documented
298 1 my ($infile) = @_;
299 1 my $intext = read_text ($infile);
300 1 $intext = linedirective ($intext, $infile, '#linein');
301 1 return $intext;
302 }
303  
304 sub lineout
305 1 ▶{
Subroutine lineout: called CC=1 SCAR=0.0
Pod documented
306 1 my ($outtext, $outfile) = @_;
307  
308 1 $outtext = linedirective ($outtext, $outfile, "#lineout");
309 1 write_text ($outfile, $outtext);
310 }
311  
312 sub stamp_file
313 0 ▶{
Subroutine stamp_file: not called CC=2 SCAR=17.9
Pod documented
314 0 my ($fh, $name) = @_;
315 0 ▶ if (! defined $name) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $name) 0 0 0
316 0 $name = "This C file";
317 }
318 0 my $now = scalar localtime ();
319 0 my $stamp =<<EOF;
320 /*
321 $name was generated by $0 at $now.
322 */
323 EOF
324 0 print_out ($fh, $stamp);
325 }
326  
327 sub read_includes
328 0 ▶{
Subroutine read_includes: not called CC=2 SCAR=17.9
Pod documented
329 0 my ($file) = @_;
330 0 my $text = read_text ($file);
331 # Remove all the comments from the file so that things like
332 # /*#include "something.h"*/ don't create false positives.
333 0 $text =~ s/$comment_re//g;
334 0 my @hfiles;
335 0 ▶ while ($text =~ /$include/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /$C::Utility::include/g) 0 0 0
336 0 push @hfiles, $1;
337 }
338 0 return \@hfiles;
339 }
340  
341 3 1;
↑ Summary