pre 1.13 release
[spider.git] / perl / Prefix.pm
1 #
2 # prefix handling
3 #
4 # Copyright (c) - Dirk Koopman G1TLH
5 #
6 # $Id$
7 #
8
9 package Prefix;
10
11 use Carp;
12 use DXVars;
13 use DB_File;
14 use Data::Dumper;
15 use Carp;
16
17 use strict;
18 use vars qw($db  %prefix_loc %pre);
19
20 $db = undef;     # the DB_File handle
21 %prefix_loc = ();   # the meat of the info
22 %pre = ();       # the prefix list
23
24 sub load
25 {
26   if ($db) {
27     untie %pre;
28         %pre = ();
29         %prefix_loc = ();
30   }
31   $db = tie(%pre, "DB_File", undef, O_RDWR|O_CREAT, 0666, $DB_BTREE) or confess "can't tie \%pre ($!)";  
32   my $out = $@ if $@;
33   do "$main::data/prefix_data.pl" if !$out;
34   $out = $@ if $@;
35 #  print Data::Dumper->Dump([\%pre, \%prefix_loc], [qw(pre prefix_loc)]);
36   return $out;
37 }
38
39 sub store
40 {
41   my ($k, $l);
42   my $fh = new FileHandle;
43   my $fn = "$main::data/prefix_data.pl";
44   
45   confess "Prefix system not started" if !$db;
46   
47   # save versions!
48   rename "$fn.oooo", "$fn.ooooo" if -e "$fn.oooo";
49   rename "$fn.ooo", "$fn.oooo" if -e "$fn.ooo";
50   rename "$fn.oo", "$fn.ooo" if -e "$fn.oo";
51   rename "$fn.o", "$fn.oo" if -e "$fn.o";
52   rename "$fn", "$fn.o" if -e "$fn";
53   
54   $fh->open(">$fn") or die "Can't open $fn ($!)";
55
56   # prefix location data
57   $fh->print("%prefix_loc = (\n");
58   foreach $l (sort {$a <=> $b} keys %prefix_loc) {
59     my $r = $prefix_loc{$l};
60         $fh->printf("   $l => bless( { name => '%s', dxcc => %d, itu => %d, utcoff => %d, lat => %f, long => %f }, 'Prefix'),\n",
61                     $r->{name}, $r->{dxcc}, $r->{itu}, $r->{cq}, $r->{utcoff}, $r->{lat}, $r->{long});
62   }
63   $fh->print(");\n\n");
64
65   # prefix data
66   $fh->print("%pre = (\n");
67   foreach $k (sort keys %pre) {
68     $fh->print("   '$k' => [");
69         my @list = @{$pre{$k}};
70         my $l;
71         my $str;
72         foreach $l (@list) {
73       $str .= " $l,";
74     }
75         chop $str;  
76         $fh->print("$str ],\n");
77   }
78   $fh->print(");\n");
79   $fh->close;
80 }
81
82 # what you get is a list that looks like:-
83
84 # prefix => @list of blessed references to prefix_locs 
85 #
86 # This routine will only do what you ask for, if you wish to be intelligent
87 # then that is YOUR problem!
88 #
89 sub get
90 {
91   my $key = shift;
92   my @out;
93   my @outref;
94   my $ref;
95   my $gotkey;
96   
97   $gotkey = $key;
98   return () if $db->seq($gotkey, $ref, R_CURSOR);
99   return () if $key ne substr $gotkey, 0, length $key;
100
101   @outref = map { $prefix_loc{$_} } split ',', $ref;
102   return ($gotkey, @outref);
103 }
104
105 #
106 # get the next key that matches, this assumes that you have done a 'get' first
107 #
108
109 sub next
110 {
111   my $key = shift;
112   my @out;
113   my @outref;
114   my $ref;
115   my $gotkey;
116   
117   return () if $db->seq($gotkey, $ref, R_NEXT);
118   return () if $key ne substr $gotkey, 0, length $key;
119   
120   @outref = map { $prefix_loc{$_} } split ',', $ref;
121   return ($gotkey, @outref);
122 }
123
124 #
125 # extract a 'prefix' from a callsign, in other words the largest entity that will
126 # obtain a result from the prefix table.
127 #
128 # This is done by repeated probing, callsigns of the type VO1/G1TLH or
129 # G1TLH/VO1 (should) return VO1
130 #
131
132 sub extract
133 {
134   my $call = uc shift;
135   my @out;
136   my @nout;
137   my $p;
138   my @parts;
139   my ($sp, $i);
140   
141   # first check if the whole thing succeeds
142   @out = get($call);
143   return @out if @out > 0 && $out[0] eq $call;
144   
145   # now split the call into parts if required
146   @parts = ($call =~ '/') ? split('/', $call) : ($call);
147
148   # remove any /0-9 /P /A /M /MM /AM suffixes etc
149   if (@parts > 1) {
150     $p = $parts[$#parts];
151         pop @parts if $p =~ /^(\d+|[PABM]|AM|MM|BCN|SIX|Q\w+)$/o;
152     $p = $parts[$#parts];
153         pop @parts if $p =~ /^(\d+|[PABM]|AM|MM|BCN|SIX|Q\w+)$/o;
154   
155     # can we resolve them by direct lookup
156         foreach $p (@parts) {
157       @out = get($p);
158           return @out if @out > 0 && $out[0] eq $call;
159         }
160   }
161   
162   # which is the shortest part (first if equal)?
163   $sp = $parts[0];
164   foreach $p (@parts) {
165     $sp = $p if length $sp > length $p;
166   }
167   # now start to resolve it from the left hand end
168   for (@out = (), $i = 1; $i <= length $sp; ++$i) {
169     @nout = get(substr($sp, 0, $i));
170         last if @nout > 0 && $nout[0] gt $sp;
171         last if @nout == 0;
172         @out = @nout;
173   }
174   
175   # not found
176   return (@out > 0) ? @out : ();
177 }
178
179 my %valid = (
180   lat => '0,Latitude,slat',
181   long => '0,Longitude,slong',
182   dxcc => '0,DXCC',
183   name => '0,Name',
184   itu => '0,ITU',
185   cq => '0,CQ',
186   utcoff => '0,UTC offset',
187 );
188
189 no strict;
190 sub AUTOLOAD
191 {
192   my $self = shift;
193   my $name = $AUTOLOAD;
194   
195   return if $name =~ /::DESTROY$/;
196   $name =~ s/.*:://o;
197   
198   confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
199   if (@_) {
200     $self->{$name} = shift;
201   }
202   return $self->{$name};
203 }
204 use strict;
205
206 #
207 # return a prompt for a field
208 #
209
210 sub field_prompt
211
212   my ($self, $ele) = @_;
213   return $valid{$ele};
214 }
215 1;
216
217 __END__