store.c (832B)
1 /* store.c - guardado en data_dir()/calc.sav, con escritura atomica (tmp + rename). */ 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include "paths.h" 5 #include "store.h" 6 7 static char buf[32000]; 8 9 void store_load(Calc *c) 10 { 11 char path[512]; 12 snprintf(path, sizeof path, "%s/calc.sav", data_dir()); 13 FILE *f = fopen(path, "rb"); 14 if (!f) return; 15 int n = (int)fread(buf, 1, sizeof buf - 1, f); 16 fclose(f); 17 buf[n] = 0; 18 persist_load(c, buf, n); 19 } 20 21 void store_save(Calc *c) 22 { 23 char path[512], tmp[520]; 24 snprintf(path, sizeof path, "%s/calc.sav", data_dir()); 25 snprintf(tmp, sizeof tmp, "%s.tmp", path); 26 int n = persist_save(c, buf, sizeof buf); 27 FILE *f = fopen(tmp, "wb"); 28 if (!f) return; 29 fwrite(buf, 1, (size_t)n, f); 30 if (fclose(f) == 0) rename(tmp, path); 31 c->dirty_save = false; 32 }