Hey folks
I am trying to write simple function, that would filter out a char
from text/string. It's being a while since I last time wrote c
function for postgresql (8.1), and few things are gone in API there.
Can someone tell me what's wrong with that function please ?
#include "postgres.h"
#include <string.h>
#include "fmgr.h"
#include "utils/builtins.h"
#include "libpq/pqformat.h"
PG_FUNCTION_INFO_V1(filter_text);
PG_MODULE_MAGIC;
// filter_text(text, char);
Datum
filter_text(PG_FUNCTION_ARGS)
{
text *arg1 = PG_GETARG_TEXT_P(0);
char c = PG_GETARG_CHAR(1);
int i, j;
text *new_text;
if (PG_ARGISNULL(0) || PG_ARGISNULL(1))
PG_RETURN_NULL();
new_text = (text *) palloc(VARSIZE(arg1)+VARHDRSZ);
SET_VARSIZE(new_text, VARSIZE(arg1)+VARHDRSZ);
for(i=0, j=0; i!=VARSIZE(arg1)-VARHDRSZ; i++)
{
if ( VARDATA(arg1)[i] != c )
{
VARDATA(new_text)[j++] = VARDATA(arg1)[i];
}
}
VARDATA(new_text)[j++] = '\0';
SET_VARSIZE(new_text, j+VARHDRSZ);
PG_RETURN_TEXT_P(new_text);
}
--
GJ