1 /* DLIFE Copyright (C) 2000 Richard W.M. Jones <rich@annexia.org>
2 * and other authors listed in the ``AUTHORS'' file.
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.
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.
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.
18 * $Id: load.c,v 1.2 2002/04/05 16:47:12 rich Exp $
34 load_cell (struct state *state, const char *filename)
43 struct soup_frag *frag;
45 fp = fopen (filename, "r");
46 if (fp == 0) { perror (filename); return 0; }
48 while (fgets (line, sizeof line, fp) != 0 && len < MAX_CELL_SIZE)
50 for (i = 0; i < strlen (line); ++i)
52 if (line[i] >= '0' && line[i] <= '9')
58 else if (line[i] >= 'a' && line[i] <= 'f')
61 n += line[i] - 'a' + 10;
64 else if (line[i] >= 'A' && line[i] <= 'F')
67 n += line[i] - 'A' + 10;
72 if (allocated - len <= 0)
74 code = realloc (code, allocated += 256);
75 if (code == 0) { perror ("realloc"); abort (); }
85 fprintf (stderr, "%s: error in input file\n", filename);
93 /* Refuse any cells which are too small or too large. */
94 if (len < MIN_CELL_SIZE || len > MAX_CELL_SIZE)
96 fprintf (stderr, "%s: cell is too small or too large (len = %d)\n",
102 /* Allocate soup fragment. */
103 frag = soup_frag_malloc (state, 0, len);
104 if (frag == 0) { free (code); return 0; }
107 cell = cell_malloc (state, 0, frag);
108 if (cell == 0) { free (code); return 0; }
111 for (i = 0; i < len; ++i)
112 set_soup (state, cell->frag, code[i], i);