File Coverage

blib/lib/Palm/Magellan/NavCompanion.pm
Criterion Covered Total %
statement 64 90 71.1
branch 1 2 50.0
condition n/a
subroutine 16 21 76.1
pod 7 7 100.0
total 88 120 73.3


line stmt bran cond sub pod time code
1             # $Id: NavCompanion.pm 2490 2008-01-16 10:42:32Z comdog $
2             package Palm::Magellan::NavCompanion::Record;
3 4     4   3995 use strict;
  4         10  
  4         193  
4              
5 4     4   26 use warnings;
  4         9  
  4         147  
6 4     4   29 no warnings;
  4         9  
  4         212  
7              
8 4     4   22 use vars qw( $AUTOLOAD );
  4         8  
  4         336  
9              
10             =head1 NAME
11              
12             Palm::Magellan::NavCompanion - access the Magellan GPS Companion waypoints file
13              
14             =head1 SYNOPSIS
15              
16             use Palm::Magellan::NavCompanion;
17              
18             my $pdb = Palm::Magellan::NavCompanion->new;
19             $pdb->Load( $file );
20              
21             my $waypoints = $pdb->{records};
22              
23             $, = ", ";
24             foreach my $wp ( @$waypoints )
25             {
26             print $wp->name, $wp->latitude, $wp->longitude;
27             print "\n";
28             }
29              
30             =head1 DESCRIPTION
31              
32             This module gives you access to the waypoints in the Magellan's GPS
33             Companion "Companion Waypoints.pdb" file. You have to be able to load
34             that file, which probably means that you have it on your computer
35             rather than your Palm. On my machine, this file shows up in the Palm
36             directory as C< Palm/Users/..user../Backups/Companion Waypoints.pdb >.
37              
38             Behind-the-scenes, Palm::PDB does all the work, so this module has
39             part of its interface and data structure. For instance, the Load()
40             method accesses and parses the file and returns the general data
41             structure that Palm::PDB creates. The interesting bits (the
42             waypoints) is an anonymous array which is the value for the key
43             C.
44              
45             # an anonymous array
46             my $waypoints = $pdb->{records};
47              
48             Each element in C< @{ $waypoints } > is an object of class
49             C, which is really just a class
50             of accessor methods (for now).
51              
52             =head2 Methods
53              
54             =over 4
55              
56             =item new
57              
58             Create a new object. This method takes no arguments
59              
60             =item Load( FILENAME )
61              
62             Load a file in Palm Database format
63              
64             =item name
65              
66             The description. The format allows up to 20
67             characters.
68              
69             =item description
70              
71             The description. The format allows up to 32
72             characters.
73              
74             =item elevation
75              
76             The altitude, in meters
77              
78             =item latitude
79              
80             The latitude, as a decimal number. Positive numbers are north latitude
81             and negative numbers are south latitude.
82              
83             =item longitude
84              
85             The longitude, as a decimal number. Positive numbers are east longitude
86             and negative numbers are west longitude.
87              
88             =item creation_date
89              
90             The creation date of the waypoint, in the format MM/DD/YYYY.
91             This comprises the individual elements found in the database
92             format, which are also available individually.
93              
94             =item creation_time
95              
96             The creation time of the waypoint, in the format HH:MM.ss
97             This comprises the individual elements found in the database
98             format, which are also available individually.
99              
100             =item creation_sec
101              
102             The second the waypoint was created.
103              
104             =item creation_min
105              
106             The minute the waypoint was created.
107              
108             =item creation_hour
109              
110             The hour the waypoint was created.
111              
112             =item creation_day
113              
114             The day the waypoint was created.
115              
116             =item creation_mon
117              
118             The month the waypoint was created.
119              
120             =item creation_year
121              
122             The year the waypoint was created. It includes the century.
123              
124             =back
125              
126             =head1 TO DO
127              
128             * write records too
129              
130             =head1 SEE ALSO
131              
132             L
133              
134             =head1 SOURCE AVAILABILITY
135              
136             This source is part of a SourceForge project which always has the
137             latest sources in SVN, as well as all of the previous releases.
138              
139             http://sourceforge.net/projects/brian-d-foy/
140              
141             If, for some reason, I disappear from the world, one of the other
142             members of the project can shepherd this module appropriately.
143              
144             =head1 AUTHOR
145              
146             brian d foy C<< >>
147              
148             =head1 COPYRIGHT AND LICENSE
149              
150             Copyright (c) 2004-2008 brian d foy. All rights reserved.
151              
152             This program is free software; you can redistribute it and/or modify
153             it under the same terms as Perl itself.
154              
155             =cut
156              
157 4     4   25 use Carp qw(carp);
  4         8  
  4         312  
158 4     4   9141 use UNIVERSAL;
  4         71  
  4         24  
159              
160             my %Allowed = map { $_, 1 } qw(
161             name
162             description
163             latitude
164             longitude
165             elevation
166             creation_date
167             creation_time
168             creation_sec
169             creation_min
170             creation_hour
171             creation_day
172             creation_mon
173             creation_year
174             );
175              
176             sub AUTOLOAD {
177 2     2   1063 my $self = shift;
178 2         5 my $method = $AUTOLOAD;
179 2         12 $method =~ s/.*:://;
180              
181 2 50       9 if( exists $Allowed{ $method } )
182             {
183 2         14 $self->{$method}
184             }
185             else
186             {
187 0         0 carp( "Unknown method call [$method]" )
188             }
189             }
190              
191 66     66   2437 sub DESTROY { 1 };
192              
193             # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
194             # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
195             # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
196             package Palm::Magellan::NavCompanion;
197              
198 4     4   952 use strict;
  4         25  
  4         164  
