#!/usr/bin/perl

open($file_in, "geolocation.db");
open($file_out, ">geolocation.ip");

LINE: while(<$file_in>) {
	next LINE if /^#/;
	chomp;
	@elements = split(' ', $_);
	line2file(@elements[0], @elements[1], @elements[2]);
}

close $file_in;
close $file_out;

sub line2file
{
	$line_new = int2quad($_[0]) . " " . int2quad($_[1]) . " " . $_[2] . "\n";
	print $file_out $line_new;
}

sub ip2long
{
	return unpack('N', pack('C4', split('\.', $_[0])));
}

sub int2quad
{
	return join('.',unpack('C4', pack("N", $_[0])));
}
