File Coverage

script/fitdump.pl
Criterion Covered Total %
statement 68 87 78.1
branch 20 46 43.4
condition 3 12 25.0
subroutine 7 8 87.5
pod n/a
total 98 153 64.0


line stmt bran cond sub pod time code
1             #!/usr/bin/env perl
2 1     1   7222 use strict;
  1         2  
  1         52  
3 1     1   7 use warnings;
  1         2  
  1         88  
4 1     1   9 use Carp;
  1         2  
  1         171  
5              
6 1         161819 our $VERSION = '1.13';
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 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 1     1   2498 use Geo::FIT;
  1         11  
  1         859  
27 1     1   1022 use Getopt::Long;
  1         15111  
  1         6  
28              
29 1         7 my ($mps_to_kph, $semicircles_to_deg, $use_gmtime, $maybe_chained, $force, $version, $help) = (1, 1, 1, 0, 0, 0, 0);
30 0     0   0 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 1 50       12 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 1 50       1154 if ($version) {
42 0         0 print $0, " version: ", $VERSION, "\n";
43             exit
44 0         0 }
45 1 50       2 die usage() if $help;
46              
47 1         3 my ($input_file, $output_file);
48 1 50       3 if (@ARGV) {
49 1         2 $input_file = shift @ARGV;
50 1 50       4 @ARGV and $output_file = shift @ARGV
51             }
52              
53 1         3 my $fh;
54 1 50       4 if ($output_file) {
55 1 50       82 if (-f $output_file) {
56 0 0       0 croak "$output_file already exists (specify --force to overwrite)" unless $force
57             }
58 1 50       192 open( STDOUT, '>', $output_file) or die "cannot open $output_file: $!"
59             }
60              
61             sub dump_it {
62 243     243   663 my ($self, $desc, $v, $o_cbmap) = @_;
63              
64 243 100       834 if (defined $desc->{message_name}) {
65 238         575 my $o_cb = $o_cbmap->{$desc->{message_name}};
66 238 50 33     676 ref $o_cb eq 'ARRAY' and ref $o_cb->[0] eq 'CODE' and $o_cb->[0]->($self, $desc, $v, @$o_cb[1 .. $#$o_cb])
67             }
68              
69 243         951 print "Local message type: $desc->{local_message_type} ($desc->{message_length} octets";
70 243 100       734 print ", message name: $desc->{message_name}" if defined $desc->{message_name};
71 243         439 print ", message number: $desc->{message_number})\n";
72 243         831 $self->print_all_fields($desc, $v, indent => ' ')
73             }
74              
75             sub fetch_from {
76 1     1   4 my ($input_file, $output_fh) = (shift, shift);
77              
78 1         12 my $obj = new Geo::FIT;
79              
80 1         4 $obj->mps_to_kph($mps_to_kph);
81 1         2 $obj->semicircles_to_degree($semicircles_to_deg);
82 1         2 $obj->use_gmtime($use_gmtime);
83 1         5 $obj->maybe_chained($maybe_chained);
84 1         4 $obj->file($input_file);
85              
86 1         3 my $o_cbmap = $obj->data_message_callback_by_name('');
87              
88 1         3 for my $msgname (keys %$o_cbmap) {
89 2         5 $obj->data_message_callback_by_name($msgname, \&dump_it, $o_cbmap);
90             }
91              
92 1         3 $obj->data_message_callback_by_name('', \&dump_it, $o_cbmap);
93              
94 1 50       4 unless ($obj->open) {
95 0         0 print STDERR $obj->error, "\n";
96             return
97 0         0 }
98              
99 1         2 my $chained;
100              
101 1         2 for (;;) {
102 1         5 my ($fsize, $proto_ver, $prof_ver, $h_extra, $h_crc_expected, $h_crc_calculated) = $obj->fetch_header;
103              
104 1 50       5 unless (defined $fsize) {
105 0 0 0     0 $obj->EOF and $chained and last;
106 0         0 print STDERR $obj->error, "\n";
107 0         0 $obj->close;
108             return
109 0         0 }
110              
111 1         26 my $protocol_version = $obj->protocol_version( $proto_ver );
112 1         10 my ($prof_major, $prof_minor) = $obj->profile_version_major( $prof_ver );
113              
114 1 50       3 print "\n" if $chained;
115 1         16 printf "File size: %lu, protocol version: %u, profile_version: %u.%02u\n", $fsize, $protocol_version, $prof_major, $prof_minor;
116              
117 1 50       4 if ($h_extra ne '') {
118 0         0 print "Hex dump of extra octets in the file header";
119 0         0 my ($i, $n);
120 0         0 for ($i = 0, $n = length($h_extra) ; $i < $n ; ++$i) {
121 0 0       0 print "\n " if !($i % 16);
122 0 0       0 print ' ' if !($i % 4);
123 0         0 printf " %02x", ord(substr($h_extra, $i, 1))
124             }
125 0         0 print "\n"
126             }
127              
128 1 50       16 if (defined $h_crc_calculated) {
129 1         4 printf "File header CRC: expected=0x%04X, calculated=0x%04X\n", $h_crc_expected, $h_crc_calculated
130             }
131              
132 1         17 1 while ($obj->fetch);
133              
134 1 0 33     4 print STDERR $obj->error, "\n" if !$obj->end_of_chunk && !$obj->EOF;
135 1         4 printf "CRC: expected=0x%04X, calculated=0x%04X\n", $obj->crc_expected, $obj->crc;
136              
137 1 50       4 if ($maybe_chained) {
138 0         0 $obj->reset;
139 0         0 $chained = 1
140             } else {
141 1         5 my $garbage_size = $obj->trailing_garbages;
142 1 50 33     5 print "Trailing $garbage_size octets garbages skipped\n" if defined $garbage_size and $garbage_size > 0;
143             last
144 1         3 }
145             }
146 1         7 $obj->close
147             }
148              
149 1         7 fetch_from( $input_file, $fh );
150              
151             =head2 Options
152              
153             =over 4
154              
155             =item C<--mps_to_kph=($boolean)>
156              
157             =item C<--semicircle_to_deg=($boolean)>
158              
159             =item C<--use_gmtime=($boolean)>
160              
161             Options corresponding to object methods in L. All of the above default to true.
162              
163             =item C<--$maybe_chained=($boolean)>
164              
165             Boolean to indicate that the input may be a chained FIT file (defaults to false).
166              
167             =back
168              
169             =head1 DEPENDENCIES
170              
171             L
172              
173             =head1 SEE ALSO
174              
175             L
176              
177             =head1 BUGS AND LIMITATIONS
178              
179             No bugs have been reported.
180              
181             Please report any bugs or feature requests to C, or through the web interface at L.
182              
183             =head1 AUTHOR
184              
185             Originally written by Kiyokazu Suto C<< suto@ks-and-ks.ne.jp >>.
186              
187             This version is maintained by Patrick Joly C<< >>.
188              
189             Please visit the project page at: L.
190              
191             =head1 VERSION
192              
193             1.13
194              
195             =head1 LICENSE AND COPYRIGHT
196              
197             Copyright 2022, Patrick Joly C<< patjol@cpan.org >>. All rights reserved.
198              
199             Copyright 2016-2022, Kiyokazu Suto C<< suto@ks-and-ks.ne.jp >>. All rights reserved.
200              
201             This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See L.
202              
203             =head1 DISCLAIMER OF WARRANTY
204              
205             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.
206              
207             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.
208              
209             =cut
210              
211             1;
212