/* * pg_unac.c * * Author: Nhan NGO DINH * Version: 1.1 * Description: PostgreSQL external function * unaccent the given string * */ #include #include #include #include #include PG_FUNCTION_INFO_V1(unac); Datum unac(PG_FUNCTION_ARGS) { text *str = PG_GETARG_TEXT_P(0); text *result; int tlen, nlen; char *tstr, *nstr; tlen = VARSIZE(str) - VARHDRSZ; tstr = (char *) palloc(tlen + 1); memcpy(tstr, VARDATA(str), tlen); tstr[tlen] = '\0'; nstr = NULL; nlen = 0; unac_string("UTF-8", tstr, strlen(tstr), &nstr, &nlen); /* It may happen that unac_string returns NULL, because iconv * can't translate the input string. In this case we output * the string as it is. */ if (nstr == NULL) nstr = tstr; result = (text *) palloc(strlen(nstr) + VARHDRSZ); memcpy(VARDATA(result), nstr, strlen(nstr)); VARATT_SIZEP(result) = strlen(nstr) + VARHDRSZ; PG_RETURN_TEXT_P(result); }