lib/XBase/Base.pm

filter: Statementstmt 76.9% 150 / 195 Branchbran 71.1% 74 / 104 Conditioncond 66.6% 12 / 18 MC/DCmcdc 45.4% 5 / 11 Subroutinesub 72.4% 21 / 29 Podpod 43.4% 10 / 23 totaltotal 71.5% 272 / 380 CC 59 SCAR 18.0
CC
59
Cov
74%
open
13.1
read_from
9.5
read_record
7.2
CRAP 6.1
« lib/XBase/Memo.pm ↑ Summary script/dbfdump »
1  
2 =head1 NAME
3  
4 XBase::Base - Base input output module for XBase suite
5  
6 =cut
7  
8 package XBase::Base;
9  
10 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 13 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 13 ▶use Fcntl qw( O_RDWR O_RDONLY O_BINARY );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 13 $XBase::Base::VERSION = '1.02';
15  
16 # Sets the debug level
17 13 $XBase::Base::DEBUG = 0;
18 sub DEBUG () {
19 0 ▶ $XBase::Base::DEBUG
Subroutine DEBUG: not called CC=1 SCAR=6.9
Pod undocumented
20 }
21  
22 13 my $SEEK_VIA_READ = 0;
23  
24 # Holds the text of the global error, if there was one
25 13 $XBase::Base::errstr = '';
26 # Fetch the error message
27 sub errstr () {
28 2 ▶ ( ref $_[0] ? $_[0]->{'errstr'} : $XBase::Base::errstr );
Subroutine errstr: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 1 1 2
29 }
30  
31 # Set errstr and print error on STDERR if there is debug level
32 sub Error (@) {
33 269 ▶ my $self = shift;
Subroutine Error: called CC=2 SCAR=6.9
Pod undocumented
34 269 ▶ ( ref $self ? $self->{'errstr'} : $XBase::Base::errstr ) = join '', @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 168 101 269
35 }
36 # Null the errstr
37 sub NullError {
38 263 ▶ shift->Error('');
Subroutine NullError: called CC=1 SCAR=0.0
Pod undocumented
39 }
40  
41  
42 # Build the object in the memory, open the file
43 sub new {
44 49 ▶ __PACKAGE__->NullError();
Subroutine new: called CC=3 SCAR=11.0
Pod documented
45 49 my $class = shift;
46 49 my $new = bless {}, $class;
47 49 ▶ if (@_ and not $new->open(@_)) { return; }
Branch: 1 branch
BranchTrueFalseTotal
if (@_ and not $new->open(@_)) 4 45 49
Condition100%3/3
@_ and not $new->open(@_)
!ll&&!rl&&r
3415
MC/DC100%2/2
@_ and not $new->open(@_)
@_ not $new->open(@_)
Truth table100%3/3
A and not(B)
A= @_B= not $new->open(@_)
ABresult
0X0
100
111
48 45 return $new;
49 }
50 # Open the specified file. Use the read_header to load the header data
51 sub open {
52 46 ▶ __PACKAGE__->NullError();
Subroutine open: called CC=13 SCAR=25.8
Pod documented
53 46 my $self = shift;
54 46 my %options;
55 46 ▶ if (scalar(@_) % 2) { $options{'name'} = shift; }
Branch: 1 branch
BranchTrueFalseTotal
if (scalar(@_) % 2) 25 21 46
56 46 ▶ $self->{'openoptions'} = { %options, @_ } unless defined $self->{'openoptions'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"openoptions"} 25 21 46
57 46 %options = (%options, @_);
58 46 ▶ if (defined $self->{'fh'}) { $self->close(); }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"fh"}) 0 46 46
59  
60 46 my $external_fh = 0;
61 46 my $fh = new IO::File;
62 46 my $rw;
63
64 46 ▶ if ($options{'name'} eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{'name'} eq '-') { } 2 44 46
65 2 ▶ if (defined $options{'fh'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options{'fh'}) { } 1 1 2
66 1 $fh = $options{'fh'};
67 1 $external_fh = 1;
68 } else {
69 1 $fh->fdopen(fileno(STDIN), 'r');
70 1 $self->{'stream'} = 1;
71 1 SEEK_VIA_READ(1);
72 }
73 2 $rw = 0;
74 } else {
75 44 my $ok = 1;
76 44 ▶ if (not $options{'readonly'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($options{"readonly"}) 44 0 44
77 44 ▶ if ($fh->open($options{'name'}, O_RDWR|O_BINARY)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fh->open($options{'name'}, 2)) { } 40 4 44
78 40 $rw = 1;
79 } else {
80 4 $ok = 0;
81 }
82 }
83 44 ▶ if (not $ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 4 40 44
84 4 ▶ if ($fh->open($options{'name'}, O_RDONLY|O_BINARY)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fh->open($options{'name'}, 0)) { } 0 4 4
85 0 $rw = 0; $ok = 1;
86 } else {
87 4 $ok = 0;
88 }
89 }
90 44 ▶ if (not $ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 4 40 44
91 4 __PACKAGE__->Error("Error opening file $options{'name'}: $!\n");
92 4 return;
93 }
94 }
95  
96 42 ▶ $self->{'tell'} = 0 if $SEEK_VIA_READ;
Branch: 1 branch
BranchTrueFalseTotal
if $SEEK_VIA_READ 2 40 42
97 42 $fh->autoflush();
98  
99 42 ▶ binmode($fh) unless $external_fh;
Branch: 1 branch
BranchTrueFalseTotal
unless $external_fh 41 1 42
100 42 @{$self}{ qw( fh filename rw ) } = ($fh, $options{'name'}, $rw);
101 ## $self->locksh();
102  
103 # read_header should be defined in the derived class
104 42 $self->read_header(@_);
105 }
106 # Close the file
107 sub close {
108 13 ▶ my $self = shift;
Subroutine close: called CC=2 SCAR=7.5
Pod documented
109 13 $self->NullError();
110 13 ▶ if (not defined $self->{'fh'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"fh"}) 0 13 13
111 0 $self->Error("Can't close file that is not opened\n");
112 0 return;
113 }
114 13 $self->{'fh'}->close();
115 13 delete $self->{'fh'};
116 13 1;
117 }
118 # Read from the filehandle
119 sub read {
120 1842 ▶ my $self = shift;
Subroutine read: called CC=4 SCAR=14.0
Pod undocumented
121 1842 ▶ my $fh = $self->{'fh'} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $fh = $self->{"fh"} or return 0 1842 1842
122 1842 my $result = $fh->read(@_);
123 1842 ▶ if (defined $result and defined $self->{'tell'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $result and defined $self->{"tell"}) 66 1776 1842
Condition66%2/3
defined $result and defined $self->{"tell"}
!ll&&!rl&&r
0177666
MC/DC50%1/2
defined $result and defined $self->{"tell"}
defined $result defined $self->{"tell"}
Truth table66%2/3
A and B
A= defined $resultB= defined $self->{"tell"}
ABresult
0X0
100
111
124 66 $self->{'tell'} += $result;
125 }
126 1842 $result;
127 }
128 # Tell the position
129 sub tell {
130 130 ▶ my $self = shift;
Subroutine tell: called CC=2 SCAR=6.9
Pod undocumented
131 130 ▶ if (defined $self->{'tell'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"tell"}) 56 74 130
132 56 return $self->{'tell'};
133 }
134 74 return $self->{'fh'}->tell();
135 }
136 # Drop (unlink) the file
137 sub drop {
138 2 ▶ my $self = shift;
Subroutine drop: called CC=4 SCAR=15.2
Pod documented
139 2 $self->NullError();
140 2 ▶ if (defined $self->{'filename'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"filename"}) 2 0 2
141 2 my $filename = $self->{'filename'};
142 2 ▶ $self->close() if defined $self->{'fh'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"fh"} 2 0 2
143 2 ▶ if (not unlink $filename) {
Branch: 1 branch
BranchTrueFalseTotal
unless (unlink $filename) 0 2 2
144 0 $self->Error("Error unlinking file $filename: $!\n");
145 0 return;
146 }
147 }
148 2 1;
149 }
150  
151 # Create new file
152 sub create_file {
153 5 ▶ my $self = shift;
Subroutine create_file: called CC=5 SCAR=17.4
Pod documented
154 5 my ($filename, $perms) = @_;
155 5 ▶ if (not defined $filename) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $filename) 0 5 5
156 0 __PACKAGE__->Error("Name has to be specified when creating new file\n");
157 0 return;
158 }
159 5 ▶ if (-f $filename) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $filename) 0 5 5
160 0 __PACKAGE__->Error("File $filename already exists\n");
161 0 return;
162 }
163  
164 5 ▶ $perms = 0644 unless defined $perms;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $perms 3 2 5
165 5 my $fh = new IO::File;
166 5 ▶ $fh->open($filename, 'w+', $perms) or return;
Branch: 1 branch
BranchTrueFalseTotal
$fh->open($filename, "w+", $perms) or return 0 5 5
167 5 binmode($fh);
168 5 @{$self}{ qw( fh filename rw ) } = ($fh, $filename, 1);
169 5 return $self;
170 }
171  
172  
173 # Compute the offset of the record
174 sub get_record_offset {
175 1730 ▶ my ($self, $num) = @_;
Subroutine get_record_offset: called CC=4 SCAR=17.9
Pod undocumented
176 my ($header_len, $record_len) = ($self->{'header_len'},
177 1730 $self->{'record_len'});
178 1730 ▶ unless (defined $header_len and defined $record_len) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $header_len and defined $record_len) 0 1730 1730
Condition33%1/3
defined $header_len and defined $record_len
!ll&&!rl&&r
001730
MC/DC0%0/2
defined $header_len and defined $record_len
defined $header_len defined $record_len
Truth table33%1/3
A and B
A= defined $header_lenB= defined $record_len
ABresult
0X0
100
111
179 0 $self->Error("Header and record lengths not known in get_record_offset\n");
180 0 return;
181 }
182 1730 ▶ unless (defined $num) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $num) 0 1730 1730
183 0 $self->Error("Number of the record must be specified in get_record_offset\n");
184 0 return;
185 }
186 1730 return $header_len + $num * $record_len;
187 }
188  
189  
190 # Seek to start of the record
191 sub seek_to_record {
192 0 ▶ my ($self, $num) = @_;
Subroutine seek_to_record: not called CC=2 SCAR=17.9
Pod documented
193 0 ▶ defined (my $offset = $self->get_record_offset($num)) or return;
Branch: 1 branch
BranchTrueFalseTotal
defined(my $offset = $self->get_record_offset($num)) or return 0 0 0
194 0 $self->seek_to($offset);
195 }
196 # Seek to absolute position
197 sub seek_to_seek {
198 1718 ▶ my ($self, $offset) = @_;
Subroutine seek_to_seek: called CC=3 SCAR=14.2
Pod undocumented
199 1718 ▶ unless (defined $self->{'fh'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"fh"}) 0 1718 1718
200 0 $self->Error("Cannot seek on unopened file\n");
201 0 return;
202 }
203 1718 ▶ unless ($self->{'fh'}->seek($offset, 0)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"fh"}->seek($offset, 0)) 0 1718 1718
204 0 $self->Error("Seek error (file $self->{'filename'}, offset $offset): $!\n");
205 0 return;
206 }
207 1718 1;
208 }
209 sub seek_to_read {
210 45 ▶ my ($self, $offset) = @_;
Subroutine seek_to_read: called CC=5 SCAR=18.4
Pod undocumented
211 45 ▶ unless (defined $self->{'fh'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"fh"}) 0 45 45
212 0 $self->Error("Cannot seek on unopened file\n");
213 0 return;
214 }
215 45 my $tell = $self->tell();
216 45 ▶ if ($offset < $tell) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset < $tell) 0 45 45
217 0 $self->Error("Cannot seek backwards without using seek ($offset < $tell)\n");
218 0 return;
219 }
220 45 ▶ if ($offset > $tell) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset > $tell) 2 43 45
221 2 my $undef;
222 2 $self->read($undef, $offset - $tell);
223 2 $tell = $self->tell();
224 }
225 45 ▶ if ($tell != $offset) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tell != $offset) 0 45 45
226 0 $self->Error("Some error occured during read-seek: $!\n");
227 0 return;
228 }
229 45 1;
230 }
231 sub SEEK_VIA_READ {
232 16 ▶ local $^W = 0;
Subroutine SEEK_VIA_READ: called CC=2 SCAR=6.9
Pod undocumented
233 16 ▶ if ($_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]) { } 2 14 16
234 2 *seek_to = \&seek_to_read; $SEEK_VIA_READ = 1;
235 } else {
236 14 *seek_to = \&seek_to_seek; $SEEK_VIA_READ = 0;
237 }
238 }
239 13 SEEK_VIA_READ(0);
240  
241 # Read the record of given number. The second parameter is the length of
242 # the record to read. It can be undefined, meaning read the whole record,
243 # and it can be negative, meaning at most the length
244 sub read_record {
245 1688 ▶ my ($self, $num, $in_length) = @_;
Subroutine read_record: called CC=7 SCAR=19.7
Pod documented
246 1688 ▶ if (not defined $num) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $num) 0 1688 1688
247 0 $self->Error("Number of the record must be defined when reading it\n");
248 0 return;
249 }
250 1688 ▶ if ($self->last_record > 0 and $num > $self->last_record) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->last_record > 0 and $num > $self->last_record) 2 1686 1688
Condition100%3/3
$self->last_record > 0 and $num > $self->last_record
!ll&&!rl&&r
2616602
MC/DC100%2/2
$self->last_record > 0 and $num > $self->last_record
$self->last_record > 0 $num > $self->last_record
Truth table100%3/3
A and B
A= $self->last_record > 0B= $num > $self->last_record
ABresult
0X0
100
111
251 2 $self->Error("Can't read record $num, there is not so many of them\n");
252 2 return;
253 }
254 1686 ▶ if (not defined $in_length) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $in_length) 1672 14 1686
255 1672 $in_length = $self->{'record_len'};
256 }
257 1686 ▶ if ($in_length < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_length < 0) 14 1672 1686
258 14 $in_length = -$self->{'record_len'};
259 }
260  
261 1686 ▶ defined (my $offset = $self->get_record_offset($num)) or return;
Branch: 1 branch
BranchTrueFalseTotal
defined(my $offset = $self->get_record_offset($num)) or return 0 1686 1686
262 1686 $self->read_from($offset, $in_length);
263 }
264 sub read_from {
265 1686 ▶ my ($self, $offset, $in_length) = @_;
Subroutine read_from: called CC=7 SCAR=22.5
Pod documented
266 1686 ▶ unless (defined $offset) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $offset) 0 1686 1686
267 0 $self->Error("Offset to read from must be specified\n");
268 0 return;
269 }
270 1686 ▶ $self->seek_to($offset) or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->seek_to($offset) or return 0 1686 1686
271 1686 my $length = $in_length;
272 1686 ▶ $length = -$length if $length < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $length < 0 14 1672 1686
273 1686 my $buffer;
274 1686 my $read = $self->read($buffer, $length);
275 1686 ▶ if (not defined $read or ($in_length > 0 and $read != $in_length)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $read or $in_length > 0 and $read != $in_length) 0 1686 1686
Condition50%3/6
not defined $read or $in_length > 0 and $read != $in_length
l!l&&r!l&&!r
001686
$in_length > 0 and $read != $in_length
!ll&&!rl&&r
1416720
MC/DC0%0/3
not defined $read or $in_length > 0 and $read != $in_length
not defined $read $in_length > 0 $read != $in_length
Truth table25%1/4
not(A) or B and C
A= not defined $readB= $in_length > 0C= $read != $in_length
ABCresult
1XX1
0111
00X0
0100
276 0 $self->Error("Error reading $in_length bytes from $self->{'filename'}\n");
277 0 return;
278 }
279 1686 $buffer;
280 }
281  
282 # Write the given record
283 sub write_record {
284 44 ▶ my ($self, $num) = (shift, shift);
Subroutine write_record: called CC=4 SCAR=14.2
Pod documented
285 44 ▶ defined (my $offset = $self->get_record_offset($num)) or return;
Branch: 1 branch
BranchTrueFalseTotal
defined(my $offset = $self->get_record_offset($num)) or return 0 44 44
286 44 ▶ defined $self->write_to($offset, @_) or return;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->write_to($offset, @_) or return 0 44 44
287 44 ▶ $num == 0 ? '0E0' : $num;
Branch: 1 branch
BranchTrueFalseTotal
$num == 0 ? : 3 41 44
288 }
289 # Write data directly to offset
290 sub write_to {
291 77 ▶ my ($self, $offset) = (shift, shift);
Subroutine write_to: called CC=5 SCAR=18.7
Pod documented
292 77 ▶ if (not $self->{'rw'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"rw"}) 0 77 77
293 0 $self->Error("The file $self->{'filename'} is not writable\n");
294 0 return;
295 }
296 77 ▶ $self->seek_to($offset) or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->seek_to($offset) or return 0 77 77
297 77 local ($,, $\) = ('', '');
298 $self->{'fh'}->print(@_) or
299 77 ▶ do { $self->Error("Error writing to offset $offset in file $self->{'filename'}: $!\n");
Branch: 1 branch
BranchTrueFalseTotal
$self->{"fh"}->print(@_) or do { $self->Error(qq[Error writing to offset $offset in file $self->{"filename"}: $!\n]); return } 0 77 77
300 0 return;
301 };
302 77 ▶ $offset == 0 ? '0E0' : $offset;
Branch: 1 branch
BranchTrueFalseTotal
$offset == 0 ? : 13 64 77
303 }
304  
305  
306 0 ▶sub locksh { _locksh(shift->{'fh'}) }
Subroutine locksh: not called CC=1 SCAR=6.9
Pod undocumented
307 0 ▶sub lockex { _lockex(shift->{'fh'}) }
Subroutine lockex: not called CC=1 SCAR=6.9
Pod undocumented
308 0 ▶sub unlock { _unlock(shift->{'fh'}) }
Subroutine unlock: not called CC=1 SCAR=6.9
Pod undocumented
309  
310 0 ▶sub _locksh { flock(shift, 1); }
Subroutine _locksh: not called CC=1 SCAR=6.9
311 0 ▶sub _lockex { flock(shift, 2); }
Subroutine _lockex: not called CC=1 SCAR=6.9
312 0 ▶sub _unlock { flock(shift, 8); }
Subroutine _unlock: not called CC=1 SCAR=6.9
313  
314  
315 13 1;
316  
317 __END__
« lib/XBase/Memo.pm ↑ Summary script/dbfdump »