4 # Copyright (c) - Dirk Koopman G1TLH
22 use vars qw($VERSION $BRANCH);
23 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
24 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/ || (0,0));
25 $main::build += $VERSION;
26 $main::branch += $BRANCH;
28 use vars qw($db %prefix_loc %pre $lru $lrusize $misses $hits $matchtotal);
30 $db = undef; # the DB_File handle
31 %prefix_loc = (); # the meat of the info
32 %pre = (); # the prefix list
33 $hits = $misses = $matchtotal = 1; # cache stats
34 $lrusize = 2000; # size of prefix LRU cache
36 $lru = LRU->newbase('Prefix', $lrusize);
48 # tie the main prefix database
49 $db = tie(%pre, "DB_File", undef, O_RDWR|O_CREAT, 0664, $DB_BTREE) or confess "can't tie \%pre ($!)";
51 do "$main::data/prefix_data.pl" if !$out;
60 my $fh = new IO::File;
61 my $fn = "$main::data/prefix_data.pl";
63 confess "Prefix system not started" if !$db;
66 rename "$fn.oooo", "$fn.ooooo" if -e "$fn.oooo";
67 rename "$fn.ooo", "$fn.oooo" if -e "$fn.ooo";
68 rename "$fn.oo", "$fn.ooo" if -e "$fn.oo";
69 rename "$fn.o", "$fn.oo" if -e "$fn.o";
70 rename "$fn", "$fn.o" if -e "$fn";
72 $fh->open(">$fn") or die "Can't open $fn ($!)";
74 # prefix location data
75 $fh->print("%prefix_loc = (\n");
76 foreach $l (sort {$a <=> $b} keys %prefix_loc) {
77 my $r = $prefix_loc{$l};
78 $fh->printf(" $l => bless( { name => '%s', dxcc => %d, itu => %d, utcoff => %d, lat => %f, long => %f }, 'Prefix'),\n",
79 $r->{name}, $r->{dxcc}, $r->{itu}, $r->{cq}, $r->{utcoff}, $r->{lat}, $r->{long});
84 $fh->print("%pre = (\n");
85 foreach $k (sort keys %pre) {
86 $fh->print(" '$k' => [");
87 my @list = @{$pre{$k}};
94 $fh->print("$str ],\n");
101 # what you get is a list that looks like:-
103 # prefix => @list of blessed references to prefix_locs
105 # This routine will only do what you ask for, if you wish to be intelligent
106 # then that is YOUR problem!
114 return () if $db->seq($gotkey, $ref, R_CURSOR);
115 return () if $key ne substr $gotkey, 0, length $key;
117 return ($gotkey, map { $prefix_loc{$_} } split ',', $ref);
121 # get the next key that matches, this assumes that you have done a 'get' first
130 return () if $db->seq($gotkey, $ref, R_NEXT);
131 return () if $key ne substr $gotkey, 0, length $key;
133 return ($gotkey, map { $prefix_loc{$_} } split ',', $ref);
137 # put the key LRU incluing the city state info
142 my ($call, $ref) = @_;
143 my @s = USDB::get($call);
146 # this is deep magic, because this is a reference to static data, it
148 my $h = { %{$ref->[1]} };
149 bless $h, ref $ref->[1];
154 $ref->[1]->{city} = $ref->[1]->{state} = "" unless exists $ref->[1]->{state};
157 dbg("Prefix::lru_put $call -> ($ref->[1]->{city}, $ref->[1]->{state})") if isdbg('prefix');
158 $lru->put($call, $ref);
162 # search for the nearest match of a prefix string (starting
163 # from the RH end of the string passed)
171 for (my $i = length $pref; $i; $i--) {
173 my $s = substr($pref, 0, $i);
175 my $p = $lru->get($s);
178 if (isdbg('prefix')) {
179 my $percent = sprintf "%.1f", $hits * 100 / $misses;
180 dbg("Partial Prefix Cache Hit: $s Hits: $hits/$misses of $matchtotal = $percent\%");
182 lru_put($_, $p) for @partials;
187 if (isdbg('prefix')) {
188 my $part = $out[0] || "*";
189 $part .= '*' unless $part eq '*' || $part eq $s;
190 dbg("Partial prefix: $pref $s $part" );
192 if (@out && $out[0] eq $s) {
201 # extract a 'prefix' from a callsign, in other words the largest entity that will
202 # obtain a result from the prefix table.
204 # This is done by repeated probing, callsigns of the type VO1/G1TLH or
205 # G1TLH/VO1 (should) return VO1
210 my $calls = uc shift;
216 LM: foreach $call (split /,/, $calls) {
218 # first check if the whole thing succeeds either because it is cached
219 # or because it simply is a stored prefix as callsign (or even a prefix)
221 $call =~ s/-\d+$//; # ignore SSIDs
222 my $p = $lru->get($call);
226 if (isdbg('prefix')) {
227 my $percent = sprintf "%.1f", $hits * 100 / $misses;
228 dbg("Prefix Cache Hit: $call Hits: $hits/$misses of $matchtotal = $percent\%");
234 # is it in the USDB, force a matchprefix to match?
235 my @s = USDB::get($call);
238 @nout = matchprefix($call) unless @nout;
239 $nout[0] = $call if @nout;
245 if (@nout && $nout[0] eq $call) {
247 lru_put($call, \@nout);
248 dbg("got exact prefix: $nout[0]") if isdbg('prefix');
254 # now split the call into parts if required
255 @parts = ($call =~ '/') ? split('/', $call) : ($call);
256 dbg("Parts: $call = " . join(' ', @parts)) if isdbg('prefix');
258 # remove any /0-9 /P /A /M /MM /AM suffixes etc
260 @parts = grep { !/^\d+$/ && !/^[PABM]$/ && !/^(?:|AM|MM|BCN|JOTA|SIX|WEB|NET|Q\w+)$/; } @parts;
262 # can we resolve them by direct lookup
263 my $s = join('/', @parts);
265 if (@nout && $nout[0] eq $s) {
266 dbg("got exact multipart prefix: $call $s") if isdbg('prefix');
268 lru_put($call, \@nout);
273 dbg("Parts now: $call = " . join(' ', @parts)) if isdbg('prefix');
275 # at this point we should have two or three parts
276 # if it is three parts then join the first and last parts together
279 # first deal with prefix/x00xx/single letter things
280 if (@parts == 3 && length $parts[0] <= length $parts[1]) {
281 @nout = matchprefix($parts[0]);
283 my $s = join('/', $nout[0], $parts[2]);
285 if (@try && $try[0] eq $s) {
286 dbg("got 3 part prefix: $call $s") if isdbg('prefix');
288 lru_put($call, \@try);
293 # if the second part is a callsign and the last part is one letter
294 if (is_callsign($parts[1]) && length $parts[2] == 1) {
300 # if it is a two parter
303 # try it as it is as compound, taking the first part as the prefix
304 @nout = matchprefix($parts[0]);
306 my $s = join('/', $nout[0], $parts[1]);
308 if (@try && $try[0] eq $s) {
309 dbg("got 2 part prefix: $call $s") if isdbg('prefix');
311 lru_put($call, \@try);
318 # remove the problematic /J suffix
319 pop @parts if @parts > 1 && $parts[$#parts] eq 'J';
323 @nout = matchprefix($parts[0]);
325 dbg("got prefix: $call = $nout[0]") if isdbg('prefix');
327 lru_put($call, \@nout);
336 L1: for ($n = 0; $n < @parts; $n++) {
339 for ($i = $k = 0; $i < @parts; $i++) {
340 next if $checked[$i];
342 if (!$sp || length $p < length $sp) {
343 dbg("try part: $p") if isdbg('prefix');
349 $sp =~ s/-\d+$//; # remove any SSID
351 # now start to resolve it from the right hand end
352 @nout = matchprefix($sp);
354 # try and search for it in the descriptions as
355 # a whole callsign if it has multiple parts and the output
356 # is more two long, this should catch things like
357 # FR5DX/T without having to explicitly stick it into
362 $parts[$k] = $nout[0];
363 my $try = join('/', @parts);
365 if (isdbg('prefix')) {
366 my $part = $try[0] || "*";
367 $part .= '*' unless $part eq '*' || $part eq $try;
368 dbg("Compound prefix: $try $part" );
370 if (@try && $try eq $try[0]) {
372 lru_put($call, \@try);
376 lru_put($call, \@nout);
381 lru_put($call, \@nout);
389 @nout = matchprefix('Q');
391 lru_put($call, \@nout);
395 if (isdbg('prefixdata')) {
396 my $dd = new Data::Dumper([ \@out ], [qw(@out)]);
403 # turn a list of prefixes / dxcc numbers into a list of dxcc/itu/zone numbers
417 if ($cmd ne 'ns' && $v =~ /^\d+$/) {
418 push @out, $v unless grep $_ eq $v, @out;
420 if ($cmd eq 'ns' && $v =~ /^[A-Z][A-Z]$/i) {
421 push @out, uc $v unless grep $_ eq uc $v, @out;
423 my @pre = Prefix::extract($v);
426 foreach my $p (@pre) {
427 my $n = $p->dxcc if $cmd eq 'nc' ;
428 $n = $p->itu if $cmd eq 'ni' ;
429 $n = $p->cq if $cmd eq 'nz' ;
430 $n = $p->state if $cmd eq 'ns';
431 push @out, $n unless grep $_ eq $n, @out;
441 lat => '0,Latitude,slat',
442 long => '0,Longitude,slong',
449 utcoff => '0,UTC offset',
450 cont => '0,Continent',
457 my $name = $AUTOLOAD;
459 return if $name =~ /::DESTROY$/;
462 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
463 # this clever line of code creates a subroutine which takes over from autoload
464 # from OO Perl - Conway
465 *$AUTOLOAD = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}} ;
466 &$AUTOLOAD($self, @_);
470 # return a prompt for a field
475 my ($self, $ele) = @_;