Allow synonyms for localhost
[spider.git] / perl / USDB.pm
1 #
2 # Package to handle US Callsign -> City, State translations
3 #
4 # Copyright (c) 2002 Dirk Koopman G1TLH
5 #
6
7
8 package USDB;
9
10 use strict;
11
12 use DXVars;
13 use SysVar;
14 use DB_File;
15 use File::Copy;
16 use DXDebug;
17 use DXUtil;
18
19 #use Compress::Zlib;
20
21 use vars qw(%db $present $dbfn);
22
23 localdata_mv("usdb.v1");
24 $dbfn = localdata("usdb.v1");
25
26 sub init
27 {
28         end();
29         if (tie %db, 'DB_File', $dbfn, O_RDWR, 0664, $DB_BTREE) {
30                 $present = 1;
31                 return "US Database loaded";
32         }
33         return "US Database not loaded";
34 }
35
36 sub end
37 {
38         return unless $present;
39         untie %db;
40         undef $present;
41 }
42
43 sub get
44 {
45         return () unless $present;
46         my $ctyn = $db{$_[0]};
47         my @s = split /\|/, $db{$ctyn} if $ctyn;
48         return @s;
49 }
50
51 sub _add
52 {
53         my ($db, $call, $city, $state) = @_;
54         
55         # lookup the city 
56         my $s = uc "$city|$state";
57         my $ctyn = $db->{$s};
58         unless ($ctyn) {
59                 my $no = $db->{'##'} || 1;
60                 $ctyn = "#$no";
61                 $db->{$s} = $ctyn;
62                 $db->{$ctyn} = $s; 
63                 $no++;
64                 $db->{'##'} = "$no";
65         }
66         $db->{uc $call} = $ctyn; 
67 }
68
69 sub add
70 {
71         _add(\%db, @_);
72 }
73
74 sub getstate
75 {
76         return () unless $present;
77         my @s = get($_[0]);
78         return @s ? $s[1] : undef;
79 }
80
81 sub getcity
82 {
83         return () unless $present;
84         my @s = get($_[0]);
85         return @s ? $s[0] : undef;
86 }
87
88 sub del
89 {
90         my $call = uc shift;
91         delete $db{$call};
92 }
93
94 #
95 # load in / update an existing DB with a standard format (GZIPPED)
96 # "raw" file.
97 #
98 # Note that this removes and overwrites the existing DB file
99 # You will need to init again after doing this
100
101
102 sub load
103 {
104         return "Need a filename" unless @_;
105         
106         # create the new output file
107         my $a = new DB_File::BTREEINFO;
108         $a->{psize} = 4096 * 2;
109         my $s = 0;
110
111         # guess a cache size
112         for (@_) {
113                 my $ts = -s;
114                 $s = $ts if $ts > $s;
115         }
116         if ($s > 1024 * 1024) {
117                 $a->{cachesize} = int($s / (1024*1024)) * 3 * 1024 * 1024;
118         }
119
120 #       print "cache size " . $a->{cachesize} . "\n";
121         
122         my %dbn;
123         if (-e $dbfn ) {
124                 copy($dbfn, "$dbfn.old") or return "cannot copy $dbfn -> $dbfn.old $!";
125         }
126
127         unlink "$dbfn.new";
128         tie %dbn, 'DB_File', "$dbfn.new", O_RDWR|O_CREAT, 0664, $a or return "cannot tie $dbfn.new $!";
129         
130         # now write away all the files
131         my $count = 0;
132         for (@_) {
133                 my $ofn = shift;
134
135                 return "Cannot find $ofn" unless -r $ofn;
136                 
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)}
141                 my $nfn = $ofn;
142                 if ($nfn =~ /.gz$/i) {
143                         my $gz;
144                         eval qq{use Compress::Zlib; \$gz = gzopen(\$ofn, "rb")};
145                         return "Cannot read compressed files $@ $!" if $@ || !$gz;
146                         $nfn =~ s/.gz$//i;
147                         my $of = new IO::File ">$nfn" or return "Cannot write to $nfn $!";
148                         my ($l, $buf);
149                         $of->write($buf, $l) while ($l = $gz->gzread($buf));
150                         $gz->gzclose;
151                         $of->close;
152                         $ofn = $nfn;
153                 }
154
155                 my $of = new IO::File "$ofn" or return "Cannot read $ofn $!";
156
157                 while (<$of>) {
158                         my $l = $_;
159                         $l =~ s/[\r\n]+$//;
160                         my ($call, $city, $state) = split /\|/, $l;
161
162                         _add(\%dbn, $call, $city, $state);
163                         
164                         $count++;
165                 }
166                 $of->close;
167                 unlink $nfn;
168         }
169         
170         untie %dbn;
171         rename "$dbfn.new", $dbfn;
172         return "$count records";
173 }
174
175 1;