199              
200 4     4   22 use base qw(Palm::StdAppInfo Palm::Raw Exporter);
  4         8  
  4         5616  
201              
202 4     4   57574 use vars qw($VERSION);
  4         13  
  4         152  
203              
204 4     4   20 use Palm::Raw;
  4         8  
  4         21  
205 4     4   149 use Palm::StdAppInfo();
  4         7  
  4         5362  
206              
207             $VERSION = '0.53';
208              
209             our $Creator = "MGtz";
210             our $Type = "Twpt";
211              
212             sub import
213             {
214 4     4   54 &Palm::PDB::RegisterPDBHandlers( __PACKAGE__,
215             [ $Creator, $Type ] );
216             }
217              
218             sub new
219             {
220 0     0 1 0 my $class = shift;
221 0         0 my $self = $class->SUPER::new(@_);
222             # Create a generic PDB. No need to rebless it,
223             # though.
224              
225 0         0 $self->{name} = "MagNavDB"; # Default
226 0         0 $self->{creator} = $Creator;
227 0         0 $self->{type} = $Type;
228              
229 0         0 $self->{attributes}{resource} = 0;
230             # The PDB is not a resource database by
231             # default, but it's worth emphasizing,
232             # since MemoDB is explicitly not a PRC.
233              
234             # Initialize the AppInfo block
235 0         0 $self->{appinfo} = {
236             sortOrder => undef, # XXX - ?
237             };
238              
239             # Add the standard AppInfo block stuff
240 0         0 &Palm::StdAppInfo::seed_StdAppInfo($self->{appinfo});
241              
242             # Give the PDB a blank sort block
243 0         0 $self->{sort} = undef;
244              
245             # Give the PDB an empty list of records
246 0         0 $self->{records} = [];
247              
248 0         0 return $self;
249             }
250              
251             sub new_Record
252             {
253 0     0 1 0 my $class = shift;
254 0         0 my $hash = $class->SUPER::new_Record(@_);
255              
256 0         0 $hash->{data} = "";
257              
258 0         0 return $hash;
259             }
260              
261             # ParseAppInfoBlock
262             # Parse the AppInfo block for Memo databases.
263             sub ParseAppInfoBlock
264             {
265 3     3 1 5279 my $self = shift;
266 3         7 my $data = shift;
267 3         7 my $sortOrder;
268             my $i;
269 3         6 my $appinfo = {};
270 3         7 my $std_len;
271              
272             # Get the standard parts of the AppInfo block
273 3         13 $std_len = &Palm::StdAppInfo::parse_StdAppInfo($appinfo, $data);
274              
275 3         651 $data = $appinfo->{other}; # Look at the non-category part
276              
277 3         16 return $appinfo;
278             }
279              
280             sub PackAppInfoBlock
281             {
282 0     0 1 0 my $self = shift;
283 0         0 my $retval;
284             my $i;
285              
286             # Pack the non-category part of the AppInfo block
287 0         0 $self->{appinfo}{other} =
288             pack("x4 C x1", $self->{appinfo}{sortOrder});
289              
290             # Pack the AppInfo block
291 0         0 $retval = &Palm::StdAppInfo::pack_StdAppInfo($self->{appinfo});
292              
293 0         0 return $retval;
294             }
295              
296             sub PackSortBlock
297             {
298 0     0 1 0 return undef;
299             }
300              
301             sub ParseRecord
302             {
303 66     66 1 1999 my $self = shift;
304 66         205 my %record = @_;
305              
306 66         99 my @created = ();
307 66         75 my @unk_time = ();
308 66         74 my( $latitude, $longitude, $elevation, $plot, $name );
309              
310 66         594 ( @created[0..5], undef, @unk_time[0..5], undef,
311             @record{ qw(latitude longitude elevation plot ) },
312             undef,
313             ) = unpack 's6 s s6 s l l l C C', $record{data};
314              
315 66         364 @record{ qw(name description) } = split /\000/, substr( $record{data}, 42 );
316              
317 66         253 @record{ qw(creation_sec creation_min creation_hour) }
318             = @created[2,1,0];
319 66         201 @record{ qw(creation_date creation_mon creation_year) }
320             = @created[3,4,5];
321              
322 66         342 $record{'creation_time'} = sprintf "%d:%02d.%02d", @created[2,1,0];
323 66         251 $record{'creation_date'} = sprintf "%d/%d/%04d", @created[3,4,5];
324              
325 66         118 @record{ qw(latitude longitude) } = map { $_ / 1e5 }
  132         275  
326             @record{ qw(latitude longitude) };
327              
328 66         116 foreach my $key ( qw(data offset id category) )
329             {
330 264         423 delete $record{ $key };
331             }
332              
333             #require Data::Dumper;
334             #print STDERR Data::Dumper::Dumper( \%record );
335              
336 66         340 return bless \%record, 'Palm::Magellan::NavCompanion::Record';
337             }
338              
339             sub PackRecord
340             {
341 0     0 1   my $self = shift;
342 0           my $record = shift;
343              
344 0           die "Writing records not implemented";
345              
346 0           return $record->{data} . "\0"; # Add the trailing NUL
347             }
348              
349              
350             1;
351              
352             __END__