script/fitdump.pl

filter: Statementstmt 78.4% 69 / 88 Branchbran 46.0% 23 / 50 Conditioncond 22.2% 2 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 88.8% 8 / 9 Podpod n/a totaltotal 62.9% 102 / 162 CC 21 SCAR 33.2
CC
21
Cov
64%
fetch_from
45.4
dump_it
5.1
usage
2.0
CRAP 27.8
« script/fit2tcx.pl ↑ Summary script/locations2gpx.pl »
1 #!/usr/bin/env perl
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 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 1 ▶use Geo::FIT;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 1 my ($mps_to_kph, $semicircles_to_deg, $use_gmtime, $maybe_chained, $force, $version, $help) = (1, 1, 0, 0, 0, 0, 0);
30 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" }
Subroutine usage: not called CC=1 SCAR=6.9
31  
32 1 ▶GetOptions( "mps_to_kph=i" => \$mps_to_kph,
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions("mps_to_kph=i", \$mps_to_kph, "semicircles_to_deg=i", \$semicircles_to_deg, "use_gmtime=i", \$use_gmtime, "maybe_chained=i", \$maybe_chained, "force", \$force, "version", \$version, "help", \$help) or die usage() 0 1 1
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 ▶if ($version) {
Branch: 1 branch
BranchTrueFalseTotal
if ($version) 0 1 1
42 0 print $0, " version: ", $VERSION, "\n";
43 exit
44 0 }
45 1 ▶die usage() if $help;
Branch: 1 branch
BranchTrueFalseTotal
if $help 0 1 1
46  
47 1 my ($input_file, $output_file);
48 1 ▶if (@ARGV) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ARGV) 1 0 1
49 1 $input_file = shift @ARGV;
50 1 ▶ @ARGV and $output_file = shift @ARGV
Branch: 1 branch
BranchTrueFalseTotal
@ARGV and $output_file = shift @ARGV 1 0 1
51 }
52  
53 # disable warnings until I resolve them all, will soon delete this
54 1 ▶local $SIG{__WARN__} = sub { };
Subroutine __ANON__: called CC=1 SCAR=0.0
55  
56 1 my $fh;
57 1 ▶if ($output_file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($output_file) 1 0 1
58 1 ▶ if (-f $output_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $output_file) 0 1 1
59 0 ▶ croak "$output_file already exists (specify --force to overwrite)" unless $force
Branch: 1 branch
BranchTrueFalseTotal
unless $force 0 0 0
60 }
61 1 ▶ open( STDOUT, '>', $output_file) or die "cannot open $output_file: $!"
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">", $output_file or die "cannot open ${output_file}: $!" 0 1 1
62 }
63  
64 sub dump_it {
65 243 ▶ my ($self, $desc, $v, $o_cbmap) = @_;
Subroutine dump_it: called CC=5 SCAR=16.4
66  
67 243 ▶ if ($desc->{message_name} ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($desc->{"message_name"} ne "") 238 5 243
68 238 my $o_cb = $o_cbmap->{$desc->{message_name}};
69 238 ▶ ref $o_cb eq 'ARRAY' and ref $o_cb->[0] eq 'CODE' and $o_cb->[0]->($self, $desc, $v, @$o_cb[1 .. $#$o_cb])
Branch: 1 branch
BranchTrueFalseTotal
ref $o_cb eq "ARRAY" and ref $o_cb->[0] eq "CODE" and $o_cb->[0]->($self, $desc, $v, @$o_cb[1 .. $o_cb->$#*]) 0 238 238
Condition33%1/3
ref $o_cb eq "ARRAY" and ref $o_cb->[0] eq "CODE"
!ll&&!rl&&r
23800
MC/DC0%0/2
ref $o_cb eq "ARRAY" and ref $o_cb->[0] eq "CODE"
ref $o_cb eq "ARRAY" ref $o_cb->[0] eq "CODE"
Truth table33%1/3
A and B
A= ref $o_cb eq "ARRAY"B= ref $o_cb->[0] eq "CODE"
ABresult
0X0
100
111
70 }
71  
72 243 print "Local message type: $desc->{local_message_type} ($desc->{message_length} octets";
73 243 ▶ print ", message name: $desc->{message_name}" if $desc->{message_name} ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $desc->{"message_name"} ne "" 238 5 243
74 243 print ", message number: $desc->{message_number})\n";
75 243 $self->print_all_fields($desc, $v, indent => ' ')
76 }
77  
78 sub fetch_from {
79 1 ▶ my ($input_file, $output_fh) = (shift, shift);
Subroutine fetch_from: called CC=17 SCAR=38.2
80  
81 1 my $obj = new Geo::FIT;
82  
83 1 $obj->mps_to_kph($mps_to_kph);
84 1 $obj->semicircles_to_degree($semicircles_to_deg);
85 1 $obj->use_gmtime($use_gmtime);
86 1 $obj->maybe_chained($maybe_chained);
87 1 $obj->file($input_file);
88  
89 1 my $o_cbmap = $obj->data_message_callback_by_name('');
90  
91 1 for my $msgname (keys %$o_cbmap) {
92 2 $obj->data_message_callback_by_name($msgname, \&dump_it, $o_cbmap);
93 }
94  
95 1 $obj->data_message_callback_by_name('', \&dump_it, $o_cbmap);
96  
97 1 ▶ unless ($obj->open) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($obj->open) 0 1 1
98 0 print STDERR $obj->error, "\n";
99 return
100 0 }
101  
102 1 my $chained;
103  
104 1 for (;;) {
105 1 my ($fsize, $proto_ver, $prof_ver, $h_extra, $h_crc_expected, $h_crc_calculated) = $obj->fetch_header;
106  
107 1 ▶ unless (defined $fsize) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $fsize) 0 1 1
108 0 ▶ $obj->EOF and $chained and last;
Branch: 1 branch
BranchTrueFalseTotal
$obj->EOF and $chained and last 0 0 0
Condition0%0/3
$obj->EOF and $chained
!ll&&!rl&&r
000
MC/DC0%0/2
$obj->EOF and $chained
$obj->EOF $chained
Truth table0%0/3
A and B
A= $obj->EOFB= $chained
ABresult
0X0
100
111
109 0 print STDERR $obj->error, "\n";
110 0 $obj->close;
111 return
112 0 }
113  
114 1 my $protocol_version = $obj->protocol_version( $proto_ver );
115 1 my ($prof_major, $prof_minor) = $obj->profile_version_major( $prof_ver );
116  
117 1 ▶ print "\n" if $chained;
Branch: 1 branch
BranchTrueFalseTotal
if $chained 0 1 1
118 1 printf "File size: %lu, protocol version: %u, profile_version: %u.%02u\n", $fsize, $protocol_version, $prof_major, $prof_minor;
119  
120 1 ▶ if ($h_extra ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($h_extra ne "") 0 1 1
121 0 print "Hex dump of extra octets in the file header";
122 0 my ($i, $n);
123 0 ▶ for ($i = 0, $n = length($h_extra) ; $i < $n ; ++$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 0 0 0
124 0 ▶ print "\n " if !($i % 16);
Branch: 1 branch
BranchTrueFalseTotal
unless $i % 16 0 0 0
125 0 ▶ print ' ' if !($i % 4);
Branch: 1 branch
BranchTrueFalseTotal
unless $i % 4 0 0 0
126 0 printf " %02x", ord(substr($h_extra, $i, 1))
127 }
128 0 print "\n"
129 }
130  
131 1 ▶ if (defined $h_crc_calculated) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $h_crc_calculated) 1 0 1
132 1 printf "File header CRC: expected=0x%04X, calculated=0x%04X\n", $h_crc_expected, $h_crc_calculated
133 }
134  
135 1 ▶ 1 while ($obj->fetch);
Branch: 1 branch
BranchTrueFalseTotal
$obj->fetch and do { '???' } 264 1 265
136  
137 1 ▶ print STDERR $obj->error, "\n" if !$obj->end_of_chunk && !$obj->EOF;
Branch: 1 branch
BranchTrueFalseTotal
unless $obj->end_of_chunk or $obj->EOF 0 1 1
Condition33%1/3
$obj->end_of_chunk or $obj->EOF
l!l&&r!l&&!r
100
MC/DC0%0/2
$obj->end_of_chunk or $obj->EOF
$obj->end_of_chunk $obj->EOF
Truth table33%1/3
A or B
A= $obj->end_of_chunkB= $obj->EOF
ABresult
1X1
011
000
138 1 printf "CRC: expected=0x%04X, calculated=0x%04X\n", $obj->crc_expected, $obj->crc;
139  
140 1 ▶ if ($maybe_chained) {
Branch: 1 branch
BranchTrueFalseTotal
if ($maybe_chained) { } 0 1 1
141 0 $obj->reset;
142 0 $chained = 1
143 } else {
144 1 my $garbage_size = $obj->trailing_garbages;
145 1 ▶ print "Trailing $garbage_size octets garbages skipped\n" if $garbage_size > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $garbage_size > 0 0 1 1
146 last
147 1 }
148 }
149 1 $obj->close
150 }
151  
152 1 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  
« script/fit2tcx.pl ↑ Summary script/locations2gpx.pl »