blib/lib/Text/NumericData/App/txdfilter.pm

filter: Statementstmt 85.7% 114 / 133 Branchbran 59.7% 43 / 72 Conditioncond 33.3% 6 / 18 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 100% 9 / 9 Podpod 0.0% 0 / 7 totaltotal 69.0% 172 / 249 SCAR 45.0
CC
47
Cov
73%
process_line
49.6
header_workout
24.9
prepare
6.0
CRAP 90.0
« blib/lib/Text/NumericData/App/txdcalc.pm ↑ Summary blib/lib/Text/NumericData/App/txdodeint.pm »
1 package Text::NumericData::App::txdfilter;
2  
3 1 ▶use Text::NumericData::App;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 # This is just a placeholder because of a past build system bug.
8 # The one and only version for Text::NumericData is kept in
9 # the Text::NumericData module itself.
10 1 our $VERSION = '1';
11 1 $VERSION = eval $VERSION;
12  
13 #the infostring says it all
14 1 my $infostring = 'filter textual data files
15  
16 This program filters/transforms textual data files (pipe operation) concering the syntax and header stuff. The data itself is preserved. Any Parameters after options are file title and data column titles (overriding the named parameters).';
17  
18 1 our @ISA = ('Text::NumericData::App');
19  
20 sub new
21 {
22 1 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
23 1 my @pars =
24 (
25 'touchdata',1,'','touch the data lines (otherwise just copy them)',
26 'touchhead',1,'','touch the header (otherwise just copy)',
27 'newhead',0,'n','make completely new header',
28 'comment',[],'C','comment to include between file title and column titles, lines in array',
29 'headlines',undef,'H','use this fixed number of lines as header (overriding any heuristics)',
30 'delaftercom',0,'D','delete any comment lines after data',
31 'lhex','','L','regex for last header line (alternative to fdex)',
32 'fdex','','F','regex for first data line (alterative to lhex)',
33 'title',undef,'t','new title',
34 'coltitles',[],'i','new column titles (as in "title1","title2","title3")',
35 'modtitles',{},'m','modify existing titles... hash with column indices as key, in Perl: (1=>"NeSpalte",4=>"AndereSpalte")',
36 'origin',0,'o','create Origin-friendly format with tab separation and coltitles as only header line and NO comment character, triggers also quote=0 and delaftercom=1',
37 'data',1,'','include the data in printout',
38 'head',1,'','include the header in printout',
39 'history',0,'','keep old title(s) lines as historic comments (writing new overall title before, new column titles below), otherwise replace them',
40 'index',0,'x','Add a dataset index as first column (maybe just to make other tools happy so with the data lines not starting with text). You can influence the column name via modtitles.'
41 );
42  
43 1 return $class->SUPER::new
44 ({
45 parconf=>
46 {
47 info=>$infostring
48 # default copyright
49 # default version
50 }
51 ,pardef=>\@pars
52 ,pipemode=>1
53 ,pipe_init=>\&prepare
54 ,pipe_begin=>\&init
55 ,pipe_line=>\&process_line
56 ,pipe_end=>\&endhook
57 });
58 }
59  
60 sub prepare
61 {
62 7 ▶ my $self = shift;
Subroutine prepare: called CC=6 SCAR=17.9
Pod undocumented
63 7 my $param = $self->{param};
64  
65 7 ▶ if($param->{origin})
Branch: 1 branch
BranchTrueFalseTotal
if ($param->{"origin"}) 1 6 7
66 {
67 1 $param->{comchar} = '';
68 1 $param->{outsep} = "\t";
69 1 $param->{quote} = 0;
70 1 $param->{delaftercom} = 1;
71 }
72  
73 #plain command line parameters are file and column titles
74 7 ▶ $self->{title} = @{$self->{argv}} ? shift(@{$self->{argv}}) : $param->{title};
Branch: 1 branch
BranchTrueFalseTotal
@{$self->{'argv'};} ? : 2 5 7
75 7 ▶ $self->{titles} = @{$self->{argv}} ? $self->{argv} : $param->{coltitles};
Branch: 1 branch
BranchTrueFalseTotal
@{$self->{'argv'};} ? : 2 5 7
76 # precompile header/data matches
77 7 ▶ $self->{lhex} = qr/$param->{lhex}/ if $param->{lhex} ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $param->{"lhex"} ne "" 0 7 7
78 7 ▶ $self->{fdex} = qr/$param->{fdex}/ if $param->{fdex} ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $param->{"fdex"} ne "" 0 7 7
79  
80 7 return 0;
81 }
82  
83 sub init
84 {
85 7 ▶ my $self = shift;
Subroutine init: called CC=1 SCAR=0.0
Pod undocumented
86 7 $self->new_txd();
87 #storage for old header lines
88 7 $self->{headlines} = [];
89 #counter/switch
90 7 $self->{l} = 0;
91 7 $self->{lasthead} = 0;
92 }
93  
94 sub process_line
95 {
96 32 ▶ my $self = shift;
Subroutine process_line: called CC=20 SCAR=39.0
Pod undocumented
97 32 my $c = $self->{txd};
98 32 my $param = $self->{param};
99 32 my $pre = ''; # prepend text just before output
100  
101 32 ▶ if(!$self->{state}{data})
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"state"}{"data"}) 26 6 32
102 {
103 #maybe still in header
104 26 ++$self->{l};
105 26 my $is_data = 0;
106 26 ▶ if(defined $param->{headlines} and $self->{l} > $param->{headlines})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $param->{'headlines'} and $self->{'l'} > $param->{'headlines'}) { } 0 26 26
Condition33%1/3
defined $param->{"headlines"} and $self->{"l"} > $param->{"headlines"}
!ll&&!rl&&r
2600
MC/DC0%0/2
defined $param->{"headlines"} and $self->{"l"} > $param->{"headlines"}
defined $param->{"headlines"} $self->{"l"} > $param->{"headlines"}
Truth table33%1/3
A and B
A= defined $param->{"headlines"}B= $self->{"l"} > $param->{"headlines"}
ABresult
0X0
100
111
107 {
108 0 $is_data = 1;
109 } else {
110 26 ▶ $is_data = $self->{lasthead} ? 1 : $c->line_check($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
$self->{'lasthead'} ? : 0 26 26
111 # Behaviour when both expressions are specified:
112 # The first one that triggers defines beginning of data part.
113 # An idea would be to intentionally skip a part between.
114 # Think about that ...
115 26 ▶ if(!$self->{lasthead} and defined $self->{lhex})
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"lasthead"} and defined $self->{"lhex"}) 0 26 26
Condition33%1/3
not $self->{"lasthead"} and defined $self->{"lhex"}
!ll&&!rl&&r
0260
MC/DC0%0/2
not $self->{"lasthead"} and defined $self->{"lhex"}
not $self->{"lasthead"} defined $self->{"lhex"}
Truth table33%1/3
not(A) and B
A= not $self->{"lasthead"}B= defined $self->{"lhex"}
ABresult
0X0
100
111
116 {
117 0 $is_data = 0;
118 0 $self->{lasthead} = $_[0] =~ $self->{lhex};
119 }
120 26 ▶ if(defined $self->{fdex})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"fdex"}) 0 26 26
121 {
122 # possibly overriding line_check
123 0 $is_data = $_[0] =~ $self->{fdex};
124 }
125 }
126 # End header on specified number of lines or when thinking that data was found.
127 26 ▶ if($is_data)
Branch: 1 branch
BranchTrueFalseTotal
if ($is_data) { } 4 22 26
128 {
129 #first data line found
130 4 $self->{state}{data} = 1;
131  
132 4 $self->header_workout($pre);
133 }
134 else #collect headlines
135 {
136 22 my $h = $_[0];
137 22 $c->make_naked($h);
138 22 push(@{$self->{headlines}},$h);
139 22 $_[0] = '';
140 }
141 }
142 32 ▶ if($self->{state}{data})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"state"}{"data"}) 10 22 32
143 {
144 # skip data section if so desired
145 10 ▶ unless($param->{data})
Branch: 1 branch
BranchTrueFalseTotal
if ($param->{'data'}) { } 10 0 10
146 {
147 0 $_[0] = '';
148 }
149 else
150 {
151 # data line processing
152 # This logic is not nested right, code duplication has to go.
153 10 ▶ unless($param->{delaftercom})
Branch: 2 branches
BranchTrueFalseTotal
if ($param->{'delaftercom'}) { } 0 10 10
if (do { $c->line_check($_, 1) }) { } 0 0 0
154 {
155 # normal handling, repeat everything unless unworthy
156 10 ▶ if($_[0] eq $c->{config}{lineend})
Branch: 2 branches
BranchTrueFalseTotal
if ($_[0] eq $c->{'config'}{'lineend'}) { } 0 10 10
elsif ($param->{'touchdata'}) { } 10 0 10
157 {
158 0 ▶ $_[0] = '' if $param->{noempty};
Branch: 1 branch
BranchTrueFalseTotal
if $param->{"noempty"} 0 0 0
159 }
160 elsif($param->{touchdata})
161 {
162 10 my $d = $c->line_data($_[0]);
163 0 unshift(@{$d}, ++$self->{index})
164 10 ▶ if $param->{index};
Branch: 1 branch
BranchTrueFalseTotal
if $param->{"index"} 0 10 10
165 10 $_[0] = ${$c->data_line($d)};
166 }
167 }
168 elsif($c->line_check($_,1))
169 {
170 0 my $d = $c->line_data($_[0]);
171 0 unshift(@{$d}, ++$self->{index})
172 0 ▶ if $param->{index};
Branch: 1 branch
BranchTrueFalseTotal
if $param->{"index"} 0 0 0
173 0 $_[0] = ${$c->data_line($d)}
174 0 ▶ if($param->{touchdata});
Branch: 1 branch
BranchTrueFalseTotal
if $param->{"touchdata"} 0 0 0
175 }
176 0 else{ $_[0] = ''; }
177 }
178 }
179 32 ▶ $_[0] = $pre.$_[0] if $pre ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $pre ne "" 4 28 32
180 }
181  
182 sub endhook
183 {
184 7 ▶ my $self = shift;
Subroutine endhook: called CC=2 SCAR=6.9
Pod undocumented
185 # If there was no data, still produce a header when it was given via command line.
186 7 ▶ $self->header_workout(@_) unless($self->{state}{data});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"state"}{"data"} 3 4 7
187 }
188  
189 # generic helper for applying modtitles (actually, just numeric-key hash to array)
190 sub mod_titles
191 {
192 6 ▶ my ($t,$m) = @_;
Subroutine mod_titles: called CC=3 SCAR=15.4
Pod undocumented
193 6 foreach my $k (keys %{$m})
194 {
195 0 ▶ if($k =~ /^\d+$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($k =~ /^\d+$/) 0 0 0
196 {
197 0 $t->[$k-1] = $m->{$k};
198 }
199 }
200 }
201  
202 # construct header and prepend to current line, if demanded
203 sub header_workout
204 {
205 7 ▶ my $self = shift;
Subroutine header_workout: called CC=20 SCAR=32.1
Pod undocumented
206 7 my $param = $self->{param};
207 7 ▶ return unless $param->{head};
Branch: 1 branch
BranchTrueFalseTotal
unless $param->{"head"} 0 7 7
208  
209 #now print header
210 7 my $c = $self->{txd};
211  
212 7 ▶ unless($param->{touchhead})
Branch: 1 branch
BranchTrueFalseTotal
unless ($param->{"touchhead"}) 1 6 7
213 {
214 1 my $pre = join($c->get_end(), @{$self->{headlines}});
215 1 ▶ $pre .= $c->get_end() if $pre ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $pre ne "" 1 0 1
216 1 $_[0] = $pre.$_[0];
217 1 return;
218 }
219  
220 6 ▶ $c->{title} = $self->{title} if defined $self->{title};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"title"} 2 4 6
221 6 ▶ $c->{titles} = $self->{titles} if @{$self->{titles}};
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->{"titles"};} 2 4 6
222 0 unshift(@{$c->{titles}}, 'index')
223 6 ▶ if($param->{index} and defined $c->{titles} and @{$c->{titles}});
Branch: 1 branch
BranchTrueFalseTotal
if $param->{"index"} and defined $c->{"titles"} and @{$c->{"titles"};} 0 6 6
Condition33%2/6
$param->{"index"} and defined $c->{"titles"} and @{$c->{"titles"};}
!ll&&!rl&&r
600
$param->{"index"} and defined $c->{"titles"}
!ll&&!rl&&r
600
MC/DC0%0/3
$param->{"index"} and defined $c->{"titles"} and @{$c->{"titles"};}
$param->{"index"} defined $c->{"titles"} @{$c->{"titles"};}
Truth table25%1/4
A and B and C
A= $param->{"index"}B= defined $c->{"titles"}C= @{$c->{"titles"};}
ABCresult
0XX0
10X0
1100
1111
224 6 mod_titles($c->{titles}, $self->{param}{modtitles});
225 6 my $pre = '';
226 6 ▶ if($param->{origin})
Branch: 1 branch
BranchTrueFalseTotal
if ($param->{'origin'}) { } 1 5 6
227 {
228 #mangle it for Origin...
229 #Origin uses "titles" in first two lines as legend text
230 #since normally the same kind of data from different sources
231 #is identified in a plot via the legend, any comment text
232 #provided for the file is repeated here for every column
233 #looks senseless in file, makes sense in Origin
234 #print STDERR "TODO: revisit Origin format ... is this really the best way?\n";
235 1 my $titles = $c->{titles};
236 1 foreach my $com (@{$param->{comment}})
237 {
238 1 my @car = ();
239 1 ▶ for(my $i = 0; $i <= $#{$titles}; ++$i)
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#{$titles;}) 2 1 3
240 {
241 2 push(@car, $com);
242 }
243 1 $c->{titles} = \@car;
244 1 $pre .= ${$c->title_line()};
245 }
246 #the "real" title is still there after any comments
247 1 $c->{titles} = $titles;
248 1 $pre .= ${$c->title_line()};
249 }
250 else
251 {
252 5 ▶ my $old_comments = $param->{history} ? $self->{headlines} : $c->{comments};
Branch: 1 branch
BranchTrueFalseTotal
$param->{'history'} ? : 0 5 5
253 #file title
254 5 ▶ if(defined $c->{title}){ $pre .= ${$c->comment_line($c->{title})}; }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $c->{"title"}) 5 0 5
255 5 foreach my $l (@{$param->{comment}})
256 {
257 1 $pre .= ${$c->comment_line($l)};
258 }
259 #old stuff
260 5 ▶ unless($param->{newhead})
Branch: 1 branch
BranchTrueFalseTotal
unless ($param->{"newhead"}) 5 0 5
261 {
262 5 foreach my $h (@{$old_comments})
263 {
264 6 $pre .= ${$c->comment_line($h)};
265 }
266 }
267 #only if new one is desired
268 # Is that logic complete?
269 5 ▶ $pre .= ${$c->title_line()} if (@{$c->{titles}} or @{$self->{titles}} or (keys %{$param->{modtitles}}));
Branch: 1 branch
BranchTrueFalseTotal
if @{$c->{"titles"};} or @{$self->{"titles"};} or keys %{$$param{"modtitles"};} 0 0 0
Condition33%2/6
@{$c->{"titles"};} or @{$self->{"titles"};} or keys %{$$param{"modtitles"};}
l!l&&r!l&&!r
500
@{$c->{"titles"};} or @{$self->{"titles"};}
l!l&&r!l&&!r
500
MC/DC0%0/3
@{$c->{"titles"};} or @{$self->{"titles"};} or keys %{$$param{"modtitles"};}
@{$c->{"titles"};} @{$self->{"titles"};} keys %{$$param{"modtitles"};}
Truth table25%1/4
A or B or C
A= @{$c->{"titles"};}B= @{$self->{"titles"};}C= keys %{$$param{"modtitles"};}
ABCresult
1XX1
01X1
0011
0000
270 }
271 6 $_[0] = $pre.$_[0];
272 }
273  
274  
275 1 1;
« blib/lib/Text/NumericData/App/txdcalc.pm ↑ Summary blib/lib/Text/NumericData/App/txdodeint.pm »