示例#1
0
文件: trie.c 项目: kritik/trie
/*
 * 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;
}
示例#2
0
文件: trie.c 项目: witgo/fast_trie
/*
 * 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;
}