Subject: Re: C functions

From: Jeff Meredith <[email-masked]>
Date: Fri, 05 Feb 93 15:39:08 -0800

message <[email-masked]> 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
[email-masked]