blib/script/fitdump.pl untested

filter: Statementstmt 0.0% 0 / 90 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 24 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 161 SCAR 52.0
CC
13
Cov
0%
fetch_from
132.0
dump_it
12.0
usage
2.0
CRAP 182.0
1 #!/usr/bin/env perl
2 use strict;
3 use warnings;
4 use Carp;
5  
6 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 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, 1, 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 my $fh;
54 if ($output_file) {
55 if (-f $output_file) {
56 croak "$output_file already exists (specify --force to overwrite)" unless $force
57 }
58 open( STDOUT, '>', $output_file) or die "cannot open $output_file: $!"
59 }
60  
61 sub dump_it {
62 my ($self, $desc, $v, $o_cbmap) = @_;
63  
64 if (defined $desc->{message_name}) {
65 my $o_cb = $o_cbmap->{$desc->{message_name}};
66 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 print "Local message type: $desc->{local_message_type} ($desc->{message_length} octets";
70 print ", message name: $desc->{message_name}" if defined $desc->{message_name};
71 print ", message number: $desc->{message_number})\n";
72 $self->print_all_fields($desc, $v, indent => ' ')
73 }
74  
75 sub fetch_from {
76 my ($input_file, $output_fh) = (shift, shift);
77  
78 my $obj = new Geo::FIT;
79  
80 $obj->mps_to_kph($mps_to_kph);
81 $obj->semicircles_to_degree($semicircles_to_deg);
82 $obj->use_gmtime($use_gmtime);
83 $obj->maybe_chained($maybe_chained);
84 $obj->file($input_file);
85  
86 my $o_cbmap = $obj->data_message_callback_by_name('');
87  
88 for my $msgname (keys %$o_cbmap) {
89 $obj->data_message_callback_by_name($msgname, \&dump_it, $o_cbmap);
90 }
91  
92 $obj->data_message_callback_by_name('', \&dump_it, $o_cbmap);
93  
94 unless ($obj->open) {
95 print STDERR $obj->error, "\n";
96 return
97 }
98  
99 my $chained;
100  
101 for (;;) {
102 my ($fsize, $proto_ver, $prof_ver, $h_extra, $h_crc_expected, $h_crc_calculated) = $obj->fetch_header;
103  
104 unless (defined $fsize) {
105 $obj->EOF and $chained and last;
106 print STDERR $obj->error, "\n";
107 $obj->close;
108 return
109 }
110  
111 my $protocol_version = $obj->protocol_version( $proto_ver );
112 my ($prof_major, $prof_minor) = $obj->profile_version_major( $prof_ver );
113  
114 print "\n" if $chained;
115 printf "File size: %lu, protocol version: %u, profile_version: %u.%02u\n", $fsize, $protocol_version, $prof_major, $prof_minor;
116  
117 if ($h_extra ne '') {
118 print "Hex dump of extra octets in the file header";
119 my ($i, $n);
120 for ($i = 0, $n = length($h_extra) ; $i < $n ; ++$i) {
121 print "\n " if !($i % 16);
122 print ' ' if !($i % 4);
123 printf " %02x", ord(substr($h_extra, $i, 1))
124 }
125 print "\n"
126 }
127  
128 if (defined $h_crc_calculated) {
129 printf "File header CRC: expected=0x%04X, calculated=0x%04X\n", $h_crc_expected, $h_crc_calculated
130 }
131  
132 1 while ($obj->fetch);
133  
134 print STDERR $obj->error, "\n" if !$obj->end_of_chunk && !$obj->EOF;
135 printf "CRC: expected=0x%04X, calculated=0x%04X\n", $obj->crc_expected, $obj->crc;
136  
137 if ($maybe_chained) {
138 $obj->reset;
139 $chained = 1
140 } else {
141 my $garbage_size = $obj->trailing_garbages;
142 print "Trailing $garbage_size octets garbages skipped\n" if defined $garbage_size and $garbage_size > 0;
143 last
144 }
145 }
146 $obj->close
147 }
148  
149 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