/* 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: load.c,v 1.2 2002/04/05 16:47:12 rich Exp $ */ #include "config.h" #include #include #include #include "types.h" #include "load.h" #include "cell.h" #include "soup.h" #include "state.h" struct cell * load_cell (struct state *state, const char *filename) { FILE *fp; int allocated = 0; int len = 0; char line[256]; byte_t *code = 0; int n = 0, i, c = 0; struct cell *cell; struct soup_frag *frag; fp = fopen (filename, "r"); if (fp == 0) { perror (filename); return 0; } while (fgets (line, sizeof line, fp) != 0 && len < MAX_CELL_SIZE) { for (i = 0; i < strlen (line); ++i) { if (line[i] >= '0' && line[i] <= '9') { n *= 16; n += line[i] - '0'; c++; } else if (line[i] >= 'a' && line[i] <= 'f') { n *= 16; n += line[i] - 'a' + 10; c++; } else if (line[i] >= 'A' && line[i] <= 'F') { n *= 16; n += line[i] - 'A' + 10; c++; } if (c == 2) { if (allocated - len <= 0) { code = realloc (code, allocated += 256); if (code == 0) { perror ("realloc"); abort (); } } code[len++] = n; n = c = 0; } } } if (c != 0) { fprintf (stderr, "%s: error in input file\n", filename); fclose (fp); free (code); return 0; } fclose (fp); /* Refuse any cells which are too small or too large. */ if (len < MIN_CELL_SIZE || len > MAX_CELL_SIZE) { fprintf (stderr, "%s: cell is too small or too large (len = %d)\n", filename, len); free (code); return 0; } /* Allocate soup fragment. */ frag = soup_frag_malloc (state, 0, len); if (frag == 0) { free (code); return 0; } /* Allocate cell. */ cell = cell_malloc (state, 0, frag); if (cell == 0) { free (code); return 0; } /* Populate cell. */ for (i = 0; i < len; ++i) set_soup (state, cell->frag, code[i], i); free (code); return cell; }