lib/Palm/ListDB/Writer.pm

filter: Statementstmt 88.6% 117 / 132 Branchbran 53.8% 28 / 52 Conditioncond 52.1% 12 / 23 MC/DCmcdc 25.0% 3 / 12 Subroutinesub 100% 11 / 11 Podpod 100% 5 / 5 totaltotal 74.8% 176 / 235 CC 42 SCAR 28.1
CC
42
Cov
76%
write
27.6
add
15.6
new
7.1
CRAP 16.6
↑ Summary script/csv2lsdb »
1 #! perl
2  
3 package Palm::ListDB::Writer;
4  
5 # ListDB.pm -- Create databases for Palm List application.
6  
7 # Author : Johan Vromans
8 # Created On : Sun Aug 31 20:18:31 2003
9 # Last Modified By: Johan Vromans
10 # Last Modified On: Tue Apr 11 12:01:23 2017
11 # Update Count : 87
12 # Status : Unknown, Use with caution!
13  
14 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 2 our $VERSION = "1.13";
19  
20 =head1 NAME
21  
22 Palm::ListDB::Writer - Create databases for Palm List application
23  
24 =head1 SYNOPSIS
25  
26 use Palm::ListDB::Writer;
27 my $db = new Palm::ListDB::Writer
28 "MyDataBase",
29 "label1" => "Name",
30 "label2" => "Address");
31 $db->add("Private", "Wife", "16 Localstreet, Netcity", "Some comments");
32 $db->add("Public", "John Doe", "1 Main St., Connecticut", "Blah blah");
33 $db->write("addr.pdb");
34  
35 =head1 ABSTRACT
36  
37 Palm::ListDB::Writer creates databases for the Palm utility List.
38  
39 =head1 DESCRIPTION
40  
41 Palm::ListDB::Writer can be used to create databases for the Palm
42 utility List, a simple but convenient database application. List is
43 written by Andrew Low (roo@magma.ca, http://www.magma.ca/~roo).
44  
45 List databases consist of a collection of records, each having two
46 label fields and one note field (arbitrary data).
47  
48 The basic usage is to create a Palm::ListDB::Writer object with
49 attributes like the name of the database, the primary and secondary
50 field labels, and then add records by calling its add() method.
51  
52 The write() method writes the collected data out in the form of a Palm
53 database.
54  
55 Limitations posed by the Palm operating system and/or the List
56 application: database name - 31 characters; field names - 15
57 characters; category names - 15 characters; 15 categories excluding
58 the default (Unfiled) catagory; field values - 63 characters; length
59 of note data - 1023 characters.
60  
61 This module is not related to L<Palm::ListDB>; the latter can also
62 import Palm List databases, and requires some knowledge about Palm
63 databases.
64  
65 =cut
66  
67 2 my @_atts = qw(cat truncate readonly private backup
68 autocat label1 label2);
69  
70 =head1 METHODS
71  
72 =over 4
73  
74 =item new I<database>, [ I<attributes> ]
75  
76 Default constructor for a new database object.
77  
78 new() requires one argument, the name of the database.
79  
80 Initial attributes for the database can be specified after the
81 database name in the form of key => value pairs:
82  
83 =over 4
84  
85 =item label1
86  
87 The name for the primary record field, max. 15 characters.
88  
89 =item label2
90  
91 The name for the secondary record field, max. 15 characters.
92  
93 =item cat
94  
95 An array reference with the names of the categories. Max. 15
96 categories are allowed, and category names must not exceed 15
97 characters in length.
98  
99 =item autocat
100  
101 If non-zero, new categories are automatically added when records are
102 added. Defaults to true if no initial category names were supplied.
103  
104 Additional methods can be added later with the add_cat() method.
105  
106 =item readonly
107  
108 If true, the database will be readonly and cannot be modified by the
109 List application.
110  
111 =item backup
112  
113 If false, the database will not be backupped upon the next HotSync.
114 Note that the List application may change this, for example when
115 modifications are made.
116  
117 =item private
118  
119 If true, the database is private and cannot be beamed.
120  
121 =item truncate
122  
123 Controls truncation of names and fields that are too long.
124 If zero, no truncation takes place (the program is terminated).
125 If one, excess data for the record fields is truncated.
126 If two, also truncates names for categories and fields.
127 If three, also truncates the name of the database if needed.
128  
129 =back
130  
131 =cut
132  
133 2 ▶sub new($;@) {
Subroutine new: called CC=7 SCAR=19.5
Pod documented
134 2 my ($pkg, $name, %opts) = @_;
135 2 my $self =
136 { name => $name,
137 cat => [],
138 _cat => {},
139 truncate => 0,
140 readonly => 0,
141 private => 0,
142 backup => 1,
143 autocat => undef,
144 label1 => "Field1",
145 label2 => "Field2",
146 _data => [],
147 };
148 2 bless($self, $pkg);
149  
150 2 $self->{name} = $self->_checklen("Database name", $name, 31, 2);
151  
152 2 foreach my $att ( @_atts ) {
153 16 ▶ if ( exists($opts{$att}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{$att}) 4 12 16
154 4 $self->{$att} = delete($opts{$att});
155 }
156 }
157 2 ▶ croak(__PACKAGE__.": Unknown constructor attributes: ".
Branch: 1 branch
BranchTrueFalseTotal
if %opts 0 2 2
158 join(" ", sort(keys(%opts)))) if %opts;
159  
160 2 $self->{autocat} = @{$self->{cat}} ? 0 : 1
161 2 ▶ unless defined $self->{autocat};
Branch: 2 branches
BranchTrueFalseTotal
unless defined $self->{"autocat"} 2 0 2
@{$self->{'cat'};} ? : 2 0 2
162 2 unshift(@{$self->{cat}}, "Unfiled");
163 2 my @a = @{$self->{cat}};
164 2 $self->{cat} = [];
165 2 foreach my $cat ( @a ) {
166 10 $self->_addcat($cat);
167 }
168 2 $self->{label1} = $self->_checklen("Label1", $self->{label1}, 15, 1);
169 2 $self->{label2} = $self->_checklen("Label2", $self->{label2}, 15, 1);
170 2 $self->{ctime} = $self->{mtime} = $self->{btime} = time;
171 2 $self;
172 }
173  
174 30 ▶sub _checklen {
Subroutine _checklen: called CC=3 SCAR=18.4
175 30 my ($self, $name, $value, $max, $lvl) = @_;
176 30 ▶ if ( length($value) > $max ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $value > $max) 0 30 30
177 0 my $v = substr($value,0,30);
178 0 $v =~ s/[^\040-\177]/./g;
179 0 my $msg = __PACKAGE__.": $name (".
180 $v.") too long (".
181 length($value)." > $max)";
182 0 ▶ if ( $self->{truncate} > $lvl ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'truncate'} > $lvl) { } 0 0 0
183 0 warn("$msg, truncated\n");
184 0 substr($value, $max) = "";
185 }
186 else {
187 0 croak($msg);
188 }
189 }
190 30 $value;
191 }
192  
193 12 ▶sub _addcat {
Subroutine _addcat: called CC=3 SCAR=11.3
194 12 my ($self, $value) = @_;
195 12 $value = $self->_checklen("Category name", $value, 15, 1);
196 return $self->{_cat}->{$value}
197 12 ▶ if defined($self->{_cat}->{$value});
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"_cat"}{$value} 0 12 12
198 12 ▶ if ( @{$self->{cat}} == 16 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"cat"};} == 16) 0 12 12
199 0 croak(__PACKAGE__.": Too many categories ($value)");
200 }
201 12 push(@{$self->{cat}}, $value);
202 12 $self->{_cat}->{$value} = @{$self->{cat}};
203 }
204  
205 =item add I<category>, I<field1>, I<field2>, I<note data>
206  
207 As the name suggests, add() adds records to the database.
208  
209 Add() takes exactly four arguments: the category for the record, its
210 first field, its second field, and the note data. Fields may be left
211 empty (or undefined), but not all of them.
212  
213 If the named category does not exists, and autocat is in effect, it is
214 automatically added to the list of categories.
215  
216 Add() returns true if the record was successfully added.
217  
218 =cut
219  
220 4 ▶sub add($$$$$) {
Subroutine add: called CC=9 SCAR=27.5
Pod documented
221 4 my ($self, $cat, $f1, $f2, $note) = @_;
222  
223 4 ▶ if ( $self->{_cat}->{$cat} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'_cat'}{$cat}) { } 3 1 4
elsif ($self->{'autocat'}) { } 1 0 1
224 3 $cat = $self->{_cat}->{$cat};
225 }
226 elsif ( $self->{autocat} ) {
227 1 $cat = $self->_addcat($cat);
228 }
229 else {
230 0 carp(__PACKAGE__.": Unknown category ($cat)");
231 0 return 0;
232 }
233 4 ▶ if ( $f1 eq "" && $f2 eq "" && $note eq "" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f1 eq "" and $f2 eq "" and $note eq "") 0 4 4
Condition33%2/6
$f1 eq "" and $f2 eq "" and $note eq ""
!ll&&!rl&&r
400
$f1 eq "" and $f2 eq ""
!ll&&!rl&&r
400
MC/DC0%0/3
$f1 eq "" and $f2 eq "" and $note eq ""
$f1 eq "" $f2 eq "" $note eq ""
Truth table25%1/4
A and B and C
A= $f1 eq ""B= $f2 eq ""C= $note eq ""
ABCresult
0XX0
10X0
1100
1111
234 0 carp(__PACKAGE__.": Record needs data");
235 0 return 0;
236 }
237 4 ▶ push(@{$self->{_data}},
Condition50%3/6
$f1 || ''
l!l
40
$f2 || ''
l!l
40
$note || ''
l!l
40
MC/DC0%0/1
$f1 || ''
$f1
MC/DC0%0/1
$f2 || ''
$f2
MC/DC0%0/1
$note || ''
$note
Truth table50%1/2
A
A= $f1
Aresult
11
00
Truth table50%1/2
A
A= $f2
Aresult
11
00
Truth table50%1/2
A
A= $note
Aresult
11
00
238 [$cat,
239 $self->_checklen("field1", $f1||"", 63, 1),
240 $self->_checklen("field2", $f2||"", 63, 1),
241 $self->_checklen("note", $note||"", 1023, 0)]);
242 4 1;
243 }
244  
245 =item add_cat I<name>
246  
247 Adds a new category. One parameter, the name of the category to be
248 added. If the category already exists, nothing happens.
249  
250 =cut
251  
252 1 ▶sub add_cat($$) {
Subroutine add_cat: called CC=2 SCAR=7.0
Pod documented
253 1 my ($self, $cat) = @_;
254 1 my $catcode = $self->{_cat}->{$cat};
255 1 ▶ return $catcode if $catcode;
Branch: 1 branch
BranchTrueFalseTotal
if $catcode 0 1 1
256 1 $self->_addcat($cat);
257 }
258  
259 =item categories
260  
261 Returns an array with the current set of categories.
262 Note that this excludes the (default) 'Unfiled' category.
263  
264 =cut
265  
266 1 ▶sub categories($) {
Subroutine categories: called CC=1 SCAR=0.0
Pod documented
267 1 my ($self) = @_;
268 1 my @a = @{$self->{cat}};
269 1 shift(@a);
270 1 @a;
271 }
272  
273 =item write I<filename>
274  
275 Write() takes one argument: the file name for the database.
276  
277 Returns true if the database was successfully written.
278  
279 =cut
280  
281 2 ▶sub write($$) {
Subroutine write: called CC=23 SCAR=33.2
Pod documented
282 2 my ($self, $file) = @_;
283 2 ▶ unless ( @{$self->{_data}} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$self->{"_data"};}) 0 2 2
284 0 carp(__PACKAGE__.": No records to write to $file");
285 0 return 0;
286 }
287  
288 # Based on information derived from code by Gustaf Naeser and
289 # Darren Dunham.
290  
291 2 my $n_records = scalar(@{$self->{_data}});
292 2 my $dbname = $self->{name};
293 2 my $field1label = $self->{label1};
294 2 my $field2label = $self->{label2};
295 2 my $numcats = @{$self->{cat}};
296  
297 # Pre-sort the records. This eliminates the need for List to resort,
298 # which makes opening the database very fast.
299 2 ▶ use locale;
Subroutine BEGIN: called CC=1 SCAR=0.0
300 my @records = sort {
301 2 ▶ lc($a->[1]) cmp lc($b->[1])
Condition33%2/6
lc $Palm::ListDB::Writer::a->[1] cmp lc $Palm::ListDB::Writer::b->[1] || lc $Palm::ListDB::Writer::a->[2] cmp lc $Palm::ListDB::Writer::b->[2] || $Palm::ListDB::Writer::a->[0] <=> $Palm::ListDB::Writer::b->[0]
l!l&&r!l&&!r
200
lc $Palm::ListDB::Writer::a->[1] cmp lc $Palm::ListDB::Writer::b->[1] or lc $Palm::ListDB::Writer::a->[2] cmp lc $Palm::ListDB::Writer::b->[2]
l!l&&r!l&&!r
200
MC/DC0%0/3
lc $Palm::ListDB::Writer::a->[1] cmp lc $Palm::ListDB::Writer::b->[1] || lc $Palm::ListDB::Writer::a->[2] cmp lc $Palm::ListDB::Writer::b->[2] || $Palm::ListDB::Writer::a->[0] <=> $Palm::ListDB::Writer::b->[0]
lc $Palm::ListDB::Writer::a->[1] cmp lc $Palm::ListDB::Writer::b->[1] lc $Palm::ListDB::Writer::a->[2] cmp lc $Palm::ListDB::Writer::b->[2] $Palm::ListDB::Writer::a->[0] <=> $Palm::ListDB::Writer::b->[0]
Truth table25%1/4
A or B || C
A= lc $Palm::ListDB::Writer::a->[1] cmp lc $Palm::ListDB::Writer::b->[1]B= lc $Palm::ListDB::Writer::a->[2] cmp lc $Palm::ListDB::Writer::b->[2]C= $Palm::ListDB::Writer::a->[0] <=> $Palm::ListDB::Writer::b->[0]
ABCresult
1XX1
01X1
0011
0000
302 ||
303 lc($a->[2]) cmp lc($b->[2])
304 ||
305 $a->[0] <=> $b->[0]
306 2 } @{$self->{_data}};
307  
308 2 my $fh;
309 2 ▶ open($fh, ">$file")
Branch: 1 branch
BranchTrueFalseTotal
open $fh, ">$file" or croak('Palm::ListDB::Writer' . ": ${file}: $!") 2 0 2
310 || croak(__PACKAGE__.": $file: $!");
311 2 binmode($fh);
312  
313 # Structure of the database
314 #
315 # - Database header (78 bytes)
316 # - Index table ($n_records * 8 bytes + 0x8000 padding)
317 # - Application info (512 bytes)
318 # - Data (records)
319  
320 # The database header (78 bytes)
321 # 32 bytes database name, nul filled, nul terminated
322 # 2 bytes of attributes, set to 0x0008 (backup)
323 # 2 bytes of version information, set to 0x0000
324 # 12 bytes dates (creation, modification, last backup; 4 bytes each)
325 # 4 bytes modification number, set to 0x00000000
326 # 4 bytes offset to application info
327 # 4 bytes offset to sort info (set to 0x00000000)
328 # 4 bytes type = "DATA"
329 # 4 bytes creator = "LSdb"
330 # 4 bytes unique seed, set to 0x00000000
331 # 4 bytes next record list, set to 0x00000000
332 # 2 bytes number of records
333  
334 2 my $headerfmt = "Z32 n n NNN N N N A4 A4 N N n";
335 my $hdr = pack($headerfmt,
336 $dbname,
337 0 | ($self->{backup} ? 0x0008 : 0x0)
338 | ($self->{private} ? 0x0040 : 0x0),
339 0x0000,
340 $self->{ctime},
341 $self->{mtime},
342 $self->{btime},
343 2 ▶ 0,
Branch: 2 branches
BranchTrueFalseTotal
$self->{'backup'} ? : 2 0 2
$self->{'private'} ? : 0 2 2
344 78 + ($n_records * 8) + 2,
345 0,
346 "DATA",
347 "LSdb",
348 0,
349 0,
350 $n_records);
351 2 ▶ croak(__PACKAGE__.": Header is ".length($hdr)." instead of 78")
Branch: 1 branch
BranchTrueFalseTotal
unless length $hdr == 78 0 2 2
352 unless length($hdr) == 78;
353 2 print $fh ($hdr);
354  
355 # Index table (8 bytes/record + 0x8000 padding)
356 # 4 bytes offset to record data
357 # 1 byte attributes = index of the category the record belongs to
358 # 3 bytes unique id = index of the record (counting from 0)
359  
360 2 my $offset = (78 + ($n_records * 8) + 512 + 2);
361 2 my $index = 0;
362 2 foreach my $record ( @records ) {
363 4 my ($cat, $field1, $field2, $note, $len) = @$record;
364 4 $len = 3;
365 4 ▶ $len += length($field1)+1 if $field1 ne "";
Branch: 1 branch
BranchTrueFalseTotal
if $field1 ne "" 4 0 4
366 4 ▶ $len += length($field2)+1 if $field2 ne "";
Branch: 1 branch
BranchTrueFalseTotal
if $field2 ne "" 4 0 4
367 4 ▶ $len += length($note)+1 if $note ne "";
Branch: 1 branch
BranchTrueFalseTotal
if $note ne "" 4 0 4
368 4 ▶ $len++ if $len == 3;
Branch: 1 branch
BranchTrueFalseTotal
if $len == 3 0 4 4
369 4 print $fh (pack("NN", $offset, $index | (($cat-1) << 24)));
370 4 $offset += $len;
371 4 $index++;
372 }
373 # Padding.
374 2 print $fh (pack("n", 0x8000));
375  
376 # Application info (size = 512 bytes)
377 # 2 bytes renamed categories, set to 0x000e
378 # 16 * 16 bytes of category labels, nul padded, nul terminated
379 # 16 * 1 byte of category unique ids
380 # (first (Unfiled)) = 0x00
381 # (rest) index + 0x0f if used, index otherwise
382 # E.g. 00 10 11 12 14 15 06 07 08 09 0a 0b 0c 0d 0e 0f
383 # 1 byte last unique id, set to the highest category unique id
384 # 1 byte display style, set to 0x00 (no resort, field1/field2)
385 # 1 byte write protect, 0x00 for off, 0x01 for on
386 # 1 byte last category, 0xff for all, 0x00 for Unfiled
387 # (The category view the DB opens with)
388 # 16 bytes custom field 1 label, nul padded, nul terminated
389 # 16 bytes custom field 2 label, as above
390 # 202 bytes padding to make the size 512 bytes
391  
392 # Note: repeat groups ups the requirement to 5.8. Not needed.
393 2 my $appinfofmt = "n ".("Z16" x 16)." C16 C C C C Z16Z16 x202";
394 my $appinfo = pack($appinfofmt,
395 0x000e,
396 32 ▶ (map { $self->{cat}->[$_] || '' } 0..15),
Condition100%2/2
$self->{'cat'}[$_] || ''
l!l
1220
MC/DC100%1/1
$self->{'cat'}[$_] || ''
$self->{'cat'}[$_]
Truth table100%2/2
A
A= $self->{'cat'}[$_]
Aresult
11
00
397 32 ▶ (map { $_ && $_ < $numcats ? $_ + 0x0f : $_ } 0..15),
Branch: 2 branches
BranchTrueFalseTotal
$_ && $_ < $numcats ? : 10 22 32
$self->{'readonly'} ? : 0 2 2
Condition100%3/3
$_ and $_ < $numcats
!ll&&!rl&&r
22010
MC/DC100%2/2
$_ and $_ < $numcats
$_ $_ < $numcats
Truth table100%3/3
A and B
A= $_B= $_ < $numcats
ABresult
0X0
100
111
398 $numcats - 1 + 0x0f,
399 0x00, # no resort, field1/field2
400 # 0x80, # force resort, field1/field2
401 # 0x81, # force resort, field2/field1
402 2 $self->{readonly} ? 0x01 : 0x00,
403 0xff, # last category -- all
404 $field1label, $field2label);
405 2 ▶ croak(__PACKAGE__.": AppInfo is ".length($appinfo)." instead of 512")
Branch: 1 branch
BranchTrueFalseTotal
unless length $appinfo == 512 0 2 2
406 unless length($appinfo) == 512;
407 2 print $fh ($appinfo);
408  
409 # Records
410 # 1 byte offset to field 1, 0 if no data in field
411 # 1 byte offset to field 2, 0 if no data in field
412 # 1 byte offset to note, 0 if no data in field
413 # up to 3 0x00 terminated fields of max length 63, 63, 1023
414 # If no fields, then a nul pad is necessary (though this will never
415 # be the case since we disallow that).
416  
417 2 foreach my $record ( @records ) {
418 4 my ($cat, $field1, $field2, $note) = @$record;
419 4 $offset = 3;
420 4 foreach ( $field1, $field2, $note ) {
421 12 my $len = length($_);
422 12 ▶ if ( $len ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len) { } 12 0 12
423 12 print $fh (pack("C", $offset));
424 12 $offset += $len + 1;
425 }
426 else {
427 0 print $fh (pack("C", 0));
428 }
429 }
430 4 foreach ( $field1, $field2, $note ) {
431 12 ▶ next unless length($_);
Branch: 1 branch
BranchTrueFalseTotal
unless length $_ 0 12 12
432 12 print $fh (pack("a*x", $_));
433 }
434 }
435  
436 # Everything has been written
437 2 close($fh);
438  
439 2 1;
440 }
441  
442 2 1;
443 __END__
↑ Summary script/csv2lsdb »