lib/XBase.pm

filter: Statementstmt 70.0% 439 / 627 Branchbran 53.2% 163 / 306 Conditioncond 40.0% 24 / 60 MC/DCmcdc 18.4% 7 / 38 Subroutinesub 63.9% 55 / 86 Podpod 51.2% 21 / 41 totaltotal 61.2% 709 / 1158 CC 184 SCAR 35.7
CC
184
Cov
63%
dump_records
191.3
read_header
89.4
create
57.1
CRAP 35.5
« lib/XBase/Index.pm ↑ Summary lib/XBase/SDBM.pm »
1  
2 13 ▶use XBase::Memo;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 =head1 NAME
5  
6 XBase - Perl module for reading and writing the dbf files
7  
8 =cut
9  
10 # ############
11 package XBase;
12  
13 13 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 13 ▶use XBase::Base; # will give us general methods
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 # ##############
18 # General things
19  
20 13 ▶use vars qw( $VERSION $errstr $CLEARNULLS @ISA );
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 13 @ISA = qw( XBase::Base );
23 13 $VERSION = '1.06';
24 13 $CLEARNULLS = 1; # Cut off white spaces from ends of char fields
25  
26 13 *errstr = \$XBase::Base::errstr;
27  
28  
29 # #########################################
30 # Open, read_header, init_memo_field, close
31  
32 # Open the specified file or try to append the .dbf suffix.
33 sub open {
34 21 ▶ my ($self) = shift;
Subroutine open: called CC=5 SCAR=16.1
Pod documented
35 21 my %options;
36 21 ▶ if (scalar(@_) % 2) { $options{'name'} = shift; }
Branch: 1 branch
BranchTrueFalseTotal
if (scalar(@_) % 2) 19 2 21
37 21 $self->{'openoptions'} = { %options, @_ };
38  
39 21 my %locoptions;
40 @locoptions{ qw( name readonly ignorememo fh ) }
41 21 = @{$self->{'openoptions'}}{ qw( name readonly ignorememo fh ) };
42 21 my $filename = $locoptions{'name'};
43 21 ▶ if ($filename eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($filename eq "-") 2 19 21
44 2 return $self->SUPER::open(%locoptions);
45 }
46 19 for my $ext ('', '.dbf', '.DBF') {
47 31 ▶ if (-f $filename.$ext) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $filename . $ext) 18 13 31
48 18 $locoptions{'name'} = $filename.$ext;
49 18 $self->NullError();
50 18 return $self->SUPER::open(%locoptions);
51 }
52 }
53 1 $locoptions{'name'} = $filename;
54 1 return $self->SUPER::open(%locoptions); # for nice error message
55 }
56  
57 # We have to provide way to fill up the object upon open
58 sub read_header {
59 20 ▶ my $self = shift;
Subroutine read_header: called CC=29 SCAR=44.9
Pod undocumented
60 20 my $fh = $self->{'fh'};
61  
62 20 my $header; # read the header
63 20 ▶ $self->read($header, 32) == 32 or do {
Branch: 1 branch
BranchTrueFalseTotal
$self->read($header, 32) == 32 or do { __PACKAGE__->Error(qq[Error reading header of $self->{"filename"}: $!\n]); return } 0 20 20
64 0 __PACKAGE__->Error("Error reading header of $self->{'filename'}: $!\n");
65 0 return;
66 };
67  
68 20 @{$self}{ qw( version last_update num_rec
69 header_len record_len encrypted ) }
70 = unpack 'Ca3Vvv@15a1', $header; # parse the data
71  
72 ### if (0 and $self->{'encrypted'} ne "\000")
73 ### { __PACKAGE__->Error("We don't support encrypted files, sorry.\n"); return; };
74  
75 20 my $header_len = $self->{'header_len'};
76  
77 20 my ($names, $types, $lengths, $decimals) = ( [], [], [], [] );
78 20 my ($unpacks, $readproc, $writeproc) = ( [], [], [] );
79 20 my $lastoffset = 1;
80  
81 20 ▶ while ($self->tell() < $header_len - 1) { # read the field desc's
Branch: 1 branch
BranchTrueFalseTotal
if ($self->tell < $header_len - 1) 64 19 83
82 64 my $field_def;
83 64 $self->read($field_def, 1);
84 64 ▶ last if $field_def eq "\r"; # we have found the terminator
Branch: 1 branch
BranchTrueFalseTotal
if $field_def eq "\r" 1 63 64
85 63 my $read = $self->read($field_def, 31, 1);
86 63 ▶ if ($read != 31) {
Branch: 1 branch
BranchTrueFalseTotal
if ($read != 31) 0 63 63
87 0 __PACKAGE__->Error("Error reading field description: $!\n");
88 0 return;
89 }
90  
91 63 my ($name, $type, $length, $decimal)
92 = unpack 'A11a1 @16CC', $field_def;
93 63 my ($rproc, $wproc);
94  
95 63 ▶ if ($type eq 'C') { # char
Branch: 9 branches
BranchTrueFalseTotal
if ($type eq 'C') { } 25 38 63
elsif ($type eq 'L') { } 8 30 38
elsif ($type =~ /^[NFD]$/) { } 20 10 30
elsif ($type eq 'I') { } 0 10 10
elsif ($type eq 'B') { } 0 10 10
elsif ($type =~ /^[WMGP]$/) { } 10 0 10
elsif ($type eq 'T') { } 0 0 0
elsif ($type eq '0') { } 0 0 0
elsif ($type eq 'Y') { } 0 0 0
96 # fixup for char length > 256
97 25 ▶ if ($decimal and not $self->{'openoptions'}{'nolongchars'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($decimal and not $self->{"openoptions"}{"nolongchars"}) 0 25 25
Condition33%1/3
$decimal and not $self->{"openoptions"}{"nolongchars"}
!ll&&!rl&&r
2500
MC/DC0%0/2
$decimal and not $self->{"openoptions"}{"nolongchars"}
$decimal not $self->{"openoptions"}{"nolongchars"}
Truth table33%1/3
A and not(B)
A= $decimalB= not $self->{"openoptions"}{"nolongchars"}
ABresult
0X0
100
111
98 0 $length += 256 * $decimal; $decimal = 0;
99 }
100 2029 ▶ $rproc = sub { my $value = $_[0];
Subroutine __ANON__: called CC=3 SCAR=13.3
101 2029 ▶ if ($self->{'ChopBlanks'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"ChopBlanks"}) 2029 0 2029
102 2029 $value =~ s/\s+$//;
103 }
104 2029 return $value;
105 0 ▶ ( $value eq '' ? undef : $value );
Branch: 1 branch
BranchTrueFalseTotal
$value eq '' ? : 0 0 0
106 25 };
107 16 ▶ $wproc = sub { my $value = shift;
Subroutine __ANON__: called CC=2 SCAR=7.2
108 16 ▶ sprintf '%-*.*s', $length, $length,
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 16 0 16
109 (defined $value ? $value : '');
110 25 };
111 }
112 elsif ($type eq 'L') { # logical (boolean)
113 14 ▶ $rproc = sub { my $value = shift;
Subroutine __ANON__: called CC=3 SCAR=11.0
114 14 ▶ if ($value =~ /^[YyTt]$/) { return 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^[YyTt]$/) 4 10 14
115 10 ▶ if ($value =~ /^[NnFf]$/) { return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^[NnFf]$/) 3 7 10
116 7 undef;
117 8 };
118 4 ▶ $wproc = sub { my $value = shift;
Subroutine __ANON__: called CC=3 SCAR=11.0
119 4 ▶ sprintf '%-*.*s', $length, $length,
Branch: 2 branches
BranchTrueFalseTotal
defined $value ? : 3 1 4
$value ? : 2 1 3
120 (defined $value ? ( $value ? 'T' : 'F') : '?');
121 8 };
122 }
123 elsif ($type =~ /^[NFD]$/) { # numbers, dates
124 60 ▶ $rproc = sub { my $value = shift;
Subroutine __ANON__: called CC=2 SCAR=7.2
125 60 ▶ ($value =~ /\d/) ? $value + 0 : undef;
Branch: 1 branch
BranchTrueFalseTotal
$value =~ /\d/ ? : 60 0 60
126 20 };
127 17 ▶ $wproc = sub { my $value = shift;
Subroutine __ANON__: called CC=2 SCAR=7.6
128 17 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) { } 17 0 17
129 17 substr(sprintf('%*.*f', $length, $decimal, ($value + 0)), -$length);
130 } else {
131 0 ' ' x $length;
132 }
133 20 };
134 }
135 elsif ($type eq 'I') { # Fox integer
136 0 ▶ $rproc = sub { unpack 'V', shift; };
Subroutine __ANON__: not called CC=1 SCAR=6.9
137 0 ▶ $wproc = sub { pack 'V', shift; };
Subroutine __ANON__: not called CC=1 SCAR=6.9
138 }
139 elsif ($type eq 'B') { # Fox double
140 0 if (pack("L", 1) eq pack("V", 1)) {
141 0 ▶ $rproc = sub { unpack 'd', scalar shift; };
Subroutine __ANON__: not called CC=1 SCAR=6.9
142 0 ▶ $wproc = sub { scalar pack 'd', shift; };
Subroutine __ANON__: not called CC=1 SCAR=6.9
143 } else {
144 0 ▶ $rproc = sub { unpack 'd', reverse scalar shift; };
Subroutine __ANON__: not called CC=1 SCAR=6.9
145 0 ▶ $wproc = sub { reverse scalar pack 'd', shift; };
Subroutine __ANON__: not called CC=1 SCAR=6.9
146 }
147 }
148 elsif ($type =~ /^[WMGP]$/) { # memo fields
149 10 my $memo = $self->{'memo'};
150 10 ▶ if (not defined $memo and not $self->{'openoptions'}{'ignorememo'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $memo or $self->{"openoptions"}{"ignorememo"}) 7 3 10
Condition100%3/3
defined $memo or $self->{"openoptions"}{"ignorememo"}
l!l&&r!l&&!r
127
MC/DC100%2/2
defined $memo or $self->{"openoptions"}{"ignorememo"}
defined $memo $self->{"openoptions"}{"ignorememo"}
Truth table100%3/3
A or B
A= defined $memoB= $self->{"openoptions"}{"ignorememo"}
ABresult
1X1
011
000
151 7 ▶ $memo = $self->{'memo'} = $self->init_memo_field() or return;
Branch: 1 branch
BranchTrueFalseTotal
$memo = $self->{"memo"} = $self->init_memo_field or return 0 7 7
152 }
153 10 ▶ if (defined $memo and $length == 10) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $memo and $length == 10) { } 6 4 10
elsif (defined $memo and $length == 4) { } 2 2 4
Condition100%3/3
defined $memo and $length == 10
!ll&&!rl&&r
226
MC/DC100%2/2
defined $memo and $length == 10
defined $memo $length == 10
Truth table100%3/3
A and B
A= defined $memoB= $length == 10
ABresult
0X0
100
111
154 6 ▶ if (ref $memo eq 'XBase::Memo::Apollo') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $memo eq 'XBase::Memo::Apollo') { } 0 6 6
155 0 ▶ $rproc = sub { $memo->read_record(shift); };
Subroutine __ANON__: not called CC=1 SCAR=6.9
156 0 ▶ $wproc = sub { $memo->write_record(shift); };
Subroutine __ANON__: not called CC=1 SCAR=6.9
157 } else {
158 $rproc = sub {
159 8 ▶ my $value = shift;
Subroutine __ANON__: called CC=4 SCAR=16.1
160 8 ▶ return if not $value =~ /\d/ or $value < 0;
Branch: 1 branch
BranchTrueFalseTotal
if not $value =~ /\d/ or $value < 0 0 8 8
Condition33%1/3
not $value =~ /\d/ or $value < 0
l!l&&r!l&&!r
008
MC/DC0%0/2
not $value =~ /\d/ or $value < 0
not $value =~ /\d/ $value < 0
Truth table33%1/3
not(A) or B
A= not $value =~ /\d/B= $value < 0
ABresult
1X1
011
000
161 8 ▶ $memo->read_record($value - 1) if defined $memo;
Branch: 1 branch
BranchTrueFalseTotal
if defined $memo 8 0 8
162 6 };
163 $wproc = sub {
164 4 ▶ my $value = $memo->write_record(-1, $type, $_[0]) if defined $memo and defined $_[0] and $_[0] ne '';
Subroutine __ANON__: called CC=5 SCAR=20.9
Branch: 1 branch
BranchTrueFalseTotal
if defined $memo and defined $_[0] and $_[0] ne "" 4 0 4
Condition33%2/6
defined $memo and defined $_[0] and $_[0] ne ""
!ll&&!rl&&r
004
defined $memo and defined $_[0]
!ll&&!rl&&r
004
MC/DC0%0/3
defined $memo and defined $_[0] and $_[0] ne ""
defined $memo defined $_[0] $_[0] ne ""
Truth table25%1/4
A and B and C
A= defined $memoB= defined $_[0]C= $_[0] ne ""
ABCresult
0XX0
10X0
1100
1111
165 4 ▶ sprintf '%*.*s', $length, $length,
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 4 0 4
166 6 (defined $value ? $value + 1: ''); };
167 }
168 }
169 ▶ elsif (defined $memo and $length == 4) {
Condition66%2/3
defined $memo and $length == 4
!ll&&!rl&&r
202
MC/DC50%1/2
defined $memo and $length == 4
defined $memo $length == 4
Truth table66%2/3
A and B
A= defined $memoB= $length == 4
ABresult
0X0
100
111
170 $rproc = sub {
171 4 ▶ my $val = unpack('V', $_[0]) - 1;
Subroutine __ANON__: called CC=3 SCAR=11.7
172 4 ▶ return if $val < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $val < 0 0 4 4
173 4 ▶ $memo->read_record($val) if defined $memo;
Branch: 1 branch
BranchTrueFalseTotal
if defined $memo 4 0 4
174 2 };
175 $wproc = sub {
176 2 ▶ my $value = $memo->write_record(-1, $type, shift) if defined $memo;
Subroutine __ANON__: called CC=3 SCAR=11.7
Branch: 1 branch
BranchTrueFalseTotal
if defined $memo 2 0 2
177 2 ▶ pack 'V', (defined $value ? $value + 1: 0); };
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 2 0 2
178 } else {
179 2 ▶ $rproc = sub { undef; };
Subroutine __ANON__: called CC=1 SCAR=0.0
180 2 ▶ $wproc = sub { ' ' x $length; };
Subroutine __ANON__: not called CC=1 SCAR=1.2
181 }
182 }
183 elsif ($type eq 'T') { # time fields
184 # datetime is stored internally as two
185 # four-byte numbers; the first is the day under
186 # the Julian Day System (JDS) and the second is
187 # the number of milliseconds since midnight
188 $rproc = sub {
189 0 ▶ my ($day, $time) = unpack 'VV', $_[0];
Subroutine __ANON__: not called CC=1 SCAR=6.9
190  
191  
192 0 my $localday = $day - 2440588;
193 0 my $localtime = $localday * 24 * 3600;
194 0 $localtime += $time / 1000;
195 ### print STDERR "day,time: ($day,$time -> $localtime)\n";
196 0 return $localtime;
197  
198 0 my $localdata = "[$localday] $localtime: @{[localtime($localtime)]}";
199  
200 0 my $usec = $time % 1000;
201 0 my $hour = int($time / 3600000);
202 0 my $min = int(($time % 3600000) / 60000);
203 0 my $sec = int(($time % 60000) / 1000);
204 0 return "$day($localdata)-$hour:$min:$sec.$usec";
205 0 };
206 $wproc = sub {
207 0 ▶ my $localtime = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
208 0 my $day = int($localtime / (24 * 3600)) + 2440588;
209 0 my $time = int(($localtime % (3600 * 24)) * 1000);
210  
211 ### print STDERR "day,time: ($localtime -> $day,$time)\n";
212  
213 0 return pack 'VV', $day, $time;
214 }
215 0 }
216 elsif ($type eq '0') { # SNa : field "_NULLFLAGS"
217 0 ▶ $rproc = $wproc = sub { '' };
Subroutine __ANON__: not called CC=1 SCAR=6.9
218 } elsif ($type eq 'Y') { # Fox money
219 $rproc = sub {
220 0 ▶ my ($x, $y) = unpack 'VV', scalar shift;
Subroutine __ANON__: not called CC=2 SCAR=17.9
221 0 ▶ if ($y & 0x80000000) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y & 2147483648) { } 0 0 0
222 0 - ($y ^ 0xffffffff) * (2**32 / 10**$decimal) - (($x - 1) ^ 0xffffffff) / 10**$decimal;
223 } else {
224 0 $y * (2**32 / 10**$decimal) + $x / 10**$decimal;
225 }
226 0 };
227 $wproc = sub {
228 0 ▶ my $value = shift;
Subroutine __ANON__: not called CC=2 SCAR=17.9
229 0 ▶ if ($value < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value < 0) { } 0 0 0
230 0 pack 'VV',
231 (-$value * 10**$decimal + 1) ^ 0xffffffff,
232 (-$value * 10**$decimal / 2**32) ^ 0xffffffff;
233 } else {
234 0 pack 'VV',
235 ($value * 10**$decimal) % 2**32,
236 (($value * 10**$decimal) >> 32);
237 }
238 0 };
239 }
240  
241  
242 63 $name =~ s/[\000 ].*$//s;
243 63 $name = uc $name; # no locale yet
244 63 push @$names, $name;
245 63 push @$types, $type;
246 63 push @$lengths, $length;
247 63 push @$decimals, $decimal;
248 63 push @$unpacks, '@' . $lastoffset . 'a' . $length;
249 63 push @$readproc, $rproc;
250 63 push @$writeproc, $wproc;
251 63 $lastoffset += $length;
252 }
253  
254 20 ▶ if ($lastoffset > $self->{'record_len'}
Branch: 1 branch
BranchTrueFalseTotal
if ($lastoffset > $self->{"record_len"} and not defined $self->{"openoptions"}{"nolongchars"}) 0 20 20
Condition33%1/3
$lastoffset > $self->{"record_len"} and not defined $self->{"openoptions"}{"nolongchars"}
!ll&&!rl&&r
2000
MC/DC0%0/2
$lastoffset > $self->{"record_len"} and not defined $self->{"openoptions"}{"nolongchars"}
$lastoffset > $self->{"record_len"} not defined $self->{"openoptions"}{"nolongchars"}
Truth table33%1/3
A and not(B)
A= $lastoffset > $self->{"record_len"}B= not defined $self->{"openoptions"}{"nolongchars"}
ABresult
0X0
100
111
255 and not defined $self->{'openoptions'}{'nolongchars'}) {
256 0 $self->seek_to(0);
257 0 $self->{'openoptions'}{'nolongchars'} = 1;
258 0 return $self->read_header;
259 }
260  
261 20 ▶ if ($lastoffset != $self->{'record_len'}
Branch: 1 branch
BranchTrueFalseTotal
if ($lastoffset != $self->{"record_len"} and not defined $self->{"openoptions"}{"ignorebadheader"}) 0 20 20
Condition33%1/3
$lastoffset != $self->{"record_len"} and not defined $self->{"openoptions"}{"ignorebadheader"}
!ll&&!rl&&r
2000
MC/DC0%0/2
$lastoffset != $self->{"record_len"} and not defined $self->{"openoptions"}{"ignorebadheader"}
$lastoffset != $self->{"record_len"} not defined $self->{"openoptions"}{"ignorebadheader"}
Truth table33%1/3
A and not(B)
A= $lastoffset != $self->{"record_len"}B= not defined $self->{"openoptions"}{"ignorebadheader"}
ABresult
0X0
100
111
262 and not defined $self->{'openoptions'}{'ignorebadheader'}) {
263 0 __PACKAGE__->Error("Missmatch in header of $self->{'filename'}: record_len $self->{'record_len'} but offset $lastoffset\n");
264 0 return;
265 }
266 20 ▶ if ($self->{'openoptions'}{'recompute_lastrecno'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"openoptions"}{"recompute_lastrecno"}) 1 19 20
267 $self->{num_rec} = int(((-s $self->{'fh'}) - $self->{header_len})
268 1 / $self->{record_len});
269 }
270  
271 20 my $hashnames = {}; # create name-to-num_of_field hash
272 20 @{$hashnames}{ reverse @$names } = reverse ( 0 .. $#$names );
273  
274 # now it's the time to store the values to the object
275 20 @{$self}{ qw( field_names field_types field_lengths field_decimals
276 hash_names last_field field_unpacks
277 field_rproc field_wproc ChopBlanks) } =
278 ( $names, $types, $lengths, $decimals,
279 $hashnames, $#$names, $unpacks,
280 $readproc, $writeproc, $CLEARNULLS );
281  
282  
283 20 1; # return true since everything went fine
284 }
285  
286 # When there is a memo field in dbf, try to open the memo file
287 sub init_memo_field {
288 7 ▶ my $self = shift;
Subroutine init_memo_field: called CC=7 SCAR=21.3
Pod undocumented
289 7 ▶ return $self->{'memo'} if defined $self->{'memo'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"memo"} 0 7 7
290 7 require XBase::Memo;
291 my %options = ( 'dbf_version' => $self->{'version'},
292 7 'memosep' => $self->{'openoptions'}{'memosep'} );
293
294 7 ▶ if (defined $self->{'openoptions'}{'memofile'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"openoptions"}{"memofile"}) 0 7 7
295 0 return XBase::Memo->new($self->{'openoptions'}{'memofile'}, %options);
296 }
297
298 7 for (qw( dbt DBT fpt FPT smt SMT dbt )) {
299 10 my $memo;
300 10 my $memoname = $self->{'filename'};
301 10 ▶ ($memoname =~ s/\.dbf$/.$_/i or $memoname =~ s/(\.dbf)?$/.$_/i)
Branch: 1 branch
BranchTrueFalseTotal
$memoname =~ s/\.dbf$/.$_/i || $memoname =~ s/(\.dbf)?$/.$_/i and $memo = "XBase::Memo"->new($memoname, %options) and return $memo 7 3 10
Condition50%3/6
$memoname =~ s/\.dbf$/.$_/i || $memoname =~ s/(\.dbf)?$/.$_/i and $memo = "XBase::Memo"->new($memoname, %options)
!ll&&!rl&&r
037
$memoname =~ s/\.dbf$/.$_/i or $memoname =~ s/(\.dbf)?$/.$_/i
l!l&&r!l&&!r
1000
MC/DC33%1/3
$memoname =~ s/\.dbf$/.$_/i || $memoname =~ s/(\.dbf)?$/.$_/i and $memo = "XBase::Memo"->new($memoname, %options)
$memoname =~ s/\.dbf$/.$_/i $memoname =~ s/(\.dbf)?$/.$_/i $memo = "XBase::Memo"->new($memoname, %options)
Truth table40%2/5
A or B and C
A= $memoname =~ s/\.dbf$/.$_/iB= $memoname =~ s/(\.dbf)?$/.$_/iC= $memo = "XBase::Memo"->new($memoname, %options)
ABCresult
00X0
1X00
0100
1X11
0111
302 and $memo = XBase::Memo->new($memoname, %options)
303 and return $memo;
304 }
305 0 return;
306 }
307  
308 # Close the file (and memo)
309 sub close {
310 7 ▶ my $self = shift;
Subroutine close: called CC=2 SCAR=6.9
Pod documented
311 7 ▶ if (defined $self->{'memo'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"memo"}) 2 5 7
312 2 $self->{'memo'}->close(); delete $self->{'memo'};
313 }
314 7 $self->SUPER::close();
315 }
316  
317 # ###############
318 # Little decoding
319 5 ▶sub version { shift->{'version'}; }
Subroutine version: called CC=1 SCAR=0.0
Pod undocumented
320 3524 ▶sub last_record { shift->{'num_rec'} - 1; }
Subroutine last_record: called CC=1 SCAR=0.0
Pod documented
321 105 ▶sub last_field { shift->{'last_field'}; }
Subroutine last_field: called CC=1 SCAR=0.0
Pod documented
322  
323 # List of field names, types, lengths and decimals
324 11 ▶sub field_names { @{shift->{'field_names'}}; }
Subroutine field_names: called CC=1 SCAR=0.0
Pod documented
325 14 ▶sub field_types { @{shift->{'field_types'}}; }
Subroutine field_types: called CC=1 SCAR=0.0
Pod documented
326 2 ▶sub field_lengths { @{shift->{'field_lengths'}}; }
Subroutine field_lengths: called CC=1 SCAR=0.0
Pod documented
327 0 ▶sub field_decimals { @{shift->{'field_decimals'}}; }
Subroutine field_decimals: not called CC=1 SCAR=6.9
Pod documented
328  
329 # Return field number for field name
330 sub field_name_to_num {
331 56 ▶ my ($self, $name) = @_; $self->{'hash_names'}{uc $name};
Subroutine field_name_to_num: called CC=1 SCAR=0.0
Pod undocumented
332 }
333 sub field_type {
334 14 ▶ my ($self, $name) = @_;
Subroutine field_type: called CC=2 SCAR=7.1
Pod documented
335 14 ▶ defined (my $num = $self->field_name_to_num($name)) or return;
Branch: 1 branch
BranchTrueFalseTotal
defined(my $num = $self->field_name_to_num($name)) or return 0 14 14
336 14 ($self->field_types)[$num];
337 }
338 sub field_length {
339 2 ▶ my ($self, $name) = @_;
Subroutine field_length: called CC=2 SCAR=7.1
Pod documented
340 2 ▶ defined (my $num = $self->field_name_to_num($name)) or return;
Branch: 1 branch
BranchTrueFalseTotal
defined(my $num = $self->field_name_to_num($name)) or return 0 2 2
341 2 ($self->field_lengths)[$num];
342 }
343 sub field_decimal {
344 0 ▶ my ($self, $name) = @_;
Subroutine field_decimal: not called CC=2 SCAR=17.9
Pod documented
345 0 ▶ defined (my $num = $self->field_name_to_num($name)) or return;
Branch: 1 branch
BranchTrueFalseTotal
defined(my $num = $self->field_name_to_num($name)) or return 0 0 0
346 0 ($self->field_decimals)[$num];
347 }
348  
349  
350 # #############################
351 # Header, field and record info
352  
353 # Returns (not prints!) the info about the header of the object
354 13 *header_info = \&get_header_info;
355 sub get_header_info {
356 2 ▶ my $self = shift;
Subroutine get_header_info: called CC=1 SCAR=0.0
Pod undocumented
357 2 my $hexversion = sprintf '0x%02x', $self->version;
358 2 my $longversion = $self->get_version_info()->{'string'};
359 2 my $printdate = $self->get_last_change;
360 2 my $numfields = $self->last_field() + 1;
361 2 my $result = sprintf <<"EOF";
362 Filename: $self->{'filename'}
363 Version: $hexversion ($longversion)
364 Num of records: $self->{'num_rec'}
365 Header length: $self->{'header_len'}
366 Record length: $self->{'record_len'}
367 Last change: $printdate
368 Num fields: $numfields
369 Field info:
370 Num Name Type Len Decimal
371 EOF
372 2 return join '', $result, map { $self->get_field_info($_) }
373 (0 .. $self->last_field);
374 }
375 # Return info about field in dbf file
376 sub get_field_info {
377 10 ▶ my ($self, $num) = @_;
Subroutine get_field_info: called CC=1 SCAR=0.0
Pod undocumented
378 sprintf "%d.\t%-16.16s%-8.8s%-8.8s%s\n", $num + 1,
379 10 map { $self->{$_}[$num] }
380 qw( field_names field_types field_lengths field_decimals );
381 }
382 # Return last_change item as printable string
383 sub get_last_change {
384 2 ▶ my $self = shift;
Subroutine get_last_change: called CC=3 SCAR=11.0
Pod undocumented
385 2 my $date = $self;
386 2 ▶ if (ref $self) { $date = $self->{'last_update'}; }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) 2 0 2
387 2 my ($year, $mon, $day) = unpack 'C3', $date;
388 2 ▶ $year += ($year >= 70) ? 1900 : 2000;
Branch: 1 branch
BranchTrueFalseTotal
$year >= 70 ? : 1 1 2
389 2 return "$year/$mon/$day";
390 }
391 # Return text description of the version value
392 sub get_version_info {
393 2 ▶ my $version = shift;
Subroutine get_version_info: called CC=9 SCAR=27.3
Pod undocumented
394 2 ▶ $version = $version->version() if ref $version;
Branch: 1 branch
BranchTrueFalseTotal
if ref $version 2 0 2
395 2 my $result = {};
396 2 $result->{'vbits'} = $version & 0x07;
397 2 ▶ if ($version == 0x30 or $version == 0xf5) {
Branch: 3 branches
BranchTrueFalseTotal
if ($version == 48 or $version == 245) { } 0 2 2
elsif ($version & 8) { } 0 2 2
elsif ($version & 128) { } 2 0 2
Condition33%1/3
$version == 48 or $version == 245
l!l&&r!l&&!r
002
MC/DC0%0/2
$version == 48 or $version == 245
$version == 48 $version == 245
Truth table33%1/3
A or B
A= $version == 48B= $version == 245
ABresult
1X1
011
000
398 0 $result->{'vbits'} = 5; $result->{'foxpro'} = 1;
399 } elsif ($version & 0x08) {
400 0 $result->{'vbits'} = 4; $result->{'memo'} = 1;
401 } elsif ($version & 0x80) {
402 2 $result->{'dbt'} = 1;
403 }
404  
405 2 my $string = "ver. $result->{'vbits'}";
406 2 ▶ if (exists $result->{'foxpro'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $result->{"foxpro"}) 0 2 2
407 0 $string .= " (FoxPro)";
408 }
409 2 ▶ if (exists $result->{'memo'}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $result->{'memo'}) { } 0 2 2
elsif (exists $result->{'dbt'}) { } 2 0 2
410 0 $string .= " with memo file";
411 } elsif (exists $result->{'dbt'}) {
412 2 $string .= " with DBT file";
413 }
414 2 $result->{'string'} = $string;
415  
416 2 $result;
417 }
418  
419  
420 # Print the records as colon separated fields
421 sub dump_records {
422 2 ▶ my $self = shift;
Subroutine dump_records: called CC=21 SCAR=52.5
Pod undocumented
423 2 my %options = ( 'rs' => "\n", 'fs' => ':', 'undef' => '' );
424 2 my %inoptions = @_;
425 2 for my $key (keys %inoptions) {
426 1 my $value = $inoptions{$key};
427 1 my $outkey = lc $key;
428 1 $outkey =~ s/[^a-z]//g;
429 1 $options{$outkey} = $value;
430 }
431 my ($rs, $fs, $undef, $fields, $table)
432 2 = @options{ qw( rs fs undef fields table ) };
433 2 ▶ if (defined $table) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $table) 0 2 2
434 0 eval 'use Data::ShowTable';
435 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
436 0 warn "You requested table output format but the module Data::ShowTable doesn't\nseem to be installed correctly. Falling back to standard\n";
437 0 $table = undef;
438 } else {
439 0 delete $options{'rs'};
440 0 delete $options{'fs'};
441 }
442 }
443  
444 2 my @fields = ();
445 2 my @unknown_fields;
446 2 ▶ if (defined $fields) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fields) 0 2 2
447 0 ▶ if (ref $fields eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $fields eq 'ARRAY') { } 0 0 0
448 0 @fields = @$fields;
449 } else {
450 0 @fields = split /\s*,\s*/, $fields;
451 0 my $i = 0;
452 0 ▶ while ($i < @fields) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @fields) 0 0 0
453 0 ▶ if (defined $self->field_name_to_num($fields[$i])) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $self->field_name_to_num($fields[$i])) { } 0 0 0
elsif ($fields[$i] =~ /^(.*)-(.*)/) { } 0 0 0
454 0 $i++;
455 } elsif ($fields[$i] =~ /^(.*)-(.*)/) {
456 0 local $^W = 0;
457 0 my @allfields = $self->field_names;
458 0 my ($start, $end) = ($1, $2);
459 0 ▶ if ($start eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($start eq "") 0 0 0
460 0 $start = $allfields[0];
461 }
462 0 ▶ if ($end eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($end eq "") 0 0 0
463 0 $end = $allfields[$#allfields];
464 }
465 0 my $start_num = $self->field_name_to_num($start);
466 0 my $end_num = $self->field_name_to_num($end);
467 0 ▶ if ($start ne '' and not defined $start_num) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start ne "" and not defined $start_num) 0 0 0
Condition0%0/3
$start ne "" and not defined $start_num
!ll&&!rl&&r
000
MC/DC0%0/2
$start ne "" and not defined $start_num
$start ne "" not defined $start_num
Truth table0%0/3
A and not(B)
A= $start ne ""B= not defined $start_num
ABresult
0X0
100
111
468 0 push @unknown_fields, $start;
469 }
470 0 ▶ if ($end ne '' and not defined $end_num) {
Branch: 1 branch
BranchTrueFalseTotal
if ($end ne "" and not defined $end_num) 0 0 0
Condition0%0/3
$end ne "" and not defined $end_num
!ll&&!rl&&r
000
MC/DC0%0/2
$end ne "" and not defined $end_num
$end ne "" not defined $end_num
Truth table0%0/3
A and not(B)
A= $end ne ""B= not defined $end_num
ABresult
0X0
100
111
471 0 push @unknown_fields, $end;
472 }
473 0 ▶ unless (defined $start and defined $end) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $start and defined $end) 0 0 0
Condition0%0/3
defined $start and defined $end
!ll&&!rl&&r
000
MC/DC0%0/2
defined $start and defined $end
defined $start defined $end
Truth table0%0/3
A and B
A= defined $startB= defined $end
ABresult
0X0
100
111
474 0 $start = 0; $end = -1;
475 }
476
477 0 splice @fields, $i, 1, @allfields[$start_num .. $end_num];
478 } else {
479 0 push @unknown_fields, $fields[$i];
480 0 $i++;
481 }
482 }
483 }
484 }
485  
486 2 ▶ if (@unknown_fields) {
Branch: 1 branch
BranchTrueFalseTotal
if (@unknown_fields) 0 2 2
487 0 $self->Error("There have been unknown fields `@unknown_fields' specified.\n");
488 0 return 0;
489 }
490 2 my $cursor = $self->prepare_select(@fields);
491 2 my @record;
492 2 ▶ if (defined $table) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $table) { } 0 2 2
493 0 local $^W = 0;
494 &ShowBoxTable( $cursor->names(), [], [],
495 sub {
496 0 ▶ if ($_[0]) { $cursor->rewind(); }
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]) { } 0 0 0
497 0 else { $cursor->fetch() }
498 0 });
499 } else {
500 2 ▶ while (@record = $cursor->fetch) {
Branch: 1 branch
BranchTrueFalseTotal
if (@record = $cursor->fetch) 84 2 86
501 84 ▶ print join($fs, map { defined $_ ? $_ : $undef } @record), $rs;
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 168 0 168
502 }
503 }
504 2 1;
505 }
506  
507  
508 # ###################
509 # Reading the records
510  
511 # Returns fields of the specified record; parameters and number of the
512 # record (starting from 0) and optionally names of the required
513 # fields. If no names are specified, all fields are returned. The
514 # first value in the returned list if always 1/0 deleted flag. Returns
515 # empty list on error.
516  
517 sub get_record {
518 117 ▶ my ($self, $num) = (shift, shift);
Subroutine get_record: called CC=1 SCAR=0.0
Pod documented
519 117 $self->NullError();
520 117 $self->get_record_nf( $num, map { $self->field_name_to_num($_); } @_);
521 }
522 13 *get_record_as_hash = \&get_record_hash;
523 sub get_record_hash {
524 1 ▶ my ($self, $num) = @_;
Subroutine get_record_hash: called CC=3 SCAR=11.6
Pod undocumented
525 1 ▶ my @list = $self->get_record($num) or return;
Branch: 1 branch
BranchTrueFalseTotal
my(@list) = $self->get_record($num) or return 0 1 1
526 1 my $hash = {};
527 1 @{$hash}{ '_DELETED', $self->field_names() } = @list;
528 1 ▶ return %$hash if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 1 0 1
529 0 $hash;
530 }
531 sub get_record_nf {
532 1629 ▶ my ($self, $num, @fieldnums) = @_;
Subroutine get_record_nf: called CC=8 SCAR=21.4
Pod documented
533 1629 ▶ my $data = $self->read_record($num) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $data = $self->read_record($num) or return 2 1627 1629
534 1627 ▶ if (not @fieldnums) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@fieldnums) 100 1527 1627
535 100 @fieldnums = ( 0 .. $self->last_field );
536 }
537 my $unpack = join ' ', '@0a1',
538 1627 map { my $e;
539 2121 ▶ defined $_ and $e = $self->{'field_unpacks'}[$_];
Branch: 1 branch
BranchTrueFalseTotal
defined $_ and $e = $self->{"field_unpacks"}[$_] 2121 0 2121
540 2121 ▶ defined $e ? $e : '@0a0'; } @fieldnums;
Branch: 1 branch
BranchTrueFalseTotal
defined $e ? : 2121 0 2121
541
542 1627 my $rproc = $self->{'field_rproc'};
543 1627 ▶ my @fns = (\&_read_deleted, map { (defined $_ and defined $rproc->[$_]) ? $rproc->[$_] : sub { undef; }; } @fieldnums);
Subroutine __ANON__: not called CC=1 SCAR=1.2
Branch: 1 branch
BranchTrueFalseTotal
defined $_ && defined $rproc->[$_] ? : 2121 0 2121
Condition33%1/3
defined $_ and defined $rproc->[$_]
!ll&&!rl&&r
002121
MC/DC0%0/2
defined $_ and defined $rproc->[$_]
defined $_ defined $rproc->[$_]
Truth table33%1/3
A and B
A= defined $_B= defined $rproc->[$_]
ABresult
0X0
100
111
544  
545 1627 my @out = unpack $unpack, $data;
546 ### if ($self->{'encrypted'} ne "\000") {
547 ### for my $data (@out) {
548 ### for (my $i = 0; $i < length($data); $i++) {
549 ### ## my $num = unpack 'C', substr($data, $i, 1);
550 ### ## substr($data, $i, 1) = pack 'C', (($num >> 3) | ($num << 5) ^ 020);
551 ### my $num = unpack 'C', substr($data, $i, 1);
552 ### substr($data, $i, 1) = pack 'C', (($num >> 1) | ($num << 7) ^ 052);
553 ### }
554 ### }
555 ### }
556  
557 1627 for (@out) { $_ = &{ shift @fns }($_); }
558  
559 1627 @out;
560 }
561  
562 # Processing on read
563 sub _read_deleted {
564 1627 ▶ my $value = shift;
Subroutine _read_deleted: called CC=3 SCAR=11.3
565 1627 ▶ if ($value eq '*') { return 1; } elsif ($value eq ' ') { return 0; }
Branch: 2 branches
BranchTrueFalseTotal
if ($value eq '*') { } 3 1624 1627
elsif ($value eq ' ') { } 1624 0 1624
566 0 undef;
567 }
568  
569 sub get_all_records {
570 1 ▶ my $self = shift;
Subroutine get_all_records: called CC=2 SCAR=6.9
Pod undocumented
571 1 my $cursor = $self->prepare_select(@_);
572  
573 1 my $result = [];
574 1 my @record;
575 1 ▶ while (@record = $cursor->fetch())
Branch: 1 branch
BranchTrueFalseTotal
if (@record = $cursor->fetch) 42 1 43
576 42 { push @$result, [ @record ]; }
577 1 $result;
578 }
579  
580 # #############
581 # Write records
582  
583 # Write record, values of the fields are in the argument list.
584 # Record is always undeleted
585 sub set_record {
586 14 ▶ my ($self, $num, @data) = @_;
Subroutine set_record: called CC=6 SCAR=17.9
Pod documented
587 14 $self->NullError();
588 14 my $wproc = $self->{'field_wproc'};
589  
590 14 ▶ if (defined $self->{'attached_index_columns'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"attached_index_columns"}) 1 13 14
591 1 my @nfs = keys %{$self->{'attached_index_columns'}};
592 1 my ($del, @old_data) = $self->get_record_nf($num, @nfs);
593  
594 1 local $^W = 0;
595 1 for my $nf (@nfs) {
596 2 ▶ if ($old_data[$nf] ne $data[$nf]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($old_data[$nf] ne $data[$nf]) 2 0 2
597 2 for my $idx (@{$self->{'attached_index_columns'}{$nf}}) {
598 5 $idx->delete($old_data[$nf], $num + 1);
599 5 $idx->insert($data[$nf], $num + 1);
600 }
601 }
602 }
603 }
604  
605 14 ▶ for (my $i = 0; $i <= $#$wproc; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $wproc->$#*) 43 14 57
606 43 $data[$i] = &{ $wproc->[$i] }($data[$i]);
607 }
608 14 unshift @data, ' ';
609  
610 ### if ($self->{'encrypted'} ne "\000") {
611 ### for my $data (@data) {
612 ### for (my $i = 0; $i < length($data); $i++) {
613 ### my $num = unpack 'C', substr($data, $i, 1);
614 ### substr($data, $i, 1) = pack 'C', (($num << 3) | ($num >> 5) ^ 020);
615 ### }
616 ### }
617 ### }
618  
619 14 $self->write_record($num, @data);
620 }
621  
622 # Write record, fields are specified as hash, unspecified are set to
623 # undef/empty
624 sub set_record_hash {
625 0 ▶ my ($self, $num, %data) = @_;
Subroutine set_record_hash: not called CC=1 SCAR=6.9
Pod documented
626 0 $self->NullError();
627 0 $self->set_record($num, map { $data{$_} } $self->field_names );
628 }
629  
630 # Write record, fields specified as hash, unspecified will be
631 # unchanged
632 sub update_record_hash {
633 0 ▶ my ($self, $num) = ( shift, shift );
Subroutine update_record_hash: not called CC=2 SCAR=17.9
Pod documented
634 0 $self->NullError();
635  
636 0 my %olddata = $self->get_record_hash($num);
637 0 ▶ return unless %olddata;
Branch: 1 branch
BranchTrueFalseTotal
unless %olddata 0 0 0
638 0 $self->set_record_hash($num, %olddata, @_);
639 }
640  
641 # Actually write the data (calling XBase::Base::write_record) and keep
642 # the overall structure of the file correct;
643 sub write_record {
644 16 ▶ my ($self, $num) = (shift, shift);
Subroutine write_record: called CC=5 SCAR=16.5
Pod documented
645 16 ▶ my $ret = $self->SUPER::write_record($num, @_) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $ret = $self->SUPER::write_record($num, @_) or return 0 16 16
646  
647 16 ▶ if ($num > $self->last_record) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num > $self->last_record) 13 3 16
648 13 $self->SUPER::write_record($num + 1, "\x1a"); # add EOF
649 13 ▶ $self->update_last_record($num) or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->update_last_record($num) or return 0 13 13
650 }
651 16 ▶ $self->update_last_change or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->update_last_change or return 0 16 16
652 16 $ret;
653 }
654  
655 # Delete and undelete record
656 sub delete_record {
657 1 ▶ my ($self, $num) = @_;
Subroutine delete_record: called CC=1 SCAR=0.0
Pod undocumented
658 1 $self->NullError();
659 1 $self->write_record($num, "*");
660 }
661 sub undelete_record {
662 1 ▶ my ($self, $num) = @_;
Subroutine undelete_record: called CC=1 SCAR=0.0
Pod undocumented
663 1 $self->NullError();
664 1 $self->write_record($num, " ");
665 }
666  
667 # Update the last change date
668 sub update_last_change {
669 18 ▶ my $self = shift;
Subroutine update_last_change: called CC=4 SCAR=14.0
Pod undocumented
670 18 ▶ return 1 if defined $self->{'updated_today'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"updated_today"} 11 7 18
671 7 ▶ my ($y, $m, $d) = (localtime)[5, 4, 3]; $m++; $y -= 100 if $y >= 100;
Branch: 1 branch
BranchTrueFalseTotal
if $y >= 100 7 0 7
672 7 ▶ $self->write_to(1, pack "C3", ($y, $m, $d)) or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->write_to(1, pack("C3", ($y, $m, $d))) or return 0 7 7
673 7 $self->{'updated_today'} = 1;
674 }
675 # Update the number of records
676 sub update_last_record {
677 13 ▶ my ($self, $last) = @_;
Subroutine update_last_record: called CC=1 SCAR=0.0
Pod undocumented
678 13 $last++;
679 13 $self->write_to(4, pack "V", $last);
680 13 $self->{'num_rec'} = $last;
681 }
682  
683 # Creating new dbf file
684 sub create {
685 2 ▶ XBase->NullError();
Subroutine create: called CC=33 SCAR=40.5
Pod documented
686 2 my $class = shift;
687 2 my %options = @_;
688 2 ▶ if (ref $class) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $class) 0 2 2
689 0 %options = ( %$class, %options ); $class = ref $class;
690 }
691  
692 2 my $version = $options{'version'};
693 2 ▶ if (not defined $version) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $version) 2 0 2
694 2 ▶ if (defined $options{'memofile'}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options{'memofile'} and $options{'memofile'} =~ /\.fpt$/i) { } 0 2 2
Condition33%1/3
defined $options{"memofile"} and $options{"memofile"} =~ /\.fpt$/i
!ll&&!rl&&r
200
MC/DC0%0/2
defined $options{"memofile"} and $options{"memofile"} =~ /\.fpt$/i
defined $options{"memofile"} $options{"memofile"} =~ /\.fpt$/i
Truth table33%1/3
A and B
A= defined $options{"memofile"}B= $options{"memofile"} =~ /\.fpt$/i
ABresult
0X0
100
111
695 and $options{'memofile'} =~ /\.fpt$/i) {
696 0 $version = 0xf5;
697 } else {
698 2 $version = 3;
699 }
700 }
701  
702 2 my $key;
703 2 for $key ( qw( field_names field_types field_lengths field_decimals ) ) {
704 8 ▶ if (not defined $options{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $options{$key}) 0 8 8
705 0 __PACKAGE__->Error("Tag $key must be specified when creating new table\n");
706 0 return;
707 }
708 }
709  
710 2 my $needmemo = 0;
711  
712 2 my $fieldspack = '';
713 2 my $record_len = 1;
714 2 my $i;
715 2 for $i (0 .. $#{$options{'field_names'}}) {
716 7 my $name = uc $options{'field_names'}[$i];
717 7 ▶ $name = "FIELD$i" unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 7 7
718 7 $name .= "\0";
719 7 my $type = $options{'field_types'}[$i];
720 7 ▶ $type = 'C' unless defined $type;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type 0 7 7
721  
722 7 my $length = $options{'field_lengths'}[$i];
723 7 my $decimal = $options{'field_decimals'}[$i];
724  
725 7 ▶ if (not defined $length) { # defaults
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $length) 0 7 7
726 0 ▶ if ($type eq 'C') { $length = 64; }
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'C') { } 0 0 0
elsif ($type =~ /^[TD]$/) { } 0 0 0
elsif ($type =~ /^[NF]$/) { } 0 0 0
727 0 elsif ($type =~ /^[TD]$/) { $length = 8; }
728 0 elsif ($type =~ /^[NF]$/) { $length = 8; }
729 }
730 # force correct lengths
731 7 ▶ if ($type =~ /^[MBGP]$/) { $length = 10; $decimal = 0; }
Branch: 3 branches
BranchTrueFalseTotal
if ($type =~ /^[MBGP]$/) { } 1 6 7
elsif ($type eq 'L') { } 1 5 6
elsif ($type eq 'Y') { } 0 5 5
732 1 elsif ($type eq 'L') { $length = 1; $decimal = 0; }
733 0 elsif ($type eq 'Y') { $length = 8; $decimal = 4; }
734  
735 7 ▶ if (not defined $decimal) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $decimal) 3 4 7
736 3 $decimal = 0;
737 }
738
739 7 $record_len += $length;
740 7 my $offset = $record_len;
741 7 ▶ if ($type eq 'C') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "C") 2 5 7
742 2 $decimal = int($length / 256);
743 2 $length %= 256;
744 }
745 7 $fieldspack .= pack 'a11a1VCCvCvCa7C', $name, $type, $offset,
746 $length, $decimal, 0, 0, 0, 0, '', 0;
747 7 ▶ if ($type eq 'M') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "M") 1 6 7
748 1 $needmemo = 1;
749 1 ▶ if ($version != 0x30) {
Branch: 1 branch
BranchTrueFalseTotal
if ($version != 48) 1 0 1
750 1 $version |= 0x80;
751 }
752 }
753 }
754 2 $fieldspack .= "\x0d";
755  
756 {
757 2 local $^W = 0;
758 2 $options{'codepage'} += 0;
759 }
760 my $header = pack 'C CCC V vvv CC a12 CC v',
761 $version,
762 0, 0, 0,
763 0,
764 (32 + length $fieldspack), $record_len, 0,
765 0, 0,
766 '',
767 2 0, $options{'codepage'},
768 0;
769 2 $header .= $fieldspack;
770 2 $header .= "\x1a";
771  
772 2 my $tmp = $class->new();
773 2 my $basename = $options{'name'};
774 2 $basename =~ s/\.dbf$//i;
775 2 my $newname = $options{'name'};
776 2 ▶ if (defined $newname and not $newname =~ /\.dbf$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $newname and not $newname =~ /\.dbf$/) 1 1 2
Condition66%2/3
defined $newname and not $newname =~ /\.dbf$/
!ll&&!rl&&r
011
MC/DC50%1/2
defined $newname and not $newname =~ /\.dbf$/
defined $newname not $newname =~ /\.dbf$/
Truth table66%2/3
A and not(B)
A= defined $newnameB= not $newname =~ /\.dbf$/
ABresult
0X0
100
111
777 1 $newname .= '.dbf';
778 }
779 2 ▶ $tmp->create_file($newname, 0700) or return;
Branch: 1 branch
BranchTrueFalseTotal
$tmp->create_file($newname, 448) or return 0 2 2
780 2 ▶ $tmp->write_to(0, $header) or return;
Branch: 1 branch
BranchTrueFalseTotal
$tmp->write_to(0, $header) or return 0 2 2
781 2 $tmp->update_last_change();
782 2 $tmp->close();
783  
784 2 ▶ if ($needmemo) {
Branch: 1 branch
BranchTrueFalseTotal
if ($needmemo) 1 1 2
785 1 require XBase::Memo;
786 1 my $dbtname = $options{'memofile'};
787 1 ▶ if (not defined $dbtname) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $dbtname) 1 0 1
788 1 $dbtname = $options{'name'};
789 1 ▶ if ($version == 0x30 or $version == 0xf5) {
Branch: 1 branch
BranchTrueFalseTotal
if ($version == 48 or $version == 245) { } 0 1 1
Condition33%1/3
$version == 48 or $version == 245
l!l&&r!l&&!r
001
MC/DC0%0/2
$version == 48 or $version == 245
$version == 48 $version == 245
Truth table33%1/3
A or B
A= $version == 48B= $version == 245
ABresult
1X1
011
000
790 0 ▶ $dbtname =~ s/\.DBF$/.FPT/ or $dbtname =~ s/(\.dbf)?$/.fpt/;
Branch: 1 branch
BranchTrueFalseTotal
$dbtname =~ s/\.DBF$/.FPT/ or $dbtname =~ s/(\.dbf)?$/.fpt/ 0 0 0
791 } else {
792 1 ▶ $dbtname =~ s/\.DBF$/.DBT/ or $dbtname =~ s/(\.dbf)?$/.dbt/;
Branch: 1 branch
BranchTrueFalseTotal
$dbtname =~ s/\.DBF$/.DBT/ or $dbtname =~ s/(\.dbf)?$/.dbt/ 1 0 1
793 }
794 }
795 1 my $dbttmp = XBase::Memo->new();
796 1 my $memoversion = ($version & 15);
797 1 ▶ $memoversion = 5 if $version == 0x30;
Branch: 1 branch
BranchTrueFalseTotal
if $version == 48 0 1 1
798 1 ▶ $dbttmp->create('name' => $dbtname,
Branch: 1 branch
BranchTrueFalseTotal
$dbttmp->create("name", $dbtname, "version", $memoversion, "dbf_filename", $basename) or return 0 1 1
799 'version' => $memoversion,
800 'dbf_filename' => $basename) or return;
801 }
802  
803 2 return $class->new($options{'name'});
804 }
805 # Drop the table
806 sub drop {
807 1 ▶ my $self = shift;
Subroutine drop: called CC=3 SCAR=11.4
Pod documented
808 1 my $filename = $self;
809 1 ▶ if (ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) 1 0 1
810 1 ▶ if (defined $self->{'memo'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"memo"}) 1 0 1
811 1 $self->{'memo'}->drop();
812 1 delete $self->{'memo'};
813 }
814 1 return $self->SUPER::drop();
815 }
816 0 XBase::Base::drop($filename);
817 }
818 # Lock and unlock
819 sub locksh {
820 0 ▶ my $self = shift;
Subroutine locksh: not called CC=4 SCAR=30.0
Pod undocumented
821 0 ▶ my $ret = $self->SUPER::locksh or return;
Branch: 1 branch
BranchTrueFalseTotal
my $ret = $self->SUPER::locksh or return 0 0 0
822 0 ▶ if (defined $self->{'memo'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"memo"}) 0 0 0
823 0 ▶ unless ($self->{'memo'}->locksh()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"memo"}->locksh) 0 0 0
824 0 $self->SUPER::unlock;
825 0 return;
826 }
827 }
828 0 $ret;
829 }
830 sub lockex {
831 0 ▶ my $self = shift;
Subroutine lockex: not called CC=4 SCAR=30.0
Pod undocumented
832 0 ▶ my $ret = $self->SUPER::lockex or return;
Branch: 1 branch
BranchTrueFalseTotal
my $ret = $self->SUPER::lockex or return 0 0 0
833 0 ▶ if (defined $self->{'memo'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"memo"}) 0 0 0
834 0 ▶ unless ($self->{'memo'}->lockex()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"memo"}->lockex) 0 0 0
835 0 $self->SUPER::unlock;
836 0 return;
837 }
838 }
839 0 $ret;
840 }
841 sub unlock {
842 0 ▶ my $self = shift;
Subroutine unlock: not called CC=2 SCAR=17.9
Pod undocumented
843 0 ▶ $self->{'memo'}->unlock() if defined $self->{'memo'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"memo"} 0 0 0
844 0 $self->SUPER::unlock;
845 }
846  
847 #
848 # Attaching index file
849 #
850  
851 sub attach_index {
852 1 ▶ my ($self, $indexfile) = @_;
Subroutine attach_index: called CC=9 SCAR=24.3
Pod undocumented
853 1 require XBase::Index;
854  
855 1 ▶ my $index = $self->XBase::Index::new($indexfile) or do {
Branch: 1 branch
BranchTrueFalseTotal
my $index = $self->XBase::Index::new($indexfile) or do { print STDERR "XBase"->errstr, "\n"; $self->Error("XBase"->errstr); return } 0 1 1
856 0 print STDERR XBase->errstr, "\n";
857 0 $self->Error(XBase->errstr);
858 0 return;
859 };
860 1 ▶print "Got index $index\n" if $XBase::Index::VERBOSE;
Branch: 1 branch
BranchTrueFalseTotal
if $XBase::Index::VERBOSE 0 1 1
861 1 my @tags = $index->tags;
862 1 my @indexes;
863 1 ▶ if (@tags) {
Branch: 1 branch
BranchTrueFalseTotal
if (@tags) { } 1 0 1
864 1 for my $tag (@tags) {
865 my $index = $self->XBase::Index::new($indexfile,
866 'tag' => $tag)
867 5 ▶ or do {
Branch: 1 branch
BranchTrueFalseTotal
my $index = $self->XBase::Index::new($indexfile, "tag", $tag) or do { print STDERR "XBase"->errstr, "\n"; $self->Error("XBase"->errstr); return } 0 5 5
868 0 print STDERR XBase->errstr, "\n";
869 0 $self->Error(XBase->errstr);
870 0 return;
871 };
872 5 push @indexes, $index;
873 }
874 } else {
875 0 @indexes = ( $index );
876 }
877 1 for my $idx (@indexes) {
878 5 my $key = $idx->{'key_string'};
879 5 my $num = $self->field_name_to_num($key);
880  
881 5 ▶ print "Got key string $key -> $num\n" if $XBase::Index::VERBOSE;
Branch: 1 branch
BranchTrueFalseTotal
if $XBase::Index::VERBOSE 0 5 5
882
883 $self->{'attached_index'} = []
884 5 ▶ unless defined $self->{'attached_index'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"attached_index"} 1 4 5
885 5 push @{$self->{'attached_index'}}, $idx;
886 5 push @{$self->{'attached_index_columns'}{$num}}, $idx;
887 }
888 1 1;
889 }
890  
891 #
892 # Cursory select
893 #
894  
895 sub prepare_select {
896 4 ▶ my $self = shift;
Subroutine prepare_select: called CC=2 SCAR=6.9
Pod documented
897 4 my $fieldnames = [ @_ ];
898 4 ▶ if (not @_) { $fieldnames = [ $self->field_names ] };
Branch: 1 branch
BranchTrueFalseTotal
unless (@_) 3 1 4
899 4 my $fieldnums = [ map { $self->field_name_to_num($_); } @$fieldnames ];
900 4 return bless [ $self, undef, $fieldnums, $fieldnames ], 'XBase::Cursor';
901 # object, recno, field numbers, field names
902 }
903  
904 sub prepare_select_nf {
905 0 ▶ my $self = shift;
Subroutine prepare_select_nf: not called CC=2 SCAR=17.9
Pod undocumented
906 0 my @fieldnames = $self->field_names;
907 0 ▶ if (@_) { @fieldnames = @fieldnames[ @_ ] }
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
908 0 return $self->prepare_select(@fieldnames);
909 }
910  
911 sub prepare_select_with_index {
912 8 ▶ my ($self, $file) = ( shift, shift );
Subroutine prepare_select_with_index: called CC=6 SCAR=19.0
Pod documented
913 8 my @tagopts = ();
914 8 ▶ if (ref $file eq 'ARRAY') { ### this is suboptimal
Branch: 1 branch
BranchTrueFalseTotal
if (ref $file eq "ARRAY") 4 4 8
915 ### interface but should suffice for the moment
916 4 @tagopts = ('tag' => $file->[1]);
917 4 ▶ if (defined $file->[2]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $file->[2]) 0 4 4
918 0 push @tagopts, ('type' => $file->[2]);
919 }
920 4 $file = $file->[0];
921 }
922 8 my $fieldnames = [ @_ ];
923 8 ▶ if (not @_) { $fieldnames = [ $self->field_names ] };
Branch: 1 branch
BranchTrueFalseTotal
unless (@_) 6 2 8
924 8 my $fieldnums = [ map { $self->field_name_to_num($_); } @$fieldnames ];
925 8 require XBase::Index;
926 my $index = new XBase::Index $file, 'dbf' => $self, @tagopts or
927 8 ▶ do { $self->Error(XBase->errstr); return; };
Branch: 1 branch
BranchTrueFalseTotal
my $index = "XBase::Index"->new($file, "dbf", $self, @tagopts) or do { $self->Error("XBase"->errstr); return } 0 8 8
928 $index->prepare_select or
929 8 ▶ do { $self->Error($index->errstr); return; };
Branch: 1 branch
BranchTrueFalseTotal
$index->prepare_select or do { $self->Error($index->errstr); return } 0 8 8
930 8 return bless [ $self, undef, $fieldnums, $fieldnames, $index ],
931 'XBase::IndexCursor';
932 # object, recno, field numbers, field names, index file
933 }
934  
935 package XBase::Cursor;
936 13 ▶use vars qw( @ISA );
Subroutine BEGIN: called CC=1 SCAR=0.0
937 13 @ISA = qw( XBase::Base );
938  
939 sub fetch {
940 198 ▶ my $self = shift;
Subroutine fetch: called CC=5 SCAR=16.4
941 198 my ($xbase, $recno, $fieldnums, $fieldnames) = @$self;
942 198 ▶ if (defined $recno) { $recno++; }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $recno) { } 194 4 198
943 4 else { $recno = 0; }
944 198 my $lastrec = $xbase->last_record;
945 198 ▶ while ($recno <= $lastrec) {
Branch: 1 branch
BranchTrueFalseTotal
if ($recno <= $lastrec) 194 4 198
946 194 my ($del, @result) = $xbase->get_record_nf($recno, @$fieldnums);
947 194 ▶ if (@result and not $del) {
Branch: 1 branch
BranchTrueFalseTotal
if (@result and not $del) 194 0 194
Condition33%1/3
@result and not $del
!ll&&!rl&&r
00194
MC/DC0%0/2
@result and not $del
@result not $del
Truth table33%1/3
A and not(B)
A= @resultB= not $del
ABresult
0X0
100
111
948 194 $self->[1] = $recno;
949 194 return @result;
950 }
951 0 $recno++;
952 }
953 4 return;
954 }
955 sub fetch_hashref {
956 0 ▶ my $self = shift;
Subroutine fetch_hashref: not called CC=2 SCAR=17.9
957 0 my @data = $self->fetch;
958 0 my $hashref = {};
959 0 ▶ if (@data) {
Branch: 1 branch
BranchTrueFalseTotal
if (@data) 0 0 0
960 0 @{$hashref}{ @{$self->[3]} } = @data;
961 0 return $hashref;
962 }
963 0 return;
964 }
965 sub last_fetched {
966 0 ▶ shift->[1];
Subroutine last_fetched: not called CC=1 SCAR=6.9
967 }
968 sub table {
969 0 ▶ shift->[0];
Subroutine table: not called CC=1 SCAR=6.9
970 }
971 sub names {
972 0 ▶ shift->[3];
Subroutine names: not called CC=1 SCAR=6.9
973 }
974 sub rewind {
975 0 ▶ shift->[1] = undef; '0E0';
Subroutine rewind: not called CC=1 SCAR=6.9
976 }
977  
978 sub attach_index {
979 0 ▶ my $self = shift;
Subroutine attach_index: not called CC=1 SCAR=6.9
980 0 require XBase::Index;
981  
982 }
983  
984 package XBase::IndexCursor;
985 13 ▶use vars qw( @ISA );
Subroutine BEGIN: called CC=1 SCAR=0.0
986 13 @ISA = qw( XBase::Cursor );
987  
988 sub find_eq {
989 42 ▶ my $self = shift;
Subroutine find_eq: called CC=1 SCAR=0.0
990 42 $self->[4]->prepare_select_eq(shift);
991 }
992 sub fetch {
993 1360 ▶ my $self = shift;
Subroutine fetch: called CC=3 SCAR=11.0
994 1360 my ($xbase, $recno, $fieldnums, $fieldnames, $index) = @$self;
995 1360 my ($key, $val);
996 1360 ▶ while (($key, $val) = $index->fetch) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = $index->fetch) 1317 43 1360
997 1317 my ($del, @result) = $xbase->get_record_nf($val - 1, @$fieldnums);
998 1317 ▶ unless ($del) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($del) 1317 0 1317
999 1317 $self->[1] = $val;
1000 1317 return @result;
1001 }
1002 }
1003 43 return;
1004 }
1005  
1006 # Indexed number the records starting from one, not zero.
1007 sub last_fetched {
1008 0 ▶ shift->[1] - 1;
Subroutine last_fetched: not called CC=1 SCAR=6.9
1009 }
1010  
1011 13 1;
1012  
1013 __END__
« lib/XBase/Index.pm ↑ Summary lib/XBase/SDBM.pm »