xref: /freebsd/tools/tools/iso/check-iso3166.pl (revision 069ac184)
1#!/usr/bin/perl -w
2
3#
4#
5# This script compares the file iso3166 (from head/share/misc) with the files
6# list-en1-semic-3.txt (from
7# http://www.iso.org/iso/list-en1-semic-3.txt) and iso3166-countrycodes.txt
8# (from ftp://ftp.ripe.net/) to see if there any differences.
9#
10# Created by Edwin Groothuis <edwin@FreeBSD.org> for the FreeBSD project.
11#
12
13use strict;
14use Data::Dumper;
15
16my %old = ();
17{
18	open(FIN, "iso3166") or die "Cannot open iso3166 (should be in head/share/misc)";
19	my @lines = <FIN>;
20	close(FIN);
21	chomp(@lines);
22
23	foreach my $l (@lines) {
24		next if ($l =~ /^#/);
25		next if ($l eq "");
26
27		die "Bad line: $l\n"
28			if ($l !~ /^([A-Z\-]*)[ \t]+([A-Z\-]+)[ \t]+(\d+)[ \t]+(.*)/);
29		my $two = $1;
30		my $three = $2;
31		my $number = $3;
32		my $name = $4;
33
34		$old{$two}{two} = $two;
35		$old{$two}{three} = $three;
36		$old{$two}{number} = $number;
37		$old{$two}{name} = $name;
38	}
39}
40
41my %new1 = ();
42{
43	open(FIN, "iso3166-countrycodes.txt") or die "Cannot open iso3166-countrycodes.txt, which can be retrieved from ftp://ftp.ripe.net/iso3166-countrycodes.txt";
44	my @lines = <FIN>;
45	close(FIN);
46	chomp(@lines);
47
48	my $noticed = 0;
49	foreach my $l (@lines) {
50		if ($l =~ /\-\-\-\-\-\-\-/) {
51			$noticed = 1;
52			next;
53		}
54		next if (!$noticed);
55		next if ($l eq "");
56
57		die "Invalid line: $l\n"
58			if ($l !~ /^(.+?)[\t ]+([A-Z]{2})[\t ]+([A-Z]{3})[\t ]+(\d+)[\t ]*$/);
59		my $two = $2;
60		my $three = $3;
61		my $number = $4;
62		my $name = $1;
63
64		$new1{$two}{two} = $two;
65		$new1{$two}{three} = $three;
66		$new1{$two}{number} = $number;
67		$new1{$two}{name} = $name;
68	}
69}
70
71my %new2 = ();
72{
73	open(FIN, "list-en1-semic-3.txt") or die "Cannot open list-en1-semic-3.txt, which can be retrieved from http://www.iso.org/iso/list-en1-semic-3.txt";
74	my @lines = <FIN>;
75	close(FIN);
76	chomp(@lines);
77
78	my $noticed = 0;
79	foreach my $l (@lines) {
80		$l =~ s/\x0d//g;
81		if (!$noticed) {	# skip the first line
82			$noticed = 1;
83			next;
84		}
85		next if ($l eq "");
86
87		my @a = split(/;/, $l);
88		die "Invalid line: $l\n" if ($#a != 1);
89		my $two = $a[1];
90		my $name = $a[0];
91
92		$new2{$two}{two} = $two;
93		$new2{$two}{name} = $name;
94	}
95}
96
97{
98	my $c = 0;
99	foreach my $two (sort(keys(%old))) {
100		if (!defined $new1{$two}) {
101			print "In old but not new1: $old{$two}{two}\t$old{$two}{three}\t$old{$two}{number}\t$old{$two}{name}\n";
102			$c++;
103		}
104		if (!defined $new2{$two}) {
105			print "In old but not new2: $old{$two}{two}\t$old{$two}{name}\n";
106			$c++;
107		}
108	}
109	print "Found $c issues\n";
110}
111
112{
113	my $c = 0;
114	foreach my $two (sort(keys(%new1))) {
115		next if (defined $old{$two});
116		print "In new1 but not old: $new1{$two}{two}\t$new1{$two}{three}\t$new1{$two}{number}\t$new1{$two}{name}\n";
117		$c++;
118	}
119	print "Found $c issues\n";
120}
121
122{
123	my $c = 0;
124	foreach my $two (sort(keys(%new2))) {
125		next if (defined $old{$two});
126		print "In new2 but not old: $new2{$two}{two}\t$new2{$two}{name}\n";
127		$c++;
128	}
129	print "Found $c issues\n";
130}
131
132{
133	my $c = 0;
134	foreach my $two (sort(keys(%old))) {
135		if (defined $new1{$two}) {
136			if ($old{$two}{two} ne $new1{$two}{two} ||
137			    $old{$two}{three} ne $new1{$two}{three} ||
138			    $old{$two}{number} ne $new1{$two}{number} ||
139			    lc($old{$two}{name}) ne lc($new1{$two}{name})) {
140				print "In old : $old{$two}{two}\t$old{$two}{three}\t$old{$two}{number}\t$old{$two}{name}\n";
141				print "In new1: $new1{$two}{two}\t$new1{$two}{three}\t$new1{$two}{number}\t$new1{$two}{name}\n";
142				$c++;
143			}
144		}
145	}
146	print "Found $c issues\n";
147}
148
149{
150	my $c = 0;
151	foreach my $two (sort(keys(%old))) {
152		if (defined $new2{$two}) {
153			if ($old{$two}{two} ne $new2{$two}{two} ||
154			    lc($old{$two}{name}) ne lc($new2{$two}{name})) {
155				print "In old : $old{$two}{two}\t$old{$two}{name}\n";
156				print "In new2: $new2{$two}{two}\t$new2{$two}{name}\n";
157				$c++;
158			}
159		}
160	}
161	print "Found $c issues\n";
162}
163
164