Advertisement
Guest User

Untitled

a guest
Feb 27th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.54 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <errno.h>
  4.  
  5. /* local headers */
  6. #include "pa3.h"
  7. #include "pa3Strings.h"
  8.  
  9. /*
  10. * Function name: loadDict()
  11. * Function prototype: int loadDict(char *filename, struct HashTable *table);
  12. *
  13. * Description: C function to open and read dictionary file word by word into
  14. * memory.
  15. *
  16. * Parameters: char *filename - the dictionary file name
  17. * struct HashTable *table - hash table where each sorted
  18. * uppercase word is mapped
  19. *
  20. * Side effects: DO THIS
  21. * Return Value: 0 on success, -1 on failure
  22. */
  23.  
  24. int loadDict(char *filename, struct HashTable *table) {
  25. errno = 0;
  26.  
  27. FILE *file = fopen(filename, "r");
  28.  
  29. (void) fprintf(stdout, "We are here after file open\n");
  30.  
  31. // print error if file could not be opened
  32. if (file == NULL) {
  33. // check for permission error
  34. if (errno == EACCES) {
  35. fprintf(stderr, STR_ERR_FILE_PERMISSION);
  36. return -1;
  37. }
  38.  
  39. // check for file not existing error
  40. else if (errno == ENOENT) {
  41. fprintf(stderr, STR_ERR_FILE_INVALID);
  42. return -1;
  43. }
  44. }
  45.  
  46. /* process dictionary */
  47. char word[BUFSIZ];
  48. const char NULLCHAR = '\0';
  49. const char NEWLINE = '\n';
  50.  
  51. // read word from file line by line
  52. while(fgets(word, BUFSIZ, file) != NULL) {
  53. (void) fprintf(stdout, "The word is: %s\n", word);
  54.  
  55. // null terminate word by replacing newline char with null char
  56. /* FIX THIS LATER WITH strchr()????? */
  57. int i = 0;
  58. int done = 0;
  59. while(done == 0) {
  60. if (word[i] == NEWLINE) {
  61. word[i] = NULLCHAR;
  62. break;
  63. }
  64. i++;
  65. }
  66.  
  67. // malloc a temporary anagram struct
  68. struct Anagram *tempAnagram = malloc(sizeof(struct Anagram *));
  69.  
  70. // if initialization of anagram fails
  71. if (initAnagram(word, tempAnagram) == -1) {
  72. return -1;
  73. }
  74.  
  75. // get index for the word to put into hash table
  76. int wordTableIndex = getTableIndex(tempAnagram->sortedWord, table->size);
  77.  
  78. // go to TableEntry corresponding to table index and check whether or
  79. // not the Anagram array has been initialized
  80. if (table->entryPtr[wordTableIndex].anagramPtr != NULL) {
  81. size_t i;
  82.  
  83. // length of the anagram array
  84. // iterate through anagram array to check for match between sorted
  85. // word in the temp anagram struct and sortedWord from array
  86. size_t len = table->entryPtr[wordTableIndex].numAnagrams;
  87. for (i = 0; i < len; i++) {
  88. // if sortedWord of current word matches with the sortedWord of an Anagram
  89. if (strcmp(tempAnagram->sortedWord, table->entryPtr[wordTableIndex].anagramPtr[i].sortedWord) == 0) {
  90. size_t numWords = table->entryPtr[wordTableIndex].anagramPtr[i].numWords + 1;
  91.  
  92. // expand size of words array in the anagram by 1 and add current word
  93. char **words = realloc(table->entryPtr[wordTableIndex].anagramPtr[i].words, numWords * sizeof(char **));
  94. table->entryPtr[wordTableIndex].anagramPtr[i].words = words;
  95.  
  96. // add current word to new allocated space in words array
  97. table->entryPtr[wordTableIndex].anagramPtr[i].words[numWords - 1] = word;
  98.  
  99. // update num of words in anagram array
  100. table->entryPtr[wordTableIndex].anagramPtr[i].numWords = numWords;
  101. }
  102.  
  103. else {
  104. // expand size of anagrams array by 1 and add tempAnagram
  105. size_t numAnagrams = table->entryPtr[wordTableIndex].numAnagrams + 1;
  106.  
  107. // expanded anagram size
  108. struct Anagram *anagramPtr = realloc(table->entryPtr[wordTableIndex].anagramPtr, numAnagrams * sizeof(struct Anagram *));
  109. table->entryPtr[wordTableIndex].anagramPtr = anagramPtr;
  110.  
  111. table->entryPtr[wordTableIndex].anagramPtr[numAnagrams - 1] = *tempAnagram;
  112.  
  113. // update size of anagram array
  114. table->entryPtr[wordTableIndex].numAnagrams = numAnagrams;
  115. }
  116. }
  117. }
  118.  
  119. // if TableEntry has not yet initialized a struct Anagram array yet
  120. else {
  121. table->entryPtr[wordTableIndex].anagramPtr = malloc(sizeof(struct Anagram *));
  122.  
  123. // set tempAnagram as first entry in newly created Anagram array
  124. table->entryPtr[wordTableIndex].anagramPtr[0] = *tempAnagram;
  125.  
  126. table->entryPtr[wordTableIndex].numAnagrams = 1;
  127. }
  128. }
  129. return 0;
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement