pg.ddx.io  pgsql-interfaces@postgresql.org mailing list archive  
help / color / mirror / Atom feed
type text in c functions
2+ messages / 2 participants
[nested] [flat]

* type text in c functions
@ 2004-12-19 01:16  Robert Wimmer <seppwimmer@hotmail.com>
  0 siblings, 1 reply; 2+ messages in thread

From: Robert Wimmer @ 2004-12-19 01:16 UTC (permalink / raw)
  To: pgsql-interfaces

i tried to write a simple typechecking function in c. but it does not work 
correct

-- CODE --
#include <postgres.h>
#include <string.h>
#include <fmgr.h>

PG_FUNCTION_INFO_V1(text_to_card);

Datum text_to_card(PG_FUNCTION_ARGS)
{
  int i,l;
  int32 ret;

  text *t = PG_GETARG_TEXT_P(0);

  l = VARSIZE(t) - VARHDRSZ;

  for (i = 0; i < l; i++) { if ( (t->vl_dat[i] < '0') || (t->vl_dat[i] > 
'9')) break; }

  if (i==l) ret = 0;
  else ret = i;

  PG_RETURN_INT32(ret);
}

--END --

this function should check if it is possible to cast a TEXT in an unsigned 
INTEGER. if OK it returns 0
otherwise the position in the string where the error occured.
( so you can produce error messages like ERROR[234] text_to_card('paramete') 
failed at <3>)

i assumed - after reading the corresponding header files and docs and google 
- the type structure of a text as above.
i only get zeros and ones as result

the algorithm works fine in 'normal' c.

is there a possibilty to treat a TEXT parameter like a char array ?

thanks and regards sepp

_________________________________________________________________
Schaffen Sie das Platzproblem in Ihrem E-Mail-Konto für immer aus der Welt! 
http://join.msn.com/?pgmarket=de-at&DI=1031&XAPID=1581




^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: type text in c functions
@ 2004-12-19 03:21  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Robert Wimmer <seppwimmer@hotmail.com>
  0 siblings, 0 replies; 2+ messages in thread

From: Tom Lane @ 2004-12-19 03:21 UTC (permalink / raw)
  To: Robert Wimmer <seppwimmer@hotmail.com>; +Cc: pgsql-interfaces

"Robert Wimmer" <seppwimmer@hotmail.com> writes:
>   text *t = PG_GETARG_TEXT_P(0);

>   l = VARSIZE(t) - VARHDRSZ;

>   for (i = 0; i < l; i++) { if ( (t->vl_dat[i] < '0') || (t->vl_dat[i] > 
> '9')) break; }

>   if (i==l) ret = 0;
>   else ret = i;

I think you need to rethink your return convention --- success and
failure at the first character both return 0.

Directly using the vl_dat field doesn't seem like good style (you won't
find it anywhere in the backend sources) but it works.

			regards, tom lane



^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2004-12-19 03:21 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2004-12-19 01:16 type text in c functions Robert Wimmer <seppwimmer@hotmail.com>
2004-12-19 03:21 ` Tom Lane <tgl@sss.pgh.pa.us>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox