C function question

From: Grzegorz Jaśkiewicz <gryzman(at)gmail(dot)com>
To: General Postgres Mailing List <pgsql-general(at)postgresql(dot)org>
Subject: C function question
Date: 2009-02-03 19:09:59
Message-ID: 2f4958ff0902031109r58fb375br6b4ff3d4c86f8e6d@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

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

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Chris Mayfield 2009-02-03 20:03:43 Re: Pet Peeves?
Previous Message David Fetter 2009-02-03 19:04:58 Re: Pet Peeves?