/* * Copyright (c) 2001 by Cliff Green. All rights reserved. Individual files * may be covered by other copyrights (as noted in the file itself). * * Redistribution and use in source and binary forms are permitted * provided that this entire copyright notice is duplicated in all such * copies. * * This software is provided "as is" and without any expressed or implied * warranties, including, without limitation, the implied warranties of * merchantibility and fitness for any particular purpose. */ /* * Copyright note - Bob Jarvis wrote the original version and my standard * copyright notice above only pertains to my modified version of his * function. */ /* //---------------------------------------------------------------------- // Source file: soundex.c // Written by: Bob Jarvis // Compiler: Metrowerks CodeWarrior Pro 6, g++ 3.0.4 // History: // Modified: 2/17/2002 // By: Cliff Green // Comments: Fixed memory leak. //---------------------------------------------------------------------- */ #include #include #include #include "soundex.h" char *soundex(char const * inputstr, char * outputstr) { /* ABCDEFGHIJKLMNOPQRSTUVWXYZ */ char *table = "01230120022455012623010202"; char *outptr = outputstr; int count = 0; char *instr; char *newbuf; if (inputstr == 0 || outputstr == 0) { return 0; } if ((newbuf = (char *) malloc(strlen(inputstr) + 1)) == 0) { return 0; } strcpy (newbuf, inputstr); instr = newbuf; while(!isalpha(instr[0]) && instr[0]) ++instr; if(!instr[0]) /* Hey! Where'd the string go? */ { free(newbuf); return(0); } if(instr[1] != 0 && toupper(instr[0]) == 'P' && toupper(instr[1]) == 'H') { instr[0] = 'F'; instr[1] = 'A'; } *outptr++ = (char)toupper(*instr++); while(*instr && count < 5) { if(isalpha(*instr) && *instr != *(instr-1)) { *outptr = table[toupper(instr[0]) - 'A']; if(*outptr != '0') { ++outptr; ++count; } } ++instr; } *outptr = '\0'; free(newbuf); return(outputstr); } #ifdef TEST #include #include main(int argc, char *argv[]) { char code[6]; if (argc != 2) { puts("Usage: SOUNDEX string"); return EXIT_FAILURE; } printf("soundex(\"%s\") returned %s\n", argv[1], soundex(argv[1], code)); return EXIT_SUCCESS; } #endif /* TEST */