|
| 1 | +#! /usr/bin/perl |
| 2 | +# |
| 3 | +# Copyright 2002 by Bill Huang |
| 4 | +# |
| 5 | +# $Id: UCS_to_GB18030.pl,v 1.1 2002/06/13 08:28:55 ishii Exp $ |
| 6 | +# |
| 7 | +# Generate UTF-8 <--> GB18030 code conversion tables from |
| 8 | +# map files provided by Unicode organization. |
| 9 | +# Unfortunately it is prohibited by the organization |
| 10 | +# to distribute the map files. So if you try to use this script, |
| 11 | +# you have to obtain ISO10646-GB18030.TXT from |
| 12 | +# the organization's ftp site. |
| 13 | +# |
| 14 | +# ISO10646-GB18030.TXT format: |
| 15 | +# GB18030 code in hex |
| 16 | +# UCS-2 code in hex |
| 17 | +# # and Unicode name (not used in this script) |
| 18 | + |
| 19 | +require"ucs2utf.pl"; |
| 20 | + |
| 21 | +# first generate UTF-8 --> GB18030 table |
| 22 | + |
| 23 | +$in_file ="ISO10646-GB18030.TXT"; |
| 24 | + |
| 25 | +open( FILE,$in_file ) ||die("cannot open$in_file" ); |
| 26 | + |
| 27 | +while( <FILE> ){ |
| 28 | +chop; |
| 29 | +if(/^#/ ){ |
| 30 | +next; |
| 31 | +} |
| 32 | +($u,$c,$rest ) =split; |
| 33 | +$utf =hex($u); |
| 34 | +$code =hex($c); |
| 35 | +$count++; |
| 36 | +$array{$utf } = ($code); |
| 37 | +} |
| 38 | +close( FILE ); |
| 39 | + |
| 40 | +# |
| 41 | +# first, generate UTF8 --> GB18030 table |
| 42 | +# |
| 43 | + |
| 44 | +$file ="utf8_to_gb18030.map"; |
| 45 | +open( FILE,">$file" ) ||die("cannot open$file" ); |
| 46 | +print FILE"static pg_utf_to_local ULmapGB18030[$count ] = {\n"; |
| 47 | + |
| 48 | +for$index (sort {$a<=>$b}keys(%array ) ){ |
| 49 | +$code =$array{$index }; |
| 50 | +$count--; |
| 51 | +if($count == 0 ){ |
| 52 | +printf FILE" {0x%04x, 0x%04x}\n",$index,$code; |
| 53 | +}else { |
| 54 | +printf FILE" {0x%04x, 0x%04x},\n",$index,$code; |
| 55 | +} |
| 56 | +} |
| 57 | + |
| 58 | +print FILE"};\n"; |
| 59 | +close(FILE); |
| 60 | + |
| 61 | +# |
| 62 | +# then generate GB18030 --> UTF8 table |
| 63 | +# |
| 64 | +reset'array'; |
| 65 | + |
| 66 | +open( FILE,$in_file ) ||die("cannot open$in_file" ); |
| 67 | + |
| 68 | +while( <FILE> ){ |
| 69 | +chop; |
| 70 | +if(/^#/ ){ |
| 71 | +next; |
| 72 | +} |
| 73 | +($u,$c,$rest ) =split; |
| 74 | +$utf =hex($u); |
| 75 | +$code =hex($c); |
| 76 | +$count++; |
| 77 | +$array{$code } =$utf; |
| 78 | +} |
| 79 | +close( FILE ); |
| 80 | + |
| 81 | +$file ="gb18030_to_utf8.map"; |
| 82 | +open( FILE,">$file" ) ||die("cannot open$file" ); |
| 83 | +print FILE"static pg_local_to_utf LUmapGB18030[$count ] = {\n"; |
| 84 | +for$index (sort {$a<=>$b}keys(%array ) ){ |
| 85 | +$utf =$array{$index }; |
| 86 | +$count--; |
| 87 | +if($count == 0 ){ |
| 88 | +printf FILE" {0x%04x, 0x%04x}\n",$index,$utf; |
| 89 | +}else { |
| 90 | +printf FILE" {0x%04x, 0x%04x},\n",$index,$utf; |
| 91 | +} |
| 92 | +} |
| 93 | + |
| 94 | +print FILE"};\n"; |
| 95 | +close(FILE); |