]> granicus.if.org Git - postgresql/blob - src/backend/utils/mb/Unicode/UCS_to_GB18030.pl
Replace last remaining $Id$ with $PostgreSQL$.
[postgresql] / src / backend / utils / mb / Unicode / UCS_to_GB18030.pl
1 #! /usr/bin/perl
2 #
3 # Copyright (c) 2007-2010, PostgreSQL Global Development Group
4 #
5 # $PostgreSQL: pgsql/src/backend/utils/mb/Unicode/UCS_to_GB18030.pl,v 1.7 2010/09/19 16:27:17 tgl Exp $
6 #
7 # Generate UTF-8 <--> GB18030 code conversion tables from
8 # "ISO10646-GB18030.TXT"
9 #
10 # file format:
11 #               GB18030 hex code
12 #               UCS-2 hex code
13
14 require "ucs2utf.pl";
15
16
17 # first generate UTF-8 --> GB18030 table
18
19 $in_file = "ISO10646-GB18030.TXT";
20
21 open( FILE, $in_file ) || die( "cannot open $in_file" );
22
23 while( <FILE> ){
24         chop;
25         if( /^#/ ){
26                 next;
27         }
28         ( $u, $c, $rest ) = split;
29         $ucs = hex($u);
30         $code = hex($c);
31         if( $code >= 0x80 && $ucs >= 0x0080 ){
32                 $utf = &ucs2utf($ucs);
33                 if( $array{ $utf } ne "" ){
34                         printf STDERR "Warning: duplicate UTF8: %04x\n",$ucs;
35                         next;
36                 }
37                 $count++;
38
39                 $array{ $utf } = $code;
40         }
41 }
42 close( FILE );
43
44
45 #
46 # first, generate UTF8 --> GB18030 table
47 #
48
49 $file = "utf8_to_gb18030.map";
50 open( FILE, "> $file" ) || die( "cannot open $file" );
51 print FILE "static pg_utf_to_local ULmapGB18030[ $count ] = {\n";
52
53 for $index ( sort {$a <=> $b} keys( %array ) ){
54         $code = $array{ $index };
55         $count--;
56         if( $count == 0 ){
57                 printf FILE "  {0x%04x, 0x%04x}\n", $index, $code;
58         } else {
59                 printf FILE "  {0x%04x, 0x%04x},\n", $index, $code;
60         }
61 }
62
63 print FILE "};\n";
64 close(FILE);
65
66
67 #
68 # then generate GB18030 --> UTF8 table
69 #
70 reset 'array';
71
72 open( FILE, $in_file ) || die( "cannot open $in_file" );
73
74 while( <FILE> ){
75         chop;
76         if( /^#/ ){
77                 next;
78         }
79         ( $u, $c, $rest ) = split;
80         $ucs = hex($u);
81         $code = hex($c);
82         if( $code >= 0x80 && $ucs >= 0x0080 ){
83                 $utf = &ucs2utf($ucs);
84                 if( $array{ $code } ne "" ){
85                         printf STDERR "Warning: duplicate code: %04x\n",$ucs;
86                         next;
87                 }
88                 $count++;
89
90                 $array{ $code } = $utf;
91         }
92 }
93 close( FILE );
94
95 $file = "gb18030_to_utf8.map";
96 open( FILE, "> $file" ) || die( "cannot open $file" );
97 print FILE "static pg_local_to_utf LUmapGB18030[ $count ] = {\n";
98 for $index ( sort {$a <=> $b} keys( %array ) ){
99         $utf = $array{ $index };
100         $count--;
101         if( $count == 0 ){
102                 printf FILE "  {0x%04x, 0x%04x}\n", $index, $utf;
103         } else {
104                 printf FILE "  {0x%04x, 0x%04x},\n", $index, $utf;
105         }
106 }
107
108 print FILE "};\n";
109 close(FILE);