1#!/usr/local/bin/perl -w
2
3# SPDX-License-Identifier: BSD-2-Clause
4#
5# Copyright 2009 Edwin Groothuis <edwin@FreeBSD.org>
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions
9# are met:
10# 1. Redistributions of source code must retain the above copyright
11#    notice, this list of conditions and the following disclaimer.
12# 2. Redistributions in binary form must reproduce the above copyright
13#    notice, this list of conditions and the following disclaimer in the
14#    documentation and/or other materials provided with the distribution.
15#
16# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26# SUCH DAMAGE.
27#
28#
29
30use strict;
31use XML::Parser;
32use Data::Dumper;
33
34my %data = ();
35my %d = ();
36my $index = -1;
37
38sub get_xmldata {
39	my $etcdir = shift;
40
41	open(FIN, "$etcdir/charmaps.xml");
42	my @xml = <FIN>;
43	chomp(@xml);
44	close(FIN);
45
46	my $xml = new XML::Parser(Handlers => {
47					Start	=> \&h_start,
48					End	=> \&h_end,
49					Char	=> \&h_char
50					});
51	$xml->parse(join("", @xml));
52	return %d;
53}
54
55sub h_start {
56	my $expat = shift;
57	my $element = shift;
58	my @attrs = @_;
59	my %attrs = ();
60
61
62	while ($#attrs >= 0) {
63		$attrs{$attrs[0]} = $attrs[1];
64		shift(@attrs);
65		shift(@attrs);
66	}
67
68	$data{element}{++$index} = $element;
69
70	if ($index == 2
71	 && $data{element}{1} eq "languages"
72	 && $element eq "language") {
73		my $name = $attrs{name};
74		my $countries = $attrs{countries};
75		my $encoding = $attrs{encoding};
76		my $family = $attrs{family};
77		my $f = defined $attrs{family} ? $attrs{family} : "x";
78		my $nc_link = $attrs{namecountry_link};
79		my $e_link = $attrs{encoding_link};
80		my $fallback = $attrs{fallback};
81		my $definitions = $attrs{definitions};
82
83		$d{L}{$name}{$f}{fallback} = $fallback;
84		$d{L}{$name}{$f}{e_link} = $e_link;
85		$d{L}{$name}{$f}{nc_link} = $nc_link;
86		$d{L}{$name}{$f}{family} = $family;
87		$d{L}{$name}{$f}{encoding} = $encoding;
88		$d{L}{$name}{$f}{definitions} = $definitions;
89		$d{L}{$name}{$f}{countries} = $countries;
90		foreach my $c (split(" ", $countries)) {
91			if (defined $encoding) {
92				foreach my $e (split(" ", $encoding)) {
93					$d{L}{$name}{$f}{data}{$c}{$e} = undef;
94					$d{E}{$e} = 0;	# not read
95				}
96			}
97			$d{L}{$name}{$f}{data}{$c}{"UTF-8"} = undef;
98		}
99		return;
100	}
101
102	if ($index == 2
103	 && $data{element}{1} eq "translations"
104	 && $element eq "translation") {
105		foreach my $e (split(" ", $attrs{encoding})) {
106			if (defined $attrs{hex}) {
107				my $k = $attrs{cldr};
108				my $hs = $attrs{hex};
109				$d{T}{$e}{$k}{hex} = $hs;
110			}
111			if (defined $attrs{string}) {
112				my $s = "";
113				for (my $i = 0; $i < length($attrs{string}); $i++) {
114					$s .= sprintf("%02x",
115					    ord(substr($attrs{string}, $i, 1)));
116				}
117				$d{T}{$e}{$attrs{cldr}}{hex} = $s;
118			}
119			if (defined $attrs{unicode}) {
120				my $k = $attrs{cldr};
121				my $uc = $attrs{unicode};
122				$d{T}{$e}{$k}{unicode} = $uc;
123			}
124			if (defined $attrs{ucc}) {
125				my $k = $attrs{cldr};
126				my $uc = $attrs{ucc};
127				$d{T}{$e}{$k}{ucc} = $uc;
128			}
129		}
130		return;
131	}
132
133	if ($index == 2
134	 && $data{element}{1} eq "alternativemonths"
135	 && $element eq "language") {
136		my $name = $attrs{name};
137		my $countries = $attrs{countries};
138
139		$data{fields}{name} = $name;
140		$data{fields}{countries} = $countries;
141		$data{fields}{text} = "";
142
143		return;
144	}
145}
146
147sub h_end {
148	my $expat = shift;
149	my $element = shift;
150
151	if ($index == "2") {
152		if ($data{element}{1} eq "alternativemonths"
153		 && $data{element}{2} eq "language") {
154			foreach my $c (split(/,/, $data{fields}{countries})) {
155				my $m = $data{fields}{text};
156
157				$m =~ s/^[\t ]//g;
158				$m =~ s/[\t ]$//g;
159				$d{AM}{$data{fields}{name}}{$c} = $m;
160			}
161			$data{fields} = ();
162		}
163	}
164
165	$index--;
166}
167
168sub h_char {
169	my $expat = shift;
170	my $string = shift;
171
172	if ($index == "2") {
173		if ($data{element}{1} eq "alternativemonths"
174		 && $data{element}{2} eq "language") {
175			$data{fields}{text} .= $string;
176		}
177	}
178}
179
180#use Data::Dumper;
181#my %D = get_xmldata();
182#print Dumper(%D);
1831;
184