TRX.pm

filter: Statementstmt 23.6% 30 / 127 Branchbran 20.8% 10 / 48 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 33.3% 5 / 15 Podpod 9.0% 1 / 11 totaltotal 22.3% 46 / 206 CC 27 SCAR 37.7
CC
27
Cov
22%
get_member
132.0
read_trx
30.0
row2array
20.0
CRAP 43.3
↑ Summary TRX/SQLite.pm »
1 package SAS::TRX;
2  
3 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 our $VERSION = '0.07';
8  
9 1 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 #
12 # Constructor
13 #
14 sub new
15 0 ▶{
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
16 0 my $class = shift;
17 0 my $self = {
18 FH => undef,
19 TRX => {},
20 };
21
22 0 bless ($self,$class);
23 0 return $self;
24 }
25  
26 #
27 # Given a file named
28 #
29 sub load
30 0 ▶{
Subroutine load: not called CC=1 SCAR=6.9
Pod documented
31 0 my $self = shift;
32 0 my $src = shift;
33  
34 0 $self->{FH} = new IO::File $src;
35  
36 0 $self->read_trx();
37 }
38  
39 1 my $LIBRARY_HEADER = 'HEADER RECORD*******LIBRARY HEADER RECORD!!!!!!!000000000000000000000000000000 ';
40 1 my $NAMESTR_HEADER = 'HEADER RECORD\*\*\*\*\*\*\*NAMESTR HEADER RECORD!!!!!!!000000(\d{4})00000000000000000000';
41 1 my $MEMBER_HEADER = 'HEADER RECORD\*\*\*\*\*\*\*MEMBER HEADER RECORD!!!!!!!000000000000000001600000000(\d{3})';
42 1 my $DSCRPTR_HEADER = 'HEADER RECORD*******DSCRPTR HEADER RECORD!!!!!!!000000000000000000000000000000 ';
43 1 my $OBS_HEADER = 'HEADER RECORD*******OBS HEADER RECORD!!!!!!!000000000000000000000000000000 ';
44  
45 #
46 # Get LIBRARY header. Abort if not found
47 #
48 sub library_hdr
49 0 ▶{
Subroutine library_hdr: not called CC=2 SCAR=17.9
Pod undocumented
50 0 my $self = shift;
51  
52 0 my ($tmp, %lhdata);
53  
54 0 read $self->{FH}, $tmp, 80;
55 0 ▶ die "LIBRARY header not found:$tmp:" unless $tmp eq $LIBRARY_HEADER;
Branch: 1 branch
BranchTrueFalseTotal
unless $tmp eq $LIBRARY_HEADER 0 0 0
56  
57 0 read $self->{FH}, $tmp, 80;
58 0 @lhdata{qw(SYMBOL1 SYMBOL2 LIB VER OS CREATE)} = unpack '(A8)4 A32 A16', $tmp;
59  
60 0 read $self->{FH}, $tmp, 80;
61 0 @lhdata{qw(DATETIME16)} = unpack 'A16', $tmp;
62  
63 0 @{$self}{qw(LIB VER)} = @lhdata{qw(LIB VER)};
64 }
65  
66 #
67 # Get member descriptor data
68 #
69 sub descriptor_hdr
70 0 ▶{
Subroutine descriptor_hdr: not called CC=2 SCAR=17.9
Pod undocumented
71 0 my $self = shift;
72  
73 0 my ($tmp, %mhdata);
74  
75 0 read $self->{FH}, $tmp, 80;
76 0 ▶ die "DSCRPTR header not found" unless ($tmp eq $DSCRPTR_HEADER);
Branch: 1 branch
BranchTrueFalseTotal
unless $tmp eq $DSCRPTR_HEADER 0 0 0
77  
78 0 read $self->{FH}, $tmp, 80;
79 0 @mhdata{qw(SYMBOL DSNAME SASDATA OS BLANKS CREATE)} = unpack '(A8)5 A24 A16', $tmp;
80  
81 0 read $self->{FH}, $tmp, 80;
82 0 @mhdata{qw(DATETIME16 BLANKS DSLABEL DSTYPE)} = unpack 'A16 A16 A40 A8', $tmp;
83  
84 0 return @mhdata{qw{DSNAME DSLABEL DSTYPE}};
85 }
86  
87 #
88 # Get NAMESTR header. Return number of NAMESTR records
89 #
90 sub namestr_hdr
91 0 ▶{
Subroutine namestr_hdr: not called CC=2 SCAR=17.9
Pod undocumented
92 0 my $self = shift;
93  
94 0 my ($tmp, $nnames);
95 0 read $self->{FH}, $tmp, 80;
96  
97 0 ▶ die 'NAMESTR header not found' unless ($tmp =~ m/$NAMESTR_HEADER/o);
Branch: 1 branch
BranchTrueFalseTotal
unless $tmp =~ /$NAMESTR_HEADER/o 0 0 0
98 0 return $1;
99 }
100  
101 #
102 # Get NAMESTR record
103 #
104 sub namestr_rec
105 0 ▶{
Subroutine namestr_rec: not called CC=1 SCAR=6.9
Pod undocumented
106 0 my $self = shift;
107 0 my $reclen = shift;
108  
109 0 my ($tmp, %nsdata);
110  
111 0 read $self->{FH}, $tmp, $reclen;
112 0 @nsdata{qw(NTYPE NHFUN NLNG NVAR0 NNAME NLABEL NFORM NFL NFD NFJ NFILL NIFORM NIFL NIFD NPOS REST)} = unpack 'n4 A8 A40 A8 n3 A2 A8 n2 N A52', $tmp;
113 0 return \%nsdata;
114 }
115  
116 #
117 # Get OBS header
118 #
119 sub obs_hdr
120 0 ▶{
Subroutine obs_hdr: not called CC=2 SCAR=17.9
Pod undocumented
121 0 my $self = shift;
122  
123 0 my $tmp;
124 0 read $self->{FH}, $tmp, 80;
125  
126 0 ▶ die "OBS header not found:$tmp:" unless ($tmp eq $OBS_HEADER);
Branch: 1 branch
BranchTrueFalseTotal
unless $tmp eq $OBS_HEADER 0 0 0
127 }
128  
129  
130 #
131 # Read library member
132 #
133 sub get_member
134 0 ▶{
Subroutine get_member: not called CC=11 SCAR=48.8
Pod undocumented
135 0 my $self = shift;
136  
137 0 my ($vars, $i, $tmp);
138  
139 0 my $nstr_len = $self->{NSTR_LEN};
140  
141 0 my ($dsname, $dslabel, $dstype) = $self->descriptor_hdr();
142 0 $self->{TRX}{$dsname}{DSLABEL} = $dstype;
143 0 $self->{TRX}{$dsname}{DSTYPE} = $dstype;
144  
145 # Dataset structure description
146 0 $vars = $self->namestr_hdr();
147 0 ▶ for ($i=0; $i < $vars; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $vars) 0 0 0
148 0 push @{ $self->{TRX}{$dsname}{VAR} }, $self->namestr_rec($nstr_len);
149 }
150 # Align to the next punch card
151 0 ▶ if ($vars * $nstr_len % 80) {
Branch: 1 branch
BranchTrueFalseTotal
if ($vars * $nstr_len % 80) 0 0 0
152 0 seek($self->{FH}, 80 - $vars * $nstr_len % 80, 1);
153 }
154  
155  
156  
157 0 my ($databuf, $rowlen, $var, $format, @types);
158 # Compute row length
159 0 $tmp = $#{$self->{TRX}{$dsname}{VAR}};
160 $rowlen = $self->{TRX}{$dsname}{VAR}[$tmp]{NPOS}+
161 0 $self->{TRX}{$dsname}{VAR}[$tmp]{NLNG};
162  
163 0 $self->{TRX}{$dsname}{CNAMES} = [];
164 0 $self->{TRX}{$dsname}{CTYPES} = [];
165 # Compute conversion formats.
166 0 foreach $var (@{ $self->{TRX}{$dsname}{VAR} }) {
167 0 $format .= 'a' . $var->{NLNG};
168  
169 # Remember just a list of variable names
170 0 push @{$self->{TRX}{$dsname}{CNAMES}}, $var->{NNAME};
171 # And types
172 0 push @{$self->{TRX}{$dsname}{CTYPES}}, $var->{NTYPE};
173 }
174  
175 # Upload to destination. May create header for compressed INSERT
176 0 ▶ $self->data_header($dsname) if ($self->can('data_header'));
Branch: 1 branch
BranchTrueFalseTotal
if $self->can("data_header") 0 0 0
177  
178 # Observation data
179 0 $self->obs_hdr();
180 0 $databuf='';
181 0 ▶ while (read( $self->{FH}, $tmp, 80 )) {
Branch: 1 branch
BranchTrueFalseTotal
if (read $self->{"FH"}, $tmp, 80) 0 0 0
182 0 $databuf .= $tmp;
183 0 ▶ last if $databuf =~ m/$MEMBER_HEADER/o;
Branch: 1 branch
BranchTrueFalseTotal
if $databuf =~ /$MEMBER_HEADER/o 0 0 0
184  
185 0 ▶ while (length($databuf) >= $rowlen) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $databuf >= $rowlen) 0 0 0
186 0 $self->row2array($dsname, $databuf, $format);
187 0 $databuf = substr($databuf, $rowlen);
188 0 ▶ last unless $databuf =~ /[^ ]/go;
Branch: 1 branch
BranchTrueFalseTotal
unless $databuf =~ /[^ ]/go 0 0 0
189 }
190 0 ▶ last if eof $self->{FH}; # read after eof may be wrong
Branch: 1 branch
BranchTrueFalseTotal
if eof $self->{"FH"} 0 0 0
191 }
192 0 $self->{NSTR_LEN} = $1; # In case the library is joined from various platforms data
193  
194 # Upload to destination. May create header for compressed INSERT
195 0 ▶ $self->data_footer($dsname) if $self->can('data_footer');
Branch: 1 branch
BranchTrueFalseTotal
if $self->can("data_footer") 0 0 0
196 }
197  
198 #
199 # Convert TRX observation (data row)
200 # into array of values
201 #
202 sub row2array
203 0 ▶{
Subroutine row2array: not called CC=4 SCAR=30.0
Pod undocumented
204 0 my ($self, $dsname, $row, $format) = @_;
205  
206  
207 0 my @data = unpack($format, $row);
208
209 0 ▶ for (my $i=0; $i<= $#data; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#data) 0 0 0
210 0 ▶ if ($self->{TRX}{$dsname}{CTYPES}[$i] == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'TRX'}{$dsname}{'CTYPES'}[$i] == 1) { } 0 0 0
211 0 $data[$i] = ibm_float($data[$i]);
212 } else {
213 # Trim whitespaces
214 0 $data[$i] =~ s/\s+$//;
215 0 $data[$i] =~ s/^\s+//;
216 }
217 }
218  
219 # Unload to target
220 0 ▶ $self->data_row($dsname, \@data) if $self->can('data_row');
Branch: 1 branch
BranchTrueFalseTotal
if $self->can("data_row") 0 0 0
221 }
222  
223 #
224 # Decrypt TRX numeric representation
225 #
226 # I agree that "significand" is "that which is to be signified".
227 # Let the meaningful part be "mantissa". As it was before.
228 #
229 sub ibm_float
230 8 ▶{
Subroutine ibm_float: called CC=6 SCAR=17.9
Pod undocumented
231 8 my $value = shift;
232  
233 8 my ($firstbyte,$bin) = unpack "CB*", $value;
234  
235 8 ▶ if ($bin == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bin == 0) 2 6 8
236 2 ▶ return undef if ($firstbyte); # Undefined values
Branch: 1 branch
BranchTrueFalseTotal
if $firstbyte 1 1 2
237 1 return 0;
238 }
239  
240 6 my $exp=($firstbyte & 0x7F) - 0x40;
241 6 my $mantissa = 0;
242  
243 6 ▶ while (length($bin)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $bin) 296 6 302
244 296 ▶ $mantissa += 1 if (chop $bin);
Branch: 1 branch
BranchTrueFalseTotal
if chop $bin 20 276 296
245 296 $mantissa /= 2;
246 }
247  
248 6 ▶ $mantissa = -$mantissa if ($firstbyte & 0x80);
Branch: 1 branch
BranchTrueFalseTotal
if $firstbyte & 128 1 5 6
249 6 return $mantissa*(16**$exp);
250 }
251  
252 #
253 # Read library
254 #
255 sub read_trx
256 0 ▶{
Subroutine read_trx: not called CC=5 SCAR=34.0
Pod undocumented
257 0 my $self = shift;
258  
259 0 my ($tmp, $nstr_len);
260  
261 0 $self->library_hdr();
262  
263 # Skip possible junk until member header
264 do {
265 0 read $self->{FH}, $tmp, 80;
266 0 ▶ } until (eof($self->{FH}) || $tmp =~ m/$MEMBER_HEADER/o);
Branch: 1 branch
BranchTrueFalseTotal
(eof $self->{"FH"} or $tmp =~ /$MEMBER_HEADER/o) or do { do { read $self->{"FH"}, $tmp, 80 } } 0 0 0
Condition0%0/3
eof $self->{"FH"} or $tmp =~ /$MEMBER_HEADER/o
l!l&&r!l&&!r
000
MC/DC0%0/2
eof $self->{"FH"} or $tmp =~ /$MEMBER_HEADER/o
eof $self->{"FH"} $tmp =~ /$MEMBER_HEADER/o
Truth table0%0/3
A or B
A= eof $self->{"FH"}B= $tmp =~ /$MEMBER_HEADER/o
ABresult
1X1
011
000
267 0 $self->{NSTR_LEN} = $1;
268  
269 # Get library members
270 0 ▶ until (eof($self->{FH})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eof $self->{"FH"}) 0 0 0
271 0 $self->get_member();
272 }
273 # We have got it all. Dump the results, if anybody cares
274 0 ▶ $self->data_description() if $self->can('data_description');
Branch: 1 branch
BranchTrueFalseTotal
if $self->can("data_description") 0 0 0
275 }
276  
277  
278 1 1;
279  
280 __END__
↑ Summary TRX/SQLite.pm »