| 1 | # -*- perl -*- | ||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||
| 3 | # $Id: ListDB.pm Mon Aug 5 19:57:50 2002 $ | ||||||||||||||||||||||||||||
| 4 | # | ||||||||||||||||||||||||||||
| 5 | # DESCRIPTION Sub class of Palm::PDB | ||||||||||||||||||||||||||||
| 6 | # specialized for listDB by Andrew Low | ||||||||||||||||||||||||||||
| 7 | # | ||||||||||||||||||||||||||||
| 8 | # COPYRIGHT (c) 2002 by Rudiger Peusquens | ||||||||||||||||||||||||||||
| 9 | # All rights reserved. | ||||||||||||||||||||||||||||
| 10 | # This program is free software; you can redistribute it | ||||||||||||||||||||||||||||
| 11 | # and/or modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 12 | # | ||||||||||||||||||||||||||||
| 13 | # AUTHOR Rudiger Peusquens <rudy@peusquens.net> | ||||||||||||||||||||||||||||
| 14 | # | ||||||||||||||||||||||||||||
| 15 | # HISTORY | ||||||||||||||||||||||||||||
| 16 | # $Log$ | ||||||||||||||||||||||||||||
| 17 | # | ||||||||||||||||||||||||||||
| 18 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | package Palm::ListDB; | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 1 | ▶ | use Palm::StdAppInfo(); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 1 | ▶ | use Palm::Raw(); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 1 | ▶ | use Carp qw(carp); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 1 | ▶ | use vars qw( $VERSION @ISA ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 1 | @ISA = qw( Palm::StdAppInfo Palm::Raw ); | |||||||||||||||||||||||||||
| 29 | 1 | $VERSION = '0.25'; | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 1 | ▶ | sub import { | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | 1 | &Palm::PDB::RegisterPDBHandlers( __PACKAGE__, | |||||||||||||||||||||||||||
| 33 | [ "LSdb", "DATA" ], | ||||||||||||||||||||||||||||
| 34 | [ "LSdb", "" ] ); | ||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 38 | 0 | my $classname = shift; | |||||||||||||||||||||||||||
| 39 | 0 | my $self = $classname->SUPER::new(@_); | |||||||||||||||||||||||||||
| 40 | # Create a generic PDB. No need to rebless it, | ||||||||||||||||||||||||||||
| 41 | # though. | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 0 | $self->{name} = 'ListDB'; # give it a default name | |||||||||||||||||||||||||||
| 44 | 0 | $self->{creator} = 'LSdb'; | |||||||||||||||||||||||||||
| 45 | 0 | $self->{type} = 'DATA'; | |||||||||||||||||||||||||||
| 46 | 0 | $self->{attributes}{resource} = 0; | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | # Initialize the AppInfo block | ||||||||||||||||||||||||||||
| 49 | $self->{appinfo} = { | ||||||||||||||||||||||||||||
| 50 | 0 | writeProtect => 0, # *ListDb's* write protection | |||||||||||||||||||||||||||
| 51 | lastCategory => 0xff, # default: All | ||||||||||||||||||||||||||||
| 52 | field1 => '', | ||||||||||||||||||||||||||||
| 53 | field2 => '', | ||||||||||||||||||||||||||||
| 54 | }; | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | # Add the standard AppInfo block stuff | ||||||||||||||||||||||||||||
| 57 | 0 | &Palm::StdAppInfo::seed_StdAppInfo($self->{appinfo}); | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | # Give the PDB a blank sort block | ||||||||||||||||||||||||||||
| 60 | 0 | $self->{sort} = undef; | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | # Give the PDB an empty list of records | ||||||||||||||||||||||||||||
| 63 | 0 | $self->{records} = []; | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 0 | return $self; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 0 | ▶ | sub ParseAppInfoBlock { | ||||||||||||||||||||||||||
|
Subroutine
ParseAppInfoBlock: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 69 | 0 | my ($self, $data) = @_; | |||||||||||||||||||||||||||
| 70 | 0 | my $appinfo = {}; | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 0 | &Palm::StdAppInfo::parse_StdAppInfo( $appinfo, $data ); | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | # don't know what first 2 bytes are for | ||||||||||||||||||||||||||||
| 75 | 0 | my ($C1, $C2, $field1, $field2) = unpack 'C2a16a16', $appinfo->{other}; | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 0 | $appinfo->{ writeProtect } = $C1; | |||||||||||||||||||||||||||
| 78 | 0 | $appinfo->{ lastCategory } = $C2; | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | # trim after first NUL (renaming might have left "Foo\0bar") | ||||||||||||||||||||||||||||
| 81 | 0 | $field1 =~ s/\0.*$//; | |||||||||||||||||||||||||||
| 82 | 0 | $field2 =~ s/\0.*$//; | |||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 0 | $appinfo->{field1} = $field1; | |||||||||||||||||||||||||||
| 85 | 0 | $appinfo->{field2} = $field2; | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 0 | return $appinfo; | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 0 | ▶ | sub PackAppInfoBlock { | ||||||||||||||||||||||||||
|
Subroutine
PackAppInfoBlock: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 91 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 0 | my %data = ( writeProtect => 0, | |||||||||||||||||||||||||||
| 94 | lastCategory => 0xff ); # default 0xff : "All" | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 0 | foreach my $attr (qw(field1 field2)) { | |||||||||||||||||||||||||||
| 97 | 0 | ▶ | if ( defined $self->{appinfo}{$attr} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 0 | $data{$attr} = $self->{appinfo}{$attr}; | |||||||||||||||||||||||||||
| 99 | } else { | ||||||||||||||||||||||||||||
| 100 | 0 | $data{$attr} = ''; | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | 0 | ▶ | if ( $self->{appinfo}{writeProtect} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | 0 | $data{writeProtect} = 1; | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | 0 | ▶ | if ( defined $self->{appinfo}{lastCategory} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | 0 | $data{lastCategory} = $self->{appinfo}{lastCategory}; | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | # Pack the non-category part of the AppInfo block | ||||||||||||||||||||||||||||
| 111 | # We need to pad the last 202 bytes. | ||||||||||||||||||||||||||||
| 112 | $self->{appinfo}{other} = pack 'C2a16a16x202', | ||||||||||||||||||||||||||||
| 113 | 0 | @data{qw(writeProtect lastCategory | |||||||||||||||||||||||||||
| 114 | field1 field2)}; | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | # Pack the AppInfo block | ||||||||||||||||||||||||||||
| 117 | 0 | return &Palm::StdAppInfo::pack_StdAppInfo($self->{appinfo}); | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 0 | ▶ | sub ParseRecord { | ||||||||||||||||||||||||||
|
Subroutine
ParseRecord: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 122 | 0 | my ($self, %record) = @_; | |||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 0 | delete $record{offset}; # This is useless | |||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | # split into fields | ||||||||||||||||||||||||||||
| 127 | # ignore first 3 chars; they are offsets | ||||||||||||||||||||||||||||
| 128 | 0 | my ($field1, $field2, $note) = split /\0/, substr( $record{ data }, 3 ); | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 0 | ▶ | $record{ field1 } = $field1 unless $field1 eq ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 0 | ▶ | $record{ field2 } = $field2 unless $field2 eq ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | 0 | ▶ | unless ( $note eq '' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | # make sure we have our local newlines | ||||||||||||||||||||||||||||
| 134 | 0 | ( $record{ note } = $note ) =~ s/\012/\n/g; | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 0 | delete $record{ data }; | |||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 0 | return \%record; | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 0 | ▶ | sub PackRecord { | ||||||||||||||||||||||||||
|
Subroutine
PackRecord: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 143 | 0 | my ($self, $record) = @_; | |||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 0 | my %data = (); | |||||||||||||||||||||||||||
| 146 | 0 | foreach my $attr (qw(field1 field2 note)) { | |||||||||||||||||||||||||||
| 147 | 0 | ▶ | if ( defined $record->{$attr} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 148 | 0 | $data{$attr} = $record->{$attr}; | |||||||||||||||||||||||||||
| 149 | } else { | ||||||||||||||||||||||||||||
| 150 | 0 | $data{$attr} = ''; | |||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | # fix note if it's a array ref | ||||||||||||||||||||||||||||
| 155 | 0 | $data{note} = join "\012", @{ $data{note} } | |||||||||||||||||||||||||||
| 156 | 0 | ▶ | if ref $data{note} eq 'ARRAY'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 0 | $data{note} =~ s/\r?\n\r?/\012/g; # fix PalmOS newlines | |||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | my $data = pack('CCC', | ||||||||||||||||||||||||||||
| 161 | 3, | ||||||||||||||||||||||||||||
| 162 | 4 + length( $data{field1} ), | ||||||||||||||||||||||||||||
| 163 | 5 + length( $data{field1} ) | ||||||||||||||||||||||||||||
| 164 | 0 | + length( $data{field2} ) ); | |||||||||||||||||||||||||||
| 165 | 0 | $data .= $data{field1} . "\0"; | |||||||||||||||||||||||||||
| 166 | 0 | $data .= $data{field2} . "\0"; | |||||||||||||||||||||||||||
| 167 | 0 | $data .= $data{note} . "\0"; | |||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 0 | return $data ; | |||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | 0 | ▶ | sub new_Record { | ||||||||||||||||||||||||||
|
Subroutine
new_Record: not called CC=12 SCAR=50.5
Pod
documented
| |||||||||||||||||||||||||||||
| 173 | 0 | my ($self, %args) = @_; | |||||||||||||||||||||||||||
| 174 | 0 | my $record = $self->SUPER::new_Record(); | |||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | # maybe set category | ||||||||||||||||||||||||||||
| 177 | 0 | ▶ | if ( defined( my $category = $args{category} ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 178 | 0 | my @categories = @{ $self->{appinfo}{categories} }; | |||||||||||||||||||||||||||
| 179 | 0 | my $catIndex = undef; | |||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | 0 | ▶ | if ( $category eq '' ) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 182 | # empty string silently mapped to Unfiled | ||||||||||||||||||||||||||||
| 183 | 0 | $catIndex = 0; | |||||||||||||||||||||||||||
| 184 | } elsif ( $category =~ /\D/ ) { | ||||||||||||||||||||||||||||
| 185 | # category by name | ||||||||||||||||||||||||||||
| 186 | 0 | foreach my $i ( 0 .. $#categories ) { | |||||||||||||||||||||||||||
| 187 | 0 | my $cat = $categories[$i]; | |||||||||||||||||||||||||||
| 188 | 0 | ▶ | if ( defined $cat->{name} and $cat->{name} eq $category ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $cat->{"name"} and $cat->{"name"} eq $category
MC/DC0%0/2
defined $cat->{"name"} and $cat->{"name"} eq $category
defined $cat->{"name"}
$cat->{"name"} eq $category
Truth table0%0/3
A and B
A= defined $cat->{"name"}B= $cat->{"name"} eq $category
| |||||||||||||||||||||||||||||
| 189 | 0 | $catIndex = $i; | |||||||||||||||||||||||||||
| 190 | 0 | last; | |||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||
| 193 | ▶ | } elsif ( $category >= 0 and $category <= $#categories ) { | |||||||||||||||||||||||||||
|
Condition0%0/3
$category >= 0 and $category <= $#categories
MC/DC0%0/2
$category >= 0 and $category <= $#categories
$category >= 0
$category <= $#categories
Truth table0%0/3
A and B
A= $category >= 0B= $category <= $#categories
| |||||||||||||||||||||||||||||
| 194 | # category by index | ||||||||||||||||||||||||||||
| 195 | 0 | $catIndex = $category; | |||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | # fall back to "Unfiled" | ||||||||||||||||||||||||||||
| 199 | 0 | ▶ | unless ( defined $catIndex ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 200 | 0 | carp "Bad category `$category'. Using `Unfiled' (0)"; | |||||||||||||||||||||||||||
| 201 | 0 | $catIndex = 0; | |||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | 0 | $record->{category} = $catIndex; | |||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | # set field1, field2 and note | ||||||||||||||||||||||||||||
| 207 | 0 | foreach my $field (qw(field1 field2 note)) { | |||||||||||||||||||||||||||
| 208 | 0 | ▶ | if ( defined $args{$field} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 209 | 0 | $record->{$field} = $args{$field}; | |||||||||||||||||||||||||||
| 210 | } else { | ||||||||||||||||||||||||||||
| 211 | 0 | $record->{$field} = undef; | |||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | 0 | return $record; | |||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | 1 | 1; # module must return success | |||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | __END__ | ||||||||||||||||||||||||||||