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 = 1000; # size of prefix LRU cache
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;
53 $lru = LRU->newbase('Prefix', $lrusize);
61 my $fh = new IO::File;
62 my $fn = "$main::data/prefix_data.pl";
64 confess "Prefix system not started" if !$db;
67 rename "$fn.oooo", "$fn.ooooo" if -e "$fn.oooo";
68 rename "$fn.ooo", "$fn.oooo" if -e "$fn.ooo";
69 rename "$fn.oo", "$fn.ooo" if -e "$fn.oo";
70 rename "$fn.o", "$fn.oo" if -e "$fn.o";
71 rename "$fn", "$fn.o" if -e "$fn";
73 $fh->open(">$fn") or die "Can't open $fn ($!)";
75 # prefix location data
76 $fh->print("%prefix_loc = (\n");
77 foreach $l (sort {$a <=> $b} keys %prefix_loc) {
78 my $r = $prefix_loc{$l};
79 $fh->printf(" $l => bless( { name => '%s', dxcc => %d, itu => %d, utcoff => %d, lat => %f, long => %f }, 'Prefix'),\n",
80 $r->{name}, $r->{dxcc}, $r->{itu}, $r->{cq}, $r->{utcoff}, $r->{lat}, $r->{long});
85 $fh->print("%pre = (\n");
86 foreach $k (sort keys %pre) {
87 $fh->print(" '$k' => [");
88 my @list = @{$pre{$k}};
95 $fh->print("$str ],\n");
102 # what you get is a list that looks like:-
104 # prefix => @list of blessed references to prefix_locs
106 # This routine will only do what you ask for, if you wish to be intelligent
107 # then that is YOUR problem!
115 return () if $db->seq($gotkey, $ref, R_CURSOR);
116 return () if $key ne substr $gotkey, 0, length $key;
118 return ($gotkey, map { $prefix_loc{$_} } split ',', $ref);
122 # get the next key that matches, this assumes that you have done a 'get' first
131 return () if $db->seq($gotkey, $ref, R_NEXT);
132 return () if $key ne substr $gotkey, 0, length $key;
134 return ($gotkey, map { $prefix_loc{$_} } split ',', $ref);
138 # put the key LRU incluing the city state info
143 my ($call, $ref) = @_;
144 my @s = USDB::get($call);
147 # this is deep magic, because this is a reference to static data, it
149 my $h = { %{$ref->[1]} };
150 bless $h, ref $ref->[1];
155 $ref->[1]->{city} = $ref->[1]->{state} = "" unless exists $ref->[1]->{state};
158 dbg("Prefix::lru_put $call -> ($ref->[1]->{city}, $ref->[1]->{state})") if isdbg('prefix');
159 $lru->put($call, $ref);
163 # search for the nearest match of a prefix string (starting
164 # from the RH end of the string passed)
172 for (my $i = length $pref; $i; $i--) {
174 my $s = substr($pref, 0, $i);
176 my $p = $lru->get($s);
179 if (isdbg('prefix')) {
180 my $percent = sprintf "%.1f", $hits * 100 / $misses;
181 dbg("Partial Prefix Cache Hit: $s Hits: $hits/$misses of $matchtotal = $percent\%");
183 lru_put($_, $p) for @partials;
188 if (isdbg('prefix')) {
189 my $part = $out[0] || "*";
190 $part .= '*' unless $part eq '*' || $part eq $s;
191 dbg("Partial prefix: $pref $s $part" );
193 if (@out && $out[0] eq $s) {
202 # extract a 'prefix' from a callsign, in other words the largest entity that will
203 # obtain a result from the prefix table.
205 # This is done by repeated probing, callsigns of the type VO1/G1TLH or
206 # G1TLH/VO1 (should) return VO1
211 my $calls = uc shift;
217 LM: foreach $call (split /,/, $calls) {
219 # first check if the whole thing succeeds either because it is cached
220 # or because it simply is a stored prefix as callsign (or even a prefix)
222 $call =~ s/-\d+$//; # ignore SSIDs
223 my $p = $lru->get($call);
227 if (isdbg('prefix')) {
228 my $percent = sprintf "%.1f", $hits * 100 / $misses;
229 dbg("Prefix Cache Hit: $call Hits: $hits/$misses of $matchtotal = $percent\%");
235 # is it in the USDB, force a matchprefix to match?
236 my @s = USDB::get($call);
239 @nout = matchprefix($call) unless @nout;
240 $nout[0] = $call if @nout;
246 if (@nout && $nout[0] eq $call) {
248 lru_put($call, \@nout);
249 dbg("got exact prefix: $nout[0]") if isdbg('prefix');
255 # now split the call into parts if required
256 @parts = ($call =~ '/') ? split('/', $call) : ($call);
257 dbg("Parts: $call = " . join(' ', @parts)) if isdbg('prefix');
259 # remove any /0-9 /P /A /M /MM /AM suffixes etc
261 @parts = grep { !/^\d+$/ && !/^[PABM]$/ && !/^(?:|AM|MM|BCN|JOTA|SIX|WEB|NET|Q\w+)$/; } @parts;
263 # can we resolve them by direct lookup
264 my $s = join('/', @parts);
266 if (@nout && $nout[0] eq $s) {
267 dbg("got exact multipart prefix: $call $s") if isdbg('prefix');
269 lru_put($call, \@nout);
274 dbg("Parts now: $call = " . join(' ', @parts)) if isdbg('prefix');
276 # at this point we should have two or three parts
277 # if it is three parts then join the first and last parts together
280 # first deal with prefix/x00xx/single letter things
281 if (@parts == 3 && length $parts[0] <= length $parts[1]) {
282 @nout = matchprefix($parts[0]);
284 my $s = join('/', $nout[0], $parts[2]);
286 if (@try && $try[0] eq $s) {
287 dbg("got 3 part prefix: $call $s") if isdbg('prefix');
289 lru_put($call, \@try);
294 # if the second part is a callsign and the last part is one letter
295 if (is_callsign($parts[1]) && length $parts[2] == 1) {
301 # if it is a two parter
304 # try it as it is as compound, taking the first part as the prefix
305 @nout = matchprefix($parts[0]);
307 my $s = join('/', $nout[0], $parts[1]);
309 if (@try && $try[0] eq $s) {
310 dbg("got 2 part prefix: $call $s") if isdbg('prefix');
312 lru_put($call, \@try);
319 # remove the problematic /J suffix
320 pop @parts if @parts > 1 && $parts[$#parts] eq 'J';
324 @nout = matchprefix($parts[0]);
326 dbg("got prefix: $call = $nout[0]") if isdbg('prefix');
328 lru_put($call, \@nout);
337 L1: for ($n = 0; $n < @parts; $n++) {
340 for ($i = $k = 0; $i < @parts; $i++) {
341 next if $checked[$i];
343 if (!$sp || length $p < length $sp) {
344 dbg("try part: $p") if isdbg('prefix');
350 $sp =~ s/-\d+$//; # remove any SSID
352 # now start to resolve it from the right hand end
353 @nout = matchprefix($sp);
355 # try and search for it in the descriptions as
356 # a whole callsign if it has multiple parts and the output
357 # is more two long, this should catch things like
358 # FR5DX/T without having to explicitly stick it into
363 $parts[$k] = $nout[0];
364 my $try = join('/', @parts);
366 if (isdbg('prefix')) {
367 my $part = $try[0] || "*";
368 $part .= '*' unless $part eq '*' || $part eq $try;
369 dbg("Compound prefix: $try $part" );
371 if (@try && $try eq $try[0]) {
373 lru_put($call, \@try);
377 lru_put($call, \@nout);
382 lru_put($call, \@nout);
390 @nout = matchprefix('Q');
392 lru_put($call, \@nout);
396 if (isdbg('prefixdata')) {
397 my $dd = new Data::Dumper([ \@out ], [qw(@out)]);
404 # turn a list of prefixes / dxcc numbers into a list of dxcc/itu/zone numbers
418 if ($cmd ne 'ns' && $v =~ /^\d+$/) {
419 push @out, $v unless grep $_ eq $v, @out;
421 if ($cmd eq 'ns' && $v =~ /^[A-Z][A-Z]$/i) {
422 push @out, uc $v unless grep $_ eq uc $v, @out;
424 my @pre = Prefix::extract($v);
427 foreach my $p (@pre) {
428 my $n = $p->dxcc if $cmd eq 'nc' ;
429 $n = $p->itu if $cmd eq 'ni' ;
430 $n = $p->cq if $cmd eq 'nz' ;
431 $n = $p->state if $cmd eq 'ns';
432 push @out, $n unless grep $_ eq $n, @out;
442 lat => '0,Latitude,slat',
443 long => '0,Longitude,slong',
450 utcoff => '0,UTC offset',
451 cont => '0,Continent',
458 my $name = $AUTOLOAD;
460 return if $name =~ /::DESTROY$/;
463 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
464 # this clever line of code creates a subroutine which takes over from autoload
465 # from OO Perl - Conway
466 *$AUTOLOAD = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}} ;
467 &$AUTOLOAD($self, @_);
471 # return a prompt for a field
476 my ($self, $ele) = @_;