lib/XBase/Memo.pm

filter: Statementstmt 70.9% 134 / 189 Branchbran 39.7% 31 / 78 Conditioncond 27.7% 5 / 18 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 90.4% 19 / 21 Podpod 25.0% 1 / 4 totaltotal 59.0% 190 / 322 CC 46 SCAR 23.1
CC
46
Cov
60%
write_record
16.0
create
15.4
read_record
12.6
CRAP 10.0
« lib/XBase/SDBM.pm ↑ Summary lib/XBase/Base.pm »
1  
2 =head1 NAME
3  
4 XBase::Memo - Generic support for various memo formats
5  
6 =cut
7  
8 package XBase::Memo;
9  
10 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 13 ▶use XBase::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 13 ▶use vars qw( $VERSION @ISA );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 13 @ISA = qw( XBase::Base );
15 13 $VERSION = '1.02';
16  
17 # Read header is called from open to fill the object structures
18 sub read_header {
19 7 ▶ my $self = shift;
Subroutine read_header: called CC=9 SCAR=24.4
Pod undocumented
20 7 my %options = @_;
21  
22 7 my $header;
23 7 my $read_h_len = $self->read($header, 512);
24 7 ▶ unless ($read_h_len == 512 or $read_h_len == 24) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($read_h_len == 512 or $read_h_len == 24) 0 7 7
Condition33%1/3
$read_h_len == 512 or $read_h_len == 24
l!l&&r!l&&!r
700
MC/DC0%0/2
$read_h_len == 512 or $read_h_len == 24
$read_h_len == 512 $read_h_len == 24
Truth table33%1/3
A or B
A= $read_h_len == 512B= $read_h_len == 24
ABresult
1X1
011
000
25 0 $self->Error("Error reading header of $self->{'filename'}: $!\n");
26 0 return;
27 };
28  
29 7 my ($next_for_append, $block_size, $version);
30 7 my $filename = $self->{'filename'};
31 7 ▶ if ($filename =~ /\.fpt$/i) {
Branch: 2 branches
BranchTrueFalseTotal
if ($filename =~ /\.fpt$/i) { } 1 6 7
elsif ($filename =~ /\.smt$/i) { } 0 6 6
32 1 ($next_for_append, $block_size) = unpack 'N@6n', $header;
33 1 $version = 5;
34 1 bless $self, 'XBase::Memo::Fox';
35 } elsif ($filename =~ /\.smt$/i) {
36 0 ($next_for_append, $block_size) = unpack 'VV', $header;
37 0 bless $self, 'XBase::Memo::Apollo';
38 } else {
39 6 ($next_for_append, $version, $block_size)
40 = unpack 'V @16C @20v', $header;
41 6 my $dbf_version = $options{'dbf_version'};
42 6 ▶ $dbf_version = 15 unless defined $dbf_version;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dbf_version 0 6 6
43 6 ▶ if ((($dbf_version & 15) == 3) or $version == 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (($dbf_version & 15) == 3 or $version == 3) { } 6 0 6
Condition33%1/3
($dbf_version & 15) == 3 or $version == 3
l!l&&r!l&&!r
600
MC/DC0%0/2
($dbf_version & 15) == 3 or $version == 3
($dbf_version & 15) == 3 $version == 3
Truth table33%1/3
A or B
A= ($dbf_version & 15) == 3B= $version == 3
ABresult
1X1
011
000
44 6 $block_size = 512;
45 6 $version = 3;
46 6 bless $self, 'XBase::Memo::dBaseIII';
47 } else {
48 0 $version = 4;
49 0 bless $self, 'XBase::Memo::dBaseIV';
50 }
51 }
52  
53 7 ▶ $block_size = 512 if int($block_size) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if int $block_size == 0 0 7 7
54  
55 7 $next_for_append = int ((((-s $self->{'filename'}) - 1) / $block_size) + 1);
56  
57 7 @{$self}{ qw( next_for_append header_len record_len version ) }
58 = ( $next_for_append, $block_size, $block_size, $version );
59  
60 7 $self->{'memosep'} = $options{'memosep'};
61  
62 7 1;
63 }
64  
65 sub write_record {
66 6 ▶ my ($self, $num) = (shift, shift);
Subroutine write_record: called CC=4 SCAR=14.0
Pod documented
67 6 my $length = length(join '', @_);
68 6 my $record_len = $self->{'record_len'};
69 6 my $num_of_blocks = int (($length + $record_len - 1) / $record_len);
70 6 $self->SUPER::write_record($num, @_);
71 6 ▶ if ($num < 0 or $num > $self->last_record()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num < 0 or $num > $self->last_record) 6 0 6
Condition33%1/3
$num < 0 or $num > $self->last_record
l!l&&r!l&&!r
060
MC/DC0%0/2
$num < 0 or $num > $self->last_record
$num < 0 $num > $self->last_record
Truth table33%1/3
A or B
A= $num < 0B= $num > $self->last_record
ABresult
1X1
011
000
72 6 my $packed = pack "V", $num + $num_of_blocks + 1;
73 6 ▶ if (ref $self eq 'XBase::Memo::Fox') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self eq "XBase::Memo::Fox") 2 4 6
74 2 $packed = pack "N", $num + $num_of_blocks + 1;
75 }
76 6 $self->SUPER::write_to(0, $packed);
77 6 $self->{'next_for_append'} = $num + $num_of_blocks + 1;
78 }
79 6 $num;
80 }
81  
82 sub last_record {
83 54 ▶ shift->{'next_for_append'} - 2;
Subroutine last_record: called CC=1 SCAR=0.0
Pod undocumented
84 }
85  
86 sub create {
87 1 ▶ my $self = shift;
Subroutine create: called CC=9 SCAR=27.3
Pod undocumented
88 1 my %options = @_;
89 1 ▶ $self->create_file($options{'name'}) or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->create_file($options{"name"}) or return 0 1 1
90 1 my $version = $options{'version'};
91 1 ▶ if ($options{'name'} =~ /\.smt$/i) {
Branch: 2 branches
BranchTrueFalseTotal
if ($options{'name'} =~ /\.smt$/i) { } 0 1 1
elsif ($version == 5) { } 0 1 1
92 # For xmt file (whatever they are)
93 0 ▶ $self->write_to(0, pack 'VV a504', 1, 512, '') or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->write_to(0, "\1\0\0\0\0\2\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\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 if $version == 4;
Branch: 1 branch
BranchTrueFalseTotal
if $version == 4 0 1 1
100 $self->write_to(0, pack 'VVa8Ca3va490', 1, 0,
101 1 ▶ $options{'dbf_filename'}, $version, '', 512, '')
Branch: 1 branch
BranchTrueFalseTotal
$self->write_to(0, pack("VVa8Ca3va490", 1, 0, $options{"dbf_filename"}, $version, "", 512, "")) or return 0 1 1
102 or return;
103 }
104 1 $self->close();
105 1 return $self;
106 }
107  
108  
109 # ################################
110 # dBase III+ specific memo methods
111  
112 package XBase::Memo::dBaseIII;
113  
114 13 ▶use XBase::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
115 13 ▶use vars qw( @ISA );
Subroutine BEGIN: called CC=1 SCAR=0.0
116 13 @ISA = qw( XBase::Memo );
117  
118 sub read_record {
119 10 ▶ my ($self, $num) = @_;
Subroutine read_record: called CC=7 SCAR=23.5
120 10 my $result = '';
121 10 my $last = $self->last_record();
122 10 ▶ if (not defined $self->{'memosep'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"memosep"}) 2 8 10
123 2 $self->{'memosep'} = "\x1a\x1a";
124 2 ▶ if (not defined $self->read_record($last)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->read_record($last)) 0 2 2
125 0 $self->{'memosep'} = "\x1a";
126 0 ▶ if (not defined $self->read_record($last)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->read_record($last)) 0 0 0
127 0 $self->{'memosep'} = "\x1a\x1a";
128 }
129 }
130 }
131  
132 10 ▶ while ($num <= $last) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num <= $last) 10 0 10
133 10 ▶ my $buffer = $self->SUPER::read_record($num, -1) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $buffer = $self->SUPER::read_record($num, -1) or return 0 10 10
134 10 my $index = index($buffer, $self->{'memosep'});
135 10 ▶ if ($index >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index >= 0) 10 0 10
136 10 return $result . substr($buffer, 0, $index);
137 }
138 0 $result .= $buffer;
139 0 $num++;
140 }
141 0 return;
142 }
143  
144 sub write_record {
145 4 ▶ my ($self, $num) = (shift, shift);
Subroutine write_record: called CC=5 SCAR=24.1
146 4 my $type = shift;
147 4 my $data = join "", @_, "\x1a\x1a";
148 4 ▶ if ($num >= 0 and $num <= $self->last_record()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num >= 0 and $num <= $self->last_record) { } 0 4 4
Condition33%1/3
$num >= 0 and $num <= $self->last_record
!ll&&!rl&&r
400
MC/DC0%0/2
$num >= 0 and $num <= $self->last_record
$num >= 0 $num <= $self->last_record
Truth table33%1/3
A and B
A= $num >= 0B= $num <= $self->last_record
ABresult
0X0
100
111
149 0 my $buffer = $self->read_record($num);
150 0 ▶ if (defined $buffer) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $buffer) 0 0 0
151 0 my $length = length $buffer;
152 0 my $record_len = $self->{'record_len'};
153 0 my $space_in_blocks =
154 int (($length + $record_len - 3) / $record_len);
155 0 my $len_in_blocks =
156 int ((length($data) + $record_len - 1) / $record_len);
157 0 ▶ if ($len_in_blocks > $space_in_blocks) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len_in_blocks > $space_in_blocks) 0 0 0
158 0 $num = $self->last_record() + 1;
159 }
160 }
161 } else {
162 4 $num = $self->last_record() + 1;
163 }
164 4 $self->SUPER::write_record($num, $data);
165 4 $num;
166 }
167  
168 # ################################
169 # dBase IV specific memo methods
170  
171 package XBase::Memo::dBaseIV;
172  
173 13 ▶use XBase::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
174 13 ▶use vars qw( @ISA );
Subroutine BEGIN: called CC=1 SCAR=0.0
175 13 @ISA = qw( XBase::Memo );
176  
177 sub read_record {
178 4 ▶ my ($self, $num) = @_;
Subroutine read_record: called CC=7 SCAR=25.3
179 4 my $result = '';
180 4 my $last = $self->last_record;
181  
182 4 my $buffer = $self->SUPER::read_record($num, -1);
183 4 ▶ if (not defined $buffer) { return; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $buffer) 0 4 4
184 4 my $unpackstr;
185 4 ▶ if (ref $self eq 'XBase::Memo::Fox') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self eq 'XBase::Memo::Fox') { } 4 0 4
186 4 $unpackstr = 'NN';
187 } else {
188 0 $unpackstr = 'VV';
189 0 ▶ return unless substr($buffer, 0, 4) eq "\xff\xff\x08\x00";
Branch: 1 branch
BranchTrueFalseTotal
unless substr($buffer, 0, 4) eq "\377\377\b\0" 0 0 0
190 }
191 4 my ($unused_id, $length) = unpack $unpackstr, $buffer;
192 4 ▶ $length += 8 if ref $self eq 'XBase::Memo::Fox';
Branch: 1 branch
BranchTrueFalseTotal
if ref $self eq "XBase::Memo::Fox" 4 0 4
193  
194 4 my $block_size = $self->{'record_len'};
195 4 ▶ if ($length < $block_size) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length < $block_size) 4 0 4
196 4 return substr $buffer, 8, $length - 8;
197 }
198 0 my $rest_length = $length - $block_size;
199 0 my $rest_data = $self->SUPER::read_record($num + 1, $rest_length);
200 0 ▶ if (not defined $rest_data) { return; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rest_data) 0 0 0
201 0 return substr($buffer, 8) . $rest_data;
202 }
203  
204 sub write_record {
205 2 ▶ my ($self, $num) = (shift, shift);
Subroutine write_record: called CC=8 SCAR=27.7
206 2 my $type = shift;
207 2 my $data = join "", @_;
208 2 my $length = (length $data) + 8;
209  
210 2 my $startfield = "\xff\xff\x08\x00" . pack('V', $length);
211 2 ▶ if (ref $self eq 'XBase::Memo::Fox') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self eq "XBase::Memo::Fox") 2 0 2
212 2 ▶ if ($type eq 'P') { $startfield = pack 'N', 0; }
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'P') { } 0 2 2
elsif ($type eq 'M') { } 2 0 2
213 2 elsif ($type eq 'M') { $startfield = pack 'N', 1; }
214 0 else { $startfield = pack 'N', 2; }
215 2 $startfield .= pack 'N', ($length - 8);
216 }
217 ### $data = $startfield . $data . "\x1a\x1a";
218 2 $data = $startfield . $data;
219  
220 2 ▶ if ($num >= 0 and $num <= $self->last_record()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num >= 0 and $num <= $self->last_record) { } 0 2 2
Condition33%1/3
$num >= 0 and $num <= $self->last_record
!ll&&!rl&&r
200
MC/DC0%0/2
$num >= 0 and $num <= $self->last_record
$num >= 0 $num <= $self->last_record
Truth table33%1/3
A and B
A= $num >= 0B= $num <= $self->last_record
ABresult
0X0
100
111
221 0 my $buffer = $self->read_record($num);
222 0 ▶ if (defined $buffer) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $buffer) { } 0 0 0
223 0 my $length = (length $buffer) - 8;
224 0 my $record_len = $self->{'record_len'};
225 0 my $space_in_blocks =
226 int (($length + $record_len - 11) / $record_len);
227 0 my $len_in_blocks =
228 int ((length($data) + $record_len - 1) / $record_len);
229 0 ▶ if ($len_in_blocks > $space_in_blocks) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len_in_blocks > $space_in_blocks) 0 0 0
230 0 $num = $self->last_record() + 1;
231 }
232 } else {
233 0 $num = $self->last_record() + 1;
234 }
235 } else {
236 2 $num = $self->last_record() + 1;
237 }
238 2 my $fill = $self->{'record_len'} - (( length $data ) % $self->{'record_len'});
239 2 $data .= "\000" x $fill;
240 2 $self->SUPER::write_record($num, $data);
241 2 $num;
242 }
243  
244  
245 # #######################################
246 # FoxPro specific memo methods (fpt file)
247  
248 package XBase::Memo::Fox;
249  
250 13 ▶use XBase::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
251 13 ▶use vars qw( @ISA );
Subroutine BEGIN: called CC=1 SCAR=0.0
252 13 @ISA = qw( XBase::Memo::dBaseIV );
253  
254 # #######################################
255 # Apollo specific memo methods (smt file)
256 #
257 # This is a real hack! No documentation used but it works for all files
258 # i have tested with.
259 # Dirk Tostmann (tostmann@tiss.com)
260  
261 package XBase::Memo::Apollo;
262  
263 13 ▶use XBase::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
264 13 ▶use vars qw( @ISA );
Subroutine BEGIN: called CC=1 SCAR=0.0
265 13 @ISA = qw( XBase::Memo::dBaseIV );
266  
267 sub read_record {
268 0 ▶ my ($self, $num) = @_;
Subroutine read_record: not called CC=2 SCAR=17.9
269 0 my $result = '';
270  
271 0 ▶ return if $num =~ /^\s+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $num =~ /^\s+$/ 0 0 0
272  
273 0 my ($block, $len, $offset) = unpack('vVV', $num);
274 0 $block *= 8;
275  
276 0 $result = $self->read_from($offset * $block, $len);
277  
278 0 $result;
279 }
280  
281 sub write_record {
282 0 ▶ my ($self, $num, $type) = (shift, shift, shift);
Subroutine write_record: not called CC=3 SCAR=24.8
283 0 my $data = join "", @_;
284 0 my $length = length $data;
285 0 $num = $self->SUPER::write_record($self->{'next_for_append'}, $data);
286 0 ▶ if (defined $num and $num) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $num and $num) { } 0 0 0
Condition0%0/3
defined $num and $num
!ll&&!rl&&r
000
MC/DC0%0/2
defined $num and $num
defined $num $num
Truth table0%0/3
A and B
A= defined $numB= $num
ABresult
0X0
100
111
287 0 pack 'vVV', $self->{'block_length'} / 8 , $length, $num;
288 } else {
289 0 ' ' x 10;
290 }
291 }
292 13 1;
293  
294 __END__
« lib/XBase/SDBM.pm ↑ Summary lib/XBase/Base.pm »