2 # Package to handle US Callsign -> City, State translations
4 # Copyright (c) 2002 Dirk Koopman G1TLH
18 use vars qw($VERSION $BRANCH);
19 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
20 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/ || (0,0));
21 $main::build += $VERSION;
22 $main::branch += $BRANCH;
24 use vars qw(%db $present $dbfn);
26 $dbfn = "$main::data/usdb.v1";
31 if (tie %db, 'DB_File', $dbfn, O_RDONLY, 0664, $DB_BTREE) {
33 dbg("US Database loaded");
35 dbg("US Database not loaded");
41 return unless $present;
48 return () unless $present;
49 my $ctyn = $db{$_[0]};
50 my @s = split /\|/, $db{$ctyn} if $ctyn;
56 return () unless $present;
58 return @s ? $s[1] : undef;
63 return () unless $present;
65 return @s ? $s[0] : undef;
69 # load in / update an existing DB with a standard format (GZIPPED)
72 # Note that this removes and overwrites the existing DB file
73 # You will need to init again after doing this
78 return "Need a filename" unless @_;
80 # create the new output file
81 my $a = new DB_File::BTREEINFO;
82 $a->{psize} = 4096 * 2;
90 if ($s > 1024 * 1024) {
91 $a->{cachesize} = int($s / (1024*1024)) * 3 * 1024 * 1024;
94 # print "cache size " . $a->{cachesize} . "\n";
98 copy($dbfn, "$dbfn.new") or return "cannot copy $dbfn -> $dbfn.new $!";
101 tie %dbn, 'DB_File', "$dbfn.new", O_RDWR|O_CREAT, 0664, $a or return "cannot tie $dbfn.new $!";
103 # now write away all the files
106 my $if = gzopen($fn, "r") or return "Cannot open $fn $!";
108 my $of = new IO::File "+>$ofn" or return "Cannot open $ofn $!";
110 while ($l = $if->gzread($buf)) {
111 $of->write($buf, $l);
116 while ($of->getline()) {
118 my ($call, $city, $state) = split /\|/, $l;
121 my $s = "$city|$state";
124 my $no = $dbn{'##'} || 1;
138 rename "$dbfn.new", $dbfn;