ehahehah

load.c

Jun 12th, 2016
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.20 KB | None | 0 0
  1. /**
  2.  * Loads dictionary into memory.  Returns true if successful else false.
  3.  */
  4. bool load(const char* dictionary)
  5. {
  6.     FILE* dict = fopen(dictionary, "r");
  7.     if (dict == NULL)
  8.     {
  9.         printf("Dictionary could not be opened.\n");
  10.         return false;
  11.     }
  12.    
  13.     root = calloc(1, sizeof(node));
  14.     node* ptr = root;
  15.     for (int c = fgetc(dict); c != EOF; c = fgetc(dict))
  16.     {  
  17.         if (isalpha(c))
  18.         {
  19.             if (ptr->children[c - 'a'] == NULL)
  20.             {
  21.                 ptr->children[c - 'a'] = calloc(1, sizeof(node));
  22.                 ptr = ptr->children[c - 'a'];
  23.             }
  24.            
  25.             else
  26.             {
  27.                 ptr = ptr->children[c - 'a'];
  28.             }
  29.         }
  30.  
  31.         else if (c == '\'')
  32.         {
  33.             if (ptr->children[26] == NULL)
  34.             {
  35.                 ptr->children[26] = calloc(1, sizeof(node));
  36.                 ptr = ptr->children[26];
  37.             }
  38.  
  39.             else
  40.             {
  41.                 ptr = ptr->children[26];
  42.             }
  43.         }
  44.        
  45.         // must be new-line
  46.         else
  47.         {
  48.             ptr->is_word = true;
  49.             ptr = root;
  50.             words_loaded++;
  51.         }
  52.     }
  53.     return true;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment