blib/script/fitdump.pl untested

filter: Statementstmt 0.0% 0 / 91 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 21 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 159 CC 13 SCAR 43.2
CC
13
Cov
0%
fetch_from
132.0
dump_it
12.0
usage
2.0
CRAP 75.0
1 #!/usr/bin/env perl
2 use strict;
3 use warnings;
4 use Carp;
5  
6 our $VERSION = '1.08';
7  
8 =encoding utf-8
9  
10 =head1 NAME
11  
12 fitdump.pl - script to print the contents of Garmin FIT files to standard output or a file
13  
14 =head1 SYNOPSIS
15  
16 fitdump.pl --version
17 fitdump.pl --help
18 fitdump.pl $fit_file [ --mps_to_kph=$boole --semicircles_to_deg=$boole --use_gmtime=$boole --maybe_chained=$boole --force ] [ $output_file ]
19  
20 =head1 DESCRIPTION
21  
22 C<fitdump.pl> reads the contents of the Garmin FIT files specified on command line and prints them on standard output or in I<$output_file> if provided.
23  
24 =cut
25  
26 use Geo::FIT;
27 use Getopt::Long;
28  
29 my ($mps_to_kph, $semicircles_to_deg, $use_gmtime, $maybe_chained, $force, $version, $help) = (1, 1, 0, 0, 0, 0, 0);
30 sub usage { "Usage: $0 [ --help --version --mps_to_kph=\$boole --semicircles_to_deg=\$boole --use_gmtime=\$boole --maybe_chained=\$boole --force ] \$input_file [ \$output_file ]\n" }
31  
32 GetOptions( "mps_to_kph=i" => \$mps_to_kph,
33 "semicircles_to_deg=i" => \$semicircles_to_deg,
34 "use_gmtime=i" => \$use_gmtime,
35 "maybe_chained=i" => \$maybe_chained,
36 "force" => \$force,
37 "version" => \$version,
38 "help" => \$help,
39 ) or die usage();
40  
41 if ($version) {
42 print $0, " version: ", $VERSION, "\n";
43 exit
44 }
45 die usage() if $help;
46  
47 my ($input_file, $output_file);
48 if (@ARGV) {
49 $input_file = shift @ARGV;
50 @ARGV and $output_file = shift @ARGV
51 }
52  
53 # disable warnings until I resolve them all, will soon delete this
54 local $SIG{__WARN__} = sub { };
55  
56 my $fh;
57 if ($output_file) {
58 if (-f $output_file) {
59 croak "$output_file already exists (specify --force to overwrite)" unless $force
60 }
61 open( STDOUT, '>', $output_file) or die "cannot open $output_file: $!"
62 }
63  
64 sub dump_it {
65 my ($self, $desc, $v, $o_cbmap) = @_;
66  
67 if ($desc->{message_name} ne '') {
68 my $o_cb = $o_cbmap->{$desc->{message_name}};
69 ref $o_cb eq 'ARRAY' and ref $o_cb->[0] eq 'CODE' and $o_cb->[0]->($self, $desc, $v, @$o_cb[1 .. $#$o_cb])
70 }
71  
72 print "Local message type: $desc->{local_message_type} ($desc->{message_length} octets";
73 print ", message name: $desc->{message_name}" if $desc->{message_name} ne '';
74 print ", message number: $desc->{message_number})\n";
75 $self->print_all_fields($desc, $v, indent => ' ')
76 }
77  
78 sub fetch_from {
79 my ($input_file, $output_fh) = (shift, shift);
80  
81 my $obj = new Geo::FIT;
82  
83 $obj->mps_to_kph($mps_to_kph);
84 $obj->semicircles_to_degree($semicircles_to_deg);
85 $obj->use_gmtime($use_gmtime);
86 $obj->maybe_chained($maybe_chained);
87 $obj->file($input_file);
88  
89 my $o_cbmap = $obj->data_message_callback_by_name('');
90  
91 for my $msgname (keys %$o_cbmap) {
92 $obj->data_message_callback_by_name($msgname, \&dump_it, $o_cbmap);
93 }
94  
95 $obj->data_message_callback_by_name('', \&dump_it, $o_cbmap);
96  
97 unless ($obj->open) {
98 print STDERR $obj->error, "\n";
99 return
100 }
101  
102 my $chained;
103  
104 for (;;) {
105 my ($fsize, $proto_ver, $prof_ver, $h_extra, $h_crc_expected, $h_crc_calculated) = $obj->fetch_header;
106  
107 unless (defined $fsize) {
108 $obj->EOF and $chained and last;
109 print STDERR $obj->error, "\n";
110 $obj->close;
111 return
112 }
113  
114 my $protocol_version = $obj->protocol_version( $proto_ver );
115 my ($prof_major, $prof_minor) = $obj->profile_version_major( $prof_ver );
116  
117 print "\n" if $chained;
118 printf "File size: %lu, protocol version: %u, profile_version: %u.%02u\n", $fsize, $protocol_version, $prof_major, $prof_minor;
119  
120 if ($h_extra ne '') {
121 print "Hex dump of extra octets in the file header";
122 my ($i, $n);
123 for ($i = 0, $n = length($h_extra) ; $i < $n ; ++$i) {
124 print "\n " if !($i % 16);
125 print ' ' if !($i % 4);
126 printf " %02x", ord(substr($h_extra, $i, 1))
127 }
128 print "\n"
129 }
130  
131 if (defined $h_crc_calculated) {
132 printf "File header CRC: expected=0x%04X, calculated=0x%04X\n", $h_crc_expected, $h_crc_calculated
133 }
134  
135 1 while ($obj->fetch);
136  
137 print STDERR $obj->error, "\n" if !$obj->end_of_chunk && !$obj->EOF;
138 printf "CRC: expected=0x%04X, calculated=0x%04X\n", $obj->crc_expected, $obj->crc;
139  
140 if ($maybe_chained) {
141 $obj->reset;
142 $chained = 1
143 } else {
144 my $garbage_size = $obj->trailing_garbages;
145 print "Trailing $garbage_size octets garbages skipped\n" if $garbage_size > 0;
146 last
147 }
148 }
149 $obj->close
150 }
151  
152 fetch_from( $input_file, $fh );
153  
154 =head2 Options
155  
156 =over 4
157  
158 =item C<--mps_to_kph=($boolean)>
159  
160 =item C<--semicircle_to_deg=($boolean)>
161  
162 =item C<--use_gmtime=($boolean)>
163  
164 Options corresponding to object methods in L<Geo::FIT>. The first two default to true, C<-use_gmtime> to false.
165  
166 =item C<--$maybe_chained=($boolean)>
167  
168 Boolean to indicate that the input may be a chained FIT file (defaults to false).
169  
170 =back
171  
172 =head1 DEPENDENCIES
173  
174 L<Geo::FIT>
175  
176 =head1 SEE ALSO
177  
178 L<Geo::FIT>
179  
180 =head1 BUGS AND LIMITATIONS
181  
182 No bugs have been reported.
183  
184 Please report any bugs or feature requests to C<bug-geo-gpx@rt.cpan.org>, or through the web interface at L<http://rt.cpan.org>.
185  
186 =head1 AUTHOR
187  
188 Originally written by Kiyokazu Suto C<< suto@ks-and-ks.ne.jp >>.
189  
190 This version is maintained by Patrick Joly C<< <patjol@cpan.org> >>.
191  
192 Please visit the project page at: L<https://github.com/patjoly/geo-fit>.
193  
194 =head1 VERSION
195  
196 1.08
197  
198 =head1 LICENSE AND COPYRIGHT
199  
200 Copyright 2022, Patrick Joly C<< patjol@cpan.org >>. All rights reserved.
201  
202 Copyright 2016-2022, Kiyokazu Suto C<< suto@ks-and-ks.ne.jp >>. All rights reserved.
203  
204 This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See L<perlartistic>.
205  
206 =head1 DISCLAIMER OF WARRANTY
207  
208 BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.
209  
210 IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENSE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
211  
212 =cut
213  
214 1;
215