load is ok
authorLaurent Mazet <mazet@softndesign.org>
Sat, 24 Aug 2024 20:03:26 +0000 (22:03 +0200)
committerLaurent Mazet <mazet@softndesign.org>
Sat, 24 Aug 2024 20:03:26 +0000 (22:03 +0200)
function.c
function.h
pentomino.c

index 39174c9ba47b432a40a5f045951393739f552246..1f4404b9cc44b34b2e09cb20f2ebb4ab2d86aeb3 100644 (file)
@@ -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)
index 4662089115a1f06fdd3b152358719d5f25754c1f..d9b202cd499b6a321cc772d850672518f31d9b96 100644 (file)
@@ -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);
 
index 703a9f0ac122cfd130b92c92e7e73d9c4e279456..f1d985b4a363920ae81af1ca88a0736d9c21aa0c 100644 (file)
@@ -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;