Revert "Merge branch 'use-trunc-rounding' into rounding-fixes"
[gpstools.git] / roundgpx
blob14a34f4e7f036a97a327cc194a4a62f7f417a5af
1 #!/usr/bin/perl -w
3 #=======================================================================
4 # roundgpx
5 # File ID: d16a06e8-6138-11df-8aac-90e6ba3022ac
6 # [Description]
8 # Character set: UTF-8
9 # ©opyleft 2010– Øyvind A. Holm <sunny@sunbase.org>
10 # License: GNU General Public License version 3 or later, see end of
11 # file for legal stuff.
12 #=======================================================================
14 use strict;
15 use Getopt::Long;
17 $| = 1;
19 our $Debug = 0;
21 our %Opt = (
23 'debug' => 0,
24 'help' => 0,
25 'verbose' => 0,
26 'version' => 0,
30 our $progname = $0;
31 $progname =~ s/^.*\/(.*?)$/$1/;
32 our $VERSION = "0.00";
34 Getopt::Long::Configure("bundling");
35 GetOptions(
37 "debug" => \$Opt{'debug'},
38 "help|h" => \$Opt{'help'},
39 "verbose|v+" => \$Opt{'verbose'},
40 "version" => \$Opt{'version'},
42 ) || die("$progname: Option error. Use -h for help.\n");
44 $Opt{'debug'} && ($Debug = 1);
45 $Opt{'help'} && usage(0);
46 if ($Opt{'version'}) {
47 print_version();
48 exit(0);
51 while (<>) {
52 if (/(.*?<(?:trkpt|rtept|wpt)\s+.*?)"(.*?)"(.*?)"(.*?)"(.*)$/) {
53 my ($num1, $num2) = (sprintf("%.6f", $2), sprintf("%.6f", $4));
54 print("$1\"" . num_expand($num1) . "\"$3\"" . num_expand($num2) . "\"$5\n");
55 } elsif (/^(.*?<ele>)(-?[\d\.]+)(<\/ele>.*)$/) {
56 my $ele = sprintf("%.3f", $2);
57 print($1 . num_expand($ele) . "$3\n");
58 } else {
59 print;
63 sub num_expand {
64 # Convert scientific notation to decimal notation {{{
65 my $Retval = shift;
66 length($Retval) || return("");
67 if ($Retval =~ /^(.*)e([-+]?)(.*)$/) {
68 my ($num, $sign, $exp) = ($1, $2, $3);
69 my $sig = $sign eq '-' ? "." . ($exp - 1 + length $num) : '';
70 $Retval = sprintf("%${sig}f", $Retval);
72 $Retval =~ s/^\+//;
73 my $minus = ($Retval =~ s/^-//) ? "-" : "";
74 if ($Retval =~ /\.\d/) {
75 $Retval =~ s/0+$//;
76 $Retval =~ s/^0+/0/;
77 $Retval =~ s/^0([1-9]+)\./$1./;
78 $Retval =~ s/\.$//;
79 } else {
80 $Retval =~ s/^0+//;
82 length($Retval) || ($Retval = 0);
83 $Retval = $Retval ? "$minus$Retval" : 0;
84 return($Retval);
85 # }}}
86 } # num_expand()
88 sub print_version {
89 # Print program version {{{
90 print("$progname v$VERSION\n");
91 # }}}
92 } # print_version()
94 sub usage {
95 # Send the help message to stdout {{{
96 my $Retval = shift;
98 if ($Opt{'verbose'}) {
99 print("\n");
100 print_version();
102 print(<<END);
104 Usage: $progname [options] [file [files [...]]]
106 Options:
108 -h, --help
109 Show this help.
110 -v, --verbose
111 Increase level of verbosity. Can be repeated.
112 --version
113 Print version information.
114 --debug
115 Print debugging messages.
118 exit($Retval);
119 # }}}
120 } # usage()
122 sub msg {
123 # Print a status message to stderr based on verbosity level {{{
124 my ($verbose_level, $Txt) = @_;
126 if ($Opt{'verbose'} >= $verbose_level) {
127 print(STDERR "$progname: $Txt\n");
129 # }}}
130 } # msg()
132 sub D {
133 # Print a debugging message {{{
134 $Debug || return;
135 my @call_info = caller;
136 chomp(my $Txt = shift);
137 my $File = $call_info[1];
138 $File =~ s#\\#/#g;
139 $File =~ s#^.*/(.*?)$#$1#;
140 print(STDERR "$File:$call_info[2] $$ $Txt\n");
141 return("");
142 # }}}
143 } # D()
145 __END__
147 # Plain Old Documentation (POD) {{{
149 =pod
151 =head1 NAME
155 =head1 SYNOPSIS
157 [options] [file [files [...]]]
159 =head1 DESCRIPTION
163 =head1 OPTIONS
165 =over 4
167 =item B<-h>, B<--help>
169 Print a brief help summary.
171 =item B<-v>, B<--verbose>
173 Increase level of verbosity. Can be repeated.
175 =item B<--version>
177 Print version information.
179 =item B<--debug>
181 Print debugging messages.
183 =back
185 =head1 BUGS
189 =head1 AUTHOR
191 Made by Øyvind A. Holm S<E<lt>sunny@sunbase.orgE<gt>>.
193 =head1 COPYRIGHT
195 Copyleft © Øyvind A. Holm E<lt>sunny@sunbase.orgE<gt>
196 This is free software; see the file F<COPYING> for legalese stuff.
198 =head1 LICENCE
200 This program is free software: you can redistribute it and/or modify it
201 under the terms of the GNU General Public License as published by the
202 Free Software Foundation, either version 3 of the License, or (at your
203 option) any later version.
205 This program is distributed in the hope that it will be useful, but
206 WITHOUT ANY WARRANTY; without even the implied warranty of
207 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
208 See the GNU General Public License for more details.
210 You should have received a copy of the GNU General Public License along
211 with this program.
212 If not, see L<http://www.gnu.org/licenses/>.
214 =head1 SEE ALSO
216 =cut
218 # }}}
220 # vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :