script/fitdump.pl

filter: Statementstmt 78.1% 68 / 87 Branchbran 46.0% 23 / 50 Conditioncond 25.0% 3 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 87.5% 7 / 8 Podpod n/a totaltotal 61.2% 101 / 165 SCAR 38.4
CC
22
Cov
63%
fetch_from
51.5
dump_it
5.1
usage
2.0
CRAP 46.3
« blib/script/fitdump.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.09';
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, 1, 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 1 my $fh;
54 1 ▶if ($output_file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($output_file) 1 0 1
55 1 ▶ if (-f $output_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $output_file) 0 1 1
56 0 ▶ croak "$output_file already exists (specify --force to overwrite)" unless $force
Branch: 1 branch
BranchTrueFalseTotal
unless $force 0 0 0
57 }
58 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
59 }
60  
61 sub dump_it {
62 243 ▶ my ($self, $desc, $v, $o_cbmap) = @_;
Subroutine dump_it: called CC=5 SCAR=16.4
63  
64 243 ▶ if (defined $desc->{message_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $desc->{"message_name"}) 238 5 243
65 238 my $o_cb = $o_cbmap->{$desc->{message_name}};
66 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
67 }
68  
69 243 print "Local message type: $desc->{local_message_type} ($desc->{message_length} octets";
70 243 ▶ print ", message name: $desc->{message_name}" if defined $desc->{message_name};
Branch: 1 branch
BranchTrueFalseTotal
if defined $desc->{"message_name"} 238 5 243
71 243 print ", message number: $desc->{message_number})\n";
72 243 $self->print_all_fields($desc, $v, indent => ' ')
73 }
74  
75 sub fetch_from {
76 1 ▶ my ($input_file, $output_fh) = (shift, shift);
Subroutine fetch_from: called CC=18 SCAR=39.4
77  
78 1 my $obj = new Geo::FIT;
79  
80 1 $obj->mps_to_kph($mps_to_kph);
81 1 $obj->semicircles_to_degree($semicircles_to_deg);
82 1 $obj->use_gmtime($use_gmtime);
83 1 $obj->maybe_chained($maybe_chained);
84 1 $obj->file($input_file);
85  
86 1 my $o_cbmap = $obj->data_message_callback_by_name('');
87  
88 1 for my $msgname (keys %$o_cbmap) {
89 2 $obj->data_message_callback_by_name($msgname, \&dump_it, $o_cbmap);
90 }
91  
92 1 $obj->data_message_callback_by_name('', \&dump_it, $o_cbmap);
93  
94 1 ▶ unless ($obj->open) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($obj->open) 0 1 1
95 0 print STDERR $obj->error, "\n";
96 return
97 0 }
98  
99 1 my $chained;
100  
101 1 for (;;) {
102 1 my ($fsize, $proto_ver, $prof_ver, $h_extra, $h_crc_expected, $h_crc_calculated) = $obj->fetch_header;
103  
104 1 ▶ unless (defined $fsize) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $fsize) 0 1 1
105 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
106 0 print STDERR $obj->error, "\n";
107 0 $obj->close;
108 return
109 0 }
110  
111 1 my $protocol_version = $obj->protocol_version( $proto_ver );
112 1 my ($prof_major, $prof_minor) = $obj->profile_version_major( $prof_ver );
113  
114 1 ▶ print "\n" if $chained;
Branch: 1 branch
BranchTrueFalseTotal
if $chained 0 1 1
115 1 printf "File size: %lu, protocol version: %u, profile_version: %u.%02u\n", $fsize, $protocol_version, $prof_major, $prof_minor;
116  
117 1 ▶ if ($h_extra ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($h_extra ne "") 0 1 1
118 0 print "Hex dump of extra octets in the file header";
119 0 my ($i, $n);
120 0 ▶ for ($i = 0, $n = length($h_extra) ; $i < $n ; ++$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 0 0 0
121 0 ▶ print "\n " if !($i % 16);
Branch: 1 branch
BranchTrueFalseTotal
unless $i % 16 0 0 0
122 0 ▶ print ' ' if !($i % 4);
Branch: 1 branch
BranchTrueFalseTotal
unless $i % 4 0 0 0
123 0 printf " %02x", ord(substr($h_extra, $i, 1))
124 }
125 0 print "\n"
126 }
127  
128 1 ▶ if (defined $h_crc_calculated) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $h_crc_calculated) 1 0 1
129 1 printf "File header CRC: expected=0x%04X, calculated=0x%04X\n", $h_crc_expected, $h_crc_calculated
130 }
131  
132 1 ▶ 1 while ($obj->fetch);
Branch: 1 branch
BranchTrueFalseTotal
$obj->fetch and do { '???' } 264 1 265
133  
134 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
135 1 printf "CRC: expected=0x%04X, calculated=0x%04X\n", $obj->crc_expected, $obj->crc;
136  
137 1 ▶ if ($maybe_chained) {
Branch: 1 branch
BranchTrueFalseTotal
if ($maybe_chained) { } 0 1 1
138 0 $obj->reset;
139 0 $chained = 1
140 } else {
141 1 my $garbage_size = $obj->trailing_garbages;
142 1 ▶ print "Trailing $garbage_size octets garbages skipped\n" if defined $garbage_size and $garbage_size > 0;
Branch: 1 branch
BranchTrueFalseTotal
if defined $garbage_size and $garbage_size > 0 0 1 1
Condition33%1/3
defined $garbage_size and $garbage_size > 0
!ll&&!rl&&r
100
MC/DC0%0/2
defined $garbage_size and $garbage_size > 0
defined $garbage_size $garbage_size > 0
Truth table33%1/3
A and B
A= defined $garbage_sizeB= $garbage_size > 0
ABresult
0X0
100
111
143 last
144 1 }
145 }
146 1 $obj->close
147 }
148  
149 1 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<Geo::FIT>. 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<Geo::FIT>
172  
173 =head1 SEE ALSO
174  
175 L<Geo::FIT>
176  
177 =head1 BUGS AND LIMITATIONS
178  
179 No bugs have been reported.
180  
181 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>.
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<< <patjol@cpan.org> >>.
188  
189 Please visit the project page at: L<https://github.com/patjoly/geo-fit>.
190  
191 =head1 VERSION
192  
193 1.09
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<perlartistic>.
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  
« blib/script/fitdump.pl ↑ Summary script/locations2gpx.pl »