altos/lisp: Append a CRC to the saved image to validate on restore
[fw/altos] / src / test / ao_lisp_test.c
index 6973910016ec0fc7072f65c54d7a0edcec739696..648d1abe061a021ef5cc4964c934d2bf08e923b1 100644 (file)
 static FILE *ao_lisp_file;
 static int newline = 1;
 
+static char save_file[] = "lisp.image";
+
+int
+ao_lisp_os_save(void)
+{
+       FILE    *save = fopen(save_file, "w");
+
+       if (!save) {
+               perror(save_file);
+               return 0;
+       }
+       fwrite(ao_lisp_pool, 1, AO_LISP_POOL_TOTAL, save);
+       fclose(save);
+       return 1;
+}
+
+int
+ao_lisp_os_restore_save(struct ao_lisp_os_save *save, int offset)
+{
+       FILE    *restore = fopen(save_file, "r");
+       size_t  ret;
+
+       if (!restore) {
+               perror(save_file);
+               return 0;
+       }
+       fseek(restore, offset, SEEK_SET);
+       ret = fread(save, sizeof (struct ao_lisp_os_save), 1, restore);
+       fclose(restore);
+       if (ret != 1)
+               return 0;
+       return 1;
+}
+
+int
+ao_lisp_os_restore(void)
+{
+       FILE    *restore = fopen(save_file, "r");
+       size_t  ret;
+
+       if (!restore) {
+               perror(save_file);
+               return 0;
+       }
+       ret = fread(ao_lisp_pool, 1, AO_LISP_POOL_TOTAL, restore);
+       fclose(restore);
+       if (ret != AO_LISP_POOL_TOTAL)
+               return 0;
+       return 1;
+}
+
 int
 ao_lisp_getc(void)
 {