DX/LabelDataRecord.pm

filter: Statementstmt 97.8% 93 / 95 Branchbran 72.7% 16 / 22 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 11 / 11 Podpod 0.0% 0 / 8 totaltotal 88.2% 120 / 136 CC 16 SCAR 15.0
CC
16
Cov
93%
parse_AFFN_or_ASDF
8.0
to_string
5.0
new
4.0
CRAP 4.5
« DX.pm ↑ Summary
1 #------------------------------------------------------------------------------
2 #$Author: andrius $
3 #$Date: 2021-02-10 13:44:25 +0200 (Tr, 10 vas. 2021) $
4 #$Revision: 94 $
5 #$URL: svn+ssh://www.crystallography.net/home/coder/svn-repositories/JCAMP-DX/tags/v0.03/lib/JCAMP/DX/LabelDataRecord.pm $
6 #------------------------------------------------------------------------------
7 #*
8 # Label Data Record object
9 #**
10  
11 package JCAMP::DX::LabelDataRecord;
12  
13 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 # ABSTRACT: Label Data Record object
17 4 our $VERSION = '0.03'; # VERSION
18  
19 4 ▶use JCAMP::DX::ASDF qw(decode);
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 4 our $max_line_length = 80;
22 4 our @records_with_variable_lists = qw(
23 PEAKTABLE
24 XYDATA
25 XYPOINTS
26 );
27  
28 sub new
29 132 ▶{
Subroutine new: called CC=4 SCAR=13.9
Pod undocumented
30 132 my( $class, $label, $value ) = @_;
31  
32 132 ▶ if( defined $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 132 0 132
33 132 $value =~ s/^\s+//;
34 132 $value =~ s/\s+$//;
35 }
36  
37 132 my $self = {
38 label => $label,
39 value => $value,
40 };
41  
42 132 $self = bless $self, $class;
43 132 ▶ return $self if !defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 132 132
44  
45 # Converting records with variable lists
46 132 ▶ if( grep { $_ eq $self->canonical_label }
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq $self->canonical_label;} @JCAMP::DX::LabelDataRecord::records_with_variable_lists) 7 125 132
47 @records_with_variable_lists ) {
48 7 parse_AFFN_or_ASDF( $self );
49 }
50  
51 132 return $self;
52 }
53  
54 sub canonicalise_label
55 660 ▶{
Subroutine canonicalise_label: called CC=1 SCAR=0.0
Pod undocumented
56 660 my( $label ) = @_;
57 660 $label = uc $label;
58 660 $label =~ s/[\s\-\/\\_]//g;
59 660 return $label;
60 }
61  
62 sub label
63 677 ▶{
Subroutine label: called CC=1 SCAR=0.0
Pod undocumented
64 677 return $_[0]->{label};
65 }
66  
67 sub canonical_label
68 660 ▶{
Subroutine canonical_label: called CC=1 SCAR=0.0
Pod undocumented
69 660 return canonicalise_label( $_[0]->label );
70 }
71  
72 sub value
73 7 ▶{
Subroutine value: called CC=1 SCAR=0.0
Pod undocumented
74 7 return $_[0]->{value};
75 }
76  
77 sub length
78 4 ▶{
Subroutine length: called CC=2 SCAR=7.4
Pod undocumented
79 4 my( $self ) = @_;
80 4 ▶ if( $self->{variables} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'variables'}) { } 4 0 4
81 4 return scalar @{$self->{$self->{variables}[0]}};
82 } else {
83 0 return 1;
84 }
85 }
86  
87 sub parse_AFFN_or_ASDF
88 7 ▶{
Subroutine parse_AFFN_or_ASDF: called CC=8 SCAR=20.8
Pod undocumented
89 7 my( $ldr ) = @_;
90  
91 # Process the variable list:
92 7 $ldr->{value} =~ s/^\s*(\S+)\s*\n//s;
93 7 my $variable_list = $1;
94  
95 7 ▶ return if !$variable_list; # Not an ASDF LDR actually
Branch: 1 branch
BranchTrueFalseTotal
unless $variable_list 0 7 7
96  
97 7 my @lines = split /\n/, $ldr->value;
98 7 ▶ if( $variable_list =~ /^\((.)\+\+\((.)\.\.\2\)\)$/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($variable_list =~ /^\((.)\+\+\((.)\.\.\2\)\)$/) { } 5 2 7
elsif ($variable_list =~ /^\(([a-z0-9]+)\.\.\1\)$/i) { } 2 0 2
99 # (X++(Y..Y)) form
100 5 delete $ldr->{value};
101 5 my @variables = ( $1, $2 );
102 5 $ldr->{variables} = \@variables;
103  
104 my @checkpoints =
105 5 map { s/^\s*([+-]?\d*\.?\d+([+-]?E\d*)?)//; $1 } @lines;
106 my @lines =
107 5 map { s/^\s+//; s/\s+$//; [ decode( $_ ) ] } @lines;
108 5 my $diff;
109 5 for my $i (0..$#checkpoints) {
110 116 ▶ if( $i < $#checkpoints ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $#checkpoints) 111 5 116
111 111 $diff = ($checkpoints[$i+1] - $checkpoints[$i]) / @{$lines[$i]};
112 }
113 116 push @{$ldr->{$variables[0]}},
114 116 map { $checkpoints[$i] + $_ * $diff } 0..$#{$lines[$i]};
115 116 push @{$ldr->{$variables[1]}}, @{$lines[$i]};
116 }
117 } elsif( $variable_list =~ /^\(([a-z0-9]+)\.\.\1\)$/i ) {
118 # (XY..XY) and (XYZ..XYZ) form
119 2 delete $ldr->{value};
120 2 my @variables = split '', $1;
121 2 $ldr->{variables} = \@variables;
122  
123 2 my @pairs = map { split /[;\s]+/, $_ } @lines;
124 2 for my $pair (@pairs) {
125 20 my @var = split /,/, $pair;
126 20 for my $i (0..$#var) {
127 43 push @{$ldr->{$variables[$i]}}, $var[$i];
128 }
129 }
130 } else {
131 0 die "cannot process variable list of form '$variable_list'";
132 }
133 }
134  
135 sub to_string
136 12 ▶{
Subroutine to_string: called CC=5 SCAR=16.1
Pod undocumented
137 12 my( $ldr ) = @_;
138 12 my $output = '##' . $ldr->label . '=';
139 12 my $value;
140 12 ▶ if( $ldr->{variables} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ldr->{'variables'}) { } 4 8 12
141 4 $value = '(' . join( '', @{$ldr->{variables}} ) . '..' .
142 4 join( '', @{$ldr->{variables}} ) . ")\n";
143 4 my $line = '';
144 4 for my $i (0..($ldr->length-1)) {
145 67 my $point = join( ',', map { $ldr->{$_}[$i] }
146 32 @{$ldr->{variables}} ) . ' ';
147 32 ▶ if( CORE::length( $line . $point ) < $max_line_length ) {
Branch: 1 branch
BranchTrueFalseTotal
if (CORE::length $line . $point < $JCAMP::DX::LabelDataRecord::max_line_length) { } 31 1 32
148 31 $line .= $point;
149 } else {
150 1 $value .= "$line\n";
151 1 $line = $point;
152 }
153 }
154 4 ▶ $value .= $line . "\n" if $line;
Branch: 1 branch
BranchTrueFalseTotal
if $line 4 0 4
155 } else {
156 8 $value = $ldr->{value} . "\n";
157 }
158 12 $output .= $value;
159 12 return $output;
160 }
161  
162 4 1;
« DX.pm ↑ Summary