2 * This file contains public functions for conversion between
3 * client encoding and server internal encoding.
4 * (currently mule internal code (mic) is used)
6 * $Id: mbutils.c,v 1.8 1999/07/17 20:18:10 momjian Exp $ */
10 #include "miscadmin.h"
11 #include "mb/pg_wchar.h"
13 static int client_encoding = -1;
14 static void (*client_to_mic) ();/* something to MIC */
15 static void (*client_from_mic) (); /* MIC to something */
16 static void (*server_to_mic) ();/* something to MIC */
17 static void (*server_from_mic) (); /* MIC to something */
20 * find encoding table entry by encoding
22 static pg_encoding_conv_tbl *
23 get_enc_ent(int encoding)
25 pg_encoding_conv_tbl *p = pg_conv_tbl;
27 for (; p->encoding >= 0; p++)
29 if (p->encoding == encoding)
36 * set the client encoding. if client/server encoding is
37 * not supported, returns -1
40 pg_set_client_encoding(int encoding)
42 int current_server_encoding = GetDatabaseEncoding();
44 client_encoding = encoding;
46 if (client_encoding == current_server_encoding)
47 { /* server == client? */
48 client_to_mic = client_from_mic = 0;
49 server_to_mic = server_from_mic = 0;
51 else if (current_server_encoding == MULE_INTERNAL)
52 { /* server == MULE_INETRNAL? */
53 client_to_mic = get_enc_ent(encoding)->to_mic;
54 client_from_mic = get_enc_ent(encoding)->from_mic;
55 server_to_mic = server_from_mic = 0;
56 if (client_to_mic == 0 || client_from_mic == 0)
59 else if (encoding == MULE_INTERNAL)
60 { /* client == MULE_INETRNAL? */
61 client_to_mic = client_from_mic = 0;
62 server_to_mic = get_enc_ent(current_server_encoding)->to_mic;
63 server_from_mic = get_enc_ent(current_server_encoding)->from_mic;
64 if (server_to_mic == 0 || server_from_mic == 0)
69 client_to_mic = get_enc_ent(encoding)->to_mic;
70 client_from_mic = get_enc_ent(encoding)->from_mic;
71 server_to_mic = get_enc_ent(current_server_encoding)->to_mic;
72 server_from_mic = get_enc_ent(current_server_encoding)->from_mic;
73 if (client_to_mic == 0 || client_from_mic == 0)
75 if (server_to_mic == 0 || server_from_mic == 0)
82 * returns the current client encoding
85 pg_get_client_encoding()
87 if (client_encoding == -1)
89 /* this is the first time */
90 client_encoding = GetDatabaseEncoding();
92 return (client_encoding);
96 * convert client encoding to server encoding. if server_encoding ==
97 * client_encoding or no conversion function exists,
98 * returns s. So be careful.
101 pg_client_to_server(unsigned char *s, int len)
103 static unsigned char b1[MAX_PARSE_BUFFER * 4]; /* is this enough? */
104 static unsigned char b2[MAX_PARSE_BUFFER * 4]; /* is this enough? */
105 unsigned char *p = s;
107 if (client_encoding == GetDatabaseEncoding())
111 (*client_to_mic) (s, b1, len);
117 (*server_from_mic) (p, b2, len);
124 * convert server encoding to client encoding. if server_encoding ==
125 * client_encoding or no conversion function exists,
126 * returns s. So be careful.
129 pg_server_to_client(unsigned char *s, int len)
131 static unsigned char b1[MAX_PARSE_BUFFER * 4]; /* is this enough? */
132 static unsigned char b2[MAX_PARSE_BUFFER * 4]; /* is this enough? */
133 unsigned char *p = s;
135 if (client_encoding == GetDatabaseEncoding())
139 (*server_to_mic) (s, b1, len);
145 (*client_from_mic) (p, b2, len);
151 /* convert a multi-byte string to a wchar */
153 pg_mb2wchar(const unsigned char *from, pg_wchar * to)
155 (*pg_wchar_table[GetDatabaseEncoding()].mb2wchar_with_len) (from, to, strlen(from));
158 /* convert a multi-byte string to a wchar with a limited length */
160 pg_mb2wchar_with_len(const unsigned char *from, pg_wchar * to, int len)
162 (*pg_wchar_table[GetDatabaseEncoding()].mb2wchar_with_len) (from, to, len);
165 /* returns the byte length of a multi-byte word */
167 pg_mblen(const unsigned char *mbstr)
169 return ((*pg_wchar_table[GetDatabaseEncoding()].mblen) (mbstr));
172 /* returns the length (counted as a wchar) of a multi-byte string */
174 pg_mbstrlen(const unsigned char *mbstr)
180 mbstr += pg_mblen(mbstr);
186 /* returns the length (counted as a wchar) of a multi-byte string
187 (not necessarily NULL terminated) */
189 pg_mbstrlen_with_len(const unsigned char *mbstr, int limit)
194 while (*mbstr && limit > 0)
205 * returns the length of a multi-byte string
206 * (not necessarily NULL terminated)
207 * that is not longer than limit.
208 * this function does not break multi-byte word boundary.
211 pg_mbcliplen(const unsigned char *mbstr, int len, int limit)
216 while (*mbstr && len > 0)
219 if ((clen + l) > limit)
231 * fuctions for utils/init
233 static int DatabaseEncoding = MULTIBYTE;
235 SetDatabaseEncoding(int encoding)
237 DatabaseEncoding = encoding;
241 GetDatabaseEncoding()
243 return (DatabaseEncoding);
246 /* for builtin-function */
248 getdatabaseencoding()
250 return (pg_encoding_to_char(DatabaseEncoding));
253 /* set and get template1 database encoding */
254 static int templateEncoding;
256 SetTemplateEncoding(int encoding)
258 templateEncoding = encoding;
262 GetTemplateEncoding()
264 return (templateEncoding);