Subject: using "TEXT" in C files ..

From: [email-masked] (Bruce Taneja.)
Date: Sun, 12 Jun 1994 18:39:05 -0400 (EDT)

Greetings Netters,

What files are needed to be included to use the "TEXT"
data type of Postgres while writing code for some complex
data definition ..?

The CIRCLE example showed which file to include to use
the POINT data type .. (is any required for TEXT ?)

can we say :

typedef struct {

	CIRCLE c;
	int i;
	TEXT t;        <<<<?????

	} xyz;

Also is this TEXT of variable size .. ? >>

If so then is this xyz type to be defined as a "variable-internal
length" .. ?  (because we won't know the size of xyz till the run
time) ...

Also can we safely assume (while coding in C) that a retrieved 
glob (from disk) will have the TEXT t ending with a NULL ??

thanks in advance!
Bruce!

==============================================================================
   To add/remove yourself to/from the POSTGRES mailing list: send mail with 
   the subject line ADD or DEL to "[email-masked]"

   If this fails, send mail to "[email-masked]" and
   a human will deal with it.  DO NOT post to the "postgres" mailing list.
==============================================================================

---

From: Jolly Chen <[email-masked]>
Date: Mon, 13 Jun 1994 10:29:27 -0700

> data type of Postgres while writing code for some complex

text is defined in   src/backend/tmp/postgres.h

It is simply a  struct varlena.

> glob (from disk) will have the TEXT t ending with a NULL ??

DON'T assume that text types are null-terminated.  In general, they
won't be.   Instead, use the macros  VARSIZE, VARDATA, and VARHDRSZ
defined in postgres.h .

To convert a text to a null-terminated char*, you can do something
like this:

/** takes a text varlena and returns a char* 
    the CALLER is responsible for freeing the memory of the char* returned **/

char* text2str(text* textarg)
{
  int len;
  char* data;
  char* result;

  len = VARSIZE(textarg) - VARHDRSZ + 1;/* don't count the bytes of the size field, but add one for the '\0' */
  data = VARDATA(textarg);
  result = (char*)palloc(len); 
  strncpy(result, data, len-1);
  result[len-1] = '\0';
  return result;
}

- Jolly Chen