geoipdb builder: get rid of some global variables

This commit is contained in:
Jan Engelhardt
2011-01-27 03:05:30 +01:00
parent 80444b0d31
commit 93a17fdde0

View File

@@ -10,7 +10,6 @@ use IO::Handle;
use Text::CSV_XS; # or trade for Text::CSV
use strict;
my %country;
my $csv = Text::CSV_XS->new({
allow_whitespace => 1,
binary => 1,
@@ -35,11 +34,12 @@ foreach (qw(LE BE)) {
}
}
&collect();
&dump();
&dump(&collect());
sub collect
{
my %country;
while (my $row = $csv->getline(*ARGV)) {
if (!defined($country{$row->[4]})) {
$country{$row->[4]} = {
@@ -61,23 +61,26 @@ sub collect
}
print STDERR "\r\e[2K$. entries total\n";
return \%country;
}
sub dump
{
foreach my $iso_code (sort keys %country) {
&dump_one($iso_code);
my $country = shift @_;
foreach my $iso_code (sort keys %$country) {
&dump_one($iso_code, $country->{$iso_code});
}
}
sub dump_one
{
my $iso_code = shift @_;
my($iso_code, $country) = @_;
my($file, $fh_le, $fh_be);
printf "%5u IPv6 ranges for %s %s\n",
scalar(@{$country{$iso_code}{pool_v6}}),
$iso_code, $country{$iso_code}{name};
scalar(@{$country->{pool_v6}}),
$iso_code, $country->{name};
$file = "$target_dir/LE/".uc($iso_code).".iv6";
if (!open($fh_le, "> $file")) {
@@ -89,7 +92,7 @@ sub dump_one
print STDERR "Error opening $file: $!\n";
exit 1;
}
foreach my $range (@{$country{$iso_code}{pool_v6}}) {
foreach my $range (@{$country->{pool_v6}}) {
print $fh_be $range->[0], $range->[1];
print $fh_le &ip6_swap($range->[0]), &ip6_swap($range->[1]);
}
@@ -97,8 +100,8 @@ sub dump_one
close $fh_be;
printf "%5u IPv4 ranges for %s %s\n",
scalar(@{$country{$iso_code}{pool_v4}}),
$iso_code, $country{$iso_code}{name};
scalar(@{$country->{pool_v4}}),
$iso_code, $country->{name};
$file = "$target_dir/LE/".uc($iso_code).".iv4";
if (!open($fh_le, "> $file")) {
@@ -110,7 +113,7 @@ sub dump_one
print STDERR "Error opening $file: $!\n";
exit 1;
}
foreach my $range (@{$country{$iso_code}{pool_v4}}) {
foreach my $range (@{$country->{pool_v4}}) {
print $fh_le pack("VV", $range->[0], $range->[1]);
print $fh_be pack("NN", $range->[0], $range->[1]);
}