/* * 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 Create a new trie by loading from a file * * @param path : the path to the file * * @return a pointer to the created trie, NULL on failure * * Create a new trie and initialize its contents by loading from the file at * given @a path. * * The created object must be freed with trie_free(). */ Trie * trie_new_from_file (const char *path) { Trie *trie; FILE *trie_file; trie_file = fopen (path, "r"); if (!trie_file) return NULL; trie = (Trie *) malloc (sizeof (Trie)); if (!trie) goto exit_file_openned; if (NULL == (trie->alpha_map = alpha_map_read_bin (trie_file))) goto exit_trie_created; if (NULL == (trie->da = da_read (trie_file))) goto exit_alpha_map_created; if (NULL == (trie->tail = tail_read (trie_file))) goto exit_da_created; fclose (trie_file); trie->is_dirty = FALSE; return trie; exit_da_created: da_free (trie->da); exit_alpha_map_created: alpha_map_free (trie->alpha_map); exit_trie_created: free (trie); exit_file_openned: fclose (trie_file); return NULL; }
// pass data from PNG file in memory to libpng static void io_read(png_struct* png_ptr, u8* data, png_size_t length) { DynArray* da = (DynArray*)png_get_io_ptr(png_ptr); if(da_read(da, data, length) != 0) png_error(png_ptr, "io_read failed"); }