~vcs-imports/mammoth-replicator/trunk

« back to all changes in this revision

Viewing changes to src/backend/utils/mb/Unicode/UCS_to_GBK.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_GBK.pl,v 1.5 2005-01-01 20:44:18 tgl Exp $
 
6
#
 
7
#
 
8
# Generate UTF-8 <--> GBK code conversion tables from
 
9
# map files provided by Unicode organization.
 
10
# Unfortunately it is prohibited by the organization
 
11
# to distribute the map files. So if you try to use this script,
 
12
# you have to obtain CP936.TXT from 
 
13
# the organization's ftp site.
 
14
#
 
15
# CP936.TXT format:
 
16
#                GBK code in hex
 
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 --> GBK table
 
23
 
 
24
$in_file = "CP936.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
                $array{ $utf } = $code;
 
45
        }
 
46
}
 
47
close( FILE );
 
48
 
 
49
#
 
50
# first, generate UTF8 --> WIN949 table
 
51
#
 
52
 
 
53
$file = "utf8_to_gbk.map";
 
54
open( FILE, "> $file" ) || die( "cannot open $file" );
 
55
print FILE "static pg_utf_to_local ULmapGBK[ $count ] = {\n";
 
56
 
 
57
for $index ( sort {$a <=> $b} keys( %array ) ){
 
58
        $code = $array{ $index };
 
59
        $count--;
 
60
        if( $count == 0 ){
 
61
                printf FILE "  {0x%04x, 0x%04x}\n", $index, $code;
 
62
        } else {
 
63
                printf FILE "  {0x%04x, 0x%04x},\n", $index, $code;
 
64
        }
 
65
}
 
66
 
 
67
print FILE "};\n";
 
68
close(FILE);
 
69
 
 
70
#
 
71
# then generate WIN936 --> UTF8 table
 
72
#
 
73
reset 'array';
 
74
 
 
75
open( FILE, $in_file ) || die( "cannot open $in_file" );
 
76
 
 
77
while( <FILE> ){
 
78
        chop;
 
79
        if( /^#/ ){
 
80
                next;
 
81
        }
 
82
        ( $c, $u, $rest ) = split;
 
83
        $ucs = hex($u);
 
84
        $code = hex($c);
 
85
        if( $code >= 0x80 && $ucs >= 0x0080 ){
 
86
                $utf = &ucs2utf($ucs);
 
87
                if( $array{ $code } ne "" ){
 
88
                        printf STDERR "Warning: duplicate code: %04x\n",$ucs;
 
89
                        next;
 
90
                }
 
91
                $count++;
 
92
 
 
93
                $array{ $code } = $utf;
 
94
        }
 
95
}
 
96
close( FILE );
 
97
 
 
98
$file = "gbk_to_utf8.map";
 
99
open( FILE, "> $file" ) || die( "cannot open $file" );
 
100
print FILE "static pg_local_to_utf LUmapGBK[ $count ] = {\n";
 
101
for $index ( sort {$a <=> $b} keys( %array ) ){
 
102
        $utf = $array{ $index };
 
103
        $count--;
 
104
        if( $count == 0 ){
 
105
                printf FILE "  {0x%04x, 0x%04x}\n", $index, $utf;
 
106
        } else {
 
107
                printf FILE "  {0x%04x, 0x%04x},\n", $index, $utf;
 
108
        }
 
109
}
 
110
 
 
111
print FILE "};\n";
 
112
close(FILE);