Re: return varchar from C function - Mailing list pgsql-hackers

From Pavel Stehule
Subject Re: return varchar from C function
Date
Msg-id BAY20-F2105C029334674EEB8C8BBF98B0@phx.gbl
Whole thread Raw
In response to return varchar from C function  ("scotty@linuxtime.it" <scotty@linuxtime.it>)
Responses Re: return varchar from C function
List pgsql-hackers
Hello

cstring is clasic c (zero terminated) string and is used only in some 
PostgreSQL functions. This type isn't compatible with text and you have to 
explicit cast trick with textin function.

root=# select textin(('abc'::cstring));
textin
--------
abc
(1 row)

Standard is using VARLENA types like text, varchar, ... You can find info in 
PostgreSQL FAQ. These types are similar Pascal string -> first four bytes 
cary length and next bytes are data without spec. ending symbol. 
http://www.varlena.com/GeneralBits/68.php

using text type in C function is simple:

Datum *const_fce(PG_FUNCTION_ARGS)
{   text *txt = palloc(5 + VARHDRSZ);   memcpy(VARDATA(txt), "pavel", 5);   VARATT_SIZE(txt) = 5 + VARHDRSZ;
   PG_RETURN_TEXT_P(txt);
}

please look to source code my orafce contrib module (you can find it on 
pgfoundry).

Regards
Pavel Stehule

_________________________________________________________________
Najdete si svou lasku a nove pratele na Match.com. http://www.msn.cz/



pgsql-hackers by date:

Previous
From: Russell Smith
Date:
Subject: Re: Plan invalidation design
Next
From: Gregory Stark
Date:
Subject: Re: return varchar from C function