~ubuntu-branches/ubuntu/precise/postgresql-9.1/precise-security

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2011-05-11 10:41:53 UTC
  • Revision ID: james.westby@ubuntu.com-20110511104153-psbh2o58553fv1m0
Tags: upstream-9.1~beta1
ImportĀ upstreamĀ versionĀ 9.1~beta1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#! /usr/bin/perl
 
2
#
 
3
# Copyright (c) 2001-2011, PostgreSQL Global Development Group
 
4
#
 
5
# src/backend/utils/mb/Unicode/UCS_to_most.pl
 
6
#
 
7
# Generate UTF-8 <--> character 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 the map files from the organization's ftp site.
 
12
# ftp://www.unicode.org/Public/MAPPINGS/
 
13
# We assume the file include three tab-separated columns:
 
14
#                source character set code in hex
 
15
#                UCS-2 code in hex
 
16
#                # and Unicode name (not used in this script)
 
17
 
 
18
require "ucs2utf.pl";
 
19
 
 
20
%filename = (
 
21
        'WIN866' => 'CP866.TXT',
 
22
        'WIN874' => 'CP874.TXT',
 
23
        'WIN1250' => 'CP1250.TXT',
 
24
        'WIN1251' => 'CP1251.TXT',
 
25
        'WIN1252' => 'CP1252.TXT',
 
26
        'WIN1253' => 'CP1253.TXT',
 
27
        'WIN1254' => 'CP1254.TXT',
 
28
        'WIN1255' => 'CP1255.TXT',
 
29
        'WIN1256' => 'CP1256.TXT',
 
30
        'WIN1257' => 'CP1257.TXT',
 
31
        'WIN1258' => 'CP1258.TXT',
 
32
        'ISO8859_2' => '8859-2.TXT',
 
33
        'ISO8859_3' => '8859-3.TXT',
 
34
        'ISO8859_4' => '8859-4.TXT',
 
35
        'ISO8859_5' => '8859-5.TXT',
 
36
        'ISO8859_6' => '8859-6.TXT',
 
37
        'ISO8859_7' => '8859-7.TXT',
 
38
        'ISO8859_8' => '8859-8.TXT',
 
39
        'ISO8859_9' => '8859-9.TXT',
 
40
        'ISO8859_10' => '8859-10.TXT',
 
41
        'ISO8859_13' => '8859-13.TXT',
 
42
        'ISO8859_14' => '8859-14.TXT',
 
43
        'ISO8859_15' => '8859-15.TXT',
 
44
        'ISO8859_16' => '8859-16.TXT',
 
45
        'KOI8R' => 'KOI8-R.TXT',
 
46
        'KOI8U' => 'KOI8-U.TXT',
 
47
        'GBK' => 'CP936.TXT',
 
48
        'UHC' => 'CP949.TXT',
 
49
        'JOHAB' => 'JOHAB.TXT',
 
50
);
 
51
 
 
52
@charsets = keys(filename);
 
53
@charsets = @ARGV if scalar(@ARGV);
 
54
foreach $charset (@charsets) {
 
55
 
 
56
#
 
57
# first, generate UTF8-> charset table
 
58
#
 
59
    $in_file = $filename{$charset};
 
60
 
 
61
    open( FILE, $in_file ) || die( "cannot open $in_file" );
 
62
 
 
63
        reset 'array';
 
64
 
 
65
    while( <FILE> ){
 
66
                chop;
 
67
                if( /^#/ ){
 
68
                        next;
 
69
                }
 
70
                ( $c, $u, $rest ) = split;
 
71
                $ucs = hex($u);
 
72
                $code = hex($c);
 
73
                if( $code >= 0x80 && $ucs >= 0x0080){
 
74
                        $utf = &ucs2utf($ucs);
 
75
                        if( $array{ $utf } ne "" ){
 
76
                                printf STDERR "Warning: duplicate UTF8: %04x\n",$ucs;
 
77
                                next;
 
78
                        }
 
79
                        $count++;
 
80
                        $array{ $utf } = $code;
 
81
                }
 
82
        }
 
83
    close( FILE );
 
84
 
 
85
        $file = lc("utf8_to_${charset}.map");
 
86
    open( FILE, "> $file" ) || die( "cannot open $file" );
 
87
        print FILE "static pg_utf_to_local ULmap${charset}[ $count ] = {\n";
 
88
 
 
89
        for $index ( sort {$a <=> $b} keys( %array ) ){
 
90
                $code = $array{ $index };
 
91
                $count--;
 
92
                if( $count == 0 ){
 
93
                        printf FILE "  {0x%04x, 0x%04x}\n", $index, $code;
 
94
                } else {
 
95
                        printf FILE "  {0x%04x, 0x%04x},\n", $index, $code;
 
96
                }
 
97
        }
 
98
 
 
99
        print FILE "};\n";
 
100
        close(FILE);
 
101
 
 
102
#
 
103
# then generate character set code ->UTF8 table
 
104
#
 
105
    open( FILE, $in_file ) || die( "cannot open $in_file" );
 
106
 
 
107
        reset 'array';
 
108
 
 
109
    while( <FILE> ){
 
110
                chop;
 
111
                if( /^#/ ){
 
112
                        next;
 
113
                }
 
114
                ( $c, $u, $rest ) = split;
 
115
                $ucs = hex($u);
 
116
                $code = hex($c);
 
117
                if($code >= 0x80 && $ucs >= 0x0080){
 
118
                        $utf = &ucs2utf($ucs);
 
119
                        if( $array{ $code } ne "" ){
 
120
                                printf STDERR "Warning: duplicate UTF8: %04x\n",$ucs;
 
121
                                next;
 
122
                        }
 
123
                        $count++;
 
124
                        $array{ $code } = $utf;
 
125
                }
 
126
        }
 
127
    close( FILE );
 
128
 
 
129
        $file = lc("${charset}_to_utf8.map");
 
130
    open( FILE, "> $file" ) || die( "cannot open $file" );
 
131
        print FILE "static pg_local_to_utf LUmap${charset}[ $count ] = {\n";
 
132
        for $index ( sort {$a <=> $b} keys( %array ) ){
 
133
                $utf = $array{ $index };
 
134
                $count--;
 
135
                if( $count == 0 ){
 
136
                        printf FILE "  {0x%04x, 0x%04x}\n", $index, $utf;
 
137
                } else {
 
138
                        printf FILE "  {0x%04x, 0x%04x},\n", $index, $utf;
 
139
                }
 
140
        }
 
141
 
 
142
        print FILE "};\n";
 
143
        close(FILE);
 
144
}