roundgpx: Behave just like gpst, truncate decimals instead of rounding
[gpstools.git] / roundgpx
blob7a8cf61561c11c60c9658eed1ed086a43ae01675
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) = (trunc_number($2, 6), trunc_number($4, 6));
54 print("$1\"$num1\"$3\"$num2\"$5\n");
55 } elsif (/^(.*?<ele>)(-?[\d\.]+)(<\/ele>.*)$/) {
56 my $ele = trunc_number($2, 3);
57 print("$1$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 trunc_number {
89 # Truncate decimals of floating point number {{{
90 my ($num, $scale) = @_;
91 $num = int($num * (10**$scale)) / 10**$scale;
92 return(num_expand($num));
93 # }}}
94 } # trunc_number()
96 sub print_version {
97 # Print program version {{{
98 print("$progname v$VERSION\n");
99 # }}}
100 } # print_version()
102 sub usage {
103 # Send the help message to stdout {{{
104 my $Retval = shift;
106 if ($Opt{'verbose'}) {
107 print("\n");
108 print_version();
110 print(<<END);
112 Usage: $progname [options] [file [files [...]]]
114 Options:
116 -h, --help
117 Show this help.
118 -v, --verbose
119 Increase level of verbosity. Can be repeated.
120 --version
121 Print version information.
122 --debug
123 Print debugging messages.
126 exit($Retval);
127 # }}}
128 } # usage()
130 sub msg {
131 # Print a status message to stderr based on verbosity level {{{
132 my ($verbose_level, $Txt) = @_;
134 if ($Opt{'verbose'} >= $verbose_level) {
135 print(STDERR "$progname: $Txt\n");
137 # }}}
138 } # msg()
140 sub D {
141 # Print a debugging message {{{
142 $Debug || return;
143 my @call_info = caller;
144 chomp(my $Txt = shift);
145 my $File = $call_info[1];
146 $File =~ s#\\#/#g;
147 $File =~ s#^.*/(.*?)$#$1#;
148 print(STDERR "$File:$call_info[2] $$ $Txt\n");
149 return("");
150 # }}}
151 } # D()
153 __END__
155 # Plain Old Documentation (POD) {{{
157 =pod
159 =head1 NAME
163 =head1 SYNOPSIS
165 [options] [file [files [...]]]
167 =head1 DESCRIPTION
171 =head1 OPTIONS
173 =over 4
175 =item B<-h>, B<--help>
177 Print a brief help summary.
179 =item B<-v>, B<--verbose>
181 Increase level of verbosity. Can be repeated.
183 =item B<--version>
185 Print version information.
187 =item B<--debug>
189 Print debugging messages.
191 =back
193 =head1 BUGS
197 =head1 AUTHOR
199 Made by Øyvind A. Holm S<E<lt>sunny@sunbase.orgE<gt>>.
201 =head1 COPYRIGHT
203 Copyleft © Øyvind A. Holm E<lt>sunny@sunbase.orgE<gt>
204 This is free software; see the file F<COPYING> for legalese stuff.
206 =head1 LICENCE
208 This program is free software: you can redistribute it and/or modify it
209 under the terms of the GNU General Public License as published by the
210 Free Software Foundation, either version 3 of the License, or (at your
211 option) any later version.
213 This program is distributed in the hope that it will be useful, but
214 WITHOUT ANY WARRANTY; without even the implied warranty of
215 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
216 See the GNU General Public License for more details.
218 You should have received a copy of the GNU General Public License along
219 with this program.
220 If not, see L<http://www.gnu.org/licenses/>.
222 =head1 SEE ALSO
224 =cut
226 # }}}
228 # vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :