/*********************************************** spell.c Started by Jeff Ondich on 5/8/97 Last modified 5/9/97 Simple spell-checker to put our Dictionary type through its paces. ***********************************************/ #include #include #include #include "dictionary.h" int GetWord( FILE *f, char *word, int wordLimit ); void main( int argc, char **argv ) { FILE *dictionaryFile, *textFile; Dictionary theDictionary; char theWord[100]; /* Check for proper command-line arguments and open the files. */ if( argc < 3 ) { fprintf( stderr, "Usage: %s dictionaryfile textfile\n", argv[0] ); return; } dictionaryFile = fopen( argv[1], "r" ); if( dictionaryFile == NULL ) { fprintf( stderr, "Can't open %s for reading.\n", argv[1] ); return; } textFile = fopen( argv[2], "r" ); if( textFile == NULL ) { fprintf( stderr, "Can't open %s for reading.\n", argv[2] ); return; } /* Load the dictionary. */ if( !InitializeDictionary( &theDictionary ) ) { fprintf( stderr, "Can't initialize dictionary\n" ); return; } while( GetWord( dictionaryFile, theWord, 100 ) != EOF ) { if( !AddToDictionary( &theDictionary, theWord ) ) { fprintf( stderr, "Dictionary filled up at \"%s\"", theWord ); break; } } fclose( dictionaryFile ); /* Check the text file for spelling errors. */ while( GetWord( textFile, theWord, 100 ) != EOF ) { if( !IsInDictionary( &theDictionary, theWord ) ) printf( "%s\n", theWord ); } } /*************************************************** GetWord assumes f is a valid FILE pointer for a file that is open for reading, and that theWord is a pointer to an array of at least wordLimit characters. GetWord reads the next word from the file, and stores it as a null-terminated string in theWord. "The next word" means the next block of non-space characters. Before storing this block of characters in theWord, GetWord removes all non-letters, and makes all the letters lower case. GetWord returns EOF if it encounters EOF before it reads any letters. Otherwise, GetWord returns !EOF. ***************************************************/ int GetWord( FILE *f, char *theWord, int wordLimit ) { char c; int i = 0; /* Eat garbage until a letter appears. */ c = fgetc( f ); while( c != EOF && !isalpha(c) ) c = fgetc( f ); /* Consume letters, digits, and punctuation until a space or EOF appears. Put only the letters, made lower-case, into word[]. */ while( c != EOF && !isspace(c) ) { if( isalpha(c) && i < wordLimit-1 ) theWord[i++] = tolower(c); c = fgetc( f ); } theWord[i] = '\0'; /* Return EOF if EOF appeared before we found any letters, otherwise don't. */ if( c == EOF && i == 0 ) return( EOF ); else return( !EOF ); }