got the prefix code working quite well, changed a few country codes.
[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)/zzzref->[$1]/g;               # swap the letter n for the correct field name
113   $expr =~ s/[\@\$\%\{\}]//g;                           # remove any other funny characters
114   $expr =~ s/\&\w+\(//g;                           # remove subroutine calls
115   $expr =~ s/eval//g;                              # remove eval words
116   $expr =~ s/zzzref/\$ref/g;                       # put back the $ref
117   $expr =~ s|(/.+/)|$1oi|g;                        # add oi characters to /ccc/
118   
119   print "expr=($expr), from=$from, to=$to\n";
120   
121   # build up eval to execute
122   $eval = qq(my \$c;
123     for (\$c = \$#spots; \$c >= 0; \$c--) {
124           \$ref = \$spots[\$c];
125           if ($expr) {
126             \$count++;
127                 next if \$count < \$from;                  # wait until from 
128         push(\@out, \$ref);
129                 last LOOP if \$count >= \$to;                  # stop after to
130           }
131   });
132
133 LOOP:
134   for ($i = 0; $i < 60; ++$i) {
135     my @now = Julian::sub(@fromdate, $i);
136         last if Julian::cmp(@now, @todate) <= 0;         
137         
138         my @spots = ();
139         my $fp = Spot->open(@now);  # get the next file
140         if ($fp) {
141           my $fh = $fp->{fh};
142           my $in;
143           foreach $in (<$fh>) {
144             chomp $in;
145         push @spots, [ split('\^', $in) ];
146           }
147           my $ref;
148           eval $eval;               # do the search on this file
149           return ("error", $@) if $@;
150         }
151   }
152
153   return @out;
154 }
155
156 # open a spot file of the Julian day
157 sub open
158 {
159   my $pkg = shift;
160   return Julian::open("spot", $dirprefix, @_);
161 }
162
163 # close a spot file
164 sub close
165 {
166   # do nothing, unreferencing or overwriting the $self will close it  
167 }
168
169 1;