Merge branch 'vendor/DHCPCD'
[dragonfly.git] / tools / tools / locale / tools / charmaps.pm
1 #!/usr/local/bin/perl -w
2
3 #
4 # $FreeBSD$
5 #
6
7 use strict;
8 use XML::Parser;
9 use Data::Dumper;
10
11 my %data = ();
12 my %d = ();
13 my $index = -1;
14
15 sub get_xmldata {
16         my $etcdir = shift;
17
18         open(FIN, "$etcdir/charmaps.xml");
19         my @xml = <FIN>;
20         chomp(@xml);
21         close(FIN);
22
23         my $xml = new XML::Parser(Handlers => {
24                                         Start   => \&h_start,
25                                         End     => \&h_end,
26                                         Char    => \&h_char
27                                         });
28         $xml->parse(join("", @xml));
29         return %d;
30 }
31
32 sub h_start {
33         my $expat = shift;
34         my $element = shift;
35         my @attrs = @_;
36         my %attrs = ();
37
38
39         while ($#attrs >= 0) {
40                 $attrs{$attrs[0]} = $attrs[1];
41                 shift(@attrs);
42                 shift(@attrs);
43         }
44
45         $data{element}{++$index} = $element;
46
47         if ($index == 2
48          && $data{element}{1} eq "languages"
49          && $element eq "language") {
50                 my $name = $attrs{name};
51                 my $countries = $attrs{countries};
52                 my $encoding = $attrs{encoding};
53                 my $family = $attrs{family};
54                 my $f = defined $attrs{family} ? $attrs{family} : "x";
55                 my $nc_link = $attrs{namecountry_link};
56                 my $e_link = $attrs{encoding_link};
57                 my $fallback = $attrs{fallback};
58                 my $definitions = $attrs{definitions};
59
60                 $d{L}{$name}{$f}{fallback} = $fallback;
61                 $d{L}{$name}{$f}{e_link} = $e_link;
62                 $d{L}{$name}{$f}{nc_link} = $nc_link;
63                 $d{L}{$name}{$f}{family} = $family;
64                 $d{L}{$name}{$f}{encoding} = $encoding;
65                 $d{L}{$name}{$f}{definitions} = $definitions;
66                 $d{L}{$name}{$f}{countries} = $countries;
67                 foreach my $c (split(" ", $countries)) {
68                         if (defined $encoding) {
69                                 foreach my $e (split(" ", $encoding)) {
70                                         $d{L}{$name}{$f}{data}{$c}{$e} = undef;
71                                         $d{E}{$e} = 0;  # not read
72                                 }
73                         }
74                         $d{L}{$name}{$f}{data}{$c}{"UTF-8"} = undef;
75                 }
76                 return;
77         }
78
79         if ($index == 2
80          && $data{element}{1} eq "translations"
81          && $element eq "translation") {
82                 foreach my $e (split(" ", $attrs{encoding})) {
83                         if (defined $attrs{hex}) {
84                                 my $k = $attrs{cldr};
85                                 my $hs = $attrs{hex};
86                                 $d{T}{$e}{$k}{hex} = $hs;
87                         }
88                         if (defined $attrs{string}) {
89                                 my $s = "";
90                                 for (my $i = 0; $i < length($attrs{string}); $i++) {
91                                         $s .= sprintf("%02x",
92                                             ord(substr($attrs{string}, $i, 1)));
93                                 }
94                                 $d{T}{$e}{$attrs{cldr}}{hex} = $s;
95                         }
96                         if (defined $attrs{unicode}) {
97                                 my $k = $attrs{cldr};
98                                 my $uc = $attrs{unicode};
99                                 $d{T}{$e}{$k}{unicode} = $uc;
100                         }
101                         if (defined $attrs{ucc}) {
102                                 my $k = $attrs{cldr};
103                                 my $uc = $attrs{ucc};
104                                 $d{T}{$e}{$k}{ucc} = $uc;
105                         }
106                 }
107                 return;
108         }
109
110         if ($index == 2
111          && $data{element}{1} eq "alternativemonths"
112          && $element eq "language") {
113                 my $name = $attrs{name};
114                 my $countries = $attrs{countries};
115
116                 $data{fields}{name} = $name;
117                 $data{fields}{countries} = $countries;
118                 $data{fields}{text} = "";
119
120                 return;
121         }
122 }
123
124 sub h_end {
125         my $expat = shift;
126         my $element = shift;
127
128         if ($index == "2") {
129                 if ($data{element}{1} eq "alternativemonths"
130                  && $data{element}{2} eq "language") {
131                         foreach my $c (split(/,/, $data{fields}{countries})) {
132                                 my $m = $data{fields}{text};
133
134                                 $m =~ s/^[\t ]//g;
135                                 $m =~ s/[\t ]$//g;
136                                 $d{AM}{$data{fields}{name}}{$c} = $m;
137                         }
138                         $data{fields} = ();
139                 }
140         }
141
142         $index--;
143 }
144
145 sub h_char {
146         my $expat = shift;
147         my $string = shift;
148
149         if ($index == "2") {
150                 if ($data{element}{1} eq "alternativemonths"
151                  && $data{element}{2} eq "language") {
152                         $data{fields}{text} .= $string;
153                 }
154         }
155 }
156
157 #use Data::Dumper;
158 #my %D = get_xmldata();
159 #print Dumper(%D);
160 1;