CacheLoader *cache_loader_new(FileData *fd, CacheDataType load_mask, CacheLoaderDoneFunc done_func, gpointer done_data) { CacheLoader *cl; gchar *found; if (!fd || !isfile(fd->path)) return NULL; cl = g_new0(CacheLoader, 1); cl->fd = file_data_ref(fd); cl->done_func = done_func; cl->done_data = done_data; found = cache_find_location(CACHE_TYPE_SIM, cl->fd->path); if (found && filetime(found) == filetime(cl->fd->path)) { cl->cd = cache_sim_data_load(found); } g_free(found); if (!cl->cd) cl->cd = cache_sim_data_new(); cl->todo_mask = load_mask; cl->done_mask = CACHE_LOADER_NONE; cl->il = NULL; cl->idle_id = g_idle_add(cache_loader_idle_cb, cl); cl->error = FALSE; return cl; }
CacheData *cache_sim_data_load(const gchar *path) { FILE *f; CacheData *cd = NULL; gchar buf[32]; gint success = CACHE_LOAD_LINE_NOISE; gchar *pathl; if (!path) return NULL; pathl = path_from_utf8(path); f = fopen(pathl, "r"); g_free(pathl); if (!f) return NULL; cd = cache_sim_data_new(); cd->path = g_strdup(path); if (fread(&buf, sizeof(gchar), 9, f) != 9 || strncmp(buf, "SIMcache", 8) != 0) { DEBUG_1("%s is not a cache file", cd->path); success = 0; } while (success > 0) { gint s; s = fread(&buf, sizeof(gchar), sizeof(buf), f); if (s < 1) { success = 0; } else { if (!cache_sim_read_comment(f, buf, s, cd) && !cache_sim_read_dimensions(f, buf, s, cd) && !cache_sim_read_date(f, buf, s, cd) && !cache_sim_read_checksum(f, buf, s, cd) && !cache_sim_read_md5sum(f, buf, s, cd) && !cache_sim_read_similarity(f, buf, s, cd)) { if (!cache_sim_read_skipline(f, s)) { success = 0; } else { success--; } } else { success = CACHE_LOAD_LINE_NOISE; } } } fclose(f); if (!cd->dimensions && !cd->have_date && !cd->have_checksum && !cd->have_md5sum && !cd->similarity) { cache_sim_data_free(cd); cd = NULL; } return cd; }