1
+
2
+ /* -----------------------------------------------------------------------
3
+ * ascii.c
4
+ *
5
+ * $Header: /cvsroot/pgsql/src/backend/utils/adt/ascii.c,v 1.3 2000/08/05 14:59:17 momjian Exp $
6
+ *
7
+ * Portions Copyright (c) 1999-2000, PostgreSQL, Inc
8
+ *
9
+ *
10
+ * TO_ASCII()
11
+ *
12
+ * The PostgreSQL routine for string to ascii conversion.
13
+ *
14
+ * -----------------------------------------------------------------------
15
+ */
16
+
17
+ #include "postgres.h"
18
+ #include "utils/builtins.h"
19
+ #include "mb/pg_wchar.h"
20
+ #include "utils/ascii.h"
21
+
22
+ /* ----------
23
+ * even if MULTIBYTE is not enabled, these functions are necessary
24
+ * since pg_proc.h has references to them.
25
+ * ----------
26
+ */
27
+ #ifndef MULTIBYTE
28
+
29
+ static void multibyte_error (void );
30
+
31
+ static void
32
+ multibyte_error ()
33
+ {
34
+ elog (ERROR , "multibyte not supported." );
35
+ }
36
+
37
+ Datum
38
+ to_ascii_encname (PG_FUNCTION_ARGS )
39
+ {
40
+ multibyte_error ();
41
+ }
42
+
43
+ Datum
44
+ to_ascii_enc (PG_FUNCTION_ARGS )
45
+ {
46
+ multibyte_error ();
47
+ }
48
+
49
+ Datum
50
+ to_ascii_default (PG_FUNCTION_ARGS )
51
+ {
52
+ multibyte_error ();
53
+ }
54
+
55
+
56
+ #else /* with MULTIBYTE */
57
+
58
+
59
+ /* ----------
60
+ * even if MULTIBYTE is enabled
61
+ * ----------
62
+ */
63
+
64
+ static text * encode_to_ascii (text * data , int enc );
65
+
66
+ /* ----------
67
+ * to_ascii
68
+ * ----------
69
+ */
70
+ char *
71
+ pg_to_ascii (unsigned char * src , unsigned char * src_end , unsigned char * desc , int enc )
72
+ {
73
+ unsigned char * x = NULL ;
74
+ unsigned char * ascii = NULL ;
75
+ int range = 0 ;
76
+
77
+ /*
78
+ * relevant start for an encoding
79
+ */
80
+ #define RANGE_128 128
81
+ #define RANGE_160 160
82
+
83
+
84
+ if (enc == LATIN1 )
85
+ {
86
+ /* ----------
87
+ * ISO-8859-1 <range: 160 -- 255>
88
+ * ----------
89
+ */
90
+ ascii = " cL Y \"Ca -R 'u ., ?AAAAAAACEEEEIIII NOOOOOxOUUUUYTBaaaaaaaceeeeiiii nooooo/ouuuuyty" ;
91
+ range = RANGE_160 ;
92
+ }
93
+ else if (enc == LATIN2 )
94
+ {
95
+ /* ----------
96
+ * ISO-8859-2 <range: 160 -- 255>
97
+ * ----------
98
+ */
99
+ ascii = " A L LS \"SSTZ-ZZ a,l'ls ,sstz\"zzRAAAALCCCEEEEIIDDNNOOOOxRUUUUYTBraaaalccceeeeiiddnnoooo/ruuuuyt." ;
100
+ range = RANGE_160 ;
101
+ }
102
+ else if (enc == WIN1250 )
103
+ {
104
+ /* ----------
105
+ * Window CP1250 <range: 128 -- 255>
106
+ * ----------
107
+ */
108
+ ascii = " ' \" %S<STZZ `'\"\".-- s>stzz L A \"CS -RZ ,l'u .,as L\"lzRAAAALCCCEEEEIIDDNNOOOOxRUUUUYTBraaaalccceeeeiiddnnoooo/ruuuuyt " ;
109
+ range = RANGE_128 ;
110
+ }
111
+ else
112
+ {
113
+ elog (ERROR , "pg_to_ascii(): unsupported encoding from %s" ,
114
+ pg_encoding_to_char (enc ));
115
+ }
116
+
117
+ /* ----------
118
+ * Encode
119
+ * ----------
120
+ */
121
+ for (x = src ; x <= src_end ; x ++ )
122
+ {
123
+ if (* x < 128 )
124
+ * desc ++ = * x ;
125
+ else if (* x < range )
126
+ * desc ++ = ' ' ; /* bogus 128 to 'range' */
127
+ else
128
+ * desc ++ = ascii [* x - range ];
129
+ }
130
+
131
+ return desc ;
132
+ }
133
+
134
+ /* ----------
135
+ * encode text
136
+ * ----------
137
+ */
138
+ static text *
139
+ encode_to_ascii (text * data , int enc )
140
+ {
141
+ pg_to_ascii (
142
+ (unsigned char * ) VARDATA (data ), /* src */
143
+ VARDATA (data ) + VARSIZE (data ), /* src end */
144
+ (unsigned char * ) VARDATA (data ), /* desc */
145
+ enc ); /* encoding */
146
+
147
+ return data ;
148
+ }
149
+
150
+ /* ----------
151
+ * convert to ASCII - enc is set as 'name' arg.
152
+ * ----------
153
+ */
154
+ Datum
155
+ to_ascii_encname (PG_FUNCTION_ARGS )
156
+ {
157
+ PG_RETURN_TEXT_P
158
+ (
159
+ encode_to_ascii
160
+ (
161
+ PG_GETARG_TEXT_P_COPY (0 ),
162
+ pg_char_to_encoding ( NameStr (* PG_GETARG_NAME (1 )) )
163
+ )
164
+ );
165
+ }
166
+
167
+ /* ----------
168
+ * convert to ASCII - enc is set as int4
169
+ * ----------
170
+ */
171
+ Datum
172
+ to_ascii_enc (PG_FUNCTION_ARGS )
173
+ {
174
+ PG_RETURN_TEXT_P
175
+ (
176
+ encode_to_ascii
177
+ (
178
+ PG_GETARG_TEXT_P_COPY (0 ),
179
+ PG_GETARG_INT32 (1 )
180
+ )
181
+ );
182
+ }
183
+
184
+ /* ----------
185
+ * convert to ASCII - current enc is DatabaseEncoding
186
+ * ----------
187
+ */
188
+ Datum
189
+ to_ascii_default (PG_FUNCTION_ARGS )
190
+ {
191
+ PG_RETURN_TEXT_P
192
+ (
193
+ encode_to_ascii
194
+ (
195
+ PG_GETARG_TEXT_P_COPY (0 ),
196
+ GetDatabaseEncoding ()
197
+ )
198
+ );
199
+ }
200
+
201
+ #endif /* MULTIBYTE */
0 commit comments