Add to git.
[dlife.git] / image.c
1 /* DLIFE Copyright (C) 2000 Richard W.M. Jones <rich@annexia.org>
2  * and other authors listed in the ``AUTHORS'' file.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17  *
18  * $Id: image.c,v 1.2 2002/04/05 16:47:12 rich Exp $
19  */
20
21 #include "config.h"
22
23 #include <stdio.h>
24 #include <stdlib.h>
25
26 #include "image.h"
27 #include "state.h"
28 #include "cell.h"
29 #include "soup.h"
30
31 #define FILE_SAVE_VERSION 1
32
33 static void global_state_save (struct state *state, FILE *fp);
34 static void global_state_load (struct state *state, FILE *fp);
35
36 struct state *
37 image_load (const char *filename, int _soup_size, int thread_num)
38 {
39   FILE *fp;
40   struct state *state;
41
42   fp = fopen (filename, "r");
43   if (fp == 0) { perror (filename); return 0; }
44
45   state = state_malloc (_soup_size, thread_num);
46
47   global_state_load (state, fp);
48   cell_state_load (state, fp);
49   soup_state_load (state, fp);
50
51   fclose (fp);
52
53   return state;
54 }
55
56 int
57 image_save (struct state *state, const char *filename)
58 {
59   FILE *fp;
60
61   fp = fopen (filename, "w");
62   if (fp == 0) { perror (filename); return -1; }
63
64   global_state_save (state, fp);
65   cell_state_save (state, fp);
66   soup_state_save (state, fp);
67
68   fclose (fp);
69
70   return 0;
71 }
72
73 static void
74 global_state_save (struct state *state, FILE *fp)
75 {
76   int version = FILE_SAVE_VERSION;
77
78   fwrite (&version, sizeof version, 1, fp);
79   fwrite (&state->cell_cycle, sizeof state->cell_cycle, 1, fp);
80 }
81
82 static void
83 global_state_load (struct state *state, FILE *fp)
84 {
85   int version;
86
87   if (fread (&version, sizeof version, 1, fp) != 1)
88     {
89       fprintf (stderr, "error in global state\n");
90       abort ();
91     }
92   if (version != FILE_SAVE_VERSION)
93     {
94       fprintf (stderr, "soup image version numbers do not match\n");
95       abort ();
96     }
97
98   if (fread (&state->cell_cycle, sizeof state->cell_cycle, 1, fp) != 1)
99     {
100       fprintf (stderr, "error in global state\n");
101       abort ();
102     }
103
104   if (verbose) printf ("Total number of cycles executed so far: %LuB\n",
105                        state->cell_cycle / 1000000000);
106 }