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);
29 $db = undef; # the DB_File handle
30 %prefix_loc = (); # the meat of the info
31 %pre = (); # the prefix list
41 $db = tie(%pre, "DB_File", undef, O_RDWR|O_CREAT, 0666, $DB_BTREE) or confess "can't tie \%pre ($!)";
43 do "$main::data/prefix_data.pl" if !$out;
45 # print Data::Dumper->Dump([\%pre, \%prefix_loc], [qw(pre prefix_loc)]);
52 my $fh = new IO::File;
53 my $fn = "$main::data/prefix_data.pl";
55 confess "Prefix system not started" if !$db;
58 rename "$fn.oooo", "$fn.ooooo" if -e "$fn.oooo";
59 rename "$fn.ooo", "$fn.oooo" if -e "$fn.ooo";
60 rename "$fn.oo", "$fn.ooo" if -e "$fn.oo";
61 rename "$fn.o", "$fn.oo" if -e "$fn.o";
62 rename "$fn", "$fn.o" if -e "$fn";
64 $fh->open(">$fn") or die "Can't open $fn ($!)";
66 # prefix location data
67 $fh->print("%prefix_loc = (\n");
68 foreach $l (sort {$a <=> $b} keys %prefix_loc) {
69 my $r = $prefix_loc{$l};
70 $fh->printf(" $l => bless( { name => '%s', dxcc => %d, itu => %d, utcoff => %d, lat => %f, long => %f }, 'Prefix'),\n",
71 $r->{name}, $r->{dxcc}, $r->{itu}, $r->{cq}, $r->{utcoff}, $r->{lat}, $r->{long});
76 $fh->print("%pre = (\n");
77 foreach $k (sort keys %pre) {
78 $fh->print(" '$k' => [");
79 my @list = @{$pre{$k}};
86 $fh->print("$str ],\n");
93 # what you get is a list that looks like:-
95 # prefix => @list of blessed references to prefix_locs
97 # This routine will only do what you ask for, if you wish to be intelligent
98 # 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 @outref = map { $prefix_loc{$_} } split ',', $ref;
113 return ($gotkey, @outref);
117 # get the next key that matches, this assumes that you have done a 'get' first
128 return () if $db->seq($gotkey, $ref, R_NEXT);
129 return () if $key ne substr $gotkey, 0, length $key;
131 @outref = map { $prefix_loc{$_} } split ',', $ref;
132 return ($gotkey, @outref);
136 # search for the nearest match of a prefix string (starting
137 # from the RH end of the string passed)
144 for (my $i = length $pref; $i; $i--) {
145 my $s = substr($pref, 0, $i);
147 if (isdbg('prefix')) {
148 my $part = $out[0] || "*";
149 $part .= '*' unless $part eq '*' || $part eq $s;
150 dbg("Partial prefix: $pref $s $part" );
152 return @out if @out && $out[0] eq $s;
158 # extract a 'prefix' from a callsign, in other words the largest entity that will
159 # obtain a result from the prefix table.
161 # This is done by repeated probing, callsigns of the type VO1/G1TLH or
162 # G1TLH/VO1 (should) return VO1
167 my $calls = uc shift;
173 LM: foreach $call (split /,/, $calls) {
174 # first check if the whole thing succeeds
175 my @nout = get($call);
176 if (@nout && $nout[0] eq $call) {
177 dbg("got exact prefix: $nout[0]") if isdbg('prefix');
182 # now split the call into parts if required
183 @parts = ($call =~ '/') ? split('/', $call) : ($call);
184 dbg("Parts: $call = " . join(' ', @parts)) if isdbg('prefix');
186 # remove any /0-9 /P /A /M /MM /AM suffixes etc
188 @parts = grep { !/^\d+$/ && !/^[PABM]$/ && !/^(?:|AM|MM|BCN|JOTA|SIX|WEB|NET|Q\w+)$/; } @parts;
190 # can we resolve them by direct lookup
191 my $s = join('/', @parts);
193 if (@nout && $nout[0] eq $s) {
194 dbg("got exact multipart prefix: $call $s") if isdbg('prefix');
199 dbg("Parts now: $call = " . join(' ', @parts)) if isdbg('prefix');
201 # at this point we should have two or three parts
202 # if it is three parts then join the first and last parts together
205 # first deal with prefix/x00xx/single letter things
206 if (@parts == 3 && length $parts[0] <= length $parts[1]) {
207 @nout = matchprefix($parts[0]);
209 my $s = join('/', $nout[0], $parts[2]);
211 if (@try && $try[0] eq $s) {
212 dbg("got 3 part prefix: $call $s") if isdbg('prefix');
217 # if the second part is a callsign and the last part is one letter
218 if (is_callsign($parts[1]) && length $parts[2] == 1) {
224 # if it is a two parter
227 # try it as it is as compound, taking the first part as the prefix
228 @nout = matchprefix($parts[0]);
230 my $s = join('/', $nout[0], $parts[1]);
232 if (@try && $try[0] eq $s) {
233 dbg("got 2 part prefix: $call $s") if isdbg('prefix');
240 # remove the problematic /J suffix
241 pop @parts if @parts > 1 && $parts[$#parts] eq 'J';
245 @nout = matchprefix($parts[0]);
247 dbg("got prefix: $call ]") if isdbg('prefix');
256 L1: for ($n = 0; $n < @parts; $n++) {
259 for ($i = $k = 0; $i < @parts; $i++) {
260 next if $checked[$i];
262 if (!$sp || length $p < length $sp) {
263 dbg("try part: $p") if isdbg('prefix');
269 $sp =~ s/-\d+$//; # remove any SSID
271 # now start to resolve it from the right hand end
272 @nout = matchprefix($sp);
274 # try and search for it in the descriptions as
275 # a whole callsign if it has multiple parts and the output
276 # is more two long, this should catch things like
277 # FR5DX/T without having to explicitly stick it into
282 $parts[$k] = $nout[0];
283 my $try = join('/', @parts);
285 if (isdbg('prefix')) {
286 my $part = $try[0] || "*";
287 $part .= '*' unless $part eq '*' || $part eq $try;
288 dbg("Compound prefix: $try $part" );
290 if (@try && $try eq $try[0]) {
303 push @out, matchprefix('Q');
306 if (isdbg('prefix')) {
307 my $dd = new Data::Dumper([ \@out ], [qw(@out)]);
314 lat => '0,Latitude,slat',
315 long => '0,Longitude,slong',
320 utcoff => '0,UTC offset',
321 cont => '0,Continent',
328 my $name = $AUTOLOAD;
330 return if $name =~ /::DESTROY$/;
333 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
334 # this clever line of code creates a subroutine which takes over from autoload
335 # from OO Perl - Conway
336 *{$AUTOLOAD} = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}} ;
338 $self->{$name} = shift;
340 return $self->{$name};
345 # return a prompt for a field
350 my ($self, $ele) = @_;