Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Loads dictionary into memory. Returns true if successful else false.
- */
- bool load(const char* dictionary)
- {
- FILE* dict = fopen(dictionary, "r");
- if (dict == NULL)
- {
- printf("Dictionary could not be opened.\n");
- return false;
- }
- root = calloc(1, sizeof(node));
- node* ptr = root;
- for (int c = fgetc(dict); c != EOF; c = fgetc(dict))
- {
- if (isalpha(c))
- {
- if (ptr->children[c - 'a'] == NULL)
- {
- ptr->children[c - 'a'] = calloc(1, sizeof(node));
- ptr = ptr->children[c - 'a'];
- }
- else
- {
- ptr = ptr->children[c - 'a'];
- }
- }
- else if (c == '\'')
- {
- if (ptr->children[26] == NULL)
- {
- ptr->children[26] = calloc(1, sizeof(node));
- ptr = ptr->children[26];
- }
- else
- {
- ptr = ptr->children[26];
- }
- }
- // must be new-line
- else
- {
- ptr->is_word = true;
- ptr = root;
- words_loaded++;
- }
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment