| 1 | package Data::FixedFormat; | ||||||||||||
| 2 | |||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 1 | our $VERSION = "0.04"; | |||||||||||
| 5 | 1 | 1; | |||||||||||
| 6 | |||||||||||||
| 7 | package Data::FixedFormat; | ||||||||||||
| 8 | |||||||||||||
| 9 | 5 | ▶ | sub new { | ||||||||||
|
Subroutine
new: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||
| 10 | 5 | my ($class, $layout) = @_; | |||||||||||
| 11 | 5 | my $self; | |||||||||||
| 12 | 5 | ▶ | if (ref $layout eq "HASH") { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 13 | 1 | $self = new Data::FixedFormat::Variants $layout; | |||||||||||
| 14 | } else { | ||||||||||||
| 15 | 4 | $self = { Names=>[], Count=>[], Format=>"", Fields=>{} }; | |||||||||||
| 16 | 4 | bless $self, $class; | |||||||||||
| 17 | 4 | ▶ | $self->parse_fields($layout) if $layout; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 18 | } | ||||||||||||
| 19 | 5 | return $self; | |||||||||||
| 20 | } | ||||||||||||
| 21 | |||||||||||||
| 22 | 4 | ▶ | sub parse_fields { | ||||||||||
|
Subroutine
parse_fields: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||
| 23 | 4 | my ($self,$fmt) = @_; | |||||||||||
| 24 | 4 | my $ofs = 0; | |||||||||||
| 25 | 4 | foreach my $fld (@$fmt) { | |||||||||||
| 26 | 11 | my ($name, $format, $count) = split ':',$fld; | |||||||||||
| 27 | 11 | push @{$self->{Names}}, $name; | |||||||||||
| 28 | 11 | push @{$self->{Count}}, $count; | |||||||||||
| 29 | 11 | ▶ | $self->{Format} .= $format x ($count || 1); | ||||||||||
|
Condition100%2/2
$count || 1
MC/DC100%1/1
$count || 1
$count
Truth table100%2/2
A
A= $count
| |||||||||||||
| 30 | 11 | $self->{Fields}{$name} = $ofs; | |||||||||||
| 31 | 11 | ▶ | $ofs += ($count || 1); | ||||||||||
|
Condition100%2/2
$count || 1
MC/DC100%1/1
$count || 1
$count
Truth table100%2/2
A
A= $count
| |||||||||||||
| 32 | } | ||||||||||||
| 33 | } | ||||||||||||
| 34 | |||||||||||||
| 35 | 5 | ▶ | sub unformat { | ||||||||||
|
Subroutine
unformat: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 36 | 5 | my ($self,$frec) = @_; | |||||||||||
| 37 | 5 | my @flds = unpack $self->{Format}, $frec; | |||||||||||
| 38 | 5 | my $i = 0; | |||||||||||
| 39 | 5 | my $rec = {}; | |||||||||||
| 40 | 5 | @{$rec}{@{$self->{Names}}} = | |||||||||||
| 41 | 12 | ▶ | map { defined($_) ? [ splice @flds, 0, $_ ] : shift @flds } | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 42 | 5 | @{$self->{Count}}; | |||||||||||
| 43 | 5 | return $rec; | |||||||||||
| 44 | } | ||||||||||||
| 45 | |||||||||||||
| 46 | 1 | ▶ | sub unformat_tied { | ||||||||||
|
Subroutine
unformat_tied: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 47 | 1 | my ($self,$frec) = @_; | |||||||||||
| 48 | 1 | my @flds = unpack $self->{Format}, $frec; | |||||||||||
| 49 | 1 | tie my %h, 'Data::FixedFormat::Tied', $self, \@flds; | |||||||||||
| 50 | 1 | return \%h; | |||||||||||
| 51 | } | ||||||||||||
| 52 | |||||||||||||
| 53 | 3 | ▶ | sub format { | ||||||||||
|
Subroutine
format: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||
| 54 | 3 | my ($self,$rec) = @_; | |||||||||||
| 55 | 3 | my @flds; | |||||||||||
| 56 | 3 | my $i = 0; | |||||||||||
| 57 | 3 | foreach my $name (@{$self->{Names}}) { | |||||||||||
| 58 | 10 | ▶ | if ($self->{Count}[$i]) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 59 | 1 | push @flds,@{$rec->{$name}}; | |||||||||||
| 60 | } else { | ||||||||||||
| 61 | 9 | push @flds,$rec->{$name}; | |||||||||||
| 62 | } | ||||||||||||
| 63 | 10 | $i++; | |||||||||||
| 64 | } | ||||||||||||
| 65 | 3 | my $frec = pack $self->{Format}, @flds; | |||||||||||
| 66 | 3 | return $frec; | |||||||||||
| 67 | } | ||||||||||||
| 68 | |||||||||||||
| 69 | 0 | ▶ | sub blank { | ||||||||||
|
Subroutine
blank: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 70 | 0 | my $self = shift; | |||||||||||
| 71 | my $rec = $self->unformat(pack($self->{Format}, | ||||||||||||
| 72 | unpack($self->{Format}, | ||||||||||||
| 73 | 0 | ''))); | |||||||||||
| 74 | 0 | return $rec; | |||||||||||
| 75 | } | ||||||||||||
| 76 | |||||||||||||
| 77 | package Data::FixedFormat::Tied; | ||||||||||||
| 78 | 1 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 79 | |||||||||||||
| 80 | 1 | ▶ | sub TIEHASH { | ||||||||||
|
Subroutine
TIEHASH: called CC=1 SCAR=0.0
| |||||||||||||
| 81 | 1 | my ($class,$dff,$data) = @_; | |||||||||||
| 82 | 1 | bless { Dff=>$dff, Data=>$data },$class; | |||||||||||
| 83 | } | ||||||||||||
| 84 | |||||||||||||
| 85 | 4 | ▶ | sub FETCH { | ||||||||||
|
Subroutine
FETCH: called CC=3 SCAR=11.8
| |||||||||||||
| 86 | 4 | my ($self,$key) = @_; | |||||||||||
| 87 | 4 | ▶ | exists($self->{Dff}{Fields}{$key}) or die "Undefined key: $key"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 88 | 4 | my $idx = $self->{Dff}{Fields}{$key}; | |||||||||||
| 89 | 4 | ▶ | if ($self->{Dff}{Count}[$idx]) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 90 | 0 | die "Array fields not yet supported with tied interface"; | |||||||||||
| 91 | } else { | ||||||||||||
| 92 | 4 | $self->{Data}[$idx]; | |||||||||||
| 93 | } | ||||||||||||
| 94 | } | ||||||||||||
| 95 | |||||||||||||
| 96 | 0 | ▶ | sub STORE { | ||||||||||
|
Subroutine
STORE: not called CC=3 SCAR=24.8
| |||||||||||||
| 97 | 0 | my ($self,$key,$value) = @_; | |||||||||||
| 98 | 0 | ▶ | exists($self->{Dff}{Fields}{$key}) or die "Undefined key: $key"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 99 | 0 | my $idx = $self->{Dff}{Fields}{$key}; | |||||||||||
| 100 | 0 | ▶ | if ($self->{Dff}{Count}[$idx]) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 101 | 0 | die "Array fields not yet supported with tied interface"; | |||||||||||
| 102 | } else { | ||||||||||||
| 103 | 0 | $self->{Data}[$idx] = $value; | |||||||||||
| 104 | } | ||||||||||||
| 105 | } | ||||||||||||
| 106 | |||||||||||||
| 107 | 0 | ▶ | sub DELETE { | ||||||||||
|
Subroutine
DELETE: not called CC=1 SCAR=6.9
| |||||||||||||
| 108 | 0 | my ($self,$key) = @_; | |||||||||||
| 109 | 0 | die "Not Yet Implemented"; | |||||||||||
| 110 | } | ||||||||||||
| 111 | |||||||||||||
| 112 | 0 | ▶ | sub CLEAR { | ||||||||||
|
Subroutine
CLEAR: not called CC=1 SCAR=6.9
| |||||||||||||
| 113 | 0 | my ($self) = @_; | |||||||||||
| 114 | 0 | die "Not Yet Implemented"; | |||||||||||
| 115 | } | ||||||||||||
| 116 | |||||||||||||
| 117 | 0 | ▶ | sub EXISTS { | ||||||||||
|
Subroutine
EXISTS: not called CC=1 SCAR=6.9
| |||||||||||||
| 118 | 0 | my ($self,$key) = @_; | |||||||||||
| 119 | 0 | exists($self->{Dff}{Fields}{$key}); | |||||||||||
| 120 | } | ||||||||||||
| 121 | |||||||||||||
| 122 | 1 | ▶ | sub FIRSTKEY { | ||||||||||
|
Subroutine
FIRSTKEY: called CC=1 SCAR=0.0
| |||||||||||||
| 123 | 1 | my ($self) = @_; | |||||||||||
| 124 | 1 | $self->{Dff}{Names}[0]; | |||||||||||
| 125 | } | ||||||||||||
| 126 | |||||||||||||
| 127 | 4 | ▶ | sub NEXTKEY { | ||||||||||
|
Subroutine
NEXTKEY: called CC=1 SCAR=0.0
| |||||||||||||
| 128 | 4 | my ($self,$lastkey) = @_; | |||||||||||
| 129 | 4 | $self->{Dff}{Names}[$self->{Dff}{Fields}{$lastkey}+1]; | |||||||||||
| 130 | } | ||||||||||||
| 131 | |||||||||||||
| 132 | 0 | ▶ | sub UNTIE { | ||||||||||
|
Subroutine
UNTIE: not called CC=1 SCAR=0.0
| |||||||||||||
| 133 | # my ($self) = @_; | ||||||||||||
| 134 | } | ||||||||||||
| 135 | |||||||||||||
| 136 | 0 | ▶ | sub DESTROY { | ||||||||||
|
Subroutine
DESTROY: not called CC=1 SCAR=0.0
| |||||||||||||
| 137 | # my ($self) = @_; | ||||||||||||
| 138 | } | ||||||||||||
| 139 | |||||||||||||
| 140 | package Data::FixedFormat::Variants; | ||||||||||||
| 141 | 1 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 142 | |||||||||||||
| 143 | 1 | ▶ | sub new { | ||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
| |||||||||||||
| 144 | 1 | my ($class,$recfmt) = @_; | |||||||||||
| 145 | 1 | my $self; | |||||||||||
| 146 | 1 | $self = { Layouts=>[], Chooser=>$recfmt->{Chooser} }; | |||||||||||
| 147 | 1 | bless $self, $class; | |||||||||||
| 148 | 1 | foreach my $fmt (@{$recfmt->{Formats}}) { | |||||||||||
| 149 | 3 | push @{$self->{Layouts}},new Data::FixedFormat $fmt; | |||||||||||
| 150 | } | ||||||||||||
| 151 | 1 | return $self; | |||||||||||
| 152 | } | ||||||||||||
| 153 | |||||||||||||
| 154 | 2 | ▶ | sub unformat { | ||||||||||
|
Subroutine
unformat: called CC=2 SCAR=7.0
| |||||||||||||
| 155 | 2 | my ($self,$frec) = @_; | |||||||||||
| 156 | 2 | my $rec = $self->{Layouts}[0]->unformat($frec); | |||||||||||
| 157 | 2 | my $w = &{$self->{Chooser}}($rec); | |||||||||||
| 158 | 2 | ▶ | $rec = $self->{Layouts}[$w]->unformat($frec) if $w; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 159 | 2 | return $rec; | |||||||||||
| 160 | } | ||||||||||||
| 161 | |||||||||||||
| 162 | 0 | ▶ | sub unformat_tied { | ||||||||||
|
Subroutine
unformat_tied: not called CC=2 SCAR=17.9
| |||||||||||||
| 163 | 0 | my ($self,$frec) = @_; | |||||||||||
| 164 | 0 | my $rec = $self->{Layouts}[0]->unformat_tied($frec); | |||||||||||
| 165 | 0 | my $w = &{$self->{Chooser}}($rec); | |||||||||||
| 166 | 0 | ▶ | $rec = $self->{Layouts}[$w]->unformat_tied($frec) if $w; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 167 | 0 | return $rec; | |||||||||||
| 168 | } | ||||||||||||
| 169 | |||||||||||||
| 170 | 2 | ▶ | sub format { | ||||||||||
|
Subroutine
format: called CC=2 SCAR=7.0
| |||||||||||||
| 171 | 2 | my ($self,$rec) = @_; | |||||||||||
| 172 | 2 | my $w = 0; | |||||||||||
| 173 | 2 | ▶ | if ($self->{Chooser}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 174 | 2 | $w = &{$self->{Chooser}}($rec); | |||||||||||
| 175 | } | ||||||||||||
| 176 | 2 | my $frec = $self->{Layouts}[$w]->format($rec); | |||||||||||
| 177 | 2 | return $frec; | |||||||||||
| 178 | } | ||||||||||||
| 179 | |||||||||||||
| 180 | 0 | ▶ | sub blank { | ||||||||||
|
Subroutine
blank: not called CC=2 SCAR=17.9
| |||||||||||||
| 181 | 0 | my ($self,$w) = @_; | |||||||||||
| 182 | 0 | ▶ | $w = 0 unless $w; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 183 | 0 | my $rec = $self->{Layouts}[$w]->blank(); | |||||||||||
| 184 | 0 | return $rec; | |||||||||||
| 185 | } | ||||||||||||
| 186 | |||||||||||||
| 187 | __END__ | ||||||||||||