719ac66afe30db127d95008c32305d09ef8532dd
[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 DB_File;
14 use File::Copy;
15 use DXDebug;
16 #use Compress::Zlib;
17
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;
23
24 use vars qw(%db $present $dbfn);
25
26 $dbfn = "$main::data/usdb.v1";
27
28 sub init
29 {
30         end();
31         if (tie %db, 'DB_File', $dbfn, O_RDONLY, 0664, $DB_BTREE) {
32                 $present = 1;
33                 return "US Database loaded";
34         }
35         return "US Database not loaded";
36 }
37
38 sub end
39 {
40         return unless $present;
41         untie %db;
42         undef $present;
43 }
44
45 sub get
46 {
47         return () unless $present;
48         my $ctyn = $db{$_[0]};
49         my @s = split /\|/, $db{$ctyn} if $ctyn;
50         return @s;
51 }
52
53 sub getstate
54 {
55         return () unless $present;
56         my @s = get($_[0]);
57         return @s ? $s[1] : undef;
58 }
59
60 sub getcity
61 {
62         return () unless $present;
63         my @s = get($_[0]);
64         return @s ? $s[0] : undef;
65 }
66
67 #
68 # load in / update an existing DB with a standard format (GZIPPED)
69 # "raw" file.
70 #
71 # Note that this removes and overwrites the existing DB file
72 # You will need to init again after doing this
73
74
75 sub load
76 {
77         return "Need a filename" unless @_;
78         
79         # create the new output file
80         my $a = new DB_File::BTREEINFO;
81         $a->{psize} = 4096 * 2;
82         my $s = 0;
83
84         # guess a cache size
85         for (@_) {
86                 my $ts = -s;
87                 $s = $ts if $ts > $s;
88         }
89         if ($s > 1024 * 1024) {
90                 $a->{cachesize} = int($s / (1024*1024)) * 3 * 1024 * 1024;
91         }
92
93 #       print "cache size " . $a->{cachesize} . "\n";
94         
95         my %dbn;
96         if (-e $dbfn ) {
97                 copy($dbfn, "$dbfn.new") or return "cannot copy $dbfn -> $dbfn.new $!";
98         }
99         
100         tie %dbn, 'DB_File', "$dbfn.new", O_RDWR|O_CREAT, 0664, $a or return "cannot tie $dbfn.new $!";
101         
102         # now write away all the files
103         my $count = 0;
104         for (@_) {
105                 my $ofn = shift;
106
107                 # conditionally handle compressed files (don't cha just lurv live code, this is
108                 # a rave from the grave and is "in memoriam Flossie" the ICT 1301G I learnt on.
109                 # {for pedant computer historians a 1301G is an ICT 1301A that has been 
110                 # Galdorised[tm] (for instance had decent IOs and a 24 pre-modify instruction)}
111                 my $nfn = $ofn;
112                 if ($nfn =~ /.gz$/i) {
113                         my $gz;
114                         eval qq{use Compress::Zlib; \$gz = gzopen(\$ofn, "rb")};
115                         return "Cannot read compressed files $@" if $@;
116                         $nfn =~ s/.gz$//i;
117                         my $of = new IO::File ">$nfn" or return "Cannot write to $nfn $!";
118                         my ($l, $buf);
119                         $of->write($buf, $l) while ($l = $gz->gzread($buf));
120                         $gz->gzclose;
121                         $of->close;
122                         $ofn = $nfn;
123                 }
124
125                 my $of = new IO::File "$ofn" or return "Cannot read $ofn $!";
126
127                 while (<$of>) {
128                         my $l = $_;
129                         $l =~ s/[\r\n]+$//;
130                         my ($call, $city, $state) = split /\|/, $l;
131                         
132                         # lookup the city 
133                         my $s = "$city|$state";
134                         my $ctyn = $dbn{$s};
135                         unless ($ctyn) {
136                                 my $no = $dbn{'##'} || 1;
137                                 $ctyn = "#$no";
138                                 $dbn{$s} = $ctyn;
139                                 $dbn{$ctyn} = $s; 
140                                 $no++;
141                                 $dbn{'##'} = "$no";
142                         }
143                         $dbn{$call} = $ctyn; 
144                         $count++;
145                 }
146                 $of->close;
147                 unlink $nfn;
148         }
149         
150         untie %dbn;
151         rename "$dbfn.new", $dbfn;
152         return "$count records";
153 }
154
155 1;