summaryrefslogtreecommitdiffstats
path: root/tools/tools/iso/check-iso3166.pl
blob: a8baf4ee6a783c0057af57e938169c80d5cede5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/perl -w

#
# $FreeBSD$
#
# This script compares the file iso3166 (from head/share/misc) with the files
# list-en1-semic-3.txt (from
# http://www.iso.org/iso/list-en1-semic-3.txt) and iso3166-countrycodes.txt
# (from ftp://ftp.ripe.net/) to see if there any differences.
#
# Created by Edwin Groothuis <edwin@FreeBSD.org> for the FreeBSD project.
#

use strict;
use Data::Dumper;

my %old = ();
{
	open(FIN, "iso3166") or die "Cannot open iso3166 (should be in head/share/misc)";
	my @lines = <FIN>;
	close(FIN);
	chomp(@lines);

	foreach my $l (@lines) {
		next if ($l =~ /^#/);
		next if ($l eq "");

		die "Bad line: $l\n"
			if ($l !~ /^([A-Z\-]*)[ \t]+([A-Z\-]+)[ \t]+(\d+)[ \t]+(.*)/);
		my $two = $1;
		my $three = $2;
		my $number = $3;
		my $name = $4;

		$old{$two}{two} = $two;
		$old{$two}{three} = $three;
		$old{$two}{number} = $number;
		$old{$two}{name} = $name;
	}
}

my %new1 = ();
{
	open(FIN, "iso3166-countrycodes.txt") or die "Cannot open iso3166-countrycodes.txt, which can be retrieved from ftp://ftp.ripe.net/iso3166-countrycodes.txt";
	my @lines = <FIN>;
	close(FIN);
	chomp(@lines);

	my $noticed = 0;
	foreach my $l (@lines) {
		if ($l =~ /\-\-\-\-\-\-\-/) {
			$noticed = 1;
			next;
		}
		next if (!$noticed);
		next if ($l eq "");

		die "Invalid line: $l\n"
			if ($l !~ /^(.+?)[\t ]+([A-Z]{2})[\t ]+([A-Z]{3})[\t ]+(\d+)[\t ]*$/);
		my $two = $2;
		my $three = $3;
		my $number = $4;
		my $name = $1;

		$new1{$two}{two} = $two;
		$new1{$two}{three} = $three;
		$new1{$two}{number} = $number;
		$new1{$two}{name} = $name;
	}
}

my %new2 = ();
{
	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";
	my @lines = <FIN>;
	close(FIN);
	chomp(@lines);

	my $noticed = 0;
	foreach my $l (@lines) {
		$l =~ s/\x0d//g;
		if (!$noticed) {	# skip the first line
			$noticed = 1;
			next;
		}
		next if ($l eq "");

		my @a = split(/;/, $l);
		die "Invalid line: $l\n" if ($#a != 1);
		my $two = $a[1];
		my $name = $a[0];

		$new2{$two}{two} = $two;
		$new2{$two}{name} = $name;
	}
}

{
	my $c = 0;
	foreach my $two (sort(keys(%old))) {
		if (!defined $new1{$two}) {
			print "In old but not new1: $old{$two}{two}\t$old{$two}{three}\t$old{$two}{number}\t$old{$two}{name}\n";
			$c++;
		}
		if (!defined $new2{$two}) {
			print "In old but not new2: $old{$two}{two}\t$old{$two}{name}\n";
			$c++;
		}
	}
	print "Found $c issues\n";
}

{
	my $c = 0;
	foreach my $two (sort(keys(%new1))) {
		next if (defined $old{$two});
		print "In new1 but not old: $new1{$two}{two}\t$new1{$two}{three}\t$new1{$two}{number}\t$new1{$two}{name}\n";
		$c++;
	}
	print "Found $c issues\n";
}

{
	my $c = 0;
	foreach my $two (sort(keys(%new2))) {
		next if (defined $old{$two});
		print "In new2 but not old: $new2{$two}{two}\t$new2{$two}{name}\n";
		$c++;
	}
	print "Found $c issues\n";
}

{
	my $c = 0;
	foreach my $two (sort(keys(%old))) {
		if (defined $new1{$two}) {
			if ($old{$two}{two} ne $new1{$two}{two} ||
			    $old{$two}{three} ne $new1{$two}{three} ||
			    $old{$two}{number} ne $new1{$two}{number} ||
			    lc($old{$two}{name}) ne lc($new1{$two}{name})) {
				print "In old : $old{$two}{two}\t$old{$two}{three}\t$old{$two}{number}\t$old{$two}{name}\n";
				print "In new1: $new1{$two}{two}\t$new1{$two}{three}\t$new1{$two}{number}\t$new1{$two}{name}\n";
				$c++;
			}
		}
	}
	print "Found $c issues\n";
}

{
	my $c = 0;
	foreach my $two (sort(keys(%old))) {
		if (defined $new2{$two}) {
			if ($old{$two}{two} ne $new2{$two}{two} ||
			    lc($old{$two}{name}) ne lc($new2{$two}{name})) {
				print "In old : $old{$two}{two}\t$old{$two}{name}\n";
				print "In new2: $new2{$two}{two}\t$new2{$two}{name}\n";
				$c++;
			}
		}
	}
	print "Found $c issues\n";
}

OpenPOWER on IntegriCloud