* /trunk/src/gpstools/tests/run-tests.pl
[gpstools.git] / csv2gpx
blob7cc19d3b714e979bd2b1f6c41aaff9f4156e7412
1 #!/usr/bin/perl -w
3 #=======================================================================
4 # $Id$
5 # [Description]
7 # Character set: UTF-8
8 # ©opyleft 2006– Øyvind A. Holm <sunny@sunbase.org>
9 # License: GNU General Public License version 2 or later, see end of
10 # file for legal stuff.
11 #=======================================================================
13 BEGIN {
14 our @version_array;
17 use strict;
18 use Getopt::Long;
20 $| = 1;
22 our $Debug = 0;
24 our %Opt = (
26 'debug' => 0,
27 'help' => 0,
28 'verbose' => 0,
29 'version' => 0,
33 our $progname = $0;
34 $progname =~ s/^.*\/(.*?)$/$1/;
36 my $rcs_id = '$Id$';
37 my $id_date = $rcs_id;
38 $id_date =~ s/^.*?\d+ (\d\d\d\d-.*?\d\d:\d\d:\d\d\S+).*/$1/;
40 push(@main::version_array, $rcs_id);
42 Getopt::Long::Configure("bundling");
43 GetOptions(
45 "debug" => \$Opt{'debug'},
46 "help|h" => \$Opt{'help'},
47 "verbose|v+" => \$Opt{'verbose'},
48 "version" => \$Opt{'version'},
50 ) || die("$progname: Option error. Use -h for help.\n");
52 $Opt{'debug'} && ($Debug = 1);
53 $Opt{'help'} && usage(0);
54 if ($Opt{'version'}) {
55 print_version();
56 exit(0);
59 my $DIGIT = '0-9\.\-\+';
61 print(gpx_header());
63 while (<>) {
64 my ($Lat, $Lon, $Name, $Phone) = ("", "", "", "");
65 if (/^([$DIGIT]+),([$DIGIT]+),(.*) - (.*?)$/) {
66 ($Lon, $Lat, $Name, $Phone) =
67 (txt_to_xml($1), txt_to_xml($2), txt_to_xml($3), txt_to_xml($4));
68 print(<<END);
69 <wpt lat="$Lat" lon="$Lon">
70 <name>$Name</name>
71 <cmt>$Name - $Phone</cmt>
72 </wpt>
73 END
74 } else {
75 warn(sprintf("%s: Unknown line: \"%s\"\n", $progname, chomp($_)));
79 print("</gpx>\n");
81 sub txt_to_xml {
82 # Convert plain text to XML {{{
83 my $Txt = shift;
84 $Txt =~ s/&/&amp;/gs;
85 $Txt =~ s/</&lt;/gs;
86 $Txt =~ s/>/&gt;/gs;
87 return($Txt);
88 # }}}
91 sub gpx_header {
92 # {{{
93 return(<<END);
94 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
95 <!-- \$Id\$ -->
96 <gpx
97 version="1.1"
98 creator="csv2gpx - http://svn.sunbase.org/repos/utils/trunk/csv2gpx"
99 xmlns="http://www.topografix.com/GPX/1/1"
100 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
101 xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd"
104 # }}}
107 sub print_version {
108 # Print program version {{{
109 for (@main::version_array) {
110 print("$_\n");
112 # }}}
113 } # print_version()
115 sub usage {
116 # Send the help message to stdout {{{
117 my $Retval = shift;
119 if ($Opt{'verbose'}) {
120 print("\n");
121 print_version();
123 print(<<END);
125 Usage: $progname [options] [file [files [...]]]
127 Options:
129 -h, --help
130 Show this help.
131 -v, --verbose
132 Increase level of verbosity. Can be repeated.
133 --version
134 Print version information.
135 --debug
136 Print debugging messages.
139 exit($Retval);
140 # }}}
141 } # usage()
143 sub msg {
144 # Print a status message to stderr based on verbosity level {{{
145 my ($verbose_level, $Txt) = @_;
147 if ($Opt{'verbose'} >= $verbose_level) {
148 print(STDERR "$progname: $Txt\n");
150 # }}}
151 } # msg()
153 sub D {
154 # Print a debugging message {{{
155 $Debug || return;
156 my @call_info = caller;
157 chomp(my $Txt = shift);
158 my $File = $call_info[1];
159 $File =~ s#\\#/#g;
160 $File =~ s#^.*/(.*?)$#$1#;
161 print(STDERR "$File:$call_info[2] $$ $Txt\n");
162 return("");
163 # }}}
164 } # D()
166 __END__
168 # Plain Old Documentation (POD) {{{
170 =pod
172 =head1 NAME
176 =head1 REVISION
178 $Id$
180 =head1 SYNOPSIS
182 [options] [file [files [...]]]
184 =head1 DESCRIPTION
188 =head1 OPTIONS
190 =over 4
192 =item B<-h>, B<--help>
194 Print a brief help summary.
196 =item B<-v>, B<--verbose>
198 Increase level of verbosity. Can be repeated.
200 =item B<--version>
202 Print version information.
204 =item B<--debug>
206 Print debugging messages.
208 =back
210 =head1 BUGS
214 =head1 AUTHOR
216 Made by Øyvind A. Holm S<E<lt>sunny@sunbase.orgE<gt>>.
218 =head1 COPYRIGHT
220 Copyleft © Øyvind A. Holm E<lt>sunny@sunbase.orgE<gt>
221 This is free software; see the file F<COPYING> for legalese stuff.
223 =head1 LICENCE
225 This program is free software; you can redistribute it and/or modify it
226 under the terms of the GNU General Public License as published by the
227 Free Software Foundation; either version 2 of the License, or (at your
228 option) any later version.
230 This program is distributed in the hope that it will be useful, but
231 WITHOUT ANY WARRANTY; without even the implied warranty of
232 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
233 See the GNU General Public License for more details.
235 You should have received a copy of the GNU General Public License along
236 with this program; if not, write to the Free Software Foundation, Inc.,
237 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
239 =head1 SEE ALSO
241 =cut
243 # }}}
245 # vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :
246 # End of file $Id$