Revert "Merge branch 'use-trunc-rounding' into rounding-fixes"
[gpstools.git] / csv2gpx
blob986d619bb589e0e356950c598c3875f8783b343a
1 #!/usr/bin/perl
3 #=======================================================================
4 # csv2gpx
5 # File ID: 9867d624-f742-11dd-910a-000475e441b9
6 # [Description]
8 # Character set: UTF-8
9 # ©opyleft 2006– Ø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 warnings;
16 use Getopt::Long;
18 $| = 1;
20 our $Debug = 0;
22 our %Opt = (
24 'debug' => 0,
25 'help' => 0,
26 'verbose' => 0,
27 'version' => 0,
31 our $progname = $0;
32 $progname =~ s/^.*\/(.*?)$/$1/;
33 our $VERSION = "0.00";
35 Getopt::Long::Configure("bundling");
36 GetOptions(
38 "debug" => \$Opt{'debug'},
39 "help|h" => \$Opt{'help'},
40 "verbose|v+" => \$Opt{'verbose'},
41 "version" => \$Opt{'version'},
43 ) || die("$progname: Option error. Use -h for help.\n");
45 $Opt{'debug'} && ($Debug = 1);
46 $Opt{'help'} && usage(0);
47 if ($Opt{'version'}) {
48 print_version();
49 exit(0);
52 my $DIGIT = '0-9\.\-\+';
54 print(gpx_header());
56 while (<>) {
57 my ($Lat, $Lon, $Name, $Phone) = ("", "", "", "");
58 if (/^([$DIGIT]+),([$DIGIT]+),(.*) - (.*?)$/) {
59 ($Lon, $Lat, $Name, $Phone) =
60 (txt_to_xml($1), txt_to_xml($2), txt_to_xml($3), txt_to_xml($4));
61 print(<<END);
62 <wpt lat="$Lat" lon="$Lon">
63 <name>$Name</name>
64 <cmt>$Name - $Phone</cmt>
65 </wpt>
66 END
67 } else {
68 warn(sprintf("%s: Unknown line: \"%s\"\n", $progname, chomp($_)));
72 print("</gpx>\n");
74 sub txt_to_xml {
75 # Convert plain text to XML {{{
76 my $Txt = shift;
77 $Txt =~ s/&/&amp;/gs;
78 $Txt =~ s/</&lt;/gs;
79 $Txt =~ s/>/&gt;/gs;
80 return($Txt);
81 # }}}
84 sub gpx_header {
85 # {{{
86 return(<<END);
87 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
88 <gpx
89 version="1.1"
90 creator="csv2gpx - http://sunny256.github.com/gpstools/"
91 xmlns="http://www.topografix.com/GPX/1/1"
92 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
93 xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd"
95 END
96 # }}}
99 sub print_version {
100 # Print program version {{{
101 print("$progname v$VERSION\n");
102 # }}}
103 } # print_version()
105 sub usage {
106 # Send the help message to stdout {{{
107 my $Retval = shift;
109 if ($Opt{'verbose'}) {
110 print("\n");
111 print_version();
113 print(<<END);
115 Usage: $progname [options] [file [files [...]]]
117 Options:
119 -h, --help
120 Show this help.
121 -v, --verbose
122 Increase level of verbosity. Can be repeated.
123 --version
124 Print version information.
125 --debug
126 Print debugging messages.
129 exit($Retval);
130 # }}}
131 } # usage()
133 sub msg {
134 # Print a status message to stderr based on verbosity level {{{
135 my ($verbose_level, $Txt) = @_;
137 if ($Opt{'verbose'} >= $verbose_level) {
138 print(STDERR "$progname: $Txt\n");
140 # }}}
141 } # msg()
143 sub D {
144 # Print a debugging message {{{
145 $Debug || return;
146 my @call_info = caller;
147 chomp(my $Txt = shift);
148 my $File = $call_info[1];
149 $File =~ s#\\#/#g;
150 $File =~ s#^.*/(.*?)$#$1#;
151 print(STDERR "$File:$call_info[2] $$ $Txt\n");
152 return("");
153 # }}}
154 } # D()
156 __END__
158 # Plain Old Documentation (POD) {{{
160 =pod
162 =head1 NAME
166 =head1 SYNOPSIS
168 [options] [file [files [...]]]
170 =head1 DESCRIPTION
174 =head1 OPTIONS
176 =over 4
178 =item B<-h>, B<--help>
180 Print a brief help summary.
182 =item B<-v>, B<--verbose>
184 Increase level of verbosity. Can be repeated.
186 =item B<--version>
188 Print version information.
190 =item B<--debug>
192 Print debugging messages.
194 =back
196 =head1 BUGS
200 =head1 AUTHOR
202 Made by Øyvind A. Holm S<E<lt>sunny@sunbase.orgE<gt>>.
204 =head1 COPYRIGHT
206 Copyleft © Øyvind A. Holm E<lt>sunny@sunbase.orgE<gt>
207 This is free software; see the file F<COPYING> for legalese stuff.
209 =head1 LICENCE
211 This program is free software: you can redistribute it and/or modify it
212 under the terms of the GNU General Public License as published by the
213 Free Software Foundation, either version 3 of the License, or (at your
214 option) any later version.
216 This program is distributed in the hope that it will be useful, but
217 WITHOUT ANY WARRANTY; without even the implied warranty of
218 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
219 See the GNU General Public License for more details.
221 You should have received a copy of the GNU General Public License along
222 with this program.
223 If not, see L<http://www.gnu.org/licenses/>.
225 =head1 SEE ALSO
227 =cut
229 # }}}
231 # vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :