From 2f728d684f57443f3a8cb45793b176b3e3121224 Mon Sep 17 00:00:00 2001 From: Laurent Mazet Date: Sat, 24 Aug 2024 22:03:26 +0200 Subject: [PATCH] load is ok --- function.c | 92 +++++++++++++++++++++++++++++++++++++++++++++++------ function.h | 2 +- pentomino.c | 7 ++-- 3 files changed, 87 insertions(+), 14 deletions(-) diff --git a/function.c b/function.c index 39174c9..1f4404b 100644 --- a/function.c +++ b/function.c @@ -8,6 +8,8 @@ #include "function.h" +#define MAXNBBLOCKS 15 + int strmaxlen (char *str, char ch) { int len = 0; @@ -87,6 +89,11 @@ char _itoz (int x) return (x < 10) ? '0' + x : 'A' + x - 10; } +int _ztoi (char x) +{ + return (x > '9') ? x - 'A' + 10 : x - '0'; +} + int _sprinttab (char *buffer, char *name, int *tab, int nb) { int i; @@ -100,15 +107,10 @@ int _sprinttab (char *buffer, char *name, int *tab, int nb) int _sprintblock (char *buffer, int id, block_t *block) { - int i; int l = sprintf (buffer, "width[%d]: %d\n", id, block->width); l += sprintf (buffer + l, "height[%d]: %d\n", id, block->height); l += sprintf (buffer + l, "color[%d]: %d\n", id, block->color); - l += sprintf (buffer + l, "block[%d]: \"", id); - for (i = 0; i < block->width * block->height; i++) { - l += sprintf (buffer + l, "%c", block->tab[i]); - } - l += sprintf (buffer + l, "\"\n"); + l += sprintf (buffer + l, "block[%d]: \"%s\"\n", id, block->tab); return l; } @@ -198,14 +200,28 @@ char *atos (char *str) return ret; } -board_t *loadboard (char *str) +int _checkblockref (char *str, char *ref) +{ + return ((strncmp (str, ref, strlen (ref)) == 0) && + (strlen (str) > strlen (ref) + 2) && + (str[strlen (str) - 1] == ']')); +} + +void loadboard (char *str, board_t **pboard, blocks_t **pblocks) { + int i; int width = 0; int height = 0; char *tab = NULL; - + int nb = 0; + char *x = NULL; + char *y = NULL; + char *settle = NULL; + block_t block[MAXNBBLOCKS]; char *saveptr1, *saveptr2; + memset (block, 0, sizeof (block_t) * MAXNBBLOCKS); + char *line = strtok_r (str, "\n", &saveptr1); while (line) { @@ -222,6 +238,34 @@ board_t *loadboard (char *str) height = atoi (value); } else if (strcmp (keyword, "tab") == 0) { tab = atos (value); + } else if (strcmp (keyword, "nb") == 0) { + nb = atoi (value); + } else if (strcmp (keyword, "x") == 0) { + x = atos (value); + } else if (strcmp (keyword, "y") == 0) { + y = atos (value); + } else if (strcmp (keyword, "settle") == 0) { + settle = atos (value); + } else if (_checkblockref (keyword, "width")) { + i = atoi (keyword + 6); + if (i < MAXNBBLOCKS) { + (block + i)->width = atoi (value); + } + } else if (_checkblockref (keyword, "height")) { + i = atoi (keyword + 7); + if (i < MAXNBBLOCKS) { + (block + i)->height = atoi (value); + } + } else if (_checkblockref (keyword, "color")) { + i = atoi (keyword + 6); + if (i < MAXNBBLOCKS) { + (block + i)->color = atoi (value); + } + } else if (_checkblockref (keyword, "block")) { + i = atoi (keyword + 6); + if (i < MAXNBBLOCKS) { + (block + i)->tab = atos (value); + } } else if (strcmp (keyword, "rem") == 0) { /* nothing to do with remark */ } else { @@ -237,7 +281,37 @@ board_t *loadboard (char *str) memcpy (board->tab, tab, width * height); } - return board; + blocks_t *blocks = NULL; + int error = 1; + if ((nb > 0) && (nb <= MAXNBBLOCKS) && + (x) && (strlen (x) == (size_t)(nb)) && + (y) && (strlen (y) == (size_t)(nb)) && + (settle) && (strlen (settle) == (size_t)(nb))) { + error = 0; + blocks = initblocks (nb); + for (i = 0; i < nb; i++) { + blocks->x[i] = _ztoi (x[i]); + blocks->y[i] = _ztoi (y[i]); + blocks->settle[i] = _ztoi (settle[i]); + block_t *b = block + i; + if ((b->color <= 0) || + (b->tab == NULL) || ((size_t)(b->width * b->height) != strlen (b->tab))) { + error = 1; + break; + } + blocks->block[i] = initblock (b->width, b->height); + blocks->block[i]->color = b->color; + memcpy (blocks->block[i]->tab, b->tab, b->width * b->height); + } + } + + if ((error) || (board == NULL) || (blocks == NULL)) { + freeboard (board); + freeblocks (blocks); + return; + } + *pboard = board; + *pblocks = blocks; } block_t *initblock (int width, int height) diff --git a/function.h b/function.h index 4662089..d9b202c 100644 --- a/function.h +++ b/function.h @@ -37,7 +37,7 @@ char *readdata (char *filename); char *atos (char *str); -board_t *loadboard (char *str); +void loadboard (char *str, board_t **pboard, blocks_t **pblocks); block_t *initblock (int width, int height); diff --git a/pentomino.c b/pentomino.c index 703a9f0..f1d985b 100644 --- a/pentomino.c +++ b/pentomino.c @@ -130,13 +130,14 @@ int main (int argc, char *argv[]) /* load playground or define it */ board_t *board = NULL; + blocks_t *blocks = NULL; if (filename) { char *buffer = readdata (filename); if (buffer == NULL) { VERBOSE (ERROR, fprintf (stderr, "can't read file (%s)\n", filename)); return 1; } - board = loadboard (buffer); + loadboard (buffer, &board, &blocks); free (buffer); if (board == NULL) { VERBOSE (ERROR, fprintf (stderr, "incorrect file (%s)\n", filename)); @@ -151,6 +152,7 @@ int main (int argc, char *argv[]) VERBOSE (ERROR, fprintf (stderr, "unknown board (%s)\n", boardname)); return 1; } + blocks = getblocks ("std"); } setscale (board, scale); @@ -162,9 +164,6 @@ int main (int argc, char *argv[]) } int nbrecords = 0; - /* gets blocks */ - blocks_t *blocks = getblocks ("std"); - /* define work bench */ int wblock = 0; int hblock = 0; -- 2.30.2