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
108 # conditionally handle compressed files (don't cha just lurv live code, this is
109 # a rave from the grave and is "in memoriam Flossie" the ICT 1301G I learnt on.
110 # {for pedant computer historians a 1301G is an ICT 1301A that has been
111 # Galdorised[tm] (for instance had decent IOs and a 24 pre-modify instruction)}
113 if ($nfn =~ /.gz$/i) {
115 eval qq{use Compress::Zlib; \$gz = gzopen(\$ofn, "rb")};
116 return "Cannot read compressed files $@" if $@;
118 my $of = new IO::File ">$nfn" or return "Cannot write to $nfn $!";
120 $of->write($buf, $l) while ($l = $gz->gzread($buf));
126 my $of = new IO::File "$ofn" or return "Cannot read $ofn $!";
131 my ($call, $city, $state) = split /\|/, $l;
134 my $s = "$city|$state";
137 my $no = $dbn{'##'} || 1;
152 rename "$dbfn.new", $dbfn;
153 return "$count records";