| 1 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 2 | #$Author: andrius $ | ||||||||||||||||||||||||||||
| 3 | #$Date: 2018-12-20 11:24:42 +0200 (Kt, 20 gruod. 2018) $ | ||||||||||||||||||||||||||||
| 4 | #$Revision: 78 $ | ||||||||||||||||||||||||||||
| 5 | #$URL: svn+ssh://www.crystallography.net/home/coder/svn-repositories/jcamp-dx/tags/v0.02/lib/JCAMP/DX.pm $ | ||||||||||||||||||||||||||||
| 6 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 7 | #* | ||||||||||||||||||||||||||||
| 8 | # Parser for JCAMP-DX format. | ||||||||||||||||||||||||||||
| 9 | #** | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | package JCAMP::DX; | ||||||||||||||||||||||||||||
| 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::LabelDataRecord; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 4 | our $VERSION = '0.02'; | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | sub new | ||||||||||||||||||||||||||||
| 20 | { | ||||||||||||||||||||||||||||
| 21 | 19 | ▶ | my( $class, $title ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 22 | 19 | my $self = bless { | |||||||||||||||||||||||||||
| 23 | labels => [], | ||||||||||||||||||||||||||||
| 24 | data => {}, | ||||||||||||||||||||||||||||
| 25 | blocks => [], | ||||||||||||||||||||||||||||
| 26 | }, $class; | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 19 | ▶ | if( $title ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | 2 | $self->push_LDR( JCAMP::DX::LabelDataRecord->new( 'TITLE', $title ) ); | |||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 19 | return $self; | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | sub new_from_file | ||||||||||||||||||||||||||||
| 36 | { | ||||||||||||||||||||||||||||
| 37 | 10 | ▶ | my( $class, $filename, $options ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
new_from_file: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 38 | 10 | open( my $inp, $filename ); | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 10 | ▶ | ${$options->{store_file}} = '' if $options->{store_file}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 10 | my $title = <$inp>; | |||||||||||||||||||||||||||
| 43 | 10 | ▶ | ${$options->{store_file}} .= $title if $options->{store_file}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | 10 | $title =~ s/^\s*##title=//i; | |||||||||||||||||||||||||||
| 45 | 10 | $title =~ s/\r?\n$//; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 10 | my $block = $class->new_from_fh( $inp, $title, $options ); | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 10 | close $inp; | |||||||||||||||||||||||||||
| 50 | 10 | return $block; | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | sub new_from_fh | ||||||||||||||||||||||||||||
| 54 | { | ||||||||||||||||||||||||||||
| 55 | 16 | ▶ | my( $class, $inp, $title, $options ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
new_from_fh: called CC=14 SCAR=26.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 56 | 16 | my $block = $class->new(); | |||||||||||||||||||||||||||
| 57 | 16 | my( $last_label, $buffer ) = ( 'title', $title ); | |||||||||||||||||||||||||||
| 58 | 16 | ▶ | while( my $line = <$inp> ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 268 | ▶ | ${$options->{store_file}} .= $line if $options->{store_file}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 268 | $line =~ s/\$\$.*$//; # removing comments | |||||||||||||||||||||||||||
| 61 | 268 | $line =~ s/\r?\n$//; # removing newlines | |||||||||||||||||||||||||||
| 62 | 268 | ▶ | next if $line =~ /^\s*$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 260 | ▶ | last if $line =~ /^\s*##end=/i; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 244 | ▶ | if( $line =~ s/^\s*##title=//i ) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 65 | 6 | ▶ | if( defined $last_label && $last_label ne '' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $last_label and $last_label ne ""
MC/DC50%1/2
defined $last_label and $last_label ne ""
defined $last_label
$last_label ne ""
Truth table66%2/3
A and B
A= defined $last_labelB= $last_label ne ""
| |||||||||||||||||||||||||||||
| 66 | 2 | $block->push_LDR( | |||||||||||||||||||||||||||
| 67 | JCAMP::DX::LabelDataRecord->new( $last_label, $buffer ) | ||||||||||||||||||||||||||||
| 68 | ); | ||||||||||||||||||||||||||||
| 69 | 2 | undef $last_label; | |||||||||||||||||||||||||||
| 70 | 2 | undef $buffer; | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | 6 | $block->push_block( $class->new_from_fh( $inp, $line, $options ) ); | |||||||||||||||||||||||||||
| 73 | } elsif( $line =~ /^\s*##([^=]*)=(.*)$/ ) { | ||||||||||||||||||||||||||||
| 74 | 112 | ▶ | if( defined $last_label && $last_label ne '' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $last_label and $last_label ne ""
MC/DC50%1/2
defined $last_label and $last_label ne ""
defined $last_label
$last_label ne ""
Truth table66%2/3
A and B
A= defined $last_labelB= $last_label ne ""
| |||||||||||||||||||||||||||||
| 75 | 108 | $block->push_LDR( | |||||||||||||||||||||||||||
| 76 | JCAMP::DX::LabelDataRecord->new( $last_label, $buffer ) | ||||||||||||||||||||||||||||
| 77 | ); | ||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | 112 | ( $last_label, $buffer ) = ( $1, $2 ); | |||||||||||||||||||||||||||
| 80 | } elsif( $block->{labels} ) { | ||||||||||||||||||||||||||||
| 81 | 126 | $buffer .= "\n$line"; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 16 | ▶ | if( defined $last_label && $last_label ne '' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $last_label and $last_label ne ""
MC/DC100%2/2
defined $last_label and $last_label ne ""
defined $last_label
$last_label ne ""
Truth table100%3/3
A and B
A= defined $last_labelB= $last_label ne ""
| |||||||||||||||||||||||||||||
| 86 | 12 | $block->push_LDR( | |||||||||||||||||||||||||||
| 87 | JCAMP::DX::LabelDataRecord->new( $last_label, $buffer ) | ||||||||||||||||||||||||||||
| 88 | ); | ||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 16 | return $block; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | sub push_block | ||||||||||||||||||||||||||||
| 95 | { | ||||||||||||||||||||||||||||
| 96 | 6 | ▶ | my( $self, $block ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
push_block: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 97 | 6 | push @{$self->{blocks}}, $block; | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | sub push_LDR | ||||||||||||||||||||||||||||
| 101 | { | ||||||||||||||||||||||||||||
| 102 | 132 | ▶ | my( $self, $ldr ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
push_LDR: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 132 | ▶ | if( exists $self->{data}{$ldr->canonical_label} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | 2 | warn "duplicate values for label '" . $ldr->canonical_label . | |||||||||||||||||||||||||||
| 106 | "' were found, will not overwrite"; | ||||||||||||||||||||||||||||
| 107 | 2 | return; | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 130 | push @{$self->{labels}}, $ldr; | |||||||||||||||||||||||||||
| 111 | 130 | $self->{data}{$ldr->canonical_label} = $ldr; | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | sub title | ||||||||||||||||||||||||||||
| 115 | { | ||||||||||||||||||||||||||||
| 116 | 0 | ▶ | return $_[0]->{data}{TITLE}->value; | ||||||||||||||||||||||||||
|
Subroutine
title: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | sub order_labels | ||||||||||||||||||||||||||||
| 120 | { | ||||||||||||||||||||||||||||
| 121 | 1 | ▶ | my( $self ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
order_labels: called CC=4 SCAR=14.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | $self->{labels} = [ | ||||||||||||||||||||||||||||
| 124 | (exists $self->{data}{TITLE} ? $self->{data}{TITLE} : () ), | ||||||||||||||||||||||||||||
| 125 | (exists $self->{data}{JCAMPDX} ? $self->{data}{JCAMPDX} : () ), | ||||||||||||||||||||||||||||
| 126 | 3 | ▶ | grep { $_->label ne 'TITLE' && $_->label ne 'JCAMP-DX' } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | 1 | ▶ | @{$self->{labels}} ]; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | sub to_string | ||||||||||||||||||||||||||||
| 131 | { | ||||||||||||||||||||||||||||
| 132 | 3 | ▶ | my( $self ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
to_string: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 133 | 3 | my $output = ''; | |||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 3 | for my $label (@{$self->{labels}}) { | |||||||||||||||||||||||||||
| 136 | 8 | $output .= $label->to_string; | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 3 | for my $block (@{$self->{blocks}}) { | |||||||||||||||||||||||||||
| 140 | 0 | $output .= $block->to_string; | |||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 3 | $output .= "##END=\n"; | |||||||||||||||||||||||||||
| 144 | 3 | return $output; | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 4 | 1; | |||||||||||||||||||||||||||