allow /J as a prefix
[spider.git] / perl / Prefix.pm
index 01e3079c7387c46b2aa39d010c4d6dd95fea5c12..99fc49fdca07244831332c326ed762251a24ae6e 100644 (file)
@@ -149,9 +149,12 @@ sub extract
        foreach $call (split /,/, $calls) {
                # first check if the whole thing succeeds
                my @nout = get($call);
-               push @out, @nout if @nout;
-               next if @nout > 0 && $nout[0] eq $call;
-         
+               if (@nout && $nout[0] eq $call) {
+                       dbg("got exact prefix: $nout[0]") if isdbg('prefix');
+                       push @out, @nout;
+                       next;
+               }
+
                # now split the call into parts if required
                @parts = ($call =~ '/') ? split('/', $call) : ($call);
 
@@ -160,31 +163,97 @@ sub extract
                        $p = $parts[0];
                        shift @parts if $p =~ /^(WEB|NET)$/o;
                        $p = $parts[$#parts];
-                       pop @parts if $p =~ /^(\d+|[JPABM]|AM|MM|BCN|JOTA|SIX|WEB|NET|Q\w+)$/o;
+                       pop @parts if $p =~ /^(\d+|[PABM]|AM|MM|BCN|JOTA|SIX|WEB|NET|Q\w+)$/o;
                        $p = $parts[$#parts];
-                       pop @parts if $p =~ /^(\d+|[JPABM]|AM|MM|BCN|JOTA|SIX|WEB|NET|Q\w+)$/o;
+                       pop @parts if $p =~ /^(\d+|[PABM]|AM|MM|BCN|JOTA|SIX|WEB|NET|Q\w+)$/o;
          
                        # can we resolve them by direct lookup
                        foreach $p (@parts) {
                                @nout = get($p);
-                               push @out, @nout if @nout;
-                               next if @nout > 0 && $nout[0] eq $call;
+                               if (@nout && $nout[0] eq $call) {
+                                       dbg("got exact prefix: $nout[0]") if isdbg('prefix');
+                                       push @out, @nout;
+                                       next;
+                               }
                        }
                }
   
                # which is the shortest part (first if equal)?
-               $sp = $parts[0];
-               foreach $p (@parts) {
-                       $sp = $p if length $sp > length $p;
-               }
-               # now start to resolve it from the left hand end
-               for ($i = 1; $i <= length $sp; ++$i) {
-                       my @wout = get(substr($sp, 0, $i));
-                       last if @wout > 0 && $wout[0] gt $sp;
-                       last if @wout == 0;
-                       push @out, @wout;
+               dbg("Parts: $call = " . join(' ', @parts))      if isdbg('prefix');
+               
+               # try ALL the parts
+        my @checked;
+               my $n;
+L1:            for ($n = 0; $n < @parts; $n++) {
+                       my $sp = '';
+                       my ($k, $i);
+                       for ($i = $k = 0; $i < @parts; $i++) {
+                               next if $checked[$i];
+                               my $p = $parts[$i];
+                               if (!$sp || length $p < length $sp) {
+                                       dbg("try part: $p") if isdbg('prefix');
+                                       $k = $i;
+                                       $sp = $p;
+                               }
+                       }
+                       $checked[$k] = 1;
+                       $sp =~ s/-\d+$//;     # remove any SSID
+                       
+                       #               # now start to resolve it from the left hand end
+                       #               for ($i = 1; $i <= length $sp; ++$i) {
+                       # now start to resolve it from the right hand end
+                       for ($i = length $sp; $i >= 1; --$i) {
+                               my $ssp = substr($sp, 0, $i);
+                               my @wout = get($ssp);
+                               if (isdbg('prefix')) {
+                                       my $part = $wout[0] || "*";
+                                       $part .= '*' unless $part eq '*' || $part eq $ssp;
+                                       dbg("Partial prefix: $sp $ssp $part" );
+                               } 
+                               next if @wout > 0 && $wout[0] gt $ssp;
+
+                               # try and search for it in the descriptions as
+                               # a whole callsign if it has multiple parts and the output
+                               # is more two long, this should catch things like
+                               # FR5DX/T without having to explicitly stick it into
+                               # the prefix table.
+
+                               if (@wout) {
+                                       if (@parts > 1) {
+                                               $parts[$k] = $ssp;
+                                               my $try = join('/', @parts);
+                                               my @try = get($try);
+                                               if (isdbg('prefix')) {
+                                                       my $part = $try[0] || "*";
+                                                       $part .= '*' unless $part eq '*' || $part eq $try;
+                                                       dbg("Compound prefix: $try $part" );
+                                               }
+                                               if (@try == 0) {
+                                                       $try = join('/', reverse @parts);
+                                                       @try = get($try);
+                                                       if (isdbg('prefix')) {
+                                                               my $part = $try[0] || "*";
+                                                               $part .= '*' unless $part eq '*' || $part eq $try;
+                                                               dbg("Compound prefix: $try $part" );
+                                                       }
+                                               }
+                                               if (@try && $try eq $try[0]) {
+                                                       push @out, @try;
+                                               } else {
+                                                       push @out, @wout;
+                                               }
+                                       } else {
+                                               push @out, @wout;
+                                       }
+                                       last L1;
+                               }
+                       }
                }
        }
+       if (isdbg('prefix')) {
+               my $dd = new Data::Dumper([ \@out ], [qw(@out)]);
+               dbg($dd->Dumpxs);
+       }
        return @out;
 }