2 # Package to handle US Callsign -> City, State translations
4 # Copyright (c) 2002 Dirk Koopman G1TLH
20 use vars qw(%db $present $dbfn);
22 localdata_mv("usdb.v1");
23 $dbfn = localdata("usdb.v1");
28 if (tie %db, 'DB_File', $dbfn, O_RDWR, 0664, $DB_BTREE) {
30 return "US Database loaded";
32 return "US Database not loaded";
37 return unless $present;
44 return () unless $present;
45 my $ctyn = $db{$_[0]};
46 my @s = split /\|/, $db{$ctyn} if $ctyn;
52 my ($db, $call, $city, $state) = @_;
55 my $s = uc "$city|$state";
58 my $no = $db->{'##'} || 1;
65 $db->{uc $call} = $ctyn;
75 return () unless $present;
77 return @s ? $s[1] : undef;
82 return () unless $present;
84 return @s ? $s[0] : undef;
94 # load in / update an existing DB with a standard format (GZIPPED)
97 # Note that this removes and overwrites the existing DB file
98 # You will need to init again after doing this
103 return "Need a filename" unless @_;
105 # create the new output file
106 my $a = new DB_File::BTREEINFO;
107 $a->{psize} = 4096 * 2;
113 $s = $ts if $ts > $s;
115 if ($s > 1024 * 1024) {
116 $a->{cachesize} = int($s / (1024*1024)) * 3 * 1024 * 1024;
119 # print "cache size " . $a->{cachesize} . "\n";
123 copy($dbfn, "$dbfn.new") or return "cannot copy $dbfn -> $dbfn.new $!";
126 tie %dbn, 'DB_File', "$dbfn.new", O_RDWR|O_CREAT, 0664, $a or return "cannot tie $dbfn.new $!";
128 # now write away all the files
133 return "Cannot find $ofn" unless -r $ofn;
135 # conditionally handle compressed files (don't cha just lurv live code, this is
136 # a rave from the grave and is "in memoriam Flossie" the ICT 1301G I learnt on.
137 # {for pedant computer historians a 1301G is an ICT 1301A that has been
138 # Galdorised[tm] (for instance had decent IOs and a 24 pre-modify instruction)}
140 if ($nfn =~ /.gz$/i) {
142 eval qq{use Compress::Zlib; \$gz = gzopen(\$ofn, "rb")};
143 return "Cannot read compressed files $@ $!" if $@ || !$gz;
145 my $of = new IO::File ">$nfn" or return "Cannot write to $nfn $!";
147 $of->write($buf, $l) while ($l = $gz->gzread($buf));
153 my $of = new IO::File "$ofn" or return "Cannot read $ofn $!";
158 my ($call, $city, $state) = split /\|/, $l;
160 _add(\%dbn, $call, $city, $state);
169 rename "$dbfn.new", $dbfn;
170 return "$count records";