Return-Path: pg_adm@postgres.berkeley.edu
Received: by postgres.Berkeley.EDU (5.61/1.29)
	id AA26601; Fri, 5 Feb 93 15:39:26 -0800
Message-Id: <9302052339.AA26601@postgres.Berkeley.EDU>
From: Jeff Meredith <mer@miro.com>
Subject: Re: C functions
To: postgres@postgres.berkeley.edu
Sender: pg_adm@postgres.berkeley.edu
In-Reply-To: the message from Mike Krus <cs.concordia.ca!krus@netcomsv.netcom.com>, Mike Krus <cs.concordia.ca!krus@netcomsv.netcom.com> 
	     sent "Fri, 05 Feb 93 16:36:10 EST."
Date: Fri, 05 Feb 93 15:39:08 -0800
From: Jeff Meredith <mer@miro.com>

message <9302051636.aa17151@concour.cs.concordia.ca> read as follows:

> Ok, I'm pretty sure this is dumb question to which there is an obvious answer
> but I simply can't figure it out.
> 
> I have a table PLANE of which one field named PID is of type text.
> I wrote the following C function to test if the first letter is a B :
> 
> #include <tmp/libpq-fe.h>
> 
> extern char* GetAttributeByName();
> 
> bool test(plane)
> TUPLE plane;
> {
> 	char *PID;
> 
> 	PID = GetAttributeByName(plane, "PID");
> 	return ( *PID == 'B' );
> }

text attributes are what postgres calls a varlena.  i believe the user
manual goes into some detail about how to deal with them.  basically you
need to declare you local variable as:

	struct varlena *PID;

where struct varlena is defined as:

	struct varlena {
	    long vl_len;
	    char vl_dat[1];
	}

your test should then be:

	return ( PID->vl_dat[0] == 'B');

you might also want to be sure that the PID attributes that you are fetching
are not null.  i don't remember how to do that in the 4.0.1 system offhand.

> Related question : can a C function like the one above call functions from the
> standard C library, i.e. is the library linked in some way to the back end?

i believe that dynamically loaded code is linked against postgres, libc,
and libm (so the answer to your question is yes).


jeff meredith
mer@miro.com
