Merge branch 'use-trunc-rounding' into rounding-fixes
[gpstools.git] / gpsfold
blob03cf1373fe32e216b893db212e4720f331d6236a
1 #!/usr/bin/perl
3 # gpsfold
4 # Inserts fold marks in GPS data files before and after stdin.
5 # Select text in visual line mode and filter the block through this script.
7 use strict;
8 use warnings;
10 use Getopt::Std;
11 our ($opt_h, $opt_m) = (0, 0);
12 getopts('hm');
14 $| = 1;
16 if ($opt_h) {
17 print(<<END);
19 Syntax: gpsfold [options]
21 Options:
23 -h Help me, please
24 -m Add a "# move" after the fold.
26 END
27 exit(0);
30 my $Title = "";
31 my @Data = ();
33 defined($ARGV[0]) && ($Title = join(" ", @ARGV));
34 my $colon_str = (length($Title) ? ": " : "");
36 my $Line = "";
37 my ($start_date, $end_date) = ("", "");
39 while ($Line = <STDIN>) {
40 push(@Data, $Line);
41 unless (length($start_date)) {
42 if ($Line =~ /^(?:# )?(\d\d\d\d)-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)(Z?)\t/) {
43 $start_date = "$1$2${3}T$4$5$6$7";
44 } elsif ($Line =~ /^(?:# )?(\d\d\d\d\d\d\d\dT\d\d\d\d\d\dZ?)\t/) {
45 $start_date = $1;
46 } elsif ($Line =~ /^(?:# )?1 (\S+) (\S+) (\S+) (\S+) (\d\d)\/(\d\d)\/(\d\d\d\d) (\d\d):(\d\d):(\d\d)/) {
47 $start_date = "$7$5${6}T$8$9$10";
50 if ($Line =~ /^(?:# )?(\d\d\d\d)-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)(Z?)\t/) {
51 $end_date = "$1$2${3}T$4$5$6$7";
52 } elsif ($Line =~ /^(?:# )?(\d\d\d\d\d\d\d\dT\d\d\d\d\d\dZ?)\t/) {
53 $end_date = $1;
54 } elsif ($Line =~ /^(?:# )?1 (\S+) (\S+) (\S+) (\S+) (\d\d)\/(\d\d)\/(\d\d\d\d) (\d\d):(\d\d):(\d\d)/) {
55 $end_date = "$7$5${6}T$8$9$10";
59 push(@Data, "# $start_date-$end_date$colon_str$Title \x7D\x7D\x7D\n");
60 unshift(@Data, "# $start_date-$end_date$colon_str$Title \x7B\x7B\x7B\n");
61 $opt_m && push(@Data, "# move\n");
63 for (@Data) {
64 print $_;