4 # Copyright (c) - Dirk Koopman G1TLH
21 use vars qw($VERSION $BRANCH);
22 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
23 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/ || (0,0));
24 $main::build += $VERSION;
25 $main::branch += $BRANCH;
27 use vars qw($db %prefix_loc %pre %cache $lasttime $hits $matchtotal);
29 $db = undef; # the DB_File handle
30 %prefix_loc = (); # the meat of the info
31 %pre = (); # the prefix list
32 %cache = (); # a runtime cache of matched prefixes
33 $lasttime = 0; # last time this cache was cleared
34 $hits = $matchtotal = 1; # cache stats
44 $db = tie(%pre, "DB_File", undef, O_RDWR|O_CREAT, 0666, $DB_BTREE) or confess "can't tie \%pre ($!)";
46 do "$main::data/prefix_data.pl" if !$out;
48 # print Data::Dumper->Dump([\%pre, \%prefix_loc], [qw(pre prefix_loc)]);
55 my $fh = new IO::File;
56 my $fn = "$main::data/prefix_data.pl";
58 confess "Prefix system not started" if !$db;
61 rename "$fn.oooo", "$fn.ooooo" if -e "$fn.oooo";
62 rename "$fn.ooo", "$fn.oooo" if -e "$fn.ooo";
63 rename "$fn.oo", "$fn.ooo" if -e "$fn.oo";
64 rename "$fn.o", "$fn.oo" if -e "$fn.o";
65 rename "$fn", "$fn.o" if -e "$fn";
67 $fh->open(">$fn") or die "Can't open $fn ($!)";
69 # prefix location data
70 $fh->print("%prefix_loc = (\n");
71 foreach $l (sort {$a <=> $b} keys %prefix_loc) {
72 my $r = $prefix_loc{$l};
73 $fh->printf(" $l => bless( { name => '%s', dxcc => %d, itu => %d, utcoff => %d, lat => %f, long => %f }, 'Prefix'),\n",
74 $r->{name}, $r->{dxcc}, $r->{itu}, $r->{cq}, $r->{utcoff}, $r->{lat}, $r->{long});
79 $fh->print("%pre = (\n");
80 foreach $k (sort keys %pre) {
81 $fh->print(" '$k' => [");
82 my @list = @{$pre{$k}};
89 $fh->print("$str ],\n");
96 # what you get is a list that looks like:-
98 # prefix => @list of blessed references to prefix_locs
100 # This routine will only do what you ask for, if you wish to be intelligent
101 # then that is YOUR problem!
109 return () if $db->seq($gotkey, $ref, R_CURSOR);
110 return () if $key ne substr $gotkey, 0, length $key;
112 return ($gotkey, map { $prefix_loc{$_} } split ',', $ref);
116 # get the next key that matches, this assumes that you have done a 'get' first
125 return () if $db->seq($gotkey, $ref, R_NEXT);
126 return () if $key ne substr $gotkey, 0, length $key;
128 return ($gotkey, map { $prefix_loc{$_} } split ',', $ref);
132 # search for the nearest match of a prefix string (starting
133 # from the RH end of the string passed)
141 for (my $i = length $pref; $i; $i--) {
143 my $s = substr($pref, 0, $i);
147 if (isdbg('prefix')) {
148 my $percent = sprintf "%.1f", $hits * 100 / $matchtotal;
149 dbg("Partial Prefix Cache Hit: $s Hits: $hits of $matchtotal = $percent\%");
155 if (isdbg('prefix')) {
156 my $part = $out[0] || "*";
157 $part .= '*' unless $part eq '*' || $part eq $s;
158 dbg("Partial prefix: $pref $s $part" );
160 if (@out && $out[0] eq $s) {
161 $cache{$_} = \@out for @partials;
170 # extract a 'prefix' from a callsign, in other words the largest entity that will
171 # obtain a result from the prefix table.
173 # This is done by repeated probing, callsigns of the type VO1/G1TLH or
174 # G1TLH/VO1 (should) return VO1
179 my $calls = uc shift;
185 # clear out the cache periodically to stop it growing for ever.
186 if ($main::systime - $lasttime >= 15*60) {
187 if (isdbg('prefix')) {
188 my $percent = sprintf "%.1f", $hits * 100 / $matchtotal;
189 dbg("Prefix Cache Cleared, Hits: $hits of $matchtotal = $percent\%") ;
192 $lasttime = $main::systime;
193 $hits = $matchtotal = 0;
196 LM: foreach $call (split /,/, $calls) {
198 # first check if the whole thing succeeds either because it is cached
199 # or because it simply is a stored prefix as callsign (or even a prefix)
201 my $p = $cache{$call};
205 if (isdbg('prefix')) {
206 my $percent = sprintf "%.1f", $hits * 100 / $matchtotal;
207 dbg("Prefix Cache Hit: $call Hits: $hits of $matchtotal = $percent\%");
213 if (@nout && $nout[0] eq $call) {
214 $cache{$call} = \@nout;
215 dbg("got exact prefix: $nout[0]") if isdbg('prefix');
221 # now split the call into parts if required
222 @parts = ($call =~ '/') ? split('/', $call) : ($call);
223 dbg("Parts: $call = " . join(' ', @parts)) if isdbg('prefix');
225 # remove any /0-9 /P /A /M /MM /AM suffixes etc
227 @parts = grep { !/^\d+$/ && !/^[PABM]$/ && !/^(?:|AM|MM|BCN|JOTA|SIX|WEB|NET|Q\w+)$/; } @parts;
229 # can we resolve them by direct lookup
230 my $s = join('/', @parts);
232 if (@nout && $nout[0] eq $s) {
233 dbg("got exact multipart prefix: $call $s") if isdbg('prefix');
234 $cache{$call} = \@nout;
239 dbg("Parts now: $call = " . join(' ', @parts)) if isdbg('prefix');
241 # at this point we should have two or three parts
242 # if it is three parts then join the first and last parts together
245 # first deal with prefix/x00xx/single letter things
246 if (@parts == 3 && length $parts[0] <= length $parts[1]) {
247 @nout = matchprefix($parts[0]);
249 my $s = join('/', $nout[0], $parts[2]);
251 if (@try && $try[0] eq $s) {
252 dbg("got 3 part prefix: $call $s") if isdbg('prefix');
253 $cache{$call} = \@try;
258 # if the second part is a callsign and the last part is one letter
259 if (is_callsign($parts[1]) && length $parts[2] == 1) {
265 # if it is a two parter
268 # try it as it is as compound, taking the first part as the prefix
269 @nout = matchprefix($parts[0]);
271 my $s = join('/', $nout[0], $parts[1]);
273 if (@try && $try[0] eq $s) {
274 dbg("got 2 part prefix: $call $s") if isdbg('prefix');
275 $cache{$call} = \@try;
282 # remove the problematic /J suffix
283 pop @parts if @parts > 1 && $parts[$#parts] eq 'J';
287 @nout = matchprefix($parts[0]);
289 dbg("got prefix: $call = $nout[0]") if isdbg('prefix');
290 $cache{$call} = \@nout;
299 L1: for ($n = 0; $n < @parts; $n++) {
302 for ($i = $k = 0; $i < @parts; $i++) {
303 next if $checked[$i];
305 if (!$sp || length $p < length $sp) {
306 dbg("try part: $p") if isdbg('prefix');
312 $sp =~ s/-\d+$//; # remove any SSID
314 # now start to resolve it from the right hand end
315 @nout = matchprefix($sp);
317 # try and search for it in the descriptions as
318 # a whole callsign if it has multiple parts and the output
319 # is more two long, this should catch things like
320 # FR5DX/T without having to explicitly stick it into
325 $parts[$k] = $nout[0];
326 my $try = join('/', @parts);
328 if (isdbg('prefix')) {
329 my $part = $try[0] || "*";
330 $part .= '*' unless $part eq '*' || $part eq $try;
331 dbg("Compound prefix: $try $part" );
333 if (@try && $try eq $try[0]) {
334 $cache{$call} = \@try;
337 $cache{$call} = \@nout;
341 $cache{$call} = \@nout;
349 @nout = matchprefix('Q');
350 $cache{$call} = \@nout;
354 if (isdbg('prefixdata')) {
355 my $dd = new Data::Dumper([ \@out ], [qw(@out)]);
362 lat => '0,Latitude,slat',
363 long => '0,Longitude,slong',
368 utcoff => '0,UTC offset',
369 cont => '0,Continent',
376 my $name = $AUTOLOAD;
378 return if $name =~ /::DESTROY$/;
381 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
382 # this clever line of code creates a subroutine which takes over from autoload
383 # from OO Perl - Conway
384 *{$AUTOLOAD} = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}} ;
386 $self->{$name} = shift;
388 return $self->{$name};
393 # return a prompt for a field
398 my ($self, $ele) = @_;