64af363ee3045c718260e83f15a6f58e0a413275
[spider.git] / perl / Spot.pm
1 #
2 # the dx spot handler
3 #
4 # Copyright (c) - 1998 Dirk Koopman G1TLH
5 #
6 # $Id$
7 #
8
9 package Spot;
10
11 use FileHandle;
12 use DXVars;
13 use DXDebug;
14 use Julian;
15 use Prefix;
16 use Carp;
17
18 @ISA = qw(Julian);
19
20 use strict;
21
22 my $fp;
23 my $maxspots = 50;      # maximum spots to return
24 my $defaultspots = 10;    # normal number of spots to return
25 my $maxdays = 35;        # normal maximum no of days to go back
26 my $dirprefix = "$main::data/spots";
27
28 sub prefix
29 {
30   return $dirprefix;
31 }
32
33 # add a spot to the data file (call as Spot::add)
34 sub add
35 {
36   my @spot = @_;    # $freq, $call, $t, $comment, $spotter = @_
37
38   # sure that the numeric things are numeric now (saves time later)
39   $spot[0] = 0 + $spot[0];
40   $spot[2] = 0 + $spot[2];
41   
42   # remove ssid if present on spotter
43   $spot[4] =~ s/-\d+$//o;
44
45   # compare dates to see whether need to open another save file (remember, redefining $fp 
46   # automagically closes the output file (if any))
47   my @date = Julian::unixtoj($spot[2]);
48   $fp = Spot->open(@date, ">>") if (!$fp || Julian::cmp(@date, $fp->{year}, $fp->{day}));
49
50   # save it
51   my $fh = $fp->{fh};
52
53   # add the 'dxcc' country on the end
54   my @dxcc = Prefix::extract($spot[1]);
55   push @spot, (@dxcc > 0 ) ? $dxcc[1]->dxcc() : 0;
56
57   $fh->print(join("\^", @spot), "\n");
58 }
59
60 # search the spot database for records based on the field no and an expression
61 # this returns a set of references to the spots
62 #
63 # the expression is a legal perl 'if' statement with the possible fields indicated
64 # by $f<n> where :-
65 #
66 #   $f0 = frequency
67 #   $f1 = call
68 #   $f2 = date in unix format
69 #   $f3 = comment
70 #   $f4 = spotter
71 #   $f5 = dxcc country
72 #
73 # In addition you can specify a range of days, this means that it will start searching
74 # from <n> days less than today to <m> days less than today
75 #
76 # Also you can select a range of entries so normally you would get the 0th (latest) entry
77 # back to the 5th latest, you can specify a range from the <x>th to the <y>the oldest.
78 #
79 # This routine is designed to be called as Spot::search(..)
80 #
81
82 sub search
83 {
84   my ($expr, $dayfrom, $dayto, $from, $to) = @_;
85   my $eval;
86   my @out;
87   my $ref;
88   my $i;
89   my $count;
90   my @today = Julian::unixtoj(time);
91   my @fromdate;
92   my @todate;
93   
94   if ($dayfrom > 0) {
95     @fromdate = Julian::sub(@today, $dayfrom);
96   } else {
97     @fromdate = @today;
98         $dayfrom = 0;
99   }
100   if ($dayto > 0) {
101     @todate = Julian::sub(@fromdate, $dayto);
102   } else {
103     @todate = Julian::sub(@fromdate, $maxdays);
104   }
105   if ($from || $to) {
106     $to = $from + $maxspots if $to - $from > $maxspots || $to - $from <= 0;
107   } else {
108     $from = 0;
109         $to = $defaultspots;
110   }
111
112   $expr =~ s/\$f(\d)/\$ref->[$1]/g;               # swap the letter n for the correct field name
113   
114   dbg("search", "expr='$expr', spotno=$from-$to, day=$dayfrom-$dayto\n");
115   
116   # build up eval to execute
117   $eval = qq(my \$c;
118     for (\$c = \$#spots; \$c >= 0; \$c--) {
119           \$ref = \$spots[\$c];
120           if ($expr) {
121             \$count++;
122                 next if \$count < \$from;                  # wait until from 
123         push(\@out, \$ref);
124                 last LOOP if \$count >= \$to;                  # stop after to
125           }
126   });
127
128 LOOP:
129   for ($i = 0; $i < 60; ++$i) {
130     my @now = Julian::sub(@fromdate, $i);
131         last if Julian::cmp(@now, @todate) <= 0;         
132         
133         my @spots = ();
134         my $fp = Spot->open(@now);  # get the next file
135         if ($fp) {
136           my $fh = $fp->{fh};
137           my $in;
138           foreach $in (<$fh>) {
139             chomp $in;
140         push @spots, [ split('\^', $in) ];
141           }
142           my $ref;
143           eval $eval;               # do the search on this file
144           return ("error", $@) if $@;
145         }
146   }
147
148   return @out;
149 }
150
151 # open a spot file of the Julian day
152 sub open
153 {
154   my $pkg = shift;
155   return Julian::open("spot", $dirprefix, @_);
156 }
157
158 # close a spot file
159 sub close
160 {
161   # do nothing, unreferencing or overwriting the $self will close it  
162 }
163
164 1;