4 # Copyright (c) - Dirk Koopman G1TLH
18 use vars qw($db %prefix_loc %pre);
20 $db = undef; # the DB_File handle
21 %prefix_loc = (); # the meat of the info
22 %pre = (); # the prefix list
32 $db = tie(%pre, "DB_File", undef, O_RDWR|O_CREAT, 0666, $DB_BTREE) or confess "can't tie \%pre ($!)";
34 do "$main::data/prefix_data.pl" if !$out;
36 # print Data::Dumper->Dump([\%pre, \%prefix_loc], [qw(pre prefix_loc)]);
43 my $fh = new FileHandle;
44 my $fn = "$main::data/prefix_data.pl";
46 confess "Prefix system not started" if !$db;
49 rename "$fn.oooo", "$fn.ooooo" if -e "$fn.oooo";
50 rename "$fn.ooo", "$fn.oooo" if -e "$fn.ooo";
51 rename "$fn.oo", "$fn.ooo" if -e "$fn.oo";
52 rename "$fn.o", "$fn.oo" if -e "$fn.o";
53 rename "$fn", "$fn.o" if -e "$fn";
55 $fh->open(">$fn") or die "Can't open $fn ($!)";
57 # prefix location data
58 $fh->print("%prefix_loc = (\n");
59 foreach $l (sort {$a <=> $b} keys %prefix_loc) {
60 my $r = $prefix_loc{$l};
61 $fh->printf(" $l => bless( { name => '%s', dxcc => %d, itu => %d, utcoff => %d, lat => %f, long => %f }, 'Prefix'),\n",
62 $r->{name}, $r->{dxcc}, $r->{itu}, $r->{cq}, $r->{utcoff}, $r->{lat}, $r->{long});
67 $fh->print("%pre = (\n");
68 foreach $k (sort keys %pre) {
69 $fh->print(" '$k' => [");
70 my @list = @{$pre{$k}};
77 $fh->print("$str ],\n");
84 # what you get is a list that looks like:-
86 # prefix => @list of blessed references to prefix_locs
88 # This routine will only do what you ask for, if you wish to be intelligent
89 # then that is YOUR problem!
100 return () if $db->seq($gotkey, $ref, R_CURSOR);
101 return () if $key ne substr $gotkey, 0, length $key;
103 @outref = map { $prefix_loc{$_} } split ',', $ref;
104 return ($gotkey, @outref);
108 # get the next key that matches, this assumes that you have done a 'get' first
119 return () if $db->seq($gotkey, $ref, R_NEXT);
120 return () if $key ne substr $gotkey, 0, length $key;
122 @outref = map { $prefix_loc{$_} } split ',', $ref;
123 return ($gotkey, @outref);
127 # extract a 'prefix' from a callsign, in other words the largest entity that will
128 # obtain a result from the prefix table.
130 # This is done by repeated probing, callsigns of the type VO1/G1TLH or
131 # G1TLH/VO1 (should) return VO1
143 # first check if the whole thing succeeds
145 return @out if @out > 0 && $out[0] eq $call;
147 # now split the call into parts if required
148 @parts = ($call =~ '/') ? split('/', $call) : ($call);
150 # remove any /0-9 /P /A /M /MM /AM suffixes etc
152 $p = $parts[$#parts];
153 pop @parts if $p =~ /^(\d+|[PABM]|AM|MM|BCN|SIX|Q\w+)$/o;
154 $p = $parts[$#parts];
155 pop @parts if $p =~ /^(\d+|[PABM]|AM|MM|BCN|SIX|Q\w+)$/o;
157 # can we resolve them by direct lookup
158 foreach $p (@parts) {
160 return @out if @out > 0 && $out[0] eq $call;
164 # which is the shortest part (first if equal)?
166 foreach $p (@parts) {
167 $sp = $p if length $sp > length $p;
169 # now start to resolve it from the left hand end
170 for (@out = (), $i = 1; $i <= length $sp; ++$i) {
171 @nout = get(substr($sp, 0, $i));
172 last if @nout > 0 && $nout[0] gt $sp;
178 return (@out > 0) ? @out : ();
182 lat => '0,Latitude,slat',
183 long => '0,Longitude,slong',
188 utcoff => '0,UTC offset',
195 my $name = $AUTOLOAD;
197 return if $name =~ /::DESTROY$/;
200 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
202 $self->{$name} = shift;
204 return $self->{$name};
209 # return a prompt for a field
214 my ($self, $ele) = @_;