c-yen

dictionary.c

Jun 12th, 2015
246
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Returns true if word is in dictionary else false.
  3.  */
  4. bool check(const char* word)
  5. {
  6.     // changes string to lowercase
  7.     char wordCheck[LENGTH+1];
  8.     strcpy(wordCheck, word);
  9.  
  10.     for (int i = 0; i < strlen(wordCheck) +1; i++)
  11.     {
  12.         wordCheck[i] = tolower(wordCheck[i]);
  13.     }
  14.  
  15.     // runs word through hash function
  16.     int index = hash(wordCheck);
  17.  
  18.     // creates a cursor for searching and points it to start of linked list
  19.     node* cursor = table[index];
  20.  
  21.     // starts searching
  22.     if (table[index] == NULL)
  23.     {
  24.         return false;
  25.     }
  26.  
  27.     while (cursor != NULL)
  28.     {
  29.         if (strcmp(cursor->word, wordCheck) == 0)
  30.         {
  31.             return true;
  32.         }
  33.     cursor = cursor->next;
  34.     }
  35.  
  36.     return false;
  37. }
  38.  
  39. /**
  40.  * Loads dictionary into memory.  Returns true if successful else false.
  41.  */
  42. bool load(const char* dictionary)
  43. {      
  44.     // open dictionary
  45.     FILE* file = fopen(dictionary, "r");
  46.     if (file == NULL)
  47.     {
  48.         printf("Could not load %s\n", dictionary);
  49.     }
  50.    
  51.     // declares buffer to put string from dictionary in
  52.     char str[LENGTH+1];
  53.    
  54.     while(fscanf(file, "%s\n", str) != EOF)
  55.     {
  56.         // creates new node
  57.         node* newNode = malloc(sizeof(node));
  58.        
  59.         // inserts data into new node
  60.         newNode->word = str;
  61.         newNode->next = NULL;
  62.        
  63.         // runs word through hash functions
  64.         int index = hash(newNode->word);
  65.        
  66.         // inserts new node into table
  67.         if (table[index] == NULL)
  68.         {
  69.             table[index] = newNode;
  70.             count++;
  71.         }
  72.         else
  73.         {
  74.             newNode->next = table[index];
  75.             table[index] = newNode;
  76.             count++;
  77.         }
  78.     }
  79.    
  80.     fclose(file);
  81.     return true;
  82. }
  83.  
  84. /**
  85.  * Returns number of words in dictionary if loaded else 0 if not yet loaded.
  86.  */
  87. unsigned int size(void)
  88. {
  89.     unsigned int number = count;
  90.     return number;
  91. }
  92.  
  93. /**
  94.  * Unloads dictionary from memory.  Returns true if successful else false.
  95.  */
  96. bool unload(void)
  97. {
  98.     // loop through each linked list
  99.     for (int j = 0; j < TABLESIZE; j++)
  100.     {
  101.         // creates a cursor for traversing through the linked lists
  102.         node* cursor = table[j];
  103.        
  104.         // go through all the nodes in the list
  105.         while (cursor != NULL)
  106.         {
  107.             // creates a temp node pointer to cursor
  108.             node* temp = cursor;
  109.            
  110.             // move cursor to next node (so linked list is still held on to)
  111.             cursor = cursor->next;
  112.            
  113.             // free temp
  114.             free(temp);
  115.         }
  116.     }
  117.    
  118.     return true;
  119. }
RAW Paste Data