在PostgreSQL C语言函数中更改字符编码

3
我正在使用Windows服务器上的PostgreSQL 9.5 64位版本。数据库的字符编码设置为UTF8。
我想创建一个操作多字节字符串(例如清理、替换等)的函数。
我从其他系统中复制了用于操作字符的C语言逻辑,该逻辑假定字符代码为sjis。
我不想改变C语言逻辑,因此我想在PostgreSQL的C语言函数中将UTF8转换为sjis,就像convert_to函数一样。(但是,由于convert_to函数返回bytea类型,所以我想使用TEXT类型获取它。)
请告诉我如何在C语言中将UTF8转换为sjis。
创建函数脚本:
CREATE FUNCTION CLEANSING_STRING(character varying)
RETURNS character varying AS
'$libdir/MyFunc/CLEANSING_STRING.dll', 'CLEANSING_STRING'
LANGUAGE c VOLATILE STRICT;

C代码:

#include <stdio.h>
#include <string.h>
#include <postgres.h>
#include <port.h>
#include <fmgr.h>
#include <stdlib.h>
#include <builtins.h>

#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif

extern PGDLLEXPORT Datum CLEANSING_STRING(PG_FUNCTION_ARGS);

PG_FUNCTION_INFO_V1(CLEANSING_STRING);
Datum CLEANSING_STRING(PG_FUNCTION_ARGS)
{

    // Get Arg
    text *arg1 = (text *)PG_GETARG_TEXT_P(0);

    // Text to Char[]
    char *arg;
    arg = text_to_cstring(arg1);

    // UTF8 to Sjis
    //Char *sjisChar[] = foo(arg);  // something like that..

    // Copied from other system.(Assumes that the character code is sjis.)
    cleansingString(sjisChar);
    replaceStrimg(sjisChar);

    // Sjis to UTF8
    //arg = bar(sjisChar);  // something like that..

    //Char[] to Text and Return
    PG_RETURN_TEXT_P(cstring_to_text(arg));
}

1
请查看 src/backend/utils/mb/mbutils.c 文件中的函数 any_to_serverserver_to_any,以及 mbutils.c 文件顶部的注释。 - Craig Ringer
谢谢您的回复。 我不明白如何指定第三个参数编码。 有没有编码列表?这个用法正确吗? Char sjisChar[] = server_to_any(arg, strlen(arg), / sjis 编码号 */ ); - Yuwaz
@CraigRinger 我忘了提及。 - Yuwaz
抱歉,pg_server_to_anypg_any_to_server。关于编码名称,请参阅 src/backend/utils/mb/encnames.c 中的 pg_enc2name_tblpg_char_to_encoding 函数。 - Craig Ringer
@CraigRinger 程序已按预期完成!非常感谢。源代码将在后续回答中进行描述。 - Yuwaz
1个回答

1

通过问题评论所教授的方式成功了。

#include <mb/pg_wchar.h> //Add to include.

...

Datum CLEANSING_STRING(PG_FUNCTION_ARGS)
{

    // Get Arg
    text *arg1 = (text *)PG_GETARG_TEXT_P(0);

    // Text to Char[]
    char *arg;
    arg = text_to_cstring(arg1);

    // UTF8 to Sjis
    Char *sjisChar[] = pg_server_to_any(arg, strlen(arg), PG_SJIS);

    // Copied from other system.(Assumes that the character code is sjis.)
    cleansingString(sjisChar);
    replaceStrimg(sjisChar);

    // Sjis to UTF8
    arg =  pg_any_to_server(sjisChar, strlen(sjisChar), PG_SJIS); //It converts from SJIS to server (UTF 8), the third argument sets the encoding of the conversion source.

    //Char[] to Text and Return
    PG_RETURN_TEXT_P(cstring_to_text(arg));
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接