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 $lru $lrusize $misses $hits $matchtotal);
29 $db = undef; # the DB_File handle
30 %prefix_loc = (); # the meat of the info
31 %pre = (); # the prefix list
32 $hits = $misses = $matchtotal = 1; # cache stats
33 $lrusize = 2000; # size of prefix LRU cache
35 $lru = LRU->newbase('Prefix', $lrusize);
47 # tie the main prefix database
48 $db = tie(%pre, "DB_File", undef, O_RDWR|O_CREAT, 0664, $DB_BTREE) or confess "can't tie \%pre ($!)";
50 do "$main::data/prefix_data.pl" if !$out;
59 my $fh = new IO::File;
60 my $fn = "$main::data/prefix_data.pl";
62 confess "Prefix system not started" if !$db;
65 rename "$fn.oooo", "$fn.ooooo" if -e "$fn.oooo";
66 rename "$fn.ooo", "$fn.oooo" if -e "$fn.ooo";
67 rename "$fn.oo", "$fn.ooo" if -e "$fn.oo";
68 rename "$fn.o", "$fn.oo" if -e "$fn.o";
69 rename "$fn", "$fn.o" if -e "$fn";
71 $fh->open(">$fn") or die "Can't open $fn ($!)";
73 # prefix location data
74 $fh->print("%prefix_loc = (\n");
75 foreach $l (sort {$a <=> $b} keys %prefix_loc) {
76 my $r = $prefix_loc{$l};
77 $fh->printf(" $l => bless( { name => '%s', dxcc => %d, itu => %d, utcoff => %d, lat => %f, long => %f }, 'Prefix'),\n",
78 $r->{name}, $r->{dxcc}, $r->{itu}, $r->{cq}, $r->{utcoff}, $r->{lat}, $r->{long});
83 $fh->print("%pre = (\n");
84 foreach $k (sort keys %pre) {
85 $fh->print(" '$k' => [");
86 my @list = @{$pre{$k}};
93 $fh->print("$str ],\n");
100 # what you get is a list that looks like:-
102 # prefix => @list of blessed references to prefix_locs
104 # This routine will only do what you ask for, if you wish to be intelligent
105 # then that is YOUR problem!
113 return () if $db->seq($gotkey, $ref, R_CURSOR);
114 return () if $key ne substr $gotkey, 0, length $key;
116 return ($gotkey, map { $prefix_loc{$_} } split ',', $ref);
120 # get the next key that matches, this assumes that you have done a 'get' first
129 return () if $db->seq($gotkey, $ref, R_NEXT);
130 return () if $key ne substr $gotkey, 0, length $key;
132 return ($gotkey, map { $prefix_loc{$_} } split ',', $ref);
136 # search for the nearest match of a prefix string (starting
137 # from the RH end of the string passed)
145 for (my $i = length $pref; $i; $i--) {
147 my $s = substr($pref, 0, $i);
149 my $p = $lru->get($s);
152 if (isdbg('prefix')) {
153 my $percent = sprintf "%.1f", $hits * 100 / $misses;
154 dbg("Partial Prefix Cache Hit: $s Hits: $hits/$misses of $matchtotal = $percent\%");
156 $lru->put($_, $p) for @partials;
161 if (isdbg('prefix')) {
162 my $part = $out[0] || "*";
163 $part .= '*' unless $part eq '*' || $part eq $s;
164 dbg("Partial prefix: $pref $s $part" );
166 if (@out && $out[0] eq $s) {
167 $lru->put($_, \@out) for @partials;
176 # extract a 'prefix' from a callsign, in other words the largest entity that will
177 # obtain a result from the prefix table.
179 # This is done by repeated probing, callsigns of the type VO1/G1TLH or
180 # G1TLH/VO1 (should) return VO1
185 my $calls = uc shift;
191 LM: foreach $call (split /,/, $calls) {
193 # first check if the whole thing succeeds either because it is cached
194 # or because it simply is a stored prefix as callsign (or even a prefix)
196 $call =~ s/-\d+$//; # ignore SSIDs
197 my $p = $lru->get($call);
201 if (isdbg('prefix')) {
202 my $percent = sprintf "%.1f", $hits * 100 / $misses;
203 dbg("Prefix Cache Hit: $call Hits: $hits/$misses of $matchtotal = $percent\%");
209 if (@nout && $nout[0] eq $call) {
211 $lru->put($call, \@nout);
212 dbg("got exact prefix: $nout[0]") if isdbg('prefix');
218 # now split the call into parts if required
219 @parts = ($call =~ '/') ? split('/', $call) : ($call);
220 dbg("Parts: $call = " . join(' ', @parts)) if isdbg('prefix');
222 # remove any /0-9 /P /A /M /MM /AM suffixes etc
224 @parts = grep { !/^\d+$/ && !/^[PABM]$/ && !/^(?:|AM|MM|BCN|JOTA|SIX|WEB|NET|Q\w+)$/; } @parts;
226 # can we resolve them by direct lookup
227 my $s = join('/', @parts);
229 if (@nout && $nout[0] eq $s) {
230 dbg("got exact multipart prefix: $call $s") if isdbg('prefix');
232 $lru->put($call, \@nout);
237 dbg("Parts now: $call = " . join(' ', @parts)) if isdbg('prefix');
239 # at this point we should have two or three parts
240 # if it is three parts then join the first and last parts together
243 # first deal with prefix/x00xx/single letter things
244 if (@parts == 3 && length $parts[0] <= length $parts[1]) {
245 @nout = matchprefix($parts[0]);
247 my $s = join('/', $nout[0], $parts[2]);
249 if (@try && $try[0] eq $s) {
250 dbg("got 3 part prefix: $call $s") if isdbg('prefix');
252 $lru->put($call, \@try);
257 # if the second part is a callsign and the last part is one letter
258 if (is_callsign($parts[1]) && length $parts[2] == 1) {
264 # if it is a two parter
267 # try it as it is as compound, taking the first part as the prefix
268 @nout = matchprefix($parts[0]);
270 my $s = join('/', $nout[0], $parts[1]);
272 if (@try && $try[0] eq $s) {
273 dbg("got 2 part prefix: $call $s") if isdbg('prefix');
275 $lru->put($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');
291 $lru->put($call, \@nout);
300 L1: for ($n = 0; $n < @parts; $n++) {
303 for ($i = $k = 0; $i < @parts; $i++) {
304 next if $checked[$i];
306 if (!$sp || length $p < length $sp) {
307 dbg("try part: $p") if isdbg('prefix');
313 $sp =~ s/-\d+$//; # remove any SSID
315 # now start to resolve it from the right hand end
316 @nout = matchprefix($sp);
318 # try and search for it in the descriptions as
319 # a whole callsign if it has multiple parts and the output
320 # is more two long, this should catch things like
321 # FR5DX/T without having to explicitly stick it into
326 $parts[$k] = $nout[0];
327 my $try = join('/', @parts);
329 if (isdbg('prefix')) {
330 my $part = $try[0] || "*";
331 $part .= '*' unless $part eq '*' || $part eq $try;
332 dbg("Compound prefix: $try $part" );
334 if (@try && $try eq $try[0]) {
336 $lru->put($call, \@try);
340 $lru->put($call, \@nout);
345 $lru->put($call, \@nout);
353 @nout = matchprefix('Q');
355 $lru->put($call, \@nout);
359 if (isdbg('prefixdata')) {
360 my $dd = new Data::Dumper([ \@out ], [qw(@out)]);
367 lat => '0,Latitude,slat',
368 long => '0,Longitude,slong',
373 utcoff => '0,UTC offset',
374 cont => '0,Continent',
381 my $name = $AUTOLOAD;
383 return if $name =~ /::DESTROY$/;
386 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
387 # this clever line of code creates a subroutine which takes over from autoload
388 # from OO Perl - Conway
389 *{$AUTOLOAD} = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}} ;
391 $self->{$name} = shift;
393 return $self->{$name};
398 # return a prompt for a field
403 my ($self, $ele) = @_;