Simplify pg_convert() in that it calls pg_convert2 using new fmgr interface.

This commit is contained in:
Tatsuo Ishii 2001-11-20 01:32:29 +00:00
parent 1436b212cc
commit 933761e7b1

View File

@ -3,7 +3,7 @@
* client encoding and server internal encoding. * client encoding and server internal encoding.
* (currently mule internal code (mic) is used) * (currently mule internal code (mic) is used)
* Tatsuo Ishii * Tatsuo Ishii
* $Id: mbutils.c,v 1.26 2001/11/19 06:48:39 ishii Exp $ * $Id: mbutils.c,v 1.27 2001/11/20 01:32:29 ishii Exp $
*/ */
#include "postgres.h" #include "postgres.h"
@ -212,54 +212,19 @@ pg_do_encoding_conversion(unsigned char *src, int len,
Datum Datum
pg_convert(PG_FUNCTION_ARGS) pg_convert(PG_FUNCTION_ARGS)
{ {
text *string = PG_GETARG_TEXT_P(0); Datum string = PG_GETARG_DATUM(0);
Name s = PG_GETARG_NAME(1); Datum dest_encoding_name = PG_GETARG_DATUM(1);
int encoding = pg_char_to_encoding(NameStr(*s)); Datum src_encoding_name = DirectFunctionCall1(
int db_encoding = DatabaseEncoding->encoding; namein, CStringGetDatum(DatabaseEncoding->name));
to_mic_converter src; Datum result;
from_mic_converter dest;
unsigned char *result;
text *retval;
unsigned char *str;
int len;
if (encoding < 0) result = DirectFunctionCall3(
elog(ERROR, "Invalid encoding name %s", NameStr(*s)); pg_convert2, string, src_encoding_name, dest_encoding_name);
if (pg_find_encoding_converters(db_encoding, encoding, &src, &dest) < 0) /* free memory allocated by namein */
{ pfree((void *)dest_encoding_name);
char *encoding_name = (char *) pg_encoding_to_char(db_encoding);
elog(ERROR, "Conversion from %s to %s is not possible", NameStr(*s), encoding_name); PG_RETURN_TEXT_P(result);
}
/* make sure that source string is null terminated */
len = VARSIZE(string) - VARHDRSZ;
str = palloc(len + 1);
memcpy(str, VARDATA(string), len);
*(str + len) = '\0';
result = pg_do_encoding_conversion(str, len, src, dest);
if (result == NULL)
elog(ERROR, "Encoding conversion failed");
/* build text data type structre. we cannot use textin() here,
since textin assumes that input string encoding is same as
database encoding. */
len = strlen(result) + VARHDRSZ;
retval = palloc(len);
VARATT_SIZEP(retval) = len;
memcpy(VARDATA(retval), result, len - VARHDRSZ);
/* free memory allocated by pg_do_encoding_conversion */
if (result != str)
pfree(result);
pfree(str);
/* free memory if allocated by the toaster */
PG_FREE_IF_COPY(string, 0);
PG_RETURN_TEXT_P(retval);
} }
/* /*