/* * call-seq: * read(filename_base) -> Trie * * Returns a new trie with data as read from disk. */ static VALUE rb_trie_read(VALUE self, VALUE filename_base) { VALUE da_filename = rb_str_dup(filename_base); VALUE tail_filename = rb_str_dup(filename_base); rb_str_concat(da_filename, rb_str_new2(".da")); rb_str_concat(tail_filename, rb_str_new2(".tail")); StringValue(tail_filename); StringValue(da_filename); Trie *trie = trie_new(); VALUE obj; obj = Data_Wrap_Struct(self, 0, trie_free, trie); DArray *old_da = trie->da; Tail *old_tail = trie->tail; FILE *da_file = fopen(RSTRING_PTR(da_filename), "r"); if (da_file == NULL) raise_ioerror("Error reading .da file."); trie->da = da_read(da_file); fclose(da_file); FILE *tail_file = fopen(RSTRING_PTR(tail_filename), "r"); if (tail_file == NULL) raise_ioerror("Error reading .tail file."); trie->tail = tail_read(tail_file); fclose(tail_file); da_free(old_da); tail_free(old_tail); return obj; }
/** * @brief Free a trie object * * @param trie : the trie object to free * * Destruct the @a trie and free its allocated memory. */ void trie_free (Trie *trie) { alpha_map_free (trie->alpha_map); da_free (trie->da); tail_free (trie->tail); free (trie); }