~vcs-imports/mammoth-replicator/trunk

« back to all changes in this revision

Viewing changes to src/backend/utils/adt/ascii.c

  • 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
/*-----------------------------------------------------------------------
 
2
 * ascii.c
 
3
 *       The PostgreSQL routine for string to ascii conversion.
 
4
 *
 
5
 *       Portions Copyright (c) 1999-2005, PostgreSQL Global Development Group
 
6
 *
 
7
 * IDENTIFICATION
 
8
 *        $PostgreSQL: pgsql/src/backend/utils/adt/ascii.c,v 1.24 2005-01-01 05:43:07 momjian Exp $
 
9
 *
 
10
 *-----------------------------------------------------------------------
 
11
 */
 
12
#include "postgres.h"
 
13
 
 
14
#include "utils/builtins.h"
 
15
#include "mb/pg_wchar.h"
 
16
#include "utils/ascii.h"
 
17
 
 
18
static void pg_to_ascii(unsigned char *src, unsigned char *src_end,
 
19
                        unsigned char *dest, int enc);
 
20
static text *encode_to_ascii(text *data, int enc);
 
21
 
 
22
 
 
23
/* ----------
 
24
 * to_ascii
 
25
 * ----------
 
26
 */
 
27
static void
 
28
pg_to_ascii(unsigned char *src, unsigned char *src_end, unsigned char *dest, int enc)
 
29
{
 
30
        unsigned char *x;
 
31
        const unsigned char *ascii;
 
32
        int                     range;
 
33
 
 
34
        /*
 
35
         * relevant start for an encoding
 
36
         */
 
37
#define RANGE_128       128
 
38
#define RANGE_160       160
 
39
 
 
40
        if (enc == PG_LATIN1)
 
41
        {
 
42
                /*
 
43
                 * ISO-8859-1 <range: 160 -- 255>
 
44
                 */
 
45
                ascii = "  cL Y  \"Ca  -R     'u .,      ?AAAAAAACEEEEIIII NOOOOOxOUUUUYTBaaaaaaaceeeeiiii nooooo/ouuuuyty";
 
46
                range = RANGE_160;
 
47
        }
 
48
        else if (enc == PG_LATIN2)
 
49
        {
 
50
                /*
 
51
                 * ISO-8859-2 <range: 160 -- 255>
 
52
                 */
 
53
                ascii = " A L LS \"SSTZ-ZZ a,l'ls ,sstz\"zzRAAAALCCCEEEEIIDDNNOOOOxRUUUUYTBraaaalccceeeeiiddnnoooo/ruuuuyt.";
 
54
                range = RANGE_160;
 
55
        }
 
56
        else if (enc == PG_LATIN9)
 
57
        {
 
58
                /*
 
59
                 * ISO-8859-15 <range: 160 -- 255>
 
60
                 */
 
61
                ascii = "  cL YS sCa  -R     Zu .z   EeY?AAAAAAACEEEEIIII NOOOOOxOUUUUYTBaaaaaaaceeeeiiii nooooo/ouuuuyty";
 
62
                range = RANGE_160;
 
63
        }
 
64
        else if (enc == PG_WIN1250)
 
65
        {
 
66
                /*
 
67
                 * Window CP1250 <range: 128 -- 255>
 
68
                 */
 
69
                ascii = "  ' \"    %S<STZZ `'\"\".--  s>stzz   L A  \"CS  -RZ  ,l'u .,as L\"lzRAAAALCCCEEEEIIDDNNOOOOxRUUUUYTBraaaalccceeeeiiddnnoooo/ruuuuyt ";
 
70
                range = RANGE_128;
 
71
        }
 
72
        else
 
73
        {
 
74
                ereport(ERROR,
 
75
                                (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
 
76
                         errmsg("encoding conversion from %s to ASCII not supported",
 
77
                                        pg_encoding_to_char(enc))));
 
78
                return;                                 /* keep compiler quiet */
 
79
        }
 
80
 
 
81
        /*
 
82
         * Encode
 
83
         */
 
84
        for (x = src; x < src_end; x++)
 
85
        {
 
86
                if (*x < 128)
 
87
                        *dest++ = *x;
 
88
                else if (*x < range)
 
89
                        *dest++ = ' ';          /* bogus 128 to 'range' */
 
90
                else
 
91
                        *dest++ = ascii[*x - range];
 
92
        }
 
93
}
 
94
 
 
95
/* ----------
 
96
 * encode text
 
97
 *
 
98
 * The text datum is overwritten in-place, therefore this coding method
 
99
 * cannot support conversions that change the string length!
 
100
 * ----------
 
101
 */
 
102
static text *
 
103
encode_to_ascii(text *data, int enc)
 
104
{
 
105
        pg_to_ascii((unsigned char *) VARDATA(data),            /* src */
 
106
                                (unsigned char *) (data) + VARSIZE(data),               /* src end */
 
107
                                (unsigned char *) VARDATA(data),                /* dest */
 
108
                                enc);                   /* encoding */
 
109
 
 
110
        return data;
 
111
}
 
112
 
 
113
/* ----------
 
114
 * convert to ASCII - enc is set as 'name' arg.
 
115
 * ----------
 
116
 */
 
117
Datum
 
118
to_ascii_encname(PG_FUNCTION_ARGS)
 
119
{
 
120
        text       *data = PG_GETARG_TEXT_P_COPY(0);
 
121
        int                     enc = pg_char_to_encoding(NameStr(*PG_GETARG_NAME(1)));
 
122
 
 
123
        PG_RETURN_TEXT_P(encode_to_ascii(data, enc));
 
124
}
 
125
 
 
126
/* ----------
 
127
 * convert to ASCII - enc is set as int4
 
128
 * ----------
 
129
 */
 
130
Datum
 
131
to_ascii_enc(PG_FUNCTION_ARGS)
 
132
{
 
133
        text       *data = PG_GETARG_TEXT_P_COPY(0);
 
134
        int                     enc = PG_GETARG_INT32(1);
 
135
 
 
136
        PG_RETURN_TEXT_P(encode_to_ascii(data, enc));
 
137
}
 
138
 
 
139
/* ----------
 
140
 * convert to ASCII - current enc is DatabaseEncoding
 
141
 * ----------
 
142
 */
 
143
Datum
 
144
to_ascii_default(PG_FUNCTION_ARGS)
 
145
{
 
146
        text       *data = PG_GETARG_TEXT_P_COPY(0);
 
147
        int                     enc = GetDatabaseEncoding();
 
148
 
 
149
        PG_RETURN_TEXT_P(encode_to_ascii(data, enc));
 
150
}