Return-Path: owner-postman
Received: from LOCALHOST (LOCALHOST [127.0.0.1]) by nobozo.CS.Berkeley.EDU (8.6.4/8.6.3) with SMTP id KAA00237 for postgres-redist; Mon, 13 Jun 1994 10:29:32 -0700
Resent-From: POSTGRES mailing list <postman@postgres.Berkeley.EDU>
Resent-Message-Id: <199406131729.KAA00237@nobozo.CS.Berkeley.EDU>
X-Authentication-Warning: nobozo.CS.Berkeley.EDU: Host LOCALHOST didn't use HELO protocol
Sender: owner-postman@postgres.Berkeley.EDU
X-Return-Path: owner-postman
Received: from arcadia.CS.Berkeley.EDU (arcadia.CS.Berkeley.EDU [128.32.149.40]) by nobozo.CS.Berkeley.EDU (8.6.4/8.6.3) with ESMTP id KAA00227 for <postgres@postgres.Berkeley.EDU>; Mon, 13 Jun 1994 10:29:31 -0700
Received: from localhost (jolly@localhost) by arcadia.CS.Berkeley.EDU (8.6.4/8.6.3) id KAA11943; Mon, 13 Jun 1994 10:29:27 -0700
Message-Id: <199406131729.KAA11943@arcadia.CS.Berkeley.EDU>
To: aataneja@cs.mtu.edu (Bruce Taneja.)
cc: postgres@postgres.Berkeley.EDU
Subject: Re: using "TEXT" in C files .. 
In-reply-to: Your message of "Sun, 12 Jun 1994 18:39:05 EDT."
             <9406122239.AA26597@cs.mtu.edu> 
Date: Mon, 13 Jun 1994 10:29:27 -0700
From: Jolly Chen <jolly@postgres.Berkeley.EDU>
Resent-To: postgres-redist@postgres.Berkeley.EDU
Resent-Date: Mon, 13 Jun 94 10:29:32 -0700
Resent-XMts: smtp



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

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

It is simply a  struct varlena.

> Also can we safely assume (while coding in C) that a retrieved 
> 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

==============================================================================
   To add/remove yourself to/from the POSTGRES mailing list: send mail with 
   the subject line ADD or DEL to "postgres-request@postgres.Berkeley.EDU"

   If this fails, send mail to "post_questions@postgres.Berkeley.EDU" and
   a human will deal with it.  DO NOT post to the "postgres" mailing list.
==============================================================================
