|
3 | 3 | * client encoding and server internal encoding. |
4 | 4 | * (currently mule internal code (mic) is used) |
5 | 5 | * Tatsuo Ishii |
6 | | - * $Id: mbutils.c,v 1.26 2001/11/19 06:48:39 ishii Exp $ |
| 6 | + * $Id: mbutils.c,v 1.27 2001/11/20 01:32:29 ishii Exp $ |
7 | 7 | */ |
8 | 8 | #include"postgres.h" |
9 | 9 |
|
@@ -212,54 +212,19 @@ pg_do_encoding_conversion(unsigned char *src, int len, |
212 | 212 | Datum |
213 | 213 | pg_convert(PG_FUNCTION_ARGS) |
214 | 214 | { |
215 | | -text*string=PG_GETARG_TEXT_P(0); |
216 | | -Names=PG_GETARG_NAME(1); |
217 | | -intencoding=pg_char_to_encoding(NameStr(*s)); |
218 | | -intdb_encoding=DatabaseEncoding->encoding; |
219 | | -to_mic_convertersrc; |
220 | | -from_mic_converterdest; |
221 | | -unsignedchar*result; |
222 | | -text*retval; |
223 | | -unsignedchar*str; |
224 | | -intlen; |
225 | | - |
226 | | -if (encoding<0) |
227 | | -elog(ERROR,"Invalid encoding name %s",NameStr(*s)); |
228 | | - |
229 | | -if (pg_find_encoding_converters(db_encoding,encoding,&src,&dest)<0) |
230 | | -{ |
231 | | -char*encoding_name= (char*)pg_encoding_to_char(db_encoding); |
| 215 | +Datumstring=PG_GETARG_DATUM(0); |
| 216 | +Datumdest_encoding_name=PG_GETARG_DATUM(1); |
| 217 | +Datumsrc_encoding_name=DirectFunctionCall1( |
| 218 | +namein,CStringGetDatum(DatabaseEncoding->name)); |
| 219 | +Datumresult; |
232 | 220 |
|
233 | | -elog(ERROR,"Conversion from %s to %s is not possible",NameStr(*s),encoding_name); |
234 | | -} |
| 221 | +result=DirectFunctionCall3( |
| 222 | +pg_convert2,string,src_encoding_name,dest_encoding_name); |
235 | 223 |
|
236 | | -/* make sure that source string is null terminated */ |
237 | | -len=VARSIZE(string)-VARHDRSZ; |
238 | | -str=palloc(len+1); |
239 | | -memcpy(str,VARDATA(string),len); |
240 | | -*(str+len)='\0'; |
| 224 | +/* free memory allocated by namein */ |
| 225 | +pfree((void*)dest_encoding_name); |
241 | 226 |
|
242 | | -result=pg_do_encoding_conversion(str,len,src,dest); |
243 | | -if (result==NULL) |
244 | | -elog(ERROR,"Encoding conversion failed"); |
245 | | - |
246 | | -/* build text data type structre. we cannot use textin() here, |
247 | | - since textin assumes that input string encoding is same as |
248 | | - database encoding. */ |
249 | | -len=strlen(result)+VARHDRSZ; |
250 | | -retval=palloc(len); |
251 | | -VARATT_SIZEP(retval)=len; |
252 | | -memcpy(VARDATA(retval),result,len-VARHDRSZ); |
253 | | - |
254 | | -/* free memory allocated by pg_do_encoding_conversion */ |
255 | | -if (result!=str) |
256 | | -pfree(result); |
257 | | -pfree(str); |
258 | | - |
259 | | -/* free memory if allocated by the toaster */ |
260 | | -PG_FREE_IF_COPY(string,0); |
261 | | - |
262 | | -PG_RETURN_TEXT_P(retval); |
| 227 | +PG_RETURN_TEXT_P(result); |
263 | 228 | } |
264 | 229 |
|
265 | 230 | /* |
|