/* DLIFE Copyright (C) 2000 Richard W.M. Jones * and other authors listed in the ``AUTHORS'' file. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * $Id: image.c,v 1.2 2002/04/05 16:47:12 rich Exp $ */ #include "config.h" #include #include #include "image.h" #include "state.h" #include "cell.h" #include "soup.h" #define FILE_SAVE_VERSION 1 static void global_state_save (struct state *state, FILE *fp); static void global_state_load (struct state *state, FILE *fp); struct state * image_load (const char *filename, int _soup_size, int thread_num) { FILE *fp; struct state *state; fp = fopen (filename, "r"); if (fp == 0) { perror (filename); return 0; } state = state_malloc (_soup_size, thread_num); global_state_load (state, fp); cell_state_load (state, fp); soup_state_load (state, fp); fclose (fp); return state; } int image_save (struct state *state, const char *filename) { FILE *fp; fp = fopen (filename, "w"); if (fp == 0) { perror (filename); return -1; } global_state_save (state, fp); cell_state_save (state, fp); soup_state_save (state, fp); fclose (fp); return 0; } static void global_state_save (struct state *state, FILE *fp) { int version = FILE_SAVE_VERSION; fwrite (&version, sizeof version, 1, fp); fwrite (&state->cell_cycle, sizeof state->cell_cycle, 1, fp); } static void global_state_load (struct state *state, FILE *fp) { int version; if (fread (&version, sizeof version, 1, fp) != 1) { fprintf (stderr, "error in global state\n"); abort (); } if (version != FILE_SAVE_VERSION) { fprintf (stderr, "soup image version numbers do not match\n"); abort (); } if (fread (&state->cell_cycle, sizeof state->cell_cycle, 1, fp) != 1) { fprintf (stderr, "error in global state\n"); abort (); } if (verbose) printf ("Total number of cycles executed so far: %LuB\n", state->cell_cycle / 1000000000); }