|
| 1 | +#! /usr/bin/perl |
| 2 | +# |
| 3 | +# Copyright (c) 2001-2009, PostgreSQL Global Development Group |
| 4 | +# |
| 5 | +# $PostgreSQL: pgsql/src/backend/utils/mb/Unicode/UCS_to_big5.pl,v 1.1 2009/03/18 16:17:58 heikki Exp $ |
| 6 | +# |
| 7 | +# Generate UTF-8 <--> BIG5 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 the map files from the organization's ftp site. |
| 12 | +# ftp://www.unicode.org/Public/MAPPINGS/ |
| 13 | +# |
| 14 | +# Our "big5" comes from BIG5.TXT, with the addition of the characters |
| 15 | +# in the range 0xf9d6-0xf9dc from CP950.TXT. |
| 16 | +# |
| 17 | +# BIG5.TXT format: |
| 18 | +# BIG5 code in hex |
| 19 | +# UCS-2 code in hex |
| 20 | +# # and Unicode name (not used in this script) |
| 21 | +# |
| 22 | +# CP950.TXT format: |
| 23 | +# CP950 code in hex |
| 24 | +# UCS-2 code in hex |
| 25 | +# # and Unicode name (not used in this script) |
| 26 | + |
| 27 | + |
| 28 | +require "ucs2utf.pl"; |
| 29 | + |
| 30 | + |
| 31 | +# |
| 32 | +# first, generate UTF8 --> BIG5 table |
| 33 | +# |
| 34 | +$in_file = "BIG5.TXT"; |
| 35 | + |
| 36 | +open( FILE, $in_file ) || die( "cannot open $in_file" ); |
| 37 | + |
| 38 | +reset 'array'; |
| 39 | + |
| 40 | +while( <FILE> ){ |
| 41 | + chop; |
| 42 | + if( /^#/ ){ |
| 43 | + next; |
| 44 | + } |
| 45 | + ( $c, $u, $rest ) = split; |
| 46 | + $ucs = hex($u); |
| 47 | + $code = hex($c); |
| 48 | + if( $code >= 0x80 && $ucs >= 0x0080){ |
| 49 | + $utf = &ucs2utf($ucs); |
| 50 | + if( $array{ $utf } ne "" ){ |
| 51 | + printf STDERR "Warning: duplicate UTF8: %04x\n",$ucs; |
| 52 | + next; |
| 53 | + } |
| 54 | + $count++; |
| 55 | + $array{ $utf } = $code; |
| 56 | + } |
| 57 | +} |
| 58 | +close( FILE ); |
| 59 | + |
| 60 | +$in_file = "CP950.TXT"; |
| 61 | + |
| 62 | +open( FILE, $in_file ) || die( "cannot open $in_file" ); |
| 63 | + |
| 64 | +while( <FILE> ){ |
| 65 | + chop; |
| 66 | + if( /^#/ ){ |
| 67 | + next; |
| 68 | + } |
| 69 | + ( $c, $u, $rest ) = split; |
| 70 | + $ucs = hex($u); |
| 71 | + $code = hex($c); |
| 72 | + |
| 73 | + # Pick only the ETEN extended characters in the range 0xf9d6 - 0xf9dc |
| 74 | + # from CP950.TXT |
| 75 | + if( $code >= 0x80 && $ucs >= 0x0080 && |
| 76 | + $code >= 0xf9d6 && $code <= 0xf9dc ){ |
| 77 | + $utf = &ucs2utf($ucs); |
| 78 | + if( $array{ $utf } ne "" ){ |
| 79 | + printf STDERR "Warning: duplicate UTF8: %04x\n",$ucs; |
| 80 | + next; |
| 81 | + } |
| 82 | + $count++; |
| 83 | + $array{ $utf } = $code; |
| 84 | + } |
| 85 | +} |
| 86 | +close( FILE ); |
| 87 | + |
| 88 | +$file = lc("utf8_to_big5.map"); |
| 89 | +open( FILE, "> $file" ) || die( "cannot open $file" ); |
| 90 | +print FILE "static pg_utf_to_local ULmapBIG5[ $count ] = {\n"; |
| 91 | + |
| 92 | +for $index ( sort {$a <=> $b} keys( %array ) ){ |
| 93 | + $code = $array{ $index }; |
| 94 | + $count--; |
| 95 | + if( $count == 0 ){ |
| 96 | + printf FILE " {0x%04x, 0x%04x}\n", $index, $code; |
| 97 | + } else { |
| 98 | + printf FILE " {0x%04x, 0x%04x},\n", $index, $code; |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +print FILE "};\n"; |
| 103 | +close(FILE); |
| 104 | + |
| 105 | +# |
| 106 | +# then generate BIG5 --> UTF8 table |
| 107 | +# |
| 108 | +$in_file = "BIG5.TXT"; |
| 109 | + |
| 110 | +open( FILE, $in_file ) || die( "cannot open $in_file" ); |
| 111 | + |
| 112 | +reset 'array'; |
| 113 | + |
| 114 | +while( <FILE> ){ |
| 115 | + chop; |
| 116 | + if( /^#/ ){ |
| 117 | + next; |
| 118 | + } |
| 119 | + ( $c, $u, $rest ) = split; |
| 120 | + $ucs = hex($u); |
| 121 | + $code = hex($c); |
| 122 | + if( $code >= 0x80 && $ucs >= 0x0080){ |
| 123 | + $utf = &ucs2utf($ucs); |
| 124 | + if( $array{ $utf } ne "" ){ |
| 125 | + printf STDERR "Warning: duplicate UTF8: %04x\n",$ucs; |
| 126 | + next; |
| 127 | + } |
| 128 | + $count++; |
| 129 | + $array{ $code } = $utf; |
| 130 | + } |
| 131 | +} |
| 132 | +close( FILE ); |
| 133 | + |
| 134 | +$in_file = "CP950.TXT"; |
| 135 | + |
| 136 | +open( FILE, $in_file ) || die( "cannot open $in_file" ); |
| 137 | + |
| 138 | +while( <FILE> ){ |
| 139 | + chop; |
| 140 | + if( /^#/ ){ |
| 141 | + next; |
| 142 | + } |
| 143 | + ( $c, $u, $rest ) = split; |
| 144 | + $ucs = hex($u); |
| 145 | + $code = hex($c); |
| 146 | + |
| 147 | + # Pick only the ETEN extended characters in the range 0xf9d6 - 0xf9dc |
| 148 | + # from CP950.TXT |
| 149 | + if( $code >= 0x80 && $ucs >= 0x0080 && |
| 150 | + $code >= 0xf9d6 && $code <= 0xf9dc ){ |
| 151 | + $utf = &ucs2utf($ucs); |
| 152 | + if( $array{ $utf } ne "" ){ |
| 153 | + printf STDERR "Warning: duplicate UTF8: %04x\n",$ucs; |
| 154 | + next; |
| 155 | + } |
| 156 | + $count++; |
| 157 | + $array{ $code } = $utf; |
| 158 | + } |
| 159 | +} |
| 160 | +close( FILE ); |
| 161 | + |
| 162 | +$file = lc("big5_to_utf8.map"); |
| 163 | +open( FILE, "> $file" ) || die( "cannot open $file" ); |
| 164 | +print FILE "static pg_local_to_utf LUmapBIG5[ $count ] = {\n"; |
| 165 | +for $index ( sort {$a <=> $b} keys( %array ) ){ |
| 166 | + $utf = $array{ $index }; |
| 167 | + $count--; |
| 168 | + if( $count == 0 ){ |
| 169 | + printf FILE " {0x%04x, 0x%04x}\n", $index, $utf; |
| 170 | + } else { |
| 171 | + printf FILE " {0x%04x, 0x%04x},\n", $index, $utf; |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +print FILE "};\n"; |
| 176 | +close(FILE); |
| 177 | + |
0 commit comments