~vcs-imports/mammoth-replicator/trunk

« back to all changes in this revision

Viewing changes to src/backend/utils/mb/Unicode/UCS_to_EUC_TW.pl

  • Committer: alvherre
  • Date: 2005-12-16 21:24:52 UTC
  • Revision ID: svn-v4:db760fc0-0f08-0410-9d63-cc6633f64896:trunk:1
Initial import of the REL8_0_3 sources from the Pgsql CVS repository.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#! /usr/bin/perl
 
2
#
 
3
# Copyright (c) 2001-2005, PostgreSQL Global Development Group
 
4
#
 
5
# $PostgreSQL: pgsql/src/backend/utils/mb/Unicode/UCS_to_EUC_TW.pl,v 1.6 2005-01-01 20:44:18 tgl Exp $
 
6
#
 
7
# Generate UTF-8 <--> EUC_TW 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 CNS11643.TXT from 
 
12
# the organization's ftp site.
 
13
#
 
14
# CNS11643.TXT format:
 
15
#                CNS11643 code in hex (3 bytes)
 
16
#                (I guess the first byte means the plane No.)
 
17
#                UCS-2 code in hex
 
18
#                # and Unicode name (not used in this script)
 
19
 
 
20
require "ucs2utf.pl";
 
21
 
 
22
# first generate UTF-8 --> EUC_TW table
 
23
 
 
24
$in_file = "CNS11643.TXT";
 
25
 
 
26
open( FILE, $in_file ) || die( "cannot open $in_file" );
 
27
 
 
28
while( <FILE> ){
 
29
        chop;
 
30
        if( /^#/ ){
 
31
                next;
 
32
        }
 
33
        ( $c, $u, $rest ) = split;
 
34
        $ucs = hex($u);
 
35
        $code = hex($c);
 
36
        if( $code >= 0x80 && $ucs >= 0x0080 ){
 
37
                $utf = &ucs2utf($ucs);
 
38
                if( $array{ $utf } ne "" ){
 
39
                        printf STDERR "Warning: duplicate unicode: %04x\n",$ucs;
 
40
                        next;
 
41
                }
 
42
                $count++;
 
43
 
 
44
                $plane = ($code & 0x1f0000) >> 16;
 
45
                if ($plane > 16) {
 
46
                        printf STDERR "Warning: invalid plane No.$plane. ignored\n";
 
47
                        next;
 
48
                }
 
49
 
 
50
                if ($plane == 1) {
 
51
                        $array{ $utf } = (($code & 0xffff) | 0x8080);
 
52
                } else {
 
53
                        $array{ $utf } = (0x8ea00000 + ($plane << 16)) | (($code & 0xffff) | 0x8080);
 
54
                }
 
55
        }
 
56
}
 
57
close( FILE );
 
58
 
 
59
#
 
60
# first, generate UTF8 --> EUC_TW table
 
61
#
 
62
 
 
63
$file = "utf8_to_euc_tw.map";
 
64
open( FILE, "> $file" ) || die( "cannot open $file" );
 
65
print FILE "static pg_utf_to_local ULmapEUC_TW[ $count ] = {\n";
 
66
 
 
67
for $index ( sort {$a <=> $b} keys( %array ) ){
 
68
        $code = $array{ $index };
 
69
        $count--;
 
70
        if( $count == 0 ){
 
71
                printf FILE "  {0x%04x, 0x%04x}\n", $index, $code;
 
72
        } else {
 
73
                printf FILE "  {0x%04x, 0x%04x},\n", $index, $code;
 
74
        }
 
75
}
 
76
 
 
77
print FILE "};\n";
 
78
close(FILE);
 
79
 
 
80
#
 
81
# then generate EUC_JP --> UTF8 table
 
82
#
 
83
reset 'array';
 
84
 
 
85
open( FILE, $in_file ) || die( "cannot open $in_file" );
 
86
 
 
87
while( <FILE> ){
 
88
        chop;
 
89
        if( /^#/ ){
 
90
                next;
 
91
        }
 
92
        ( $c, $u, $rest ) = split;
 
93
        $ucs = hex($u);
 
94
        $code = hex($c);
 
95
        if( $code >= 0x80 && $ucs >= 0x0080 ){
 
96
                $utf = &ucs2utf($ucs);
 
97
                if( $array{ $code } ne "" ){
 
98
                        printf STDERR "Warning: duplicate code: %04x\n",$ucs;
 
99
                        next;
 
100
                }
 
101
                $count++;
 
102
 
 
103
                $plane = ($code & 0x1f0000) >> 16;
 
104
                if ($plane > 16) {
 
105
                        printf STDERR "Warning: invalid plane No.$plane. ignored\n";
 
106
                        next;
 
107
                }
 
108
 
 
109
                if ($plane == 1) {
 
110
                        $c = (($code & 0xffff) | 0x8080);
 
111
                        $array{ $c } = $utf;
 
112
                        $count++;
 
113
                }
 
114
                $c = (0x8ea00000 + ($plane << 16)) | (($code & 0xffff) | 0x8080);
 
115
                $array{ $c } = $utf;
 
116
        }
 
117
}
 
118
close( FILE );
 
119
 
 
120
$file = "euc_tw_to_utf8.map";
 
121
open( FILE, "> $file" ) || die( "cannot open $file" );
 
122
print FILE "static pg_local_to_utf LUmapEUC_TW[ $count ] = {\n";
 
123
for $index ( sort {$a <=> $b} keys( %array ) ){
 
124
        $utf = $array{ $index };
 
125
        $count--;
 
126
        if( $count == 0 ){
 
127
                printf FILE "  {0x%04x, 0x%04x}\n", $index, $utf;
 
128
        } else {
 
129
                printf FILE "  {0x%04x, 0x%04x},\n", $index, $utf;
 
130
        }
 
131
}
 
132
 
 
133
print FILE "};\n";
 
134
close(FILE);