4 # Copyright (c) - Dirk Koopman G1TLH
19 use vars qw($db %prefix_loc %pre);
21 $db = undef; # the DB_File handle
22 %prefix_loc = (); # the meat of the info
23 %pre = (); # the prefix list
33 $db = tie(%pre, "DB_File", undef, O_RDWR|O_CREAT, 0666, $DB_BTREE) or confess "can't tie \%pre ($!)";
35 do "$main::data/prefix_data.pl" if !$out;
37 # print Data::Dumper->Dump([\%pre, \%prefix_loc], [qw(pre prefix_loc)]);
44 my $fh = new IO::File;
45 my $fn = "$main::data/prefix_data.pl";
47 confess "Prefix system not started" if !$db;
50 rename "$fn.oooo", "$fn.ooooo" if -e "$fn.oooo";
51 rename "$fn.ooo", "$fn.oooo" if -e "$fn.ooo";
52 rename "$fn.oo", "$fn.ooo" if -e "$fn.oo";
53 rename "$fn.o", "$fn.oo" if -e "$fn.o";
54 rename "$fn", "$fn.o" if -e "$fn";
56 $fh->open(">$fn") or die "Can't open $fn ($!)";
58 # prefix location data
59 $fh->print("%prefix_loc = (\n");
60 foreach $l (sort {$a <=> $b} keys %prefix_loc) {
61 my $r = $prefix_loc{$l};
62 $fh->printf(" $l => bless( { name => '%s', dxcc => %d, itu => %d, utcoff => %d, lat => %f, long => %f }, 'Prefix'),\n",
63 $r->{name}, $r->{dxcc}, $r->{itu}, $r->{cq}, $r->{utcoff}, $r->{lat}, $r->{long});
68 $fh->print("%pre = (\n");
69 foreach $k (sort keys %pre) {
70 $fh->print(" '$k' => [");
71 my @list = @{$pre{$k}};
78 $fh->print("$str ],\n");
85 # what you get is a list that looks like:-
87 # prefix => @list of blessed references to prefix_locs
89 # This routine will only do what you ask for, if you wish to be intelligent
90 # then that is YOUR problem!
101 return () if $db->seq($gotkey, $ref, R_CURSOR);
102 return () if $key ne substr $gotkey, 0, length $key;
104 @outref = map { $prefix_loc{$_} } split ',', $ref;
105 return ($gotkey, @outref);
109 # get the next key that matches, this assumes that you have done a 'get' first
120 return () if $db->seq($gotkey, $ref, R_NEXT);
121 return () if $key ne substr $gotkey, 0, length $key;
123 @outref = map { $prefix_loc{$_} } split ',', $ref;
124 return ($gotkey, @outref);
128 # extract a 'prefix' from a callsign, in other words the largest entity that will
129 # obtain a result from the prefix table.
131 # This is done by repeated probing, callsigns of the type VO1/G1TLH or
132 # G1TLH/VO1 (should) return VO1
144 # first check if the whole thing succeeds
146 return @out if @out > 0 && $out[0] eq $call;
148 # now split the call into parts if required
149 @parts = ($call =~ '/') ? split('/', $call) : ($call);
151 # remove any /0-9 /P /A /M /MM /AM suffixes etc
154 shift @parts if $p =~ /^(WEB|NET)$/o;
155 $p = $parts[$#parts];
156 pop @parts if $p =~ /^(\d+|[PABM]|AM|MM|BCN|SIX|WEB|NET|Q\w+)$/o;
157 $p = $parts[$#parts];
158 pop @parts if $p =~ /^(\d+|[PABM]|AM|MM|BCN|SIX|WEB|NET|Q\w+)$/o;
160 # can we resolve them by direct lookup
161 foreach $p (@parts) {
163 return @out if @out > 0 && $out[0] eq $call;
167 # which is the shortest part (first if equal)?
169 foreach $p (@parts) {
170 $sp = $p if length $sp > length $p;
172 # now start to resolve it from the left hand end
173 for (@out = (), $i = 1; $i <= length $sp; ++$i) {
174 @nout = get(substr($sp, 0, $i));
175 last if @nout > 0 && $nout[0] gt $sp;
181 return (@out > 0) ? @out : ();
185 lat => '0,Latitude,slat',
186 long => '0,Longitude,slong',
191 utcoff => '0,UTC offset',
198 my $name = $AUTOLOAD;
200 return if $name =~ /::DESTROY$/;
203 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
205 $self->{$name} = shift;
207 return $self->{$name};
212 # return a prompt for a field
217 my ($self, $ele) = @_;