lib/XBase.pm

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