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