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 return "US Database loaded";
35 return "US Database not loaded";
40 return unless $present;
47 return () unless $present;
48 my $ctyn = $db{$_[0]};
49 my @s = split /\|/, $db{$ctyn} if $ctyn;
55 return () unless $present;
57 return @s ? $s[1] : undef;
62 return () unless $present;
64 return @s ? $s[0] : undef;
68 # load in / update an existing DB with a standard format (GZIPPED)
71 # Note that this removes and overwrites the existing DB file
72 # You will need to init again after doing this
77 return "Need a filename" unless @_;
79 # create the new output file
80 my $a = new DB_File::BTREEINFO;
81 $a->{psize} = 4096 * 2;
89 if ($s > 1024 * 1024) {
90 $a->{cachesize} = int($s / (1024*1024)) * 3 * 1024 * 1024;
93 # print "cache size " . $a->{cachesize} . "\n";
97 copy($dbfn, "$dbfn.new") or return "cannot copy $dbfn -> $dbfn.new $!";
100 tie %dbn, 'DB_File', "$dbfn.new", O_RDWR|O_CREAT, 0664, $a or return "cannot tie $dbfn.new $!";
102 # now write away all the files
107 # conditionally handle compressed files (don't cha just lurv live code, this is
108 # a rave from the grave and is "in memoriam Flossie" the ICT 1301G I learnt on.
109 # {for pedant computer historians a 1301G is an ICT 1301A that has been
110 # Galdorised[tm] (for instance had decent IOs and a 24 pre-modify instruction)}
112 if ($nfn =~ /.gz$/i) {
114 eval qq{use Compress::Zlib; \$gz = gzopen(\$ofn, "rb")};
115 return "Cannot read compressed files $@" if $@;
117 my $of = new IO::File ">$nfn" or return "Cannot write to $nfn $!";
119 $of->write($buf, $l) while ($l = $gz->gzread($buf));
125 my $of = new IO::File "$ofn" or return "Cannot read $ofn $!";
130 my ($call, $city, $state) = split /\|/, $l;
133 my $s = "$city|$state";
136 my $no = $dbn{'##'} || 1;
151 rename "$dbfn.new", $dbfn;
152 return "$count records";