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_RDWR, 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 my ($db, $call, $city, $state) = @_;
58 my $s = uc "$city|$state";
61 my $no = $db->{'##'} || 1;
68 $db->{uc $call} = $ctyn;
78 return () unless $present;
80 return @s ? $s[1] : undef;
85 return () unless $present;
87 return @s ? $s[0] : undef;
97 # load in / update an existing DB with a standard format (GZIPPED)
100 # Note that this removes and overwrites the existing DB file
101 # You will need to init again after doing this
106 return "Need a filename" unless @_;
108 # create the new output file
109 my $a = new DB_File::BTREEINFO;
110 $a->{psize} = 4096 * 2;
116 $s = $ts if $ts > $s;
118 if ($s > 1024 * 1024) {
119 $a->{cachesize} = int($s / (1024*1024)) * 3 * 1024 * 1024;
122 # print "cache size " . $a->{cachesize} . "\n";
126 copy($dbfn, "$dbfn.new") or return "cannot copy $dbfn -> $dbfn.new $!";
129 tie %dbn, 'DB_File', "$dbfn.new", O_RDWR|O_CREAT, 0664, $a or return "cannot tie $dbfn.new $!";
131 # now write away all the files
136 # conditionally handle compressed files (don't cha just lurv live code, this is
137 # a rave from the grave and is "in memoriam Flossie" the ICT 1301G I learnt on.
138 # {for pedant computer historians a 1301G is an ICT 1301A that has been
139 # Galdorised[tm] (for instance had decent IOs and a 24 pre-modify instruction)}
141 if ($nfn =~ /.gz$/i) {
143 eval qq{use Compress::Zlib; \$gz = gzopen(\$ofn, "rb")};
144 return "Cannot read compressed files $@" if $@;
146 my $of = new IO::File ">$nfn" or return "Cannot write to $nfn $!";
148 $of->write($buf, $l) while ($l = $gz->gzread($buf));
154 my $of = new IO::File "$ofn" or return "Cannot read $ofn $!";
159 my ($call, $city, $state) = split /\|/, $l;
161 _add(\%dbn, $call, $city, $state);
170 rename "$dbfn.new", $dbfn;
171 return "$count records";