blib/lib/BTRIEVE/SAVE.pm

filter: Statementstmt 75.5% 263 / 348 Branchbran 50.9% 53 / 104 Conditioncond 36.0% 9 / 25 MC/DCmcdc 6.2% 1 / 16 Subroutinesub 75.0% 21 / 28 Podpod 100% 12 / 12 totaltotal 67.3% 359 / 533 CC 72 SCAR 24.4
CC
72
Cov
68%
openbtr
42.0
output
30.0
rdb_to_save
16.2
CRAP 11.4
↑ Summary
1 package BTRIEVE::SAVE;
2  
3 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $DEBUG $TEST
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 );
7 1 $VERSION = '0.35';
8 1 $DEBUG = 0;
9  
10 1 require Exporter;
11 1 require 5.004;
12  
13 1 @ISA = qw(Exporter);
14 1 @EXPORT= qw();
15 1 @EXPORT_OK= qw();
16  
17  
18 # Preloaded methods go here.
19  
20 ####################################################################
21  
22 # This is the constructor method that creates the BTRIEVE object.
23 # It will attempt to set up info from the config file.
24  
25 ####################################################################
26 2 ▶sub new {
Subroutine new: called CC=5 SCAR=16.3
Pod documented
27 2 my $proto = shift;
28 2 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
020
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
29 2 my $config_file= shift ;
30 2 ▶ my $file = shift ||undef;
Condition100%2/2
shift() || undef
l!l
11
MC/DC100%1/1
shift() || undef
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
31  
32 2 my $save_btr = {opt=>{}, array=>[]};
33 2 $save_btr->{'opt'}{'config'}=$config_file;
34  
35 2 bless $save_btr, $class;
36 2 ▶ if (-e $config_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $config_file) { } 2 0 2
37 2 $save_btr->config($config_file);
38 } else {
39 0 return $save_btr;
40 }
41 2 ▶ if (!$file) {return $save_btr};
Branch: 1 branch
BranchTrueFalseTotal
unless ($file) 1 1 2
42 1 $save_btr->{'opt'}{'file'}=$file;
43  
44  
45 1 $save_btr->_initbtrieve();
46 1 return $save_btr;
47 }
48  
49 ###################################################################
50 # _initbtrieve() sets up config and filehandle
51 ###################################################################
52 1 ▶sub _initbtrieve {
Subroutine _initbtrieve: called CC=2 SCAR=7.2
53 1 my $save_btr = shift;
54 1 my $config_file = $save_btr->{'opt'}{'config'};
55 1 $save_btr->{'opt'}{'increment'} = -1;
56  
57 1 my $file = $save_btr->{'opt'}{'file'};
58 0 ▶ if (not(-e $file)) {carp "File \"$file\" doesn't exist"; return}
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $file) 0 1 1
59 1 open (*file, $file);
60 1 binmode *file;
61 1 $save_btr->{'opt'}{'handle'}=\*file; #store filehandle in object
62 }
63  
64 ###################################################################
65 # config() looks for a config file which tells us where the
66 # offsets are in the fixed part of the record, their types and
67 # what to call them locally.
68 ###################################################################
69  
70 2 ▶sub config {
Subroutine config: called CC=1 SCAR=0.0
Pod documented
71 2 my $save_btr = shift;
72 2 my $proto_rec = BTRIEVE::SAVE::REC->newconfig($save_btr->{'opt'}{'config'});
73 2 $save_btr->{'opt'}{'proto_rec'} = $proto_rec;
74 }
75  
76 ###################################################################
77 # parse_file() reads from a BTRIEVE SAVE file. Can do so
78 # incrementally.
79 ###################################################################
80 1 ▶sub parse_file {
Subroutine parse_file: called CC=4 SCAR=14.1
Pod documented
81 1 my $save_btr = shift;
82 1 my $increment = $save_btr->{'opt'}{'increment'}; #pick out increment from the object
83 1 my $recordcount = 0;
84  
85 1 ▶ while ($increment==-1 or $recordcount<$increment) {
Branch: 1 branch
BranchTrueFalseTotal
if ($increment == -1 or $recordcount < $increment) 3 0 3
Condition33%1/3
$increment == -1 or $recordcount < $increment
l!l&&r!l&&!r
300
MC/DC0%0/2
$increment == -1 or $recordcount < $increment
$increment == -1 $recordcount < $increment
Truth table33%1/3
A or B
A= $increment == -1B= $recordcount < $increment
ABresult
1X1
011
000
86 3 my $curr_rec = $save_btr->next_rec;
87 3 ▶ last unless $curr_rec;
Branch: 1 branch
BranchTrueFalseTotal
unless $curr_rec 1 2 3
88 2 push @{$save_btr->{'array'}},$curr_rec;
89 2 $recordcount++;
90 } #end reading this record
91 1 return $recordcount;
92 }
93  
94 ####################################################################
95  
96 # Returns a new BTRIEVE::SAVE::REC based on the next bits.
97 # Returns undef if we have reached the end.
98  
99 ####################################################################
100 3 ▶sub next_rec {
Subroutine next_rec: called CC=3 SCAR=11.0
Pod documented
101 3 my $save_btr=shift;
102 3 my ($rec,$eor) = $save_btr->next_recbits;
103 3 ▶ return undef if $eor eq "\cZ";
Branch: 1 branch
BranchTrueFalseTotal
if $eor eq "\32" 1 2 3
104 2 ▶ return undef unless defined($rec);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rec 0 2 2
105 2 my $proto_rec = $save_btr->{'opt'}{'proto_rec'};
106 2 my $curr_rec = $proto_rec->copy_struct();
107  
108 2 $curr_rec->parse_string($rec);
109 2 return $curr_rec;
110 }
111  
112 ####################################################################
113  
114 # Reads thru the handle looking for the bits forming the $rec
115 # and for the bits that should be $eor (end-of-record).
116 # Returns ($rec,$eor). $eor is undef if the read is at EOF.
117 # $eor is undef if we are at the DOS EOF ("\cZ") at the
118 # appropriate defined place.
119  
120 ####################################################################
121 3 ▶sub next_recbits {
Subroutine next_recbits: called CC=3 SCAR=11.0
Pod documented
122 3 my $save_btr = shift;
123 3 my $handle = $save_btr->{'opt'}{'handle'};
124 #need to use read to get the right bytes. Bummer.
125 3 my $pos= tell($handle);
126 3 my $info="";
127 3 my $rc= read($handle,$info,14); #assumes that no record is more than 10 gigabytes
128 3 ▶ return (undef,undef) unless $rc; #EOF...
Branch: 1 branch
BranchTrueFalseTotal
unless $rc 0 3 3
129 3 ▶ return (undef,"\cZ") if $info=~/^\cZ/;
Branch: 1 branch
BranchTrueFalseTotal
if $info =~ /^\cZ/ 1 2 3
130 2 my ($length)= $info=~/^(\d+)[, ]/; #definition of btrieve save file format.
131 #error check here to see if $length is defined.
132 2 seek($handle,$pos,0); #go back to where we were.
133 2 my $rec="";
134 2 $rc= read($handle,$rec,$length +length($length)+1); #definition of btrieve save file format.
135 2 $rec = substr($rec,length($length)+1); #kills the \d+[, ], keeps the rest.
136 2 my $eor="";
137 2 $rc = read($handle,$eor,2); #skip over \r\n, or find \cZ
138 2 return ($rec,$eor);
139 }
140 ####################################################################
141 # openbtr() reads in a BTRIEVE SAVE file. It takes a hashref
142 # with key 'file' (name of the btrieve file). Increment
143 # defines how many records to read in and is taken from the object.
144 ####################################################################
145 0 ▶sub openbtr {
Subroutine openbtr: not called CC=6 SCAR=37.4
Pod documented
146 0 my $save_btr=shift;
147 0 my $params=shift;
148 0 my $file=$params->{'file'};
149 0 ▶ if (not(-e $file)) {carp "File \"$file\" doesn't exist"; return}
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $file) 0 0 0
150 0 my $totalrecord;
151 $save_btr->{'opt'}{'increment'}
152 0 ▶ ||= $params->{'increment'}
Condition0%0/5
$save_btr->{"opt"}{"increment"} ||= ($params->{"increment"} ||= 1)
l!l&&r!l&&!r
000
$params->{"increment"} ||= 1
l!l
00
MC/DC0%0/2
$save_btr->{"opt"}{"increment"} ||= ($params->{"increment"} ||= 1)
$save_btr->{"opt"}{"increment"} ($params->{"increment"} ||= 1)
MC/DC0%0/1
$params->{"increment"} ||= 1
$params->{"increment"}
Truth table0%0/3
A ||= B
A= $save_btr->{"opt"}{"increment"}B= ($params->{"increment"} ||= 1)
ABresult
1X1
011
000
Truth table0%0/2
A
A= $params->{"increment"}
Aresult
11
00
153 ||= 1;
154 #store increment in the object, default is 1
155 0 open (*file, $file);
156 0 binmode *file;
157 0 $save_btr->{'opt'}{'handle'}=\*file; #store filehandle in object
158
159 0 ▶ print "read in $totalrecord records\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $BTRIEVE::SAVE::DEBUG 0 0 0
160 0 ▶ if ($totalrecord==0) {$totalrecord="0 but true"}
Branch: 1 branch
BranchTrueFalseTotal
if ($totalrecord == 0) 0 0 0
161 0 return $totalrecord;
162 }
163  
164 ####################################################################
165 # closebtr() will close a file-handle that was opened with
166 # openbtr()
167 ####################################################################
168 0 ▶sub closebtr {
Subroutine closebtr: not called CC=2 SCAR=17.9
Pod documented
169 0 my $marc = shift;
170 0 $marc->{'opt'}{'increment'}=0;
171 0 ▶ if (not($marc->{'opt'}{'handle'})) {carp "There isn't a BTRIEVE SAVE file to close"; return}
Branch: 1 branch
BranchTrueFalseTotal
unless ($marc->{"opt"}{"handle"}) 0 0 0
172 0 my $ok = close $marc->{'opt'}{'handle'};
173 0 $marc->{'opt'}{'handle'}=undef;
174 0 return $ok;
175 }
176  
177 ####################################################################
178 # nextbtr() will read in more records from a file that has
179 # already been opened with openbtr(). the increment can be
180 # adjusted if necessary by passing a new value as a parameter. the
181 # new records will be APPENDED to the BTRIEVE object
182 ####################################################################
183 0 ▶sub nextbtr {
Subroutine nextbtr: not called CC=3 SCAR=24.8
Pod documented
184 0 my $save_btr=shift;
185 0 my $increment=shift;
186 0 my $totalrecord;
187 0 ▶ if (not($save_btr->{'opt'}{'handle'})) {carp "There isn't a BTRIEVE SAVE file open"; return}
Branch: 1 branch
BranchTrueFalseTotal
unless ($save_btr->{"opt"}{"handle"}) 0 0 0
188 0 ▶ if ($increment) {$save_btr->{'opt'}{'increment'}=$increment}
Branch: 1 branch
BranchTrueFalseTotal
if ($increment) 0 0 0
189  
190 0 $totalrecord = $save_btr->parse_file();
191
192 0 return $totalrecord;
193 }
194
195 ####################################################################
196 # output() actually writes the file with a string version of
197 # $save_btr unless no file is given, in which case it returns the string
198 ####################################################################
199 0 ▶sub output {
Subroutine output: not called CC=5 SCAR=34.0
Pod documented
200 0 my $save_btr=shift;
201 0 my $output = "";
202 0 my $outfile = shift;
203  
204  
205 0 $output = $save_btr->as_string();
206 0 ▶ if ($outfile) {
Branch: 1 branch
BranchTrueFalseTotal
if ($outfile) { } 0 0 0
207 0 ▶ if ($outfile !~ /^>/) {carp "Don't forget to use > or >>: $!"}
Branch: 1 branch
BranchTrueFalseTotal
unless ($outfile =~ /^>/) 0 0 0
208 0 local(*OUT);
209 0 ▶ open (OUT, "$outfile") || carp "Couldn't open file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open OUT, "$outfile" or carp("Couldn't open file: $!") 0 0 0
210 0 binmode OUT;
211 0 print OUT $output;
212 0 ▶ close OUT || carp "Couldn't close file: $!";
Branch: 1 branch
BranchTrueFalseTotal
close OUT or carp("Couldn't close file: $!") 0 0 0
213 0 return 1;
214 }
215 #if no filename was specified return the output so it can be grabbed
216 else {
217 0 return $output;
218 }
219 }
220  
221
222 ####################################################################
223  
224 # as_string() returns a string version of $save_btr. Handles packing the
225 # easily updateable version of %fixed.
226  
227 ####################################################################
228 0 ▶sub as_string {
Subroutine as_string: not called CC=2 SCAR=17.9
Pod documented
229 0 my $output = "";
230 0 my $save_btr=shift;
231  
232 0 for (@{$save_btr->{'array'}}) {
233 0 my $data = $_->data;
234 0 my $packed_rec = $_->counted_rec($data);
235 0 $output .=$packed_rec;
236 }
237 0 $output .="\cZ";
238 0 return $output;
239 }
240  
241 ####################################################################
242  
243 # Takes an rdb filename, save filename, error file name, and config
244 # file name. Also takes the field name for unindexed fixed info and
245 # var info, and strings to translate to tab and newline. Writes
246 # an rdb file with that information; warns and writes to the error file
247 # if there are problems in the data.
248  
249 ####################################################################
250  
251 1 ▶sub rdb_to_save {
Subroutine rdb_to_save: called CC=15 SCAR=27.8
Pod documented
252 1 my $save_btr = shift;
253 1 my ($rdb,$save,$errs,
254 $zzname,$varname,$tabtrans,$rettrans) = @_;
255  
256 1 local *RDB;
257 1 local *SAVE;
258 1 local *ERRS;
259  
260 1 ▶ open RDB,"$rdb" or die "Could not open $rdb:$!\n";
Branch: 1 branch
BranchTrueFalseTotal
open RDB, "$rdb" or die "Could not open ${rdb}:$!\n" 1 0 1
261 1 binmode RDB;
262 1 ▶ open SAVE,">$save" or die "Could not open $save:$!\n";
Branch: 1 branch
BranchTrueFalseTotal
open SAVE, ">$save" or die "Could not open ${save}:$!\n" 1 0 1
263 1 binmode SAVE;
264 1 ▶ open ERRS,">$errs" or die "Could not open $errs:$!\n";
Branch: 1 branch
BranchTrueFalseTotal
open ERRS, ">$errs" or die "Could not open ${errs}:$!\n" 1 0 1
265 1 binmode ERRS;
266  
267 1 my $fieldnames = <RDB>;
268 1 print ERRS $fieldnames;
269  
270 1 chomp $fieldnames;
271  
272 1 my @rdbnames = split(/\t/,$fieldnames);
273  
274 1 my $proto_rec = $save_btr->{opt}{proto_rec};
275  
276 1 my @names = @{$proto_rec->{opt}{names}};
277  
278 1 my %fieldlen = (); #Gonna use this for lookup.
279 1 my @fixed_defs = @{$proto_rec->{opt}{fixed_defs}};
280 1 for (@fixed_defs) {
281 3 $fieldlen{ $_->{name}} = $_->{len};
282 };
283  
284 1 my $dashline = <RDB>;
285 1 print ERRS $dashline;
286  
287  
288 REC:
289 1 ▶ while (<RDB>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline RDB)) 2 1 3
290 2 chomp;
291 2 ▶ next unless /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless /\S/ 0 2 2
292 2 my @fields= split(/\t/);
293  
294 2 my $var;
295 my $rhfixed;
296 2 my @fieldnames = @rdbnames;
297 2 ▶ if ($#fields != $#rdbnames) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#fields != $#rdbnames) 0 2 2
298 0 warn "Paranoia 1: Number of fields do not match rdb spec at line $..\n";
299 0 print ERRS $_;
300 0 next REC;
301 }
302 2 my $err_skip =0;
303 FIELD:
304 2 ▶ while (@fields) {
Branch: 1 branch
BranchTrueFalseTotal
if (@fields) 8 2 10
305 8 my $field = shift @fields;
306 8 $field=~s/$tabtrans/\t/;
307 8 $field=~s/$rettrans/\n/;
308 8 my $name = shift @fieldnames;
309  
310 8 ▶ if ($name eq $zzname) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq $zzname) 2 6 8
311 2 $rhfixed->{'ZZ'} = $field;
312 2 next FIELD;
313 }
314 6 ▶ if ($name eq $varname) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq $varname) 2 4 6
315 2 $var = $field;
316 2 next FIELD;
317 }
318 4 ▶ if ($fieldlen{$name} != length($field)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fieldlen{$name} != length $field) 0 4 4
319 0 warn "Paranoia 2: Lengths do not match for $name at line $..\n";
320 0 print ERRS $_;
321 0 next REC;
322 }
323 4 $rhfixed->{$name}=$field;
324 }
325
326 2 my $curr_rec = $proto_rec->copy_struct();
327 2 @{$curr_rec->{values}} = ($rhfixed,\"",\$var);
328 2 my $counted_rec = $curr_rec->counted_rec_hash();
329 2 print SAVE $counted_rec;
330 }
331
332 1 print SAVE "\cZ";
333  
334 1 ▶ close RDB or carp "Could not close $rdb:$!\n";
Branch: 1 branch
BranchTrueFalseTotal
close RDB or carp("Could not close ${rdb}:$!\n") 1 0 1
335 1 ▶ close ERRS or carp "Could not close $errs:$!\n";
Branch: 1 branch
BranchTrueFalseTotal
close ERRS or carp("Could not close ${errs}:$!\n") 1 0 1
336 1 ▶ close SAVE or carp "Could not close $save:$!\n";
Branch: 1 branch
BranchTrueFalseTotal
close SAVE or carp("Could not close ${save}:$!\n") 1 0 1
337 }
338  
339 ####################################################################
340  
341 # Takes an rdb filename, save filename, error file name, and config
342 # file name. Also takes the field name for unindexed fixed info and
343 # var info, and strings to translate to tab and newline. Writes
344 # a rdb file with that information; warns and writes to the error file
345 # if there are problems in the data.
346  
347 ####################################################################
348 1 ▶sub save_to_rdb {
Subroutine save_to_rdb: called CC=11 SCAR=24.2
Pod documented
349 1 my $save_btr = shift;
350 1 my ($rdb,$save,$errs,
351 $zzname,$varname,$tabtrans,$rettrans) = @_;
352  
353 1 local *RDB;
354 1 local *ERRS;
355  
356 1 my $btr = BTRIEVE::SAVE->new($save_btr->{opt}{config},$save);
357 1 $btr->parse_file();
358 1 my $proto_rec = $btr->{'opt'}{'proto_rec'};
359 1 my @names = @{$proto_rec->{'opt'}{'names'}};
360 1 my @rdbnames = @names;
361 1 ▶ grep {$_ = $zzname if $_ eq 'ZZ'} @rdbnames;
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "ZZ" 1 2 3
362 1 ▶ open RDB,">$rdb" or die "Could not open $rdb for write: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open RDB, ">$rdb" or die "Could not open $rdb for write: $!\n" 1 0 1
363 1 binmode RDB;
364 1 ▶ open ERRS,">$errs" or die "Could not open $errs for write: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open ERRS, ">$errs" or die "Could not open $errs for write: $!\n" 1 0 1
365 1 binmode ERRS;
366  
367  
368 1 foreach my $name (@names) {
369 3 print RDB "$name\t"; # in /rdb systems, deleting an extra column is trivial.
370 }
371 1 print RDB "$varname\n";
372
373 1 for (@names) {
374 3 my $name =$_;
375 3 $name=~s/./-/g;
376 3 print RDB $name."\t";
377 }
378 1 my $dashvar = $varname;
379 1 $dashvar=~s/./-/g;
380 1 print RDB $dashvar."\n";
381  
382 REC:
383 1 foreach my $rec (@{$btr->{array}}) {
384 2 my $rdbline = "";
385 2 foreach my $name (@names) {
386 6 my $field = $rec->{values}[0]{$name};
387 6 ▶ if ($field=~/$tabtrans|$rettrans/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field =~ /$tabtrans|$rettrans/) 0 6 6
388 0 print ERRS $rec->counted_rec($rec->fixed.$rec->var);
389 0 next REC;
390 }
391 6 $field =~s/\t/$tabtrans/g;
392 6 $field =~s/\n/$rettrans/g;
393 6 $rdbline .= $field."\t";
394 }
395 2 my $var = $rec->var();
396 2 $var =~s/\t/$tabtrans/;
397 2 $var =~s/\n/$rettrans/;
398 2 $rdbline.= $var."\n";
399 2 print RDB $rdbline;
400 }
401 1 ▶ close RDB or carp "Could not close $rdb:$!\n";
Branch: 1 branch
BranchTrueFalseTotal
close RDB or carp("Could not close ${rdb}:$!\n") 1 0 1
402 1 print ERRS "\cZ";
403 1 ▶ close ERRS or carp "Could not close $errs:$!\n";;
Branch: 1 branch
BranchTrueFalseTotal
close ERRS or carp("Could not close ${errs}:$!\n") 1 0 1
404 }
405  
406 ####################################################################
407  
408 # BTRIEVE::SAVE::REC is responsible for internal representation of btrieve
409 # records. It knows enough to parse the %fixed information from
410 # a string and can generate string representations of data and
411 # counted string.
412  
413 ####################################################################
414  
415 package BTRIEVE::SAVE::REC;
416  
417 1 ▶use vars qw( %TYPEMAP %TYPE_SCALE $VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
418 1 %TYPEMAP = (Zstring => 'a', Integer => 'V', RAW => 'a');
419 1 %TYPE_SCALE = (Zstring => 1, Integer => 0.25, RAW => 1 ); # Btrieve standard has byte counts.
420  
421 1 $VERSION = '0.35';
422 ####################################################################
423  
424 # This roughly specifies what real recs know: a template to pack and
425 # unpack strings and a list of names for %fixed keys. The arrayref
426 # stores [$rhfixed,$rfixed,$rrest] information.
427  
428 ####################################################################
429 2 ▶sub new {
Subroutine new: called CC=2 SCAR=7.2
430 2 my $proto = shift;
431 2 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
020
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
432 2 my ($ranames,$rtemplate,$packed_length,$rhfixed_defs) = @_;
433 2 my $save_rec = {opt=>{names =>$ranames,template=>$rtemplate,
434 len=>$packed_length,fixed_defs=>$rhfixed_defs },
435 values=>[]};
436 2 return bless $save_rec,$class;
437 }
438  
439 2 ▶sub newconfig {
Subroutine newconfig: called CC=12 SCAR=25.0
440 2 my $proto = shift;
441 2 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
020
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
442 2 my $handle = shift;
443 2 local $/="\n";
444 2 local(*F);
445 2 ▶ open F,$handle or die "Could not open $handle:$!\n";
Branch: 1 branch
BranchTrueFalseTotal
open F, $handle or die "Could not open ${handle}:$!\n" 2 0 2
446 2 binmode *F;
447 2 my $fixed_length = 0;
448
449 2 my $langname_preamble;
450 my $lang_len;
451 2 my $rhfixed_defs = [];
452 2 ▶ while (<F>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline F)) 88 2 90
453 88 ▶ if (/langname/i) {
Branch: 1 branch
BranchTrueFalseTotal
if (/langname/i) 2 86 88
454 2 ($langname_preamble) =/^(.*)langname/i ;
455 2 $lang_len = length($langname_preamble) ;
456 }
457 88 ▶ ($fixed_length) = /(\d+)/ if /Record Length/;
Branch: 1 branch
BranchTrueFalseTotal
if /Record Length/ 2 86 88
458 88 ▶ if (/^\s+\d+/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\s+\d+/) 4 84 88
459 4 my ($len,$type) = /^\s+\d+\s+\d+\s+\d+\s+(\d+)\s+(\S+)/o;
460 4 ▶ die "Type $type not understood" unless $TYPEMAP{$type};
Branch: 1 branch
BranchTrueFalseTotal
unless $BTRIEVE::SAVE::REC::TYPEMAP{$type} 0 4 4
461 4 my ($langname) = /^.{$lang_len}(.*)/o;
462 4 $langname=~s/\W//og;
463 4 ▶ die '"ZZ" is a reserved fieldname\n' if $langname eq "ZZ";
Branch: 1 branch
BranchTrueFalseTotal
if $langname eq "ZZ" 0 4 4
464 4 push @{$rhfixed_defs},{len=>$len,type=>$type,name=>$langname};
465 }
466 }
467 # We define the unmatched as "ZZ"
468  
469  
470  
471 2 my $template="";
472  
473 2 my $hashed_len = 0;
474 2 for (@{$rhfixed_defs}) {
475 4 $template.= $TYPEMAP{$_->{'type'}}.($_->{'len'}*$TYPE_SCALE{$_->{'type'}})." "; #works for ZString and Integer
476 4 $hashed_len += $_->{'len'};
477 }
478 2 my $ZZ_len = $fixed_length-$hashed_len;
479 2 ▶ die "Sum of field lengths exceeds fixed length by ".-$ZZ_len." bytes\n" if $ZZ_len < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $ZZ_len < 0 0 2 2
480 2 push @{$rhfixed_defs},{len=>$ZZ_len,type=>"RAW",name=>"ZZ"};
481 2 $template .= $TYPEMAP{'RAW'}.$ZZ_len;
482 2 my @names = map {$_->{'name'} } @{$rhfixed_defs};
483 # Templates with "a0" in them return empty strings, so ok
484 # to have $fixed_length= $hashed_len. Perl rules OK?
485  
486 2 $template=~s/(\D)1 /$1 /g;
487 2 ▶ close F or die "Could not close config file:$!\n";
Branch: 1 branch
BranchTrueFalseTotal
close F or die "Could not close config file:$!\n" 2 0 2
488 2 return $class->new(\@names,$template,$fixed_length,$rhfixed_defs);
489 }
490  
491 ####################################################################
492  
493 # All_index returns true iff the structure of the record implies that
494 # there are no extra bytes in the fixed portion that are not indexed.
495  
496 ####################################################################
497 0 ▶sub all_indexed {
Subroutine all_indexed: not called CC=3 SCAR=24.8
498 0 my $save_rec = shift;
499 0 my $rhfixed_defs = $save_rec->{'opt'}{'fixed_defs'};
500 0 my $hashed_len = 0;
501 0 for (@{$rhfixed_defs}) {
502 0 $hashed_len += $_->{'len'};
503 }
504 0 ▶ return 1 if $hashed_len == $save_rec->{opt}{len};
Branch: 1 branch
BranchTrueFalseTotal
if $hashed_len == $save_rec->{"opt"}{"len"} 0 0 0
505 0 return 0;
506 }
507  
508 ####################################################################
509  
510 # This produces a clone with the same structure but no data.
511  
512 ####################################################################
513 4 ▶sub copy_struct {
Subroutine copy_struct: called CC=2 SCAR=7.2
514 4 my $proto = shift;
515 4 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
400
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
516 4 my ($btr) = @_;
517  
518 4 my $save_rec = {opt=>$proto->{'opt'}, values=>[{},\"",\""]};
519  
520 4 return bless $save_rec,$class;
521 }
522  
523 ####################################################################
524  
525 # This uses the {opt} information to fill in {values} from the string
526 # passed as a parameter. {values} will look like
527 # [$rhfixed,$rfixed,$rrest].
528  
529 ####################################################################
530 2 ▶sub parse_string {
Subroutine parse_string: called CC=4 SCAR=14.2
531 2 my $save_rec = shift;
532 2 my $string = shift;
533 2 my $fixed_len = $save_rec->{'opt'}{'len'};
534 # my($fixed,$rest)= $string=~/^(.{$save_rec->{'opt'}{'len'}})(.*)/os;
535 2 my $fixed= substr($string,0,$fixed_len);
536 2 my $rest = substr($string,$fixed_len);
537  
538 2 my @fixed=unpack($save_rec->{'opt'}{'template'},$fixed);
539 2 my %fixed;
540 2 for (@{$save_rec->{'opt'}{'names'}}) {
541 #remove warnings about use of undefined value.
542 6 ▶ if ($_ eq 'ZZ' and !defined($fixed[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ eq "ZZ" and not defined $fixed[0]) 0 6 6
Condition66%2/3
$_ eq "ZZ" and not defined $fixed[0]
!ll&&!rl&&r
420
MC/DC0%0/2
$_ eq "ZZ" and not defined $fixed[0]
$_ eq "ZZ" not defined $fixed[0]
Truth table66%2/3
A and not(B)
A= $_ eq "ZZ"B= not defined $fixed[0]
ABresult
0X0
100
111
543 0 $fixed{$_} ='';
544 0 next;
545 }
546 6 $fixed{$_}= shift @fixed;
547 }
548 2 $save_rec->{'values'}=[\%fixed,\$fixed,\$rest];
549 }
550  
551 ####################################################################
552  
553 # We know how to take a string and add the necessary appurtenances
554 # for appending to the on-file btrieve set of records.
555  
556 ####################################################################
557 2 ▶sub counted_rec {
Subroutine counted_rec: called CC=1 SCAR=0.0
558 2 my ($save_rec,$data) = @_;
559 2 return length($data).",".$data."\015\012"; # octal def for x-platform.
560 }
561  
562 ####################################################################
563  
564 # We know how to use our hash to create a string
565 # for appending to the on-file btrieve set of records.
566  
567 ####################################################################
568 2 ▶sub counted_rec_hash {
Subroutine counted_rec_hash: called CC=1 SCAR=0.0
569 2 my ($save_rec) = @_;
570 2 my $data = $save_rec->data();
571 2 return $save_rec->counted_rec($data);
572 }
573  
574 ####################################################################
575  
576 # Looks up the fixed string component of self.
577  
578 ####################################################################
579 0 ▶sub fixed {
Subroutine fixed: not called CC=2 SCAR=17.9
580 0 my $save_rec = shift;
581 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
582 0 ${$save_rec->{values}[1]} = shift;
583 }
584 0 return ${$save_rec->{values}[1]};
585 }
586  
587  
588 ####################################################################
589  
590 # Looks up the var string component of self.
591  
592 ####################################################################
593 2 ▶sub var {
Subroutine var: called CC=2 SCAR=7.9
594 2 my $save_rec = shift;
595 2 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 2 2
596 0 ${$save_rec->{values}[2]} = shift;
597 }
598 2 return ${$save_rec->{values}[2]};
599 }
600  
601 ####################################################################
602  
603 # We know how to produce data from the hashed fixed info.
604  
605 ####################################################################
606 2 ▶sub data {
Subroutine data: called CC=1 SCAR=0.0
607 2 my ($save_rec) = @_;
608  
609 2 my $rrest = $save_rec->{'values'}[2];
610 2 my $fixed = $save_rec->fix_hash_to_string();
611 2 return $fixed.$$rrest;
612 }
613  
614 ####################################################################
615  
616 # We know how to produce a fixed string from the hashed fixed info.
617  
618 ####################################################################
619  
620 2 ▶sub fix_hash_to_string {
Subroutine fix_hash_to_string: called CC=3 SCAR=11.0
621 2 my ($save_rec) = @_;
622  
623 2 my $rhfixed = $save_rec->{'values'}[0];
624 2 my $template = $save_rec->{'opt'}{'template'};
625 2 ▶ $rhfixed->{"ZZ"} = '' unless defined $rhfixed->{"ZZ"};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rhfixed->{"ZZ"} 0 2 2
626 2 my @values = ();
627 # ?? do we want to use an array slice:
628 # my %fixed = %$rhfixed;
629 # my @names = @{$save_rec->{opt}{names}}
630 # @values = @fixed{@names}??
631 #
632 2 for (@{$save_rec->{'opt'}{'names'}}) {
633 6 push @values, $rhfixed->{$_};
634 }
635 2 return pack($template,@values);
636 }
637 1 1; # so the require or use succeeds
638  
639 __END__
↑ Summary