Обсуждение: postgres.h MACRO issues

Поиск
Список
Период
Сортировка

postgres.h MACRO issues

От
David Busby
Дата:
Elephant Developers,

    I've got a bit of an issue with using your macros while building my own C
function for PostgreSQL (btw: C functions are a wayyyy cool feature).
So I'm returning a char(32) from my function and I'm using the text*
structure to work with my data.  Here's where I've got trouble.

// Put my 32 bytes into the buffer, this works perfect
memcpy(VARDATA(out_text), pbuf, 32);
// Tell it the buffer size
// I don't like that I've got to add the header size myself
// could/should VARATT_SIZEP handle that for me?
VARATT_SIZEP(out_text) = VARHDRSZ + 32;

// This doesn't work right, but I assumed that it would
// I thought that VARATT_SIZEP would automagically take into
// account the VARHDSZ so I only say "my data size is X"
// not have to say "my data size is X + your header"
// Seems a little odd to me.
VARATT_SIZEP(out_text) = 32

So...I just wanted to share that with you folks, should also mention
that I really apperciate (sp?) the work you've done so far and look
forward to future versions.

Thanks for the cycles
/B

Re: postgres.h MACRO issues

От
Tom Lane
Дата:
David Busby <busby@pnts.com> writes:
> // Put my 32 bytes into the buffer, this works perfect
> memcpy(VARDATA(out_text), pbuf, 32);
> // Tell it the buffer size
> // I don't like that I've got to add the header size myself
> // could/should VARATT_SIZEP handle that for me?
> VARATT_SIZEP(out_text) = VARHDRSZ + 32;

VARATT_SIZEP can't be changed without breaking lots of extant code.
However, there's nothing stopping you from defining your own
convenience macro, along the lines of

#define SET_CHAR_LEN(ptr, len)  (VARATT_SIZEP(ptr) = (len) + VARHDRSZ)

            regards, tom lane