~vcs-imports/mammoth-replicator/trunk

« back to all changes in this revision

Viewing changes to src/backend/utils/mb/Unicode/UCS_to_EUC_JP.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_JP.pl,v 1.6 2005-01-01 20:44:18 tgl Exp $
 
6
#
 
7
# Generate UTF-8 <--> EUC_JP 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 JIS0201.TXT, JIS0208.TXT, JIS0212.TXT from 
 
12
# the organization's ftp site.
 
13
#
 
14
# JIS0201.TXT format:
 
15
#                JIS0201 code in hex
 
16
#                UCS-2 code in hex
 
17
#                # and Unicode name (not used in this script)
 
18
#
 
19
# JIS0208.TXT format:
 
20
#                JIS0208 shift-JIS code in hex
 
21
#                JIS0208 code in hex
 
22
#                UCS-2 code in hex
 
23
#                # and Unicode name (not used in this script)
 
24
#
 
25
# JIS0212.TXT format:
 
26
#                JIS0212 code in hex
 
27
#                UCS-2 code in hex
 
28
#                # and Unicode name (not used in this script)
 
29
 
 
30
require "ucs2utf.pl";
 
31
 
 
32
# first generate UTF-8 --> EUC_JP table
 
33
 
 
34
#
 
35
# JIS0201
 
36
#
 
37
$in_file = "JIS0201.TXT";
 
38
 
 
39
open( FILE, $in_file ) || die( "cannot open $in_file" );
 
40
 
 
41
reset 'array';
 
42
 
 
43
while( <FILE> ){
 
44
        chop;
 
45
        if( /^#/ ){
 
46
                next;
 
47
        }
 
48
        ( $c, $u, $rest ) = split;
 
49
        $ucs = hex($u);
 
50
        $code = hex($c);
 
51
        if( $code >= 0x80 && $ucs >= 0x0080 ){
 
52
                $utf = &ucs2utf($ucs);
 
53
                if( $array{ $utf } ne "" ){
 
54
                        printf STDERR "Warning: duplicate unicode: %04x\n",$ucs;
 
55
                        next;
 
56
                }
 
57
                $count++;
 
58
 
 
59
                # add single shift 2
 
60
                $array{ $utf } = ($code | 0x8e00);
 
61
        }
 
62
}
 
63
close( FILE );
 
64
 
 
65
#
 
66
# JIS0208
 
67
#
 
68
$in_file = "JIS0208.TXT";
 
69
 
 
70
open( FILE, $in_file ) || die( "cannot open $in_file" );
 
71
 
 
72
while( <FILE> ){
 
73
        chop;
 
74
        if( /^#/ ){
 
75
                next;
 
76
        }
 
77
        ( $s, $c, $u, $rest ) = split;
 
78
        $ucs = hex($u);
 
79
        $code = hex($c);
 
80
        if( $code >= 0x80 && $ucs >= 0x0080 ){
 
81
                $utf = &ucs2utf($ucs);
 
82
                if( $array{ $utf } ne "" ){
 
83
                        printf STDERR "Warning: duplicate unicode: %04x\n",$ucs;
 
84
                        next;
 
85
                }
 
86
                $count++;
 
87
 
 
88
                $array{ $utf } = ($code | 0x8080);
 
89
        }
 
90
}
 
91
close( FILE );
 
92
 
 
93
#
 
94
# JIS0212
 
95
#
 
96
$in_file = "JIS0212.TXT";
 
97
 
 
98
open( FILE, $in_file ) || die( "cannot open $in_file" );
 
99
 
 
100
while( <FILE> ){
 
101
        chop;
 
102
        if( /^#/ ){
 
103
                next;
 
104
        }
 
105
        ( $c, $u, $rest ) = split;
 
106
        $ucs = hex($u);
 
107
        $code = hex($c);
 
108
        if( $code >= 0x80 && $ucs >= 0x0080 ){
 
109
                $utf = &ucs2utf($ucs);
 
110
                if( $array{ $utf } ne "" ){
 
111
                        printf STDERR "Warning: duplicate unicode: %04x\n",$ucs;
 
112
                        next;
 
113
                }
 
114
                $count++;
 
115
 
 
116
                $array{ $utf } = ($code | 0x8f8080);
 
117
        }
 
118
}
 
119
close( FILE );
 
120
 
 
121
#
 
122
# first, generate UTF8 --> EUC_JP table
 
123
#
 
124
 
 
125
$file = "utf8_to_euc_jp.map";
 
126
open( FILE, "> $file" ) || die( "cannot open $file" );
 
