/*So obviously the FILEPATH macro here would need to be changed for implementation, but here is a program I wrote this week that works with
a dictionary to test if a user's input is in the given dictionary or
not
*/
//Help Users Spell Correctly
//Hailey Hanson
//February 9th, 2015
//Systems Programming CS 360
#include<stdio.h>
#include<ctype.h>
#include<string.h>
#include<stdlib.h>
#include<fcntl.h>
#include<errno.h>
#include <unistd.h>
#define RECORD_SIZE 16
#define FILEPATH "/home/hailey/Documents/Code/WSU-V/SystemsProg/SystemsProg/lib/webster"
char*word;
int fd;
char*buffer;
char* plainWord(char*original);
int ok(int fd, char*word);
int main(int argc, char**argv){
if(argc!=2){
fprintf(stderr, "single word argument required\n");
exit(1);
}
word = argv[1];
fd = open(FILEPATH, O_RDONLY, 0);
if (fd<0){
fprintf(stderr, "couldn't open file %s : %s\n", FILEPATH, strerror(errno));
exit(2);
}
if (ok(fd, word)) printf("yes, %s was found\n",word );
else printf("no, %s was not found\n",word);
close(fd);
char* plainWord(char*original){
int i=0;
char*dest;
dest = calloc(strlen(original), sizeof(char));
while(i<RECORD_SIZE){
if(isalpha(original[i])){
dest[i] = original[i];
}
i++;
}
dest[RECORD_SIZE] = '\0'; //this has to do with the specific dictionary I was using
return strdup(dest);//must compile with gnu99 for strdup to work correctly
}
int ok (int fd, char*word){
char* want = word;//from user
char*have; //from webster
int bot = 0;
int fileSize = lseek(fd, 0, SEEK_END);
int top = fileSize/RECORD_SIZE;
buffer = calloc(RECORD_SIZE, sizeof(char));
while(bot<top){
int mid = (bot+top)/2;
lseek(fd, mid*RECORD_SIZE, SEEK_SET);//moves me to middle of file
read(fd, buffer, RECORD_SIZE);//fills buffer with one line
want = plainWord(word);
have = plainWord(buffer);
int compare = strcmp(want, have);
if(compare == 0){
return 1;
}
else if (compare<0){
top = mid;
}
else if (compare>0){
bot = mid+1;
}
}
return 0;
}