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)
140 for (my $i = length $pref; $i; $i--) {
141 my $s = substr($pref, 0, $i);
143 if (isdbg('prefix')) {
144 my $part = $out[0] || "*";
145 $part .= '*' unless $part eq '*' || $part eq $s;
146 dbg("Partial prefix: $pref $s $part" );
148 return @out if @out && $out[0] eq $s;
154 # extract a 'prefix' from a callsign, in other words the largest entity that will
155 # obtain a result from the prefix table.
157 # This is done by repeated probing, callsigns of the type VO1/G1TLH or
158 # G1TLH/VO1 (should) return VO1
163 my $calls = uc shift;
169 # clear out the cache periodically to stop it growing for ever.
170 if ($main::systime - $lasttime >= 15*60) {
171 if (isdbg('prefix')) {
172 my $percent = $hits * 100 / $matchtotal;
173 dbg("Prefix Cache Cleared, Hits: $hits of $matchtotal = $percent\%")
175 my $percent = $hits * 100 / $matchtotal;
176 dbg("Prefix Cache Cleared, $percent\% hits") if isdbg('prefix');
178 $lasttime = $main::systime;
179 $hits = $matchtotal = 0;
182 LM: foreach $call (split /,/, $calls) {
184 # first check if the whole thing succeeds either because it is cached
185 # or because it simply is a stored prefix as callsign (or even a prefix)
187 my $p = $cache{$call};
191 if (isdbg('prefix')) {
192 my $percent = $hits * 100 / $matchtotal;
193 dbg("Prefix Cache Hit: $call Hits: $hits of $matchtotal = $percent\%")
199 if (@nout && $nout[0] eq $call) {
200 $cache{$call} = \@nout;
201 dbg("got exact prefix: $nout[0]") if isdbg('prefix');
207 # now split the call into parts if required
208 @parts = ($call =~ '/') ? split('/', $call) : ($call);
209 dbg("Parts: $call = " . join(' ', @parts)) if isdbg('prefix');
211 # remove any /0-9 /P /A /M /MM /AM suffixes etc
213 @parts = grep { !/^\d+$/ && !/^[PABM]$/ && !/^(?:|AM|MM|BCN|JOTA|SIX|WEB|NET|Q\w+)$/; } @parts;
215 # can we resolve them by direct lookup
216 my $s = join('/', @parts);
218 if (@nout && $nout[0] eq $s) {
219 dbg("got exact multipart prefix: $call $s") if isdbg('prefix');
220 $cache{$call} = \@nout;
225 dbg("Parts now: $call = " . join(' ', @parts)) if isdbg('prefix');
227 # at this point we should have two or three parts
228 # if it is three parts then join the first and last parts together
231 # first deal with prefix/x00xx/single letter things
232 if (@parts == 3 && length $parts[0] <= length $parts[1]) {
233 @nout = matchprefix($parts[0]);
235 my $s = join('/', $nout[0], $parts[2]);
237 if (@try && $try[0] eq $s) {
238 dbg("got 3 part prefix: $call $s") if isdbg('prefix');
239 $cache{$call} = \@try;
244 # if the second part is a callsign and the last part is one letter
245 if (is_callsign($parts[1]) && length $parts[2] == 1) {
251 # if it is a two parter
254 # try it as it is as compound, taking the first part as the prefix
255 @nout = matchprefix($parts[0]);
257 my $s = join('/', $nout[0], $parts[1]);
259 if (@try && $try[0] eq $s) {
260 dbg("got 2 part prefix: $call $s") if isdbg('prefix');
261 $cache{$call} = \@try;
268 # remove the problematic /J suffix
269 pop @parts if @parts > 1 && $parts[$#parts] eq 'J';
273 @nout = matchprefix($parts[0]);
275 dbg("got prefix: $call ]") if isdbg('prefix');
276 $cache{$call} = \@nout;
285 L1: for ($n = 0; $n < @parts; $n++) {
288 for ($i = $k = 0; $i < @parts; $i++) {
289 next if $checked[$i];
291 if (!$sp || length $p < length $sp) {
292 dbg("try part: $p") if isdbg('prefix');
298 $sp =~ s/-\d+$//; # remove any SSID
300 # now start to resolve it from the right hand end
301 @nout = matchprefix($sp);
303 # try and search for it in the descriptions as
304 # a whole callsign if it has multiple parts and the output
305 # is more two long, this should catch things like
306 # FR5DX/T without having to explicitly stick it into
311 $parts[$k] = $nout[0];
312 my $try = join('/', @parts);
314 if (isdbg('prefix')) {
315 my $part = $try[0] || "*";
316 $part .= '*' unless $part eq '*' || $part eq $try;
317 dbg("Compound prefix: $try $part" );
319 if (@try && $try eq $try[0]) {
320 $cache{$call} = \@try;
323 $cache{$call} = \@nout;
327 $cache{$call} = \@nout;
335 @nout = matchprefix('Q');
336 $cache{$call} = \@nout;
340 if (isdbg('prefix')) {
341 my $dd = new Data::Dumper([ \@out ], [qw(@out)]);
348 lat => '0,Latitude,slat',
349 long => '0,Longitude,slong',
354 utcoff => '0,UTC offset',
355 cont => '0,Continent',
362 my $name = $AUTOLOAD;
364 return if $name =~ /::DESTROY$/;
367 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
368 # this clever line of code creates a subroutine which takes over from autoload
369 # from OO Perl - Conway
370 *{$AUTOLOAD} = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}} ;
372 $self->{$name} = shift;
374 return $self->{$name};
379 # return a prompt for a field
384 my ($self, $ele) = @_;