127
print FILE "static pg_utf_to_local ULmapEUC_JP[ $count ] = {\n";
 
128
 
 
129
for $index ( sort {$a <=> $b} keys( %array ) ){
 
130
        $code = $array{ $index };
 
131
        $count--;
 
132
        if( $count == 0 ){
 
133
                printf FILE "  {0x%04x, 0x%04x}\n", $index, $code;
 
134
        } else {
 
135
                printf FILE "  {0x%04x, 0x%04x},\n", $index, $code;
 
136
        }
 
137
}
 
138
 
 
139
print FILE "};\n";
 
140
close(FILE);
 
141
 
 
142
#
 
143
# then generate EUC_JP --> UTF8 table
 
144
#
 
145
 
 
146
#
 
147
# JIS0201
 
148
#
 
149
$in_file = "JIS0201.TXT";
 
150
 
 
151
open( FILE, $in_file ) || die( "cannot open $in_file" );
 
152
 
 
153
reset 'array';
 
154
 
 
155
while( <FILE> ){
 
156
        chop;
 
157
        if( /^#/ ){
 
158
                next;
 
159
        }
 
160
        ( $c, $u, $rest ) = split;
 
161
        $ucs = hex($u);
 
162
        $code = hex($c);
 
163
        if( $code >= 0x80 && $ucs >= 0x0080 ){
 
164
                $utf = &ucs2utf($ucs);
 
165
                if( $array{ $code } ne "" ){
 
166
                        printf STDERR "Warning: duplicate code: %04x\n",$ucs;
 
167
                        next;
 
168
                }
 
169
                $count++;
 
170
 
 
171
                # add single shift 2
 
172
                $code |= 0x8e00;
 
173
                $array{ $code } = $utf;
 
174
        }
 
175
}
 
176
close( FILE );
 
177
 
 
178
#
 
179
# JIS0208
 
180
#
 
181
$in_file = "JIS0208.TXT";
 
182
 
 
183
open( FILE, $in_file ) || die( "cannot open $in_file" );
 
184
 
 
185
while( <FILE> ){
 
186
        chop;
 
187
        if( /^#/ ){
 
188
                next;
 
189
        }
 
190
        ( $s, $c, $u, $rest ) = split;
 
191
        $ucs = hex($u);
 
192
        $code = hex($c);
 
193
        if( $code >= 0x80 && $ucs >= 0x0080 ){
 
194
                $utf = &ucs2utf($ucs);
 
195
                if( $array{ $code } ne "" ){
 
196
                        printf STDERR "Warning: duplicate code: %04x\n",$ucs;
 
197
                        next;
 
198
                }
 
199
                $count++;
 
200
 
 
201
                $code |= 0x8080;
 
202
                $array{ $code } = $utf;
 
203
        }
 
204
}
 
205
close( FILE );
 
206
 
 
207
#
 
208
# JIS0212
 
209
#
 
210
$in_file = "JIS0212.TXT";
 
211
 
 
212
open( FILE, $in_file ) || die( "cannot open $in_file" );
 
213
 
 
214
while( <FILE> ){
 
215
        chop;
 
216
        if( /^#/ ){
 
217
                next;
 
218
        }
 
219
        ( $c, $u, $rest ) = split;
 
220
        $ucs = hex($u);
 
221
        $code = hex($c);
 
222
        if( $code >= 0x80 && $ucs >= 0x0080 ){
 
223
                $utf = &ucs2utf($ucs);
 
224
                if( $array{ $code } ne "" ){
 
225
                        printf STDERR "Warning: duplicate code: %04x\n",$ucs;
 
226
                        next;
 
227
                }
 
228
                $count++;
 
229
 
 
230
                $code |= 0x8f8080;
 
231
                $array{ $code } = $utf;
 
232
        }
 
233
}
 
234
close( FILE );
 
235
 
 
236
$file = "euc_jp_to_utf8.map";
 
237
open( FILE, "> $file" ) || die( "cannot open $file" );
 
238
print FILE "static pg_local_to_utf LUmapEUC_JP[ $count ] = {\n";
 
239
for $index ( sort {$a <=> $b} keys( %array ) ){
 
240
        $utf = $array{ $index };
 
241
        $count--;
 
242
        if( $count == 0 ){
 
243
                printf FILE "  {0x%04x, 0x%04x}\n", $index, $utf;
 
244
        } else {
 
245
                printf FILE "  {0x%04x, 0x%04x},\n", $index, $utf;
 
246
        }
 
247
}
 
248
 
 
249
print FILE "};\n";
 
250
close(FILE);