1996-08-19 02:14:33 +04:00
|
|
|
/*****************************************************************************/
|
|
|
|
/* soundex.c */
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
1997-09-07 09:04:48 +04:00
|
|
|
#include "postgres.h" /* for char16, etc. */
|
|
|
|
#include "utils/palloc.h" /* for palloc */
|
|
|
|
#include "libpq-fe.h" /* for TUPLE */
|
1996-08-19 02:14:33 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
/* prototype for soundex function */
|
1997-09-08 06:41:22 +04:00
|
|
|
char *soundex(char *instr, char *outstr);
|
1996-08-19 02:14:33 +04:00
|
|
|
|
1998-02-26 07:46:47 +03:00
|
|
|
text *
|
1997-09-09 01:56:23 +04:00
|
|
|
text_soundex(text *t)
|
1996-08-19 02:14:33 +04:00
|
|
|
{
|
1997-09-07 09:04:48 +04:00
|
|
|
/* ABCDEFGHIJKLMNOPQRSTUVWXYZ */
|
1997-09-08 06:41:22 +04:00
|
|
|
char *table = "01230120022455012623010202";
|
|
|
|
int count = 0;
|
|
|
|
text *new_t;
|
1997-09-07 09:04:48 +04:00
|
|
|
|
1997-09-08 06:41:22 +04:00
|
|
|
char outstr[6 + 1]; /* max length of soundex is 6 */
|
|
|
|
char *instr;
|
1997-09-07 09:04:48 +04:00
|
|
|
|
|
|
|
/* make a null-terminated string */
|
|
|
|
instr = palloc(VARSIZE(t) + 1);
|
|
|
|
memcpy(instr, VARDATA(t), VARSIZE(t) - VARHDRSZ);
|
|
|
|
instr[VARSIZE(t) - VARHDRSZ] = (char) 0;
|
|
|
|
|
|
|
|
/* load soundex into outstr */
|
|
|
|
soundex(instr, outstr);
|
|
|
|
|
|
|
|
/* Now the outstr contains the soundex of instr */
|
|
|
|
/* copy outstr to new_t */
|
|
|
|
new_t = (text *) palloc(strlen(outstr) + VARHDRSZ);
|
|
|
|
memset(new_t, 0, strlen(outstr) + 1);
|
|
|
|
VARSIZE(new_t) = strlen(outstr) + VARHDRSZ;
|
|
|
|
memcpy((void *) VARDATA(new_t),
|
|
|
|
(void *) outstr,
|
|
|
|
strlen(outstr));
|
|
|
|
|
|
|
|
/* free instr */
|
|
|
|
pfree(instr);
|
|
|
|
|
|
|
|
return (new_t);
|
1996-08-19 02:14:33 +04:00
|
|
|
}
|
|
|
|
|
1998-02-26 07:46:47 +03:00
|
|
|
char *
|
1997-09-07 09:04:48 +04:00
|
|
|
soundex(char *instr, char *outstr)
|
|
|
|
{ /* ABCDEFGHIJKLMNOPQRSTUVWXYZ */
|
1997-09-08 06:41:22 +04:00
|
|
|
char *table = "01230120022455012623010202";
|
|
|
|
int count = 0;
|
1997-09-07 09:04:48 +04:00
|
|
|
|
|
|
|
while (!isalpha(instr[0]) && instr[0])
|
|
|
|
++instr;
|
|
|
|
|
|
|
|
if (!instr[0])
|
|
|
|
{ /* Hey! Where'd the string go? */
|
|
|
|
outstr[0] = (char) 0;
|
|
|
|
return outstr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (toupper(instr[0]) == 'P' && toupper(instr[1]) == 'H')
|
|
|
|
{
|
|
|
|
instr[0] = 'F';
|
|
|
|
instr[1] = 'A';
|
|
|
|
}
|
|
|
|
|
|
|
|
*outstr++ = (char) toupper(*instr++);
|
|
|
|
|
|
|
|
while (*instr && count < 5)
|
|
|
|
{
|
|
|
|
if (isalpha(*instr) && *instr != *(instr - 1))
|
|
|
|
{
|
|
|
|
*outstr = table[toupper(instr[0]) - 'A'];
|
|
|
|
if (*outstr != '0')
|
|
|
|
{
|
|
|
|
++outstr;
|
|
|
|
++count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
++instr;
|
|
|
|
}
|
|
|
|
|
|
|
|
*outstr = '\0';
|
|
|
|
return (outstr);
|
1996-08-19 02:14:33 +04:00
|
|
|
}
|