xref: /freebsd/tools/tools/locale/tools/charmaps.pm (revision 4d846d26)
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# $FreeBSD$
29#
30
31use strict;
32use XML::Parser;
33use Data::Dumper;
34
35my %data = ();
36my %d = ();
37my $index = -1;
38
39sub get_xmldata {
40	my $etcdir = shift;
41
42	open(FIN, "$etcdir/charmaps.xml");
43	my @xml = <FIN>;
44	chomp(@xml);
45	close(FIN);
46
47	my $xml = new XML::Parser(Handlers => {
48					Start	=> \&h_start,
49					End	=> \&h_end,
50					Char	=> \&h_char
51					});
52	$xml->parse(join("", @xml));
53	return %d;
54}
55
56sub h_start {
57	my $expat = shift;
58	my $element = shift;
59	my @attrs = @_;
60	my %attrs = ();
61
62
63	while ($#attrs >= 0) {
64		$attrs{$attrs[0]} = $attrs[1];
65		shift(@attrs);
66		shift(@attrs);
67	}
68
69	$data{element}{++$index} = $element;
70
71	if ($index == 2
72	 && $data{element}{1} eq "languages"
73	 && $element eq "language") {
74		my $name = $attrs{name};
75		my $countries = $attrs{countries};
76		my $encoding = $attrs{encoding};
77		my $family = $attrs{family};
78		my $f = defined $attrs{family} ? $attrs{family} : "x";
79		my $nc_link = $attrs{namecountry_link};
80		my $e_link = $attrs{encoding_link};
81		my $fallback = $attrs{fallback};
82		my $definitions = $attrs{definitions};
83
84		$d{L}{$name}{$f}{fallback} = $fallback;
85		$d{L}{$name}{$f}{e_link} = $e_link;
86		$d{L}{$name}{$f}{nc_link} = $nc_link;
87		$d{L}{$name}{$f}{family} = $family;
88		$d{L}{$name}{$f}{encoding} = $encoding;
89		$d{L}{$name}{$f}{definitions} = $definitions;
90		$d{L}{$name}{$f}{countries} = $countries;
91		foreach my $c (split(" ", $countries)) {
92			if (defined $encoding) {
93				foreach my $e (split(" ", $encoding)) {
94					$d{L}{$name}{$f}{data}{$c}{$e} = undef;
95					$d{E}{$e} = 0;	# not read
96				}
97			}
98			$d{L}{$name}{$f}{data}{$c}{"UTF-8"} = undef;
99		}
100		return;
101	}
102
103	if ($index == 2
104	 && $data{element}{1} eq "translations"
105	 && $element eq "translation") {
106		foreach my $e (split(" ", $attrs{encoding})) {
107			if (defined $attrs{hex}) {
108				my $k = $attrs{cldr};
109				my $hs = $attrs{hex};
110				$d{T}{$e}{$k}{hex} = $hs;
111			}
112			if (defined $attrs{string}) {
113				my $s = "";
114				for (my $i = 0; $i < length($attrs{string}); $i++) {
115					$s .= sprintf("%02x",
116					    ord(substr($attrs{string}, $i, 1)));
117				}
118				$d{T}{$e}{$attrs{cldr}}{hex} = $s;
119			}
120			if (defined $attrs{unicode}) {
121				my $k = $attrs{cldr};
122				my $uc = $attrs{unicode};
123				$d{T}{$e}{$k}{unicode} = $uc;
124			}
125			if (defined $attrs{ucc}) {
126				my $k = $attrs{cldr};
127				my $uc = $attrs{ucc};
128				$d{T}{$e}{$k}{ucc} = $uc;
129			}
130		}
131		return;
132	}
133
134	if ($index == 2
135	 && $data{element}{1} eq "alternativemonths"
136	 && $element eq "language") {
137		my $name = $attrs{name};
138		my $countries = $attrs{countries};
139
140		$data{fields}{name} = $name;
141		$data{fields}{countries} = $countries;
142		$data{fields}{text} = "";
143
144		return;
145	}
146}
147
148sub h_end {
149	my $expat = shift;
150	my $element = shift;
151
152	if ($index == "2") {
153		if ($data{element}{1} eq "alternativemonths"
154		 && $data{element}{2} eq "language") {
155			foreach my $c (split(/,/, $data{fields}{countries})) {
156				my $m = $data{fields}{text};
157
158				$m =~ s/^[\t ]//g;
159				$m =~ s/[\t ]$//g;
160				$d{AM}{$data{fields}{name}}{$c} = $m;
161			}
162			$data{fields} = ();
163		}
164	}
165
166	$index--;
167}
168
169sub h_char {
170	my $expat = shift;
171	my $string = shift;
172
173	if ($index == "2") {
174		if ($data{element}{1} eq "alternativemonths"
175		 && $data{element}{2} eq "language") {
176			$data{fields}{text} .= $string;
177		}
178	}
179}
180
181#use Data::Dumper;
182#my %D = get_xmldata();
183#print Dumper(%D);
1841;
185