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