comparison src/cache.c @ 65:322bb41c9b9e

Mon Aug 15 18:27:38 2005 John Ellis <johne@verizon.net> * cache.c: Make cache loader tolerant of unknown line values, so that a cache written by newer/older versions of GQview does not result in recreating data that is actually there.
author gqview
date Mon, 15 Aug 2005 22:32:57 +0000
parents 64068b1bab89
children f6e307c7bad6
comparison
equal deleted inserted replaced
64:04ff0df3ad2f 65:322bb41c9b9e
180 *------------------------------------------------------------------- 180 *-------------------------------------------------------------------
181 * sim cache read 181 * sim cache read
182 *------------------------------------------------------------------- 182 *-------------------------------------------------------------------
183 */ 183 */
184 184
185 static gint cache_sim_read_skipline(FILE *f, int s)
186 {
187 if (!f) return FALSE;
188
189 if (fseek(f, 0 - s, SEEK_CUR) == 0)
190 {
191 char b;
192 while (fread(&b, sizeof(b), 1, f) == 1)
193 {
194 if (b == '\n') return TRUE;
195 }
196 return TRUE;
197 }
198
199 return FALSE;
200 }
201
185 static gint cache_sim_read_comment(FILE *f, char *buf, int s, CacheData *cd) 202 static gint cache_sim_read_comment(FILE *f, char *buf, int s, CacheData *cd)
186 { 203 {
187 if (!f || !buf || !cd) return FALSE; 204 if (!f || !buf || !cd) return FALSE;
188 205
189 if (buf[0] != '#') return FALSE; 206 if (s < 1 || buf[0] != '#') return FALSE;
190 207
191 if (fseek(f, 0 - (s - 1), SEEK_CUR) == 0) 208 return cache_sim_read_skipline(f, s - 1);
192 {
193 char b;
194 while(fread(&b, sizeof(b), 1, f) == 1)
195 {
196 if (b == '\n') return TRUE;
197 }
198 return TRUE;
199 }
200
201 return FALSE;
202 } 209 }
203 210
204 static gint cache_sim_read_dimensions(FILE *f, char *buf, int s, CacheData *cd) 211 static gint cache_sim_read_dimensions(FILE *f, char *buf, int s, CacheData *cd)
205 { 212 {
206 if (!f || !buf || !cd) return FALSE; 213 if (!f || !buf || !cd) return FALSE;
423 } 430 }
424 431
425 return FALSE; 432 return FALSE;
426 } 433 }
427 434
435 #define CACHE_LOAD_LINE_NOISE 8
436
428 CacheData *cache_sim_data_load(const gchar *path) 437 CacheData *cache_sim_data_load(const gchar *path)
429 { 438 {
430 FILE *f; 439 FILE *f;
431 CacheData *cd = NULL; 440 CacheData *cd = NULL;
432 char buf[32]; 441 char buf[32];
433 gint success = TRUE; 442 gint success = CACHE_LOAD_LINE_NOISE;
434 gchar *pathl; 443 gchar *pathl;
435 444
436 if (!path) return NULL; 445 if (!path) return NULL;
437 446
438 pathl = path_from_utf8(path); 447 pathl = path_from_utf8(path);
446 455
447 if (fread(&buf, sizeof(char), 9, f) != 9 || 456 if (fread(&buf, sizeof(char), 9, f) != 9 ||
448 strncmp(buf, "SIMcache", 8) != 0) 457 strncmp(buf, "SIMcache", 8) != 0)
449 { 458 {
450 if (debug) printf("%s is not a cache file\n", cd->path); 459 if (debug) printf("%s is not a cache file\n", cd->path);
451 success = FALSE; 460 success = 0;
452 } 461 }
453 462
454 while (success) 463 while (success > 0)
455 { 464 {
456 int s; 465 int s;
457 s = fread(&buf, sizeof(char), sizeof(buf), f); 466 s = fread(&buf, sizeof(char), sizeof(buf), f);
458 467
459 if (s < 1) 468 if (s < 1)
460 { 469 {
461 success = FALSE; 470 success = 0;
462 } 471 }
463 else 472 else
464 { 473 {
465 if (!cache_sim_read_comment(f, buf, s, cd) && 474 if (!cache_sim_read_comment(f, buf, s, cd) &&
466 !cache_sim_read_dimensions(f, buf, s, cd) && 475 !cache_sim_read_dimensions(f, buf, s, cd) &&
467 !cache_sim_read_date(f, buf, s, cd) && 476 !cache_sim_read_date(f, buf, s, cd) &&
468 !cache_sim_read_checksum(f, buf, s, cd) && 477 !cache_sim_read_checksum(f, buf, s, cd) &&
469 !cache_sim_read_md5sum(f, buf, s, cd) && 478 !cache_sim_read_md5sum(f, buf, s, cd) &&
470 !cache_sim_read_similarity(f, buf, s, cd)) 479 !cache_sim_read_similarity(f, buf, s, cd))
471 { 480 {
472 success = FALSE; 481 if (!cache_sim_read_skipline(f, s))
482 {
483 success = 0;
484 }
485 else
486 {
487 success--;
488 }
489 }
490 else
491 {
492 success = CACHE_LOAD_LINE_NOISE;
473 } 493 }
474 } 494 }
475 } 495 }
476 496
477 fclose(f); 497 fclose(f);