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
41 # fix up the node's default country codes
42 push @main::my_cc, (61..67) if !@main::my_cc && $main::mycall =~ /^GB/;
43 push @main::my_cc, qw(EA EA6 EA8 EA9) if !@main::my_cc && $main::mycall =~ /^E(ABCD)/;
44 push @main::my_cc, $main::mycall unless @main::my_cc;
51 my @dxcc = extract($_);
52 push @c, $dxcc[1]->dxcc if @dxcc > 1;
55 return "\@main::my_cc does not contain a valid prefix or callsign (" . join(',', @main::my_cc) . ")" unless @c;
72 # tie the main prefix database
73 $db = tie(%pre, "DB_File", undef, O_RDWR|O_CREAT, 0664, $DB_BTREE) or confess "can't tie \%pre ($!)";
75 do "$main::data/prefix_data.pl" if !$out;
77 $lru = LRU->newbase('Prefix', $lrusize);
85 my $fh = new IO::File;
86 my $fn = "$main::data/prefix_data.pl";
88 confess "Prefix system not started" if !$db;
91 rename "$fn.oooo", "$fn.ooooo" if -e "$fn.oooo";
92 rename "$fn.ooo", "$fn.oooo" if -e "$fn.ooo";
93 rename "$fn.oo", "$fn.ooo" if -e "$fn.oo";
94 rename "$fn.o", "$fn.oo" if -e "$fn.o";
95 rename "$fn", "$fn.o" if -e "$fn";
97 $fh->open(">$fn") or die "Can't open $fn ($!)";
99 # prefix location data
100 $fh->print("%prefix_loc = (\n");
101 foreach $l (sort {$a <=> $b} keys %prefix_loc) {
102 my $r = $prefix_loc{$l};
103 $fh->printf(" $l => bless( { name => '%s', dxcc => %d, itu => %d, utcoff => %d, lat => %f, long => %f }, 'Prefix'),\n",
104 $r->{name}, $r->{dxcc}, $r->{itu}, $r->{cq}, $r->{utcoff}, $r->{lat}, $r->{long});
106 $fh->print(");\n\n");
109 $fh->print("%pre = (\n");
110 foreach $k (sort keys %pre) {
111 $fh->print(" '$k' => [");
112 my @list = @{$pre{$k}};
119 $fh->print("$str ],\n");
126 # what you get is a list that looks like:-
128 # prefix => @list of blessed references to prefix_locs
130 # This routine will only do what you ask for, if you wish to be intelligent
131 # then that is YOUR problem!
139 return () if $db->seq($gotkey, $ref, R_CURSOR);
140 return () if $key ne substr $gotkey, 0, length $key;
142 return ($gotkey, map { $prefix_loc{$_} } split ',', $ref);
146 # get the next key that matches, this assumes that you have done a 'get' first
155 return () if $db->seq($gotkey, $ref, R_NEXT);
156 return () if $key ne substr $gotkey, 0, length $key;
158 return ($gotkey, map { $prefix_loc{$_} } split ',', $ref);
162 # put the key LRU incluing the city state info
167 my ($call, $ref) = @_;
168 my @s = USDB::get($call);
171 # this is deep magic, because this is a reference to static data, it
173 my $h = { %{$ref->[1]} };
174 bless $h, ref $ref->[1];
179 $ref->[1]->{city} = $ref->[1]->{state} = "" unless exists $ref->[1]->{state};
182 dbg("Prefix::lru_put $call -> ($ref->[1]->{city}, $ref->[1]->{state})") if isdbg('prefix');
183 $lru->put($call, $ref);
187 # search for the nearest match of a prefix string (starting
188 # from the RH end of the string passed)
196 for (my $i = length $pref; $i; $i--) {
198 my $s = substr($pref, 0, $i);
200 my $p = $lru->get($s);
203 if (isdbg('prefix')) {
204 my $percent = sprintf "%.1f", $hits * 100 / $misses;
205 dbg("Partial Prefix Cache Hit: $s Hits: $hits/$misses of $matchtotal = $percent\%");
207 lru_put($_, $p) for @partials;
212 if (isdbg('prefix')) {
213 my $part = $out[0] || "*";
214 $part .= '*' unless $part eq '*' || $part eq $s;
215 dbg("Partial prefix: $pref $s $part" );
217 if (@out && $out[0] eq $s) {
226 # extract a 'prefix' from a callsign, in other words the largest entity that will
227 # obtain a result from the prefix table.
229 # This is done by repeated probing, callsigns of the type VO1/G1TLH or
230 # G1TLH/VO1 (should) return VO1
235 my $calls = uc shift;
241 LM: foreach $call (split /,/, $calls) {
243 # first check if the whole thing succeeds either because it is cached
244 # or because it simply is a stored prefix as callsign (or even a prefix)
246 $call =~ s/-\d+$//; # ignore SSIDs
247 my $p = $lru->get($call);
251 if (isdbg('prefix')) {
252 my $percent = sprintf "%.1f", $hits * 100 / $misses;
253 dbg("Prefix Cache Hit: $call Hits: $hits/$misses of $matchtotal = $percent\%");
259 # is it in the USDB, force a matchprefix to match?
260 my @s = USDB::get($call);
263 @nout = matchprefix($call) unless @nout;
264 $nout[0] = $call if @nout;
270 if (@nout && $nout[0] eq $call) {
272 lru_put($call, \@nout);
273 dbg("got exact prefix: $nout[0]") if isdbg('prefix');
279 # now split the call into parts if required
280 @parts = ($call =~ '/') ? split('/', $call) : ($call);
281 dbg("Parts: $call = " . join(' ', @parts)) if isdbg('prefix');
283 # remove any /0-9 /P /A /M /MM /AM suffixes etc
285 @parts = grep { !/^\d+$/ && !/^[PABM]$/ && !/^(?:|AM|MM|BCN|JOTA|SIX|WEB|NET|Q\w+)$/; } @parts;
287 # can we resolve them by direct lookup
288 my $s = join('/', @parts);
290 if (@nout && $nout[0] eq $s) {
291 dbg("got exact multipart prefix: $call $s") if isdbg('prefix');
293 lru_put($call, \@nout);
298 dbg("Parts now: $call = " . join(' ', @parts)) if isdbg('prefix');
300 # at this point we should have two or three parts
301 # if it is three parts then join the first and last parts together
304 # first deal with prefix/x00xx/single letter things
305 if (@parts == 3 && length $parts[0] <= length $parts[1]) {
306 @nout = matchprefix($parts[0]);
308 my $s = join('/', $nout[0], $parts[2]);
310 if (@try && $try[0] eq $s) {
311 dbg("got 3 part prefix: $call $s") if isdbg('prefix');
313 lru_put($call, \@try);
318 # if the second part is a callsign and the last part is one letter
319 if (is_callsign($parts[1]) && length $parts[2] == 1) {
325 # if it is a two parter
328 # try it as it is as compound, taking the first part as the prefix
329 @nout = matchprefix($parts[0]);
331 my $s = join('/', $nout[0], $parts[1]);
333 if (@try && $try[0] eq $s) {
334 dbg("got 2 part prefix: $call $s") if isdbg('prefix');
336 lru_put($call, \@try);
343 # remove the problematic /J suffix
344 pop @parts if @parts > 1 && $parts[$#parts] eq 'J';
348 @nout = matchprefix($parts[0]);
350 dbg("got prefix: $call = $nout[0]") if isdbg('prefix');
352 lru_put($call, \@nout);
361 L1: for ($n = 0; $n < @parts; $n++) {
364 for ($i = $k = 0; $i < @parts; $i++) {
365 next if $checked[$i];
367 if (!$sp || length $p < length $sp) {
368 dbg("try part: $p") if isdbg('prefix');
374 $sp =~ s/-\d+$//; # remove any SSID
376 # now start to resolve it from the right hand end
377 @nout = matchprefix($sp);
379 # try and search for it in the descriptions as
380 # a whole callsign if it has multiple parts and the output
381 # is more two long, this should catch things like
382 # FR5DX/T without having to explicitly stick it into
387 $parts[$k] = $nout[0];
388 my $try = join('/', @parts);
390 if (isdbg('prefix')) {
391 my $part = $try[0] || "*";
392 $part .= '*' unless $part eq '*' || $part eq $try;
393 dbg("Compound prefix: $try $part" );
395 if (@try && $try eq $try[0]) {
397 lru_put($call, \@try);
401 lru_put($call, \@nout);
406 lru_put($call, \@nout);
414 @nout = matchprefix('Q');
416 lru_put($call, \@nout);
420 if (isdbg('prefixdata')) {
421 my $dd = new Data::Dumper([ \@out ], [qw(@out)]);
428 # turn a list of prefixes / dxcc numbers into a list of dxcc/itu/zone numbers
442 if ($cmd ne 'ns' && $v =~ /^\d+$/) {
443 push @out, $v unless grep $_ eq $v, @out;
445 if ($cmd eq 'ns' && $v =~ /^[A-Z][A-Z]$/i) {
446 push @out, uc $v unless grep $_ eq uc $v, @out;
448 my @pre = Prefix::extract($v);
451 foreach my $p (@pre) {
452 my $n = $p->dxcc if $cmd eq 'nc' ;
453 $n = $p->itu if $cmd eq 'ni' ;
454 $n = $p->cq if $cmd eq 'nz' ;
455 $n = $p->state if $cmd eq 'ns';
456 push @out, $n unless grep $_ eq $n, @out;
466 lat => '0,Latitude,slat',
467 long => '0,Longitude,slong',
474 utcoff => '0,UTC offset',
475 cont => '0,Continent',
481 my $name = $AUTOLOAD;
483 return if $name =~ /::DESTROY$/;
486 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
487 # this clever line of code creates a subroutine which takes over from autoload
488 # from OO Perl - Conway
489 *$AUTOLOAD = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}} ;
494 # return a prompt for a field
499 my ($self, $ele) = @_;