如何将c代码映射到postgres c函数格式?
我试图用postgres c函数风格编写以下c代码- 传递的参数是varchar类型的表列的值, 需要一些帮助才能将此纯C代码映射到类型为的postgres C格式- 基准面 函数名(PG函数参数) 有人能帮我映射并返回合适的字符值吗如何将c代码映射到postgres c函数格式?,c,postgresql,C,Postgresql,我试图用postgres c函数风格编写以下c代码- 传递的参数是varchar类型的表列的值, 需要一些帮助才能将此纯C代码映射到类型为的postgres C格式- 基准面 函数名(PG函数参数) 有人能帮我映射并返回合适的字符值吗 static uint32_t key = 0xOQ9426YP; uint32_t tmpKey = 0x00000000; int i = 0; uint32_t *in = (uint32_t *) str; uint32_t *out = (uint32_
static uint32_t key = 0xOQ9426YP;
uint32_t tmpKey = 0x00000000;
int i = 0;
uint32_t *in = (uint32_t *) str;
uint32_t *out = (uint32_t *) malloc (256);
memset (out, 0, 256);
tmpKey = key;
for (i = 0; i < (256/sizeof (uint32_t)); i++)
{
out[i] = tmpKey ^ in[i];
tmpKey = out[i];
}
memcpy (output, (char *) out, 256);
static uint32\u t key=0xOQ9426YP;
uint32_t tmpKey=0x00000000;
int i=0;
uint32_t*in=(uint32_t*)str;
uint32_t*out=(uint32_t*)malloc(256);
memset(out,0,256);
tmpKey=key;
对于(i=0;i<(256/sizeof(uint32_t));i++)
{
out[i]=tmpKey^in[i];
tmpKey=out[i];
}
memcpy(输出,(字符*)输出,256);
谢谢和问候,
VJ对于数据,PostgreSQL使用自己的内部格式和结构,不应与C语言兼容。对于字符串,使用
varlena
格式-在此格式中,前N个字节为编码长度-这些字节后为编码数据。不建议直接访问-几乎所有工作都有宏
例如,一些小函数hello
可能看起来像:
Datum
Hello(PG_FUNCTION_ARGS)
{
text *txt = PG_GETARG_TEXT_PP(0);
text *result;
char *str;
char *hello = "Hello, ";
int size;
int hello_size;
-- conversion to c string
str = text_to_cstring(txt);
-- show it on debug console
elog(NOTICE, "input string: %s", str);
hello_size = strlen(hello);
size = hello_size + VARSIZE_ANY_EXHDR(txt) + VARHDRSZ;
-- allocate memory for result, use palloc, not malloc!
result = palloc(size);
--set size of result varlena type
SET_VARSIZE(result, size);
-- set data
memcpy(VARDATA(result), str, hello_size);
memcpy(VARDATA(result) + hello_size,
VARDATA_ANY(txt), VARSIZE_ANY_EXHDR(txt));
-- returns data
PG_RETURN_TEXT_P(result);
}
你可以看到,这是C,而不是C。有很多宏,可以用于a)可移植性,b)隐藏复杂性。开始阅读或做一些介绍是很好的
本主题不太难,也不太复杂,但它并不直观-不阅读文档就无法开始。此代码完全忽略了结尾。这是您的意图吗?除此之外,
0xOQ9426YP
不是有效的十六进制文字。