/* * call-seq: * save(filename_base) -> true * * Saves the trie data to two files, filename_base.da and filename_base.tail. * Returns true if saving was successful. */ static VALUE rb_trie_save(VALUE self, VALUE filename_base) { VALUE da_filename = rb_str_dup(filename_base); rb_str_concat(da_filename, rb_str_new2(".da")); StringValue(da_filename); VALUE tail_filename = rb_str_dup(filename_base); rb_str_concat(tail_filename, rb_str_new2(".tail")); StringValue(tail_filename); Trie *trie; Data_Get_Struct(self, Trie, trie); FILE *da_file = fopen(RSTRING_PTR(da_filename), "w"); if (da_file == NULL) raise_ioerror("Error opening .da file for writing."); if (da_write(trie->da, da_file) != 0) raise_ioerror("Error writing DArray data."); fclose(da_file); FILE *tail_file = fopen(RSTRING_PTR(tail_filename), "w"); if (tail_file == NULL) raise_ioerror("Error opening .tail file for writing."); if (tail_write(trie->tail, tail_file) != 0) raise_ioerror("Error writing Tail data."); fclose(tail_file); return Qtrue; }
/** * @brief Save a trie to file * * @param trie : the trie * * @param path : the path to the file * * @return 0 on success, non-zero on failure * * Create a new file at the given @a path and write @a trie data to it. * If @a path already exists, its contents will be replaced. */ int trie_save (Trie *trie, const char *path) { FILE *file; int res = 0; file = fopen (path, "w+"); if (!file) return -1; if (alpha_map_write_bin (trie->alpha_map, file) != 0) { res = -1; goto exit_file_openned; } if (da_write (trie->da, file) != 0) { res = -1; goto exit_file_openned; } if (tail_write (trie->tail, file) != 0) { res = -1; goto exit_file_openned; } trie->is_dirty = FALSE; return 0; exit_file_openned: fclose (file); return res; }