2 # Package to handle US Callsign -> City, State translations
4 # Copyright (c) 2002 Dirk Koopman G1TLH
18 use vars qw(%db $present $dbfn);
20 $dbfn = "$main::data/usdb.v1";
25 if (tie %db, 'DB_File', $dbfn, O_RDWR, 0664, $DB_BTREE) {
27 return "US Database loaded";
29 return "US Database not loaded";
34 return unless $present;
41 return () unless $present;
42 my $ctyn = $db{$_[0]};
43 my @s = split /\|/, $db{$ctyn} if $ctyn;
49 my ($db, $call, $city, $state) = @_;
52 my $s = uc "$city|$state";
55 my $no = $db->{'##'} || 1;
62 $db->{uc $call} = $ctyn;
72 return () unless $present;
74 return @s ? $s[1] : undef;
79 return () unless $present;
81 return @s ? $s[0] : undef;
91 # load in / update an existing DB with a standard format (GZIPPED)
94 # Note that this removes and overwrites the existing DB file
95 # You will need to init again after doing this
100 return "Need a filename" unless @_;
102 # create the new output file
103 my $a = new DB_File::BTREEINFO;
104 $a->{psize} = 4096 * 2;
110 $s = $ts if $ts > $s;
112 if ($s > 1024 * 1024) {
113 $a->{cachesize} = int($s / (1024*1024)) * 3 * 1024 * 1024;
116 # print "cache size " . $a->{cachesize} . "\n";
120 copy($dbfn, "$dbfn.new") or return "cannot copy $dbfn -> $dbfn.new $!";
123 tie %dbn, 'DB_File', "$dbfn.new", O_RDWR|O_CREAT, 0664, $a or return "cannot tie $dbfn.new $!";
125 # now write away all the files
130 return "Cannot find $ofn" unless -r $ofn;
132 # conditionally handle compressed files (don't cha just lurv live code, this is
133 # a rave from the grave and is "in memoriam Flossie" the ICT 1301G I learnt on.
134 # {for pedant computer historians a 1301G is an ICT 1301A that has been
135 # Galdorised[tm] (for instance had decent IOs and a 24 pre-modify instruction)}
137 if ($nfn =~ /.gz$/i) {
139 eval qq{use Compress::Zlib; \$gz = gzopen(\$ofn, "rb")};
140 return "Cannot read compressed files $@ $!" if $@ || !$gz;
142 my $of = new IO::File ">$nfn" or return "Cannot write to $nfn $!";
144 $of->write($buf, $l) while ($l = $gz->gzread($buf));
150 my $of = new IO::File "$ofn" or return "Cannot read $ofn $!";
155 my ($call, $city, $state) = split /\|/, $l;
157 _add(\%dbn, $call, $city, $state);
166 rename "$dbfn.new", $dbfn;
167 return "$count records";