2 # Package to handle US Callsign -> City, State translations
4 # Copyright (c) 2002 Dirk Koopman G1TLH
21 use vars qw(%db $present $dbfn);
23 localdata_mv("usdb.v1");
24 $dbfn = localdata("usdb.v1");
29 if (tie %db, 'DB_File', $dbfn, O_RDWR, 0664, $DB_BTREE) {
31 return "US Database loaded";
33 return "US Database not loaded";
38 return unless $present;
45 return () unless $present;
46 my $ctyn = $db{$_[0]};
47 my @s = split /\|/, $db{$ctyn} if $ctyn;
53 my ($db, $call, $city, $state) = @_;
56 my $s = uc "$city|$state";
59 my $no = $db->{'##'} || 1;
66 $db->{uc $call} = $ctyn;
76 return () unless $present;
78 return @s ? $s[1] : undef;
83 return () unless $present;
85 return @s ? $s[0] : undef;
95 # load in / update an existing DB with a standard format (GZIPPED)
98 # Note that this removes and overwrites the existing DB file
99 # You will need to init again after doing this
104 return "Need a filename" unless @_;
106 # create the new output file
107 my $a = new DB_File::BTREEINFO;
108 $a->{psize} = 4096 * 2;
114 $s = $ts if $ts > $s;
116 if ($s > 1024 * 1024) {
117 $a->{cachesize} = int($s / (1024*1024)) * 3 * 1024 * 1024;
120 # print "cache size " . $a->{cachesize} . "\n";
124 copy($dbfn, "$dbfn.old") or return "cannot copy $dbfn -> $dbfn.old $!";
128 tie %dbn, 'DB_File', "$dbfn.new", O_RDWR|O_CREAT, 0664, $a or return "cannot tie $dbfn.new $!";
130 # now write away all the files
135 return "Cannot find $ofn" unless -r $ofn;
137 # conditionally handle compressed files (don't cha just lurv live code, this is
138 # a rave from the grave and is "in memoriam Flossie" the ICT 1301G I learnt on.
139 # {for pedant computer historians a 1301G is an ICT 1301A that has been
140 # Galdorised[tm] (for instance had decent IOs and a 24 pre-modify instruction)}
142 if ($nfn =~ /.gz$/i) {
144 eval qq{use Compress::Zlib; \$gz = gzopen(\$ofn, "rb")};
145 return "Cannot read compressed files $@ $!" if $@ || !$gz;
147 my $of = new IO::File ">$nfn" or return "Cannot write to $nfn $!";
149 $of->write($buf, $l) while ($l = $gz->gzread($buf));
155 my $of = new IO::File "$ofn" or return "Cannot read $ofn $!";
160 my ($call, $city, $state) = split /\|/, $l;
162 _add(\%dbn, $call, $city, $state);
171 rename "$dbfn.new", $dbfn;
172 return "$count records";