Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <string.h>
- #include <errno.h>
- /* local headers */
- #include "pa3.h"
- #include "pa3Strings.h"
- /*
- * Function name: loadDict()
- * Function prototype: int loadDict(char *filename, struct HashTable *table);
- *
- * Description: C function to open and read dictionary file word by word into
- * memory.
- *
- * Parameters: char *filename - the dictionary file name
- * struct HashTable *table - hash table where each sorted
- * uppercase word is mapped
- *
- * Side effects: DO THIS
- * Return Value: 0 on success, -1 on failure
- */
- int loadDict(char *filename, struct HashTable *table) {
- errno = 0;
- FILE *file = fopen(filename, "r");
- (void) fprintf(stdout, "We are here after file open\n");
- // print error if file could not be opened
- if (file == NULL) {
- // check for permission error
- if (errno == EACCES) {
- fprintf(stderr, STR_ERR_FILE_PERMISSION);
- return -1;
- }
- // check for file not existing error
- else if (errno == ENOENT) {
- fprintf(stderr, STR_ERR_FILE_INVALID);
- return -1;
- }
- }
- /* process dictionary */
- char word[BUFSIZ];
- const char NULLCHAR = '\0';
- const char NEWLINE = '\n';
- // read word from file line by line
- while(fgets(word, BUFSIZ, file) != NULL) {
- (void) fprintf(stdout, "The word is: %s\n", word);
- // null terminate word by replacing newline char with null char
- /* FIX THIS LATER WITH strchr()????? */
- int i = 0;
- int done = 0;
- while(done == 0) {
- if (word[i] == NEWLINE) {
- word[i] = NULLCHAR;
- break;
- }
- i++;
- }
- // malloc a temporary anagram struct
- struct Anagram *tempAnagram = malloc(sizeof(struct Anagram *));
- // if initialization of anagram fails
- if (initAnagram(word, tempAnagram) == -1) {
- return -1;
- }
- // get index for the word to put into hash table
- int wordTableIndex = getTableIndex(tempAnagram->sortedWord, table->size);
- // go to TableEntry corresponding to table index and check whether or
- // not the Anagram array has been initialized
- if (table->entryPtr[wordTableIndex].anagramPtr != NULL) {
- size_t i;
- // length of the anagram array
- // iterate through anagram array to check for match between sorted
- // word in the temp anagram struct and sortedWord from array
- size_t len = table->entryPtr[wordTableIndex].numAnagrams;
- for (i = 0; i < len; i++) {
- // if sortedWord of current word matches with the sortedWord of an Anagram
- if (strcmp(tempAnagram->sortedWord, table->entryPtr[wordTableIndex].anagramPtr[i].sortedWord) == 0) {
- size_t numWords = table->entryPtr[wordTableIndex].anagramPtr[i].numWords + 1;
- // expand size of words array in the anagram by 1 and add current word
- char **words = realloc(table->entryPtr[wordTableIndex].anagramPtr[i].words, numWords * sizeof(char **));
- table->entryPtr[wordTableIndex].anagramPtr[i].words = words;
- // add current word to new allocated space in words array
- table->entryPtr[wordTableIndex].anagramPtr[i].words[numWords - 1] = word;
- // update num of words in anagram array
- table->entryPtr[wordTableIndex].anagramPtr[i].numWords = numWords;
- }
- else {
- // expand size of anagrams array by 1 and add tempAnagram
- size_t numAnagrams = table->entryPtr[wordTableIndex].numAnagrams + 1;
- // expanded anagram size
- struct Anagram *anagramPtr = realloc(table->entryPtr[wordTableIndex].anagramPtr, numAnagrams * sizeof(struct Anagram *));
- table->entryPtr[wordTableIndex].anagramPtr = anagramPtr;
- table->entryPtr[wordTableIndex].anagramPtr[numAnagrams - 1] = *tempAnagram;
- // update size of anagram array
- table->entryPtr[wordTableIndex].numAnagrams = numAnagrams;
- }
- }
- }
- // if TableEntry has not yet initialized a struct Anagram array yet
- else {
- table->entryPtr[wordTableIndex].anagramPtr = malloc(sizeof(struct Anagram *));
- // set tempAnagram as first entry in newly created Anagram array
- table->entryPtr[wordTableIndex].anagramPtr[0] = *tempAnagram;
- table->entryPtr[wordTableIndex].numAnagrams = 1;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement