Missing dependencies added.
[AROS-Contrib.git] / rexx / lstring / soundex.c
blob062a39a42ef2f1c8236803731eb8d46050b25f5c
1 /*
2 * $Header$
3 * $Log$
4 * Revision 1.1 2001/04/04 05:43:38 wang
5 * First commit: compiles on Linux, Amiga, Windows, Windows CE, generic gcc
7 * Revision 1.2 1999/11/26 12:52:25 bnv
8 * Nothing important.
10 * Revision 1.1 1998/07/02 17:18:00 bnv
11 * Initial Version
15 #include <ctype.h>
16 #include <string.h>
17 #include <lstring.h>
19 /* ------------------------* Soundex *--------------------------- */
20 #define MAX_LENGTH 20 /* max # of chars to check */
21 #define SOUNDEX_LENGTH 4 /* length of Soundex code */
23 #define ALPHA_OFFSET 65 /* ALPHA_OFFSET is decimal */
24 /* value of 'A' in ASCII character set */
25 void
26 Lsoundex( const PLstr to, const PLstr str )
28 /* ctable contains the Soundex value of each
29 letter in alphabetical order. 0 represents
30 letters which are to be ignored. */
31 char *code, *name;
32 static char ctable[] = { "01230120022455012623010202" };
33 char prior=' ', c;
34 short i, y=0;
35 word len;
38 /* ---- initialise ---- */
39 L2STR(str);
40 LZEROSTR(*to);
41 Lfx(to,SOUNDEX_LENGTH);
43 len = LLEN(*str);
44 code = LSTR(*to);
45 name = LSTR(*str);
46 if (len==0) return;
48 /* convert name to all upper case */
49 if (len > MAX_LENGTH) len = MAX_LENGTH;
51 /* generate 1st character of Soundex code */
52 code[0] = l2u[(byte)name[0]];
53 y=1;
56 if (l2u[(byte)name[0]]=='K') /* modifications */
57 code[0] = 'C';
58 else
59 if (l2u[(byte)name[0]]=='P' && l2u[(byte)name[1]]=='H')
60 code[0] = 'F';
62 /* loop through the rest of name, until code complete */
63 for (i=1; i<len; i++) {
64 c = l2u[(byte)name[i]];
65 /* skip non alpha */
66 if (!ISALPHA(c))
67 continue;
69 /* skip succesive occurance */
70 if (c==prior) continue;
71 prior = c;
73 /* lookup letter in table */
74 c -= ALPHA_OFFSET;
75 if (ctable[(byte)c]=='0')
76 continue; /* ignore this letter */
78 code[y++] = ctable[(byte)c]; /* add to code */
80 if (y >= SOUNDEX_LENGTH) break; /* code is complete */
82 while (y < SOUNDEX_LENGTH) code[y++] = '0'; /* pad code with zeros */
83 code[y] = '\0';
84 LLEN(*to) = y;
85 } /* Lsoundex */