-
Notifications
You must be signed in to change notification settings - Fork 7
/
GPS.pm
123 lines (101 loc) · 2.37 KB
/
GPS.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# -*- perl -*-
#
# Author: Slaven Rezic
#
# Copyright (C) 2001,2004,2012 Slaven Rezic. All rights reserved.
# This package is free software; you can redistribute it and/or
# modify it under the same terms as Perl itself.
#
# Mail: [email protected]
# WWW: http://bbbike.de
#
# XXX rename to BBBikeGPS, to avoid conflicts with GPS:: namespace
# XXX no --- BBBikeGPS is already taken...
package GPS;
use strict;
use vars qw(@gps $_UTF8_BOM);
# KML is checked before GPX, because the KML magic is more strict
@gps = qw(GpsmanData MyNMEA G7toWin_2 G7toWin_ASCII Ovl WaypointPlus MPS Gardown KML GPX Gpsbabel);
$_UTF8_BOM = "\xef\xbb\xbf";
sub new { bless {}, shift }
sub all { @gps }
sub preload {
my $self = shift;
my $mod = shift;
my $fullmod = 'GPS::' . $mod;
eval "require $fullmod";
die $@ if $@;
$fullmod;
}
sub transfer_to_file { 1 }
sub default_extension { ".txt" }
sub transfer {
my($self, %args) = @_;
my $file = $args{-file} or die "-file argument is missing";
my $res = $args{-res} or die "-res argument is missing";
open my $F, "> $file" or die "Can't write to $file: $!";
binmode $F;
print $F $res;
close $F;
}
sub magics {
my $self = shift;
die "No magics for $self defined";
}
# check for magic
sub check {
my $self = shift;
my $file = shift;
my(%args) = @_;
my($fh, $lines_ref) = $self->overread_trash($file, %args);
defined $fh;
}
# Return ($fh, \@lines)
# $fh is a filehandle or undef
# @lines is an array reference of the magic lines
sub overread_trash {
my $self = shift;
my $file = shift;
my(%args) = @_;
my(@magics) = $self->magics;
my @last_lines;
my $found = 0;
open my $fh, $file
or die "Die Datei $file kann nicht geöffnet werden: $!";
binmode $fh;
FILETRY: {
while(<$fh>) {
if (@magics == 1) {
if (/$magics[0]/) {
push @last_lines, $_;
$found = 1;
last FILETRY;
}
last FILETRY if (!$args{-fuzzy});
} else {
if (@last_lines == @magics) {
shift @last_lines;
}
push @last_lines, $_;
if (@last_lines == @magics) {
TRY: {
for(my $i = 0; $i<=$#last_lines; $i++) {
last TRY if ($last_lines[$i] !~ /$magics[$i]/);
}
$found = 1;
last FILETRY;
}
last FILETRY if (!$args{-fuzzy});
}
}
}
}
if ($found) {
($fh, \@last_lines);
} else {
close $fh;
(undef, []);
}
}
1;
__END__