Added a few more Unicode digits from Unicode version 4.1.
[wine] / libs / unicode / cpmap.pl
1 #!/usr/bin/perl
2 #
3 # Generate code page .c files from ftp.unicode.org descriptions
4 #
5 # Copyright 2000 Alexandre Julliard
6 #
7 # This library is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU Lesser General Public
9 # License as published by the Free Software Foundation; either
10 # version 2.1 of the License, or (at your option) any later version.
11 #
12 # This library is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 # Lesser General Public License for more details.
16 #
17 # You should have received a copy of the GNU Lesser General Public
18 # License along with this library; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20 #
21
22 # base directory for ftp.unicode.org files
23 $BASEDIR = "ftp.unicode.org/Public/";
24 $MAPPREFIX = $BASEDIR . "MAPPINGS/";
25
26 # UnicodeData file
27 $UNICODEDATA = $BASEDIR . "UNIDATA/UnicodeData.txt";
28
29 # Sort keys file
30 $SORTKEYS = "www.unicode.org/reports/tr10/allkeys.txt";
31
32 # Defaults mapping
33 $DEFAULTS = "./defaults";
34
35 # Default char for undefined mappings
36 $DEF_CHAR = ord '?';
37
38 @allfiles =
39 (
40     [ 37,    "VENDORS/MICSFT/EBCDIC/CP037.TXT",   "IBM EBCDIC US Canada" ],
41     [ 424,   "VENDORS/MISC/CP424.TXT",            "IBM EBCDIC Hebrew" ],
42     [ 437,   "VENDORS/MICSFT/PC/CP437.TXT",       "OEM United States" ],
43     [ 500,   "VENDORS/MICSFT/EBCDIC/CP500.TXT",   "IBM EBCDIC International" ],
44     [ 737,   "VENDORS/MICSFT/PC/CP737.TXT",       "OEM Greek 437G" ],
45     [ 775,   "VENDORS/MICSFT/PC/CP775.TXT",       "OEM Baltic" ],
46     [ 850,   "VENDORS/MICSFT/PC/CP850.TXT",       "OEM Multilingual Latin 1" ],
47     [ 852,   "VENDORS/MICSFT/PC/CP852.TXT",       "OEM Slovak Latin 2" ],
48     [ 855,   "VENDORS/MICSFT/PC/CP855.TXT",       "OEM Cyrillic" ],
49     [ 856,   "VENDORS/MISC/CP856.TXT",            "Hebrew PC" ],
50     [ 857,   "VENDORS/MICSFT/PC/CP857.TXT",       "OEM Turkish" ],
51     [ 860,   "VENDORS/MICSFT/PC/CP860.TXT",       "OEM Portuguese" ],
52     [ 861,   "VENDORS/MICSFT/PC/CP861.TXT",       "OEM Icelandic" ],
53     [ 862,   "VENDORS/MICSFT/PC/CP862.TXT",       "OEM Hebrew" ],
54     [ 863,   "VENDORS/MICSFT/PC/CP863.TXT",       "OEM Canadian French" ],
55     [ 864,   "VENDORS/MICSFT/PC/CP864.TXT",       "OEM Arabic" ],
56     [ 865,   "VENDORS/MICSFT/PC/CP865.TXT",       "OEM Nordic" ],
57     [ 866,   "VENDORS/MICSFT/PC/CP866.TXT",       "OEM Russian" ],
58     [ 869,   "VENDORS/MICSFT/PC/CP869.TXT",       "OEM Greek" ],
59     [ 874,   "VENDORS/MICSFT/PC/CP874.TXT",       "ANSI/OEM Thai" ],
60     [ 875,   "VENDORS/MICSFT/EBCDIC/CP875.TXT",   "IBM EBCDIC Greek" ],
61     [ 878,   "VENDORS/MISC/KOI8-R.TXT",           "Russian KOI8" ],
62     [ 932,   "VENDORS/MICSFT/WINDOWS/CP932.TXT",  "ANSI/OEM Japanese Shift-JIS" ],
63     [ 936,   "VENDORS/MICSFT/WINDOWS/CP936.TXT",  "ANSI/OEM Simplified Chinese GBK" ],
64     [ 949,   "VENDORS/MICSFT/WINDOWS/CP949.TXT",  "ANSI/OEM Korean Unified Hangul" ],
65     [ 950,   "VENDORS/MICSFT/WINDOWS/CP950.TXT",  "ANSI/OEM Traditional Chinese Big5" ],
66     [ 1006,  "VENDORS/MISC/CP1006.TXT",           "IBM Arabic" ],
67     [ 1026,  "VENDORS/MICSFT/EBCDIC/CP1026.TXT",  "IBM EBCDIC Latin 5 Turkish" ],
68     [ 1250,  "VENDORS/MICSFT/WINDOWS/CP1250.TXT", "ANSI Eastern Europe" ],
69     [ 1251,  "VENDORS/MICSFT/WINDOWS/CP1251.TXT", "ANSI Cyrillic" ],
70     [ 1252,  "VENDORS/MICSFT/WINDOWS/CP1252.TXT", "ANSI Latin 1" ],
71     [ 1253,  "VENDORS/MICSFT/WINDOWS/CP1253.TXT", "ANSI Greek" ],
72     [ 1254,  "VENDORS/MICSFT/WINDOWS/CP1254.TXT", "ANSI Turkish" ],
73     [ 1255,  "VENDORS/MICSFT/WINDOWS/CP1255.TXT", "ANSI Hebrew" ],
74     [ 1256,  "VENDORS/MICSFT/WINDOWS/CP1256.TXT", "ANSI Arabic" ],
75     [ 1257,  "VENDORS/MICSFT/WINDOWS/CP1257.TXT", "ANSI Baltic" ],
76     [ 1258,  "VENDORS/MICSFT/WINDOWS/CP1258.TXT", "ANSI/OEM Viet Nam" ],
77     [ 10000, "VENDORS/MICSFT/MAC/ROMAN.TXT",      "Mac Roman" ],
78     [ 10006, "VENDORS/MICSFT/MAC/GREEK.TXT",      "Mac Greek" ],
79     [ 10007, "VENDORS/MICSFT/MAC/CYRILLIC.TXT",   "Mac Cyrillic" ],
80     [ 10029, "VENDORS/MICSFT/MAC/LATIN2.TXT",     "Mac Latin 2" ],
81     [ 10079, "VENDORS/MICSFT/MAC/ICELAND.TXT",    "Mac Icelandic" ],
82     [ 10081, "VENDORS/MICSFT/MAC/TURKISH.TXT",    "Mac Turkish" ],
83     [ 20866, "VENDORS/MISC/KOI8-R.TXT",           "Russian KOI8" ],
84     [ 20932, "OBSOLETE/EASTASIA/JIS/JIS0208.TXT", "EUC-JP" ],
85     [ 21866, "VENDORS/MISC/KOI8-U.TXT",           "Ukrainian KOI8" ],
86     [ 28591, "ISO8859/8859-1.TXT",                "ISO 8859-1 Latin 1" ],
87     [ 28592, "ISO8859/8859-2.TXT",                "ISO 8859-2 Latin 2 (East European)" ],
88     [ 28593, "ISO8859/8859-3.TXT",                "ISO 8859-3 Latin 3 (South European)" ],
89     [ 28594, "ISO8859/8859-4.TXT",                "ISO 8859-4 Latin 4 (Baltic old)" ],
90     [ 28595, "ISO8859/8859-5.TXT",                "ISO 8859-5 Cyrillic" ],
91     [ 28596, "ISO8859/8859-6.TXT",                "ISO 8859-6 Arabic" ],
92     [ 28597, "ISO8859/8859-7.TXT",                "ISO 8859-7 Greek" ],
93     [ 28598, "ISO8859/8859-8.TXT",                "ISO 8859-8 Hebrew" ],
94     [ 28599, "ISO8859/8859-9.TXT",                "ISO 8859-9 Latin 5 (Turkish)" ],
95     [ 28600, "ISO8859/8859-10.TXT",               "ISO 8859-10 Latin 6 (Nordic)" ],
96     [ 28603, "ISO8859/8859-13.TXT",               "ISO 8859-13 Latin 7 (Baltic)" ],
97     [ 28604, "ISO8859/8859-14.TXT",               "ISO 8859-14 Latin 8 (Celtic)" ],
98     [ 28605, "ISO8859/8859-15.TXT",               "ISO 8859-15 Latin 9 (Euro)" ],
99     [ 28606, "ISO8859/8859-16.TXT",               "ISO 8859-16 Latin 10 (Balkan)" ]
100 );
101
102
103 %ctype =
104 (
105     "upper"  => 0x0001,
106     "lower"  => 0x0002,
107     "digit"  => 0x0004,
108     "space"  => 0x0008,
109     "punct"  => 0x0010,
110     "cntrl"  => 0x0020,
111     "blank"  => 0x0040,
112     "xdigit" => 0x0080,
113     "alpha"  => 0x0100
114 );
115
116 %categories =
117 (
118     "Lu" => $ctype{"alpha"}|$ctype{"upper"}, # Letter, Uppercase
119     "Ll" => $ctype{"alpha"}|$ctype{"lower"}, # Letter, Lowercase
120     "Lt" => $ctype{"alpha"},    # Letter, Titlecase
121     "Mn" => $ctype{"punct"},    # Mark, Non-Spacing
122     "Mc" => $ctype{"punct"},    # Mark, Spacing Combining
123     "Me" => $ctype{"punct"},    # Mark, Enclosing
124     "Nd" => $ctype{"digit"},    # Number, Decimal Digit
125     "Nl" => $ctype{"punct"},    # Number, Letter
126     "No" => $ctype{"punct"},    # Number, Other
127     "Zs" => $ctype{"space"},    # Separator, Space
128     "Zl" => $ctype{"space"},    # Separator, Line
129     "Zp" => $ctype{"space"},    # Separator, Paragraph
130     "Cc" => $ctype{"cntrl"},    # Other, Control
131     "Cf" => 0,                  # Other, Format
132     "Cs" => 0,                  # Other, Surrogate
133     "Co" => 0,                  # Other, Private Use
134     "Cn" => 0,                  # Other, Not Assigned
135     "Lm" => $ctype{"punct"},    # Letter, Modifier
136     "Lo" => $ctype{"alpha"},    # Letter, Other
137     "Pc" => $ctype{"punct"},    # Punctuation, Connector
138     "Pd" => $ctype{"punct"},    # Punctuation, Dash
139     "Ps" => $ctype{"punct"},    # Punctuation, Open
140     "Pe" => $ctype{"punct"},    # Punctuation, Close
141     "Pi" => $ctype{"punct"},    # Punctuation, Initial quote
142     "Pf" => $ctype{"punct"},    # Punctuation, Final quote
143     "Po" => $ctype{"punct"},    # Punctuation, Other
144     "Sm" => $ctype{"punct"},    # Symbol, Math
145     "Sc" => $ctype{"punct"},    # Symbol, Currency
146     "Sk" => $ctype{"punct"},    # Symbol, Modifier
147     "So" => $ctype{"punct"}     # Symbol, Other
148 );
149
150 # a few characters need additional categories that cannot be determined automatically
151 %special_categories =
152 (
153     "xdigit" => [ ord('0')..ord('9'),ord('A')..ord('F'),ord('a')..ord('f'),
154                   0xff10..0xff19, 0xff21..0xff26, 0xff41..0xff46 ],
155     "space"  => [ 0x09..0x0d, 0x85 ],
156     "blank"  => [ 0x09, 0x20, 0xa0, 0x3000, 0xfeff ],
157     "cntrl"  => [ 0x070f, 0x180b, 0x180c, 0x180d, 0x180e, 0x200c, 0x200d,
158                   0x200e, 0x200f, 0x202a, 0x202b, 0x202c, 0x202d, 0x202e,
159                   0x206a, 0x206b, 0x206c, 0x206d, 0x206e, 0x206f, 0xfeff,
160                   0xfff9, 0xfffa, 0xfffb ]
161 );
162
163 %directions =
164 (
165     "L"   => 1,    # Left-to-Right
166     "LRE" => 11,   # Left-to-Right Embedding
167     "LRO" => 11,   # Left-to-Right Override
168     "R"   => 2,    # Right-to-Left
169     "AL"  => 2,    # Right-to-Left Arabic
170     "RLE" => 11,   # Right-to-Left Embedding
171     "RLO" => 11,   # Right-to-Left Override
172     "PDF" => 11,   # Pop Directional Format
173     "EN"  => 3,    # European Number
174     "ES"  => 4,    # European Number Separator
175     "ET"  => 5,    # European Number Terminator
176     "AN"  => 6,    # Arabic Number
177     "CS"  => 7,    # Common Number Separator
178     "NSM" => 0,    # Non-Spacing Mark
179     "BN"  => 0,    # Boundary Neutral
180     "B"   => 8,    # Paragraph Separator
181     "S"   => 9,    # Segment Separator
182     "WS"  => 10,   # Whitespace
183     "ON"  => 11    # Other Neutrals
184 );
185
186
187 ################################################################
188 # main routine
189
190 READ_DEFAULTS();
191 my @sortkeys = READ_SORTKEYS_FILE();
192 DUMP_CASE_MAPPINGS();
193 DUMP_SORTKEYS(@sortkeys);
194 DUMP_COMPOSE_TABLES();
195 DUMP_CTYPE_TABLES();
196
197 foreach $file (@allfiles) { HANDLE_FILE( @$file ); }
198
199 OUTPUT_CPTABLE();
200
201 exit(0);
202
203
204 ################################################################
205 # read in the defaults file
206 sub READ_DEFAULTS
207 {
208     @unicode_defaults = ();
209     @unicode_aliases = ();
210     @tolower_table = ();
211     @toupper_table = ();
212     @digitmap_table = ();
213     @compatmap_table = ();
214     @category_table = ();
215     @direction_table = ();
216     @decomp_table = ();
217     @compose_table = ();
218
219     # first setup a few default mappings
220
221     open DEFAULTS or die "Cannot open $DEFAULTS";
222     print "Loading $DEFAULTS\n";
223     while (<DEFAULTS>)
224     {
225         next if /^\#/;  # skip comments
226         next if /^$/;  # skip empty lines
227         if (/^(([0-9a-fA-F]+)(,[0-9a-fA-F]+)*)\s+([0-9a-fA-F]+|'.'|none)\s+(\#.*)?/)
228         {
229             my @src = map hex, split /,/,$1;
230             my $dst = $4;
231             my $comment = $5;
232             if ($#src > 0) { push @unicode_aliases, \@src; }
233             next if ($dst eq "none");
234             $dst = ($dst =~ /\'.\'/) ? ord substr($dst,1,1) : hex $dst;
235             foreach $src (@src)
236             {
237                 die "Duplicate value" if defined($unicode_defaults[$src]);
238                 $unicode_defaults[$src] = $dst;
239             }
240             next;
241         }
242         die "Unrecognized line $_\n";
243     }
244
245     # now build mappings from the decomposition field of the Unicode database
246
247     open UNICODEDATA or die "Cannot open $UNICODEDATA";
248     print "Loading $UNICODEDATA\n";
249     while (<UNICODEDATA>)
250     {
251         # Decode the fields ...
252         ($code, $name, $cat, $comb, $bidi,
253          $decomp, $dec, $dig, $num, $mirror,
254          $oldname, $comment, $upper, $lower, $title) = split /;/;
255
256         my $src = hex $code;
257
258         die "unknown category $cat" unless defined $categories{$cat};
259         die "unknown directionality $bidi" unless defined $directions{$bidi};
260
261         $uniname[$src] = $name;
262         $category_table[$src] = $categories{$cat};
263         $direction_table[$src] = $directions{$bidi};
264
265         if ($lower ne "")
266         {
267             $tolower_table[$src] = hex $lower;
268             $category_table[$src] |= $ctype{"upper"}|$ctype{"alpha"};
269         }
270         if ($upper ne "")
271         {
272             $toupper_table[$src] = hex $upper;
273             $category_table[$src] |= $ctype{"lower"}|$ctype{"alpha"};
274         }
275         if ($dec ne "")
276         {
277             $category_table[$src] |= $ctype{"digit"};
278         }
279         if ($dig ne "")
280         {
281             $digitmap_table[$src] = ord $dig;
282         }
283
284         # copy the category and direction for everything between First/Last pairs
285         if ($name =~ /, First>/) { $start = $src; }
286         if ($name =~ /, Last>/)
287         {
288             while ($start < $src)
289             {
290                 $category_table[$start] = $category_table[$src];
291                 $direction_table[$start] = $direction_table[$src];
292                 $start++;
293             }
294         }
295
296         next if $decomp eq "";  # no decomposition, skip it
297
298         if ($decomp =~ /^<([a-zA-Z]+)>\s+([0-9a-fA-F]+)$/)
299         {
300             # decomposition of the form "<foo> 1234" -> use char if type is known
301             if (($src >= 0xf900 && $src < 0xfb00) || ($src >= 0xfe30 && $src < 0xfffd))
302             {
303                 # Single char decomposition in the compatibility range
304                 $compatmap_table[$src] = hex $2;
305             }
306             next unless ($1 eq "font" ||
307                          $1 eq "noBreak" ||
308                          $1 eq "circle" ||
309                          $1 eq "super" ||
310                          $1 eq "sub" ||
311                          $1 eq "wide" ||
312                          $1 eq "narrow" ||
313                          $1 eq "compat" ||
314                          $1 eq "small");
315             $dst = hex $2;
316         }
317         elsif ($decomp =~ /^<compat>\s+0020\s+([0-9a-fA-F]+)/)
318         {
319             # decomposition "<compat> 0020 1234" -> combining accent
320             $dst = hex $1;
321         }
322         elsif ($decomp =~ /^([0-9a-fA-F]+)/)
323         {
324             # decomposition contains only char values without prefix -> use first char
325             $dst = hex $1;
326             $category_table[$src] |= $category_table[$dst];
327             # store decomposition if it contains two chars
328             if ($decomp =~ /^([0-9a-fA-F]+)\s+([0-9a-fA-F]+)$/)
329             {
330                 $decomp_table[$src] = [ hex $1, hex $2 ];
331                 push @compose_table, [ hex $1, hex $2, $src ];
332             }
333             elsif ($decomp =~ /^(<[a-z]+>\s)*([0-9a-fA-F]+)$/ &&
334                    (($src >= 0xf900 && $src < 0xfb00) || ($src >= 0xfe30 && $src < 0xfffd)))
335             {
336                 # Single char decomposition in the compatibility range
337                 $compatmap_table[$src] = hex $2;
338             }
339         }
340         else
341         {
342             next;
343         }
344
345         next if defined($unicode_defaults[$src]);  # may have been set in the defaults file
346
347         # check for loops
348         for ($i = $dst; ; $i = $unicode_defaults[$i])
349         {
350             die sprintf("loop detected for %04x -> %04x",$src,$dst) if $i == $src;
351             last unless defined($unicode_defaults[$i]);
352         }
353         $unicode_defaults[$src] = $dst;
354     }
355
356     # patch the category of some special characters
357
358     foreach $cat (keys %special_categories)
359     {
360         my $flag = $ctype{$cat};
361         foreach $i (@{$special_categories{$cat}}) { $category_table[$i] |= $flag; }
362     }
363 }
364
365
366 ################################################################
367 # parse the input file
368 sub READ_FILE
369 {
370     my $name = shift;
371     open INPUT,$name or die "Cannot open $name";
372     @cp2uni = ();
373     @lead_bytes = ();
374     @uni2cp = ();
375
376     while (<INPUT>)
377     {
378         next if /^\#/;  # skip comments
379         next if /^$/;  # skip empty lines
380         next if /\x1a/;  # skip ^Z
381         next if (/^0x([0-9a-fA-F]+)\s+\#UNDEFINED/);  # undefined char
382
383         if (/^0x([0-9a-fA-F]+)\s+\#DBCS LEAD BYTE/)
384         {
385             $cp = hex $1;
386             push @lead_bytes,$cp;
387             $cp2uni[$cp] = 0;
388             next;
389         }
390         if (/^0x([0-9a-fA-F]+)\s+0x([0-9a-fA-F]+)\s+(\#.*)?/)
391         {
392             $cp = hex $1;
393             $uni = hex $2;
394             $cp2uni[$cp] = $uni unless defined($cp2uni[$cp]);
395             $uni2cp[$uni] = $cp unless defined($uni2cp[$uni]);
396             next;
397         }
398         die "$name: Unrecognized line $_\n";
399     }
400 }
401
402
403 ################################################################
404 # build EUC-JP table from the JIS 0208 file
405 # FIXME: for proper EUC-JP we should probably read JIS 0212 too
406 # but this would require 3-byte DBCS characters
407 sub READ_JIS0208_FILE
408 {
409     my $name = shift;
410     @cp2uni = ();
411     @lead_bytes = ();
412     @uni2cp = ();
413
414     # ASCII chars
415     for ($i = 0x00; $i <= 0x7f; $i++)
416     {
417         $cp2uni[$i] = $i;
418         $uni2cp[$i] = $i;
419     }
420
421     # JIS X 0201 right plane
422     for ($i = 0xa1; $i <= 0xdf; $i++)
423     {
424         $cp2uni[0x8e00 + $i] = 0xfec0 + $i;
425         $uni2cp[0xfec0 + $i] = 0x8e00 + $i;
426     }
427
428     # lead bytes
429     foreach $i (0x8e, 0x8f, 0xa1 .. 0xfe)
430     {
431         push @lead_bytes,$i;
432         $cp2uni[$i] = 0;
433     }
434
435     # undefined chars
436     foreach $i (0x80 .. 0x8d, 0x90 .. 0xa0, 0xff)
437     {
438         $cp2uni[$i] = $DEF_CHAR;
439     }
440
441     # Shift-JIS compatibility
442     $uni2cp[0x00a5] = 0x5c;
443     $uni2cp[0x203e] = 0x7e;
444
445     # Fix backslash conversion
446     $cp2uni[0xa1c0] = 0xff3c;
447     $uni2cp[0xff3c] = 0xa1c0;
448
449     open INPUT, "$name" or die "Cannot open $name";
450     while (<INPUT>)
451     {
452         next if /^\#/;  # skip comments
453         next if /^$/;  # skip empty lines
454         next if /\x1a/;  # skip ^Z
455         if (/^0x[0-9a-fA-F]+\s+0x([0-9a-fA-F]+)\s+0x([0-9a-fA-F]+)\s+(\#.*)?/)
456         {
457             $cp = 0x8080 + hex $1;
458             $uni = hex $2;
459             $cp2uni[$cp] = $uni unless defined($cp2uni[$cp]);
460             $uni2cp[$uni] = $cp unless defined($uni2cp[$uni]);
461             next;
462         }
463         die "$name: Unrecognized line $_\n";
464     }
465 }
466
467
468 ################################################################
469 # build the sort keys table
470 sub READ_SORTKEYS_FILE
471 {
472     my @sortkeys = ();
473     for (my $i = 0; $i < 65536; $i++) { $sortkeys[$i] = [ -1, 0, 0, 0, 0 ] };
474
475     open INPUT, "$SORTKEYS" or die "Cannot open $SORTKEYS";
476     print "Loading $SORTKEYS\n";
477     while (<INPUT>)
478     {
479         next if /^\#/;  # skip comments
480         next if /^$/;  # skip empty lines
481         next if /\x1a/;  # skip ^Z
482         next if /^\@version/;  # skip @version header
483         if (/^([0-9a-fA-F]+)\s+;\s+\[([*.])([0-9a-fA-F]{4})\.([0-9a-fA-F]{4})\.([0-9a-fA-F]{4})\.([0-9a-fA-F]+)\]/)
484         {
485             my ($uni,$variable) = (hex $1, $2);
486             next if $uni > 65535;
487             $sortkeys[$uni] = [ $uni, hex $3, hex $4, hex $5, hex $6 ];
488             next;
489         }
490         if (/^([0-9a-fA-F]+\s+)+;\s+\[[*.]([0-9a-fA-F]{4})\.([0-9a-fA-F]{4})\.([0-9a-fA-F]{4})\.([0-9a-fA-F]+)\]/)
491         {
492             # multiple character sequence, ignored for now
493             next;
494         }
495         die "$SORTKEYS: Unrecognized line $_\n";
496     }
497     close INPUT;
498
499     # compress the keys to 32 bit:
500     # key 1 to 16 bits, key 2 to 8 bits, key 3 to 4 bits, key 4 to 1 bit
501
502     @sortkeys = sort { ${$a}[1] <=> ${$b}[1] or 
503                        ${$a}[2] <=> ${$b}[2] or
504                        ${$a}[3] <=> ${$b}[3] or
505                        ${$a}[4] <=> ${$b}[4] or
506                        $a cmp $b; } @sortkeys;
507
508     my ($n2, $n3) = (1, 1);
509     my @keys = (-1, -1, -1, -1, -1 );
510     my @flatkeys = ();
511
512     for (my $i = 0; $i < 65536; $i++)
513     {
514         my @current = @{$sortkeys[$i]};
515         next if $current[0] == -1;
516         if ($current[1] == $keys[1])
517         {
518             if ($current[2] == $keys[2])
519             {
520                 if ($current[3] == $keys[3])
521                 {
522                     # nothing
523                 }
524                 else
525                 {
526                     $keys[3] = $current[3];
527                     $n3++;
528                     die if ($n3 >= 16);
529                 }
530             }
531             else
532             {
533                 $keys[2] = $current[2];
534                 $keys[3] = $current[3];
535                 $n2++;
536                 $n3 = 1;
537                 die if ($n2 >= 256);
538             }
539         }
540         else
541         {
542             $keys[1] = $current[1];
543             $keys[2] = $current[2];
544             $keys[3] = $current[3];
545             $n2 = 1;
546             $n3 = 1;
547         }
548
549         if ($current[2]) { $current[2] = $n2; }
550         if ($current[3]) { $current[3] = $n3; }
551         if ($current[4]) { $current[4] = 1; }
552
553         $flatkeys[$current[0]] = ($current[1] << 16) | ($current[2] << 8) | ($current[3] << 4) | $current[4];
554     }
555     return @flatkeys;
556 }
557
558
559 ################################################################
560 # build the sort keys table
561 sub DUMP_SORTKEYS
562 {
563     my @keys = @_;
564
565     # count the number of 256-key ranges that contain something
566
567     my @offsets = ();
568     my $ranges = 2;
569     for (my $i = 0; $i < 256; $i++) { $offsets[$i] = 256; }
570     for (my $i = 0; $i < 65536; $i++)
571     {
572         next unless defined $keys[$i];
573         $offsets[$i >> 8] = $ranges * 256;
574         $ranges++;
575         $i |= 255;
576     }
577
578     # output the range offsets
579
580     open OUTPUT,">collation.c.new" or die "Cannot create collation.c";
581     printf "Building collation.c\n";
582     printf OUTPUT "/* Unicode collation element table */\n";
583     printf OUTPUT "/* generated from %s */\n", $SORTKEYS;
584     printf OUTPUT "/* DO NOT EDIT!! */\n\n";
585
586     printf OUTPUT "const unsigned int collation_table[%d] =\n{\n", $ranges*256;
587     printf OUTPUT "    /* index */\n";
588     printf OUTPUT "%s,\n", DUMP_ARRAY( "0x%08x", 0, @offsets );
589
590     # output the default values
591
592     printf OUTPUT "    /* defaults */\n";
593     printf OUTPUT "%s", DUMP_ARRAY( "0x%08x", 0, (-1) x 256 );
594
595     # output all the key ranges
596
597     for (my $i = 0; $i < 256; $i++)
598     {
599         next if $offsets[$i] == 256;
600         printf OUTPUT ",\n    /* 0x%02x00 .. 0x%02xff */\n", $i, $i;
601         printf OUTPUT "%s", DUMP_ARRAY( "0x%08x", -1, @keys[($i<<8) .. ($i<<8)+255] );
602     }
603     printf OUTPUT "\n};\n";
604     close OUTPUT;
605     save_file("collation.c");
606 }
607
608
609 ################################################################
610 # add default mappings once the file had been read
611 sub ADD_DEFAULT_MAPPINGS
612 {
613     # Apply aliases
614
615     foreach $alias (@unicode_aliases)
616     {
617         my $target = undef;
618         foreach $src (@$alias)
619         {
620             if (defined($uni2cp[$src]))
621             {
622                 $target = $uni2cp[$src];
623                 last;
624             }
625         }
626         next unless defined($target);
627
628         # At least one char of the alias set is defined, set the others to the same value
629         foreach $src (@$alias)
630         {
631             $uni2cp[$src] = $target unless defined($uni2cp[$src]);
632         }
633     }
634
635     # For every src -> target mapping in the defaults table,
636     # make uni2cp[src] = uni2cp[target] if uni2cp[target] is defined
637
638     for ($src = 0; $src < 65536; $src++)
639     {
640         next if defined($uni2cp[$src]);  # source has a definition already
641         next unless defined($unicode_defaults[$src]);  # no default for this char
642         my $target = $unicode_defaults[$src];
643
644         # do a recursive mapping until we find a target char that is defined
645         while (!defined($uni2cp[$target]) &&
646                defined($unicode_defaults[$target])) { $target = $unicode_defaults[$target]; }
647
648         if (defined($uni2cp[$target])) { $uni2cp[$src] = $uni2cp[$target]; }
649     }
650
651     # Add an identity mapping for all undefined chars
652
653     for ($i = 0; $i < 256; $i++)
654     {
655         next if defined($cp2uni[$i]);
656         next if defined($uni2cp[$i]);
657         $cp2uni[$i] = $uni2cp[$i] = $i;
658     }
659 }
660
661 ################################################################
662 # dump an array of integers
663 sub DUMP_ARRAY
664 {
665     my ($format,$default,@array) = @_;
666     my $i, $ret = "    ";
667     for ($i = 0; $i < $#array; $i++)
668     {
669         $ret .= sprintf($format, defined $array[$i] ? $array[$i] : $default);
670         $ret .= (($i % 8) != 7) ? ", " : ",\n    ";
671     }
672     $ret .= sprintf($format, defined $array[$i] ? $array[$i] : $default);
673     return $ret;
674 }
675
676 ################################################################
677 # dump an SBCS mapping table
678 sub DUMP_SBCS_TABLE
679 {
680     my ($codepage, $name) = @_;
681     my $i;
682
683     # output the ascii->unicode table
684
685     printf OUTPUT "static const WCHAR cp2uni[256] =\n";
686     printf OUTPUT "{\n%s\n};\n\n", DUMP_ARRAY( "0x%04x", $DEF_CHAR, @cp2uni[0 .. 255] );
687
688     # count the number of unicode->ascii subtables that contain something
689
690     my @filled = ();
691     my $subtables = 1;
692     for ($i = 0; $i < 65536; $i++)
693     {
694         next unless defined $uni2cp[$i];
695         $filled[$i >> 8] = 1;
696         $subtables++;
697         $i |= 255;
698     }
699
700     # output all the subtables into a single array
701
702     printf OUTPUT "static const unsigned char uni2cp_low[%d] =\n{\n", $subtables*256;
703     for ($i = 0; $i < 256; $i++)
704     {
705         next unless $filled[$i];
706         printf OUTPUT "    /* 0x%02x00 .. 0x%02xff */\n", $i, $i;
707         printf OUTPUT "%s,\n", DUMP_ARRAY( "0x%02x", $DEF_CHAR, @uni2cp[($i<<8) .. ($i<<8)+255] );
708     }
709     printf OUTPUT "    /* defaults */\n";
710     printf OUTPUT "%s\n};\n\n", DUMP_ARRAY( "0x%02x", 0, ($DEF_CHAR) x 256 );
711
712     # output a table of the offsets of the subtables in the previous array
713
714     my $pos = 0;
715     my @offsets = ();
716     for ($i = 0; $i < 256; $i++)
717     {
718         if ($filled[$i]) { push @offsets, $pos; $pos += 256; }
719         else { push @offsets, ($subtables-1) * 256; }
720     }
721     printf OUTPUT "static const unsigned short uni2cp_high[256] =\n";
722     printf OUTPUT "{\n%s\n};\n\n", DUMP_ARRAY( "0x%04x", 0, @offsets );
723
724     # output the code page descriptor
725
726     printf OUTPUT "const struct sbcs_table cptable_%03d =\n{\n", $codepage;
727     printf OUTPUT "    { %d, 1, 0x%04x, 0x%04x, \"%s\" },\n",
728                   $codepage, $DEF_CHAR, $DEF_CHAR, $name;
729     printf OUTPUT "    cp2uni,\n";
730     printf OUTPUT "    uni2cp_low,\n";
731     printf OUTPUT "    uni2cp_high\n};\n";
732 }
733
734
735 ################################################################
736 # dump a DBCS mapping table
737 sub DUMP_DBCS_TABLE
738 {
739     my ($codepage, $name) = @_;
740     my $i, $x, $y;
741
742     # build a list of lead bytes that are actually used
743
744     my @lblist = ();
745     LBLOOP: for ($y = 0; $y <= $#lead_bytes; $y++)
746     {
747         my $base = $lead_bytes[$y] << 8;
748         for ($x = 0; $x < 256; $x++)
749         {
750             if (defined $cp2uni[$base+$x])
751             {
752                 push @lblist,$lead_bytes[$y];
753                 next LBLOOP;
754             }
755         }
756     }
757     my $unused = ($#lead_bytes > $#lblist);
758
759     # output the ascii->unicode table for the single byte chars
760
761     printf OUTPUT "static const WCHAR cp2uni[%d] =\n", 256 * ($#lblist + 2 + $unused);
762     printf OUTPUT "{\n%s,\n", DUMP_ARRAY( "0x%04x", $DEF_CHAR, @cp2uni[0 .. 255] );
763
764     # output the default table for unused lead bytes
765
766     if ($unused)
767     {
768         printf OUTPUT "    /* unused lead bytes */\n";
769         printf OUTPUT "%s,\n", DUMP_ARRAY( "0x%04x", 0, ($DEF_CHAR) x 256 );
770     }
771
772     # output the ascii->unicode table for each DBCS lead byte
773
774     for ($y = 0; $y <= $#lblist; $y++)
775     {
776         my $base = $lblist[$y] << 8;
777         printf OUTPUT "    /* lead byte %02x */\n", $lblist[$y];
778         printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", $DEF_CHAR, @cp2uni[$base .. $base+255] );
779         printf OUTPUT ($y < $#lblist) ? ",\n" : "\n};\n\n";
780     }
781
782     # output the lead byte subtables offsets
783
784     my @offsets = ();
785     for ($x = 0; $x < 256; $x++) { $offsets[$x] = 0; }
786     for ($x = 0; $x <= $#lblist; $x++) { $offsets[$lblist[$x]] = $x + 1; }
787     if ($unused)
788     {
789         # increment all lead bytes offset to take into account the unused table
790         for ($x = 0; $x <= $#lead_bytes; $x++) { $offsets[$lead_bytes[$x]]++; }
791     }
792     printf OUTPUT "static const unsigned char cp2uni_leadbytes[256] =\n";
793     printf OUTPUT "{\n%s\n};\n\n", DUMP_ARRAY( "0x%02x", 0, @offsets );
794
795     # count the number of unicode->ascii subtables that contain something
796
797     my @filled = ();
798     my $subtables = 1;
799     for ($i = 0; $i < 65536; $i++)
800     {
801         next unless defined $uni2cp[$i];
802         $filled[$i >> 8] = 1;
803         $subtables++;
804         $i |= 255;
805     }
806
807     # output all the subtables into a single array
808
809     printf OUTPUT "static const unsigned short uni2cp_low[%d] =\n{\n", $subtables*256;
810     for ($y = 0; $y < 256; $y++)
811     {
812         next unless $filled[$y];
813         printf OUTPUT "    /* 0x%02x00 .. 0x%02xff */\n", $y, $y;
814         printf OUTPUT "%s,\n", DUMP_ARRAY( "0x%04x", $DEF_CHAR, @uni2cp[($y<<8) .. ($y<<8)+255] );
815     }
816     printf OUTPUT "    /* defaults */\n";
817     printf OUTPUT "%s\n};\n\n", DUMP_ARRAY( "0x%04x", 0, ($DEF_CHAR) x 256 );
818
819     # output a table of the offsets of the subtables in the previous array
820
821     my $pos = 0;
822     my @offsets = ();
823     for ($y = 0; $y < 256; $y++)
824     {
825         if ($filled[$y]) { push @offsets, $pos; $pos += 256; }
826         else { push @offsets, ($subtables-1) * 256; }
827     }
828     printf OUTPUT "static const unsigned short uni2cp_high[256] =\n";
829     printf OUTPUT "{\n%s\n};\n\n", DUMP_ARRAY( "0x%04x", 0, @offsets );
830
831     # output the code page descriptor
832
833     printf OUTPUT "const struct dbcs_table cptable_%03d =\n{\n", $codepage;
834     printf OUTPUT "    { %d, 2, 0x%04x, 0x%04x, \"%s\" },\n",
835                   $codepage, $DEF_CHAR, $DEF_CHAR, $name;
836     printf OUTPUT "    cp2uni,\n";
837     printf OUTPUT "    cp2uni_leadbytes,\n";
838     printf OUTPUT "    uni2cp_low,\n";
839     printf OUTPUT "    uni2cp_high,\n";
840     DUMP_LB_RANGES();
841     printf OUTPUT "};\n";
842 }
843
844
845 ################################################################
846 # dump the list of defined lead byte ranges
847 sub DUMP_LB_RANGES
848 {
849     my @list = ();
850     my $i = 0;
851     foreach $i (@lead_bytes) { $list[$i] = 1; }
852     my $on = 0;
853     printf OUTPUT "    { ";
854     for ($i = 0; $i < 256; $i++)
855     {
856         if ($on)
857         {
858             if (!defined $list[$i]) { printf OUTPUT "0x%02x, ", $i-1; $on = 0; }
859         }
860         else
861         {
862             if ($list[$i]) { printf OUTPUT "0x%02x, ", $i; $on = 1; }
863         }
864     }
865     if ($on) { printf OUTPUT "0xff, "; }
866     printf OUTPUT "0x00, 0x00 }\n";
867 }
868
869
870 ################################################################
871 # dump the case mapping tables
872 sub DUMP_CASE_MAPPINGS
873 {
874     open OUTPUT,">casemap.c.new" or die "Cannot create casemap.c";
875     printf "Building casemap.c\n";
876     printf OUTPUT "/* Unicode case mappings */\n";
877     printf OUTPUT "/* Automatically generated; DO NOT EDIT!! */\n\n";
878     printf OUTPUT "#include \"wine/unicode.h\"\n\n";
879
880     DUMP_CASE_TABLE( "wine_casemap_lower", @tolower_table );
881     DUMP_CASE_TABLE( "wine_casemap_upper", @toupper_table );
882     DUMP_CASE_TABLE( "wine_digitmap",  @digitmap_table );
883     DUMP_CASE_TABLE( "wine_compatmap", @compatmap_table );
884     close OUTPUT;
885     save_file("casemap.c");
886 }
887
888
889 ################################################################
890 # dump a case mapping table
891 sub DUMP_CASE_TABLE
892 {
893     my ($name,@table) = @_;
894
895     # count the number of sub tables that contain something
896     # also compute the low and upper populated bounds
897
898     my @lowerbounds = ( 0, 0 );
899     my @upperbounds = ( 0, 255 );
900     my $index = 0;
901     my @filled = ();
902     for ($i = 0; $i < 65536; $i++)
903     {
904         next unless defined $table[$i];
905         if (!defined $filled[$i >> 8])
906         {
907           $lowerbounds[$index] = $i & 0xff;
908           $upperbounds[$index] = 0xff - $lowerbounds[$index];
909           $filled[$i >> 8] = $index * 256 + 512;
910           $index++;
911         }
912         else
913         {
914           $upperbounds[$index-1] = 0xff - ($i & 0xff);
915         }
916         $table[$i] = ($table[$i] - $i) & 0xffff;
917     }
918
919     # Collapse blocks upwards if possible
920     my $removed = 0;
921     $index = 0;
922     for ($i = 0; $i < 256; $i++)
923     {
924         next unless defined $filled[$i];
925         if ($upperbounds[$index - 1] > $lowerbounds[$index])
926         {
927            $removed = $removed + $lowerbounds[$index];
928         }
929         else
930         {
931            $removed = $removed + $upperbounds[$index - 1];
932            $lowerbounds[$index] = $upperbounds[$index - 1];
933         }
934         $filled[$i] = $filled[$i] - $removed;
935         $index++;
936     }
937
938     # dump the table
939
940     printf OUTPUT "const WCHAR %s[%d] =\n", $name, $index * 256 + 512 - $removed;
941     printf OUTPUT "{\n    /* index */\n";
942     printf OUTPUT "%s,\n", DUMP_ARRAY( "0x%04x", 256, @filled );
943     printf OUTPUT "    /* defaults */\n";
944     printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0, (0) x 256 );
945     $index = 0;
946     for ($i = 0; $i < 256; $i++)
947     {
948         next unless $filled[$i];
949         printf OUTPUT ",\n    /* 0x%02x%02x .. 0x%02xff */\n", $i, $lowerbounds[$index], $i;
950         printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0,
951                       @table[($i<<8) + $lowerbounds[$index] .. ($i<<8)+255] );
952         $index++;
953     }
954     printf OUTPUT "\n};\n";
955 }
956
957
958 ################################################################
959 # dump the ctype tables
960 sub DUMP_CTYPE_TABLES
961 {
962     open OUTPUT,">wctype.c.new" or die "Cannot create wctype.c";
963     printf "Building wctype.c\n";
964     printf OUTPUT "/* Unicode ctype tables */\n";
965     printf OUTPUT "/* Automatically generated; DO NOT EDIT!! */\n\n";
966     printf OUTPUT "#include \"wine/unicode.h\"\n\n";
967
968     my $i;
969     my @array = (0) x 256;
970
971     # add the direction in the high 4 bits of the category
972     for ($i = 0; $i < 65536; $i++)
973     {
974         $category_table[$i] |= $direction_table[$i] << 12;
975     }
976
977     # try to merge table rows
978     for ($row = 0; $row < 256; $row++)
979     {
980         my $rowtxt = sprintf "%04x" x 256, @category_table[($row<<8)..($row<<8)+255];
981         if (defined($sequences{$rowtxt}))
982         {
983             # reuse an existing row
984             $array[$row] = $sequences{$rowtxt};
985         }
986         else
987         {
988             # create a new row
989             $sequences{$rowtxt} = $array[$row] = $#array + 1;
990             push @array, @category_table[($row<<8)..($row<<8)+255];
991         }
992     }
993
994     printf OUTPUT "const unsigned short wine_wctype_table[%d] =\n{\n", $#array+1;
995     printf OUTPUT "    /* offsets */\n%s,\n", DUMP_ARRAY( "0x%04x", 0, @array[0..255] );
996     printf OUTPUT "    /* values */\n%s\n};\n", DUMP_ARRAY( "0x%04x", 0, @array[256..$#array] );
997
998     close OUTPUT;
999     save_file("wctype.c");
1000 }
1001
1002
1003 ################################################################
1004 # dump the char composition tables
1005 sub DUMP_COMPOSE_TABLES
1006 {
1007     open OUTPUT,">compose.c.new" or die "Cannot create compose.c";
1008     printf "Building compose.c\n";
1009     printf OUTPUT "/* Unicode char composition */\n";
1010     printf OUTPUT "/* Automatically generated; DO NOT EDIT!! */\n\n";
1011     printf OUTPUT "#include \"wine/unicode.h\"\n\n";
1012
1013     ######### composition table
1014
1015     my @filled = ();
1016     foreach $i (@compose_table)
1017     {
1018         my @comp = @$i;
1019         push @{$filled[$comp[1]]}, [ $comp[0], $comp[2] ];
1020     }
1021
1022     # count how many different second chars we have
1023
1024     for ($i = $count = 0; $i < 65536; $i++)
1025     {
1026         next unless defined $filled[$i];
1027         $count++;
1028     }
1029
1030     # build the table of second chars and offsets
1031
1032     my $pos = $count + 1;
1033     for ($i = 0; $i < 65536; $i++)
1034     {
1035         next unless defined $filled[$i];
1036         push @table, $i, $pos;
1037         $pos += @{$filled[$i]};
1038     }
1039     # terminator with last position
1040     push @table, 0, $pos;
1041     printf OUTPUT "const WCHAR unicode_compose_table[0x%x] =\n{\n", 2*$pos;
1042     printf OUTPUT "    /* second chars + offsets */\n%s", DUMP_ARRAY( "0x%04x", 0, @table );
1043
1044     # build the table of first chars and mappings
1045
1046     for ($i = 0; $i < 65536; $i++)
1047     {
1048         next unless defined $filled[$i];
1049         my @table = ();
1050         my @list = sort { $a->[0] <=> $b->[0] } @{$filled[$i]};
1051         for ($j = 0; $j <= $#list; $j++)
1052         {
1053             push @table, $list[$j][0], $list[$j][1];
1054         }
1055         printf OUTPUT ",\n    /* 0x%04x */\n%s", $i, DUMP_ARRAY( "0x%04x", 0, @table );
1056     }
1057     printf OUTPUT "\n};\n\nconst unsigned int unicode_compose_table_size = %d;\n\n", $count;
1058
1059     ######### decomposition table
1060
1061     # first determine all the 16-char subsets that contain something
1062
1063     my @filled = (0) x 4096;
1064     my $pos = 16*2;  # for the null subset
1065     for ($i = 0; $i < 65536; $i++)
1066     {
1067         next unless defined $decomp_table[$i];
1068         $filled[$i >> 4] = $pos;
1069         $pos += 16*2;
1070         $i |= 15;
1071     }
1072     my $total = $pos;
1073
1074     # now count the 256-char subsets that contain something
1075
1076     my @filled_idx = (256) x 256;
1077     $pos = 256 + 16;
1078     for ($i = 0; $i < 4096; $i++)
1079     {
1080         next unless $filled[$i];
1081         $filled_idx[$i >> 4] = $pos;
1082         $pos += 16;
1083         $i |= 15;
1084     }
1085     my $null_offset = $pos;  # null mapping
1086     $total += $pos;
1087
1088     # add the index offsets to the subsets positions
1089
1090     for ($i = 0; $i < 4096; $i++)
1091     {
1092         next unless $filled[$i];
1093         $filled[$i] += $null_offset;
1094     }
1095
1096     # dump the main index
1097
1098     printf OUTPUT "const WCHAR unicode_decompose_table[%d] =\n", $total;
1099     printf OUTPUT "{\n    /* index */\n";
1100     printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0, @filled_idx );
1101     printf OUTPUT ",\n    /* null sub-index */\n%s", DUMP_ARRAY( "0x%04x", 0, ($null_offset) x 16 );
1102
1103     # dump the second-level indexes
1104
1105     for ($i = 0; $i < 256; $i++)
1106     {
1107         next unless ($filled_idx[$i] > 256);
1108         my @table = @filled[($i<<4)..($i<<4)+15];
1109         for ($j = 0; $j < 16; $j++) { $table[$j] ||= $null_offset; }
1110         printf OUTPUT ",\n    /* sub-index %02x */\n", $i;
1111         printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0, @table );
1112     }
1113
1114     # dump the 16-char subsets
1115
1116     printf OUTPUT ",\n    /* null mapping */\n";
1117     printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0, (0) x 32 );
1118
1119     for ($i = 0; $i < 4096; $i++)
1120     {
1121         next unless $filled[$i];
1122         my @table = (0) x 32;
1123         for ($j = 0; $j < 16; $j++)
1124         {
1125             if (defined $decomp_table[($i<<4) + $j])
1126             {
1127                 $table[2 * $j] = ${$decomp_table[($i << 4) + $j]}[0];
1128                 $table[2 * $j + 1] = ${$decomp_table[($i << 4) + $j]}[1];
1129             }
1130         }
1131         printf OUTPUT ",\n    /* 0x%03x0 .. 0x%03xf */\n", $i, $i;
1132         printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0, @table );
1133     }
1134
1135     printf OUTPUT "\n};\n";
1136     close OUTPUT;
1137     save_file("compose.c");
1138 }
1139
1140
1141 ################################################################
1142 # read an input file and generate the corresponding .c file
1143 sub HANDLE_FILE
1144 {
1145     my ($codepage,$filename,$comment) = @_;
1146
1147     # symbol codepage file is special
1148     if ($codepage == 20932) { READ_JIS0208_FILE($MAPPREFIX . $filename); }
1149     else { READ_FILE($MAPPREFIX . $filename); }
1150
1151     # hack: 0x00a5 must map to backslash in Shift-JIS
1152     if ($codepage == 932) { $uni2cp[0x00a5] = 0x5c; }
1153
1154     ADD_DEFAULT_MAPPINGS();
1155
1156     my $output = sprintf "c_%03d.c", $codepage;
1157     open OUTPUT,">$output.new" or die "Cannot create $output";
1158
1159     printf "Building %s from %s (%s)\n", $output, $filename, $comment;
1160
1161     # dump all tables
1162
1163     printf OUTPUT "/* code page %03d (%s) */\n", $codepage, $comment;
1164     printf OUTPUT "/* generated from %s */\n", $MAPPREFIX . $filename;
1165     printf OUTPUT "/* DO NOT EDIT!! */\n\n";
1166     printf OUTPUT "#include \"wine/unicode.h\"\n\n";
1167
1168     if ($#lead_bytes == -1) { DUMP_SBCS_TABLE( $codepage, $comment ); }
1169     else { DUMP_DBCS_TABLE( $codepage, $comment ); }
1170     close OUTPUT;
1171     save_file($output);
1172 }
1173
1174
1175 ################################################################
1176 # save a file if modified
1177 sub save_file($)
1178 {
1179     my $file = shift;
1180     if (!system "cmp $file $file.new >/dev/null")
1181     {
1182         unlink "$file.new";
1183     }
1184     else
1185     {
1186         rename "$file.new", "$file";
1187     }
1188 }
1189
1190
1191 ################################################################
1192 # output the list of codepage tables into the cptable.c file
1193 sub OUTPUT_CPTABLE
1194 {
1195     @tables_decl = ();
1196
1197     foreach $file (@allfiles)
1198     {
1199         my ($codepage,$filename,$comment) = @$file;
1200         push @tables_decl, sprintf("extern union cptable cptable_%03d;\n",$codepage);
1201     }
1202
1203     push @tables_decl, sprintf("\nstatic const union cptable * const cptables[%d] =\n{\n",$#allfiles+1);
1204     foreach $file (@allfiles)
1205     {
1206         my ($codepage,$filename,$comment) = @$file;
1207         push @tables_decl, sprintf("    &cptable_%03d,\n", $codepage);
1208     }
1209     push @tables_decl, "};";
1210     REPLACE_IN_FILE( "cptable.c", @tables_decl );
1211 }
1212
1213 ################################################################
1214 # replace the contents of a file between ### cpmap ### marks
1215
1216 sub REPLACE_IN_FILE
1217 {
1218     my $name = shift;
1219     my @data = @_;
1220     my @lines = ();
1221     open(FILE,$name) or die "Can't open $name";
1222     while (<FILE>)
1223     {
1224         push @lines, $_;
1225         last if /\#\#\# cpmap begin \#\#\#/;
1226     }
1227     push @lines, @data;
1228     while (<FILE>)
1229     {
1230         if (/\#\#\# cpmap end \#\#\#/) { push @lines, "\n", $_; last; }
1231     }
1232     push @lines, <FILE>;
1233     open(FILE,">$name.new") or die "Can't modify $name";
1234     print FILE @lines;
1235     close(FILE);
1236     save_file($name);
1237 }