2nd commit on restore code (not working)
authorMazet Laurent <mazet@softndesign.org>
Tue, 18 Jun 2024 05:50:09 +0000 (07:50 +0200)
committerMazet Laurent <mazet@softndesign.org>
Tue, 18 Jun 2024 05:50:09 +0000 (07:50 +0200)
function.c
scrabble.c

index 13e398d91a26ae15e17555c2f25686e59b7e044a..99e78c73a7691af191b673fde355df5e0cba1598 100644 (file)
@@ -1,7 +1,9 @@
 #include <curses.h>
+#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 
+#include "constant.h"
 #include "debug.h"
 #include "type.h"
 
@@ -590,12 +592,16 @@ char *loaddata (char *filename)
     char *buffer = NULL;
     FILE *fd = fopen (filename, "r");
     if (fd) {
-        int size = fseek (fd, 0, SEEK_END);
+        fseek (fd, 0, SEEK_END);
+        int size = ftell (fd);
         buffer = (char *) calloc (size, 1);
         CHECKALLOC (buffer);
 
-        fseek (fd, 0, SEEK_BEGIN);
-        fread (buffer, 1, size, fd);
+        fseek (fd, 0, SEEK_SET);
+        int nb = fread (buffer, 1, size, fd);
+        if (nb != size) {
+            VERBOSE (WARNING, printf ("can't fully read file (%s)\n", filename));
+        }
         fclose (fd);
     }
 
@@ -620,7 +626,7 @@ char *atos (char *str)
     return ret;
 }
 
-int loaddata (char *str, int *nbplayers, board_t **board, bag_t **bag, play_t *play, draw_t *draw, draw_t **game, int *n, play_t **turn, int *score)
+int restoredata (char *str, int *nbplayers, board_t **board, bag_t **bag, play_t *play, draw_t *draw, draw_t **game, int *n, play_t **turn, int *score)
 {
     int ret = 1;
 
@@ -651,18 +657,22 @@ int loaddata (char *str, int *nbplayers, board_t **board, bag_t **bag, play_t *p
         } else if (strcmp (name,  "game[0]->nbtiles") == 0) {
             game[0]->nbtiles = atoi (value);
         } else if (strcmp (name,  "game[0]->tiles") == 0) {
+            memset (game[0]->tiles, 0, (*board)->length);
             strcpy (game[0]->tiles, atos (value));
         } else if (strcmp (name,  "game[1]->nbtiles") == 0) {
             game[1]->nbtiles = atoi (value);
         } else if (strcmp (name,  "game[1]->tiles") == 0) {
+            memset (game[1]->tiles, 0, (*board)->length);
             strcpy (game[1]->tiles, atos (value));
         } else if (strcmp (name,  "game[2]->nbtiles") == 0) {
             game[2]->nbtiles = atoi (value);
         } else if (strcmp (name,  "game[2]->tiles") == 0) {
+            memset (game[2]->tiles, 0, (*board)->length);
             strcpy (game[2]->tiles, atos (value));
         } else if (strcmp (name,  "game[3]->nbtiles") == 0) {
             game[3]->nbtiles = atoi (value);
         } else if (strcmp (name,  "game[3]->tiles") == 0) {
+            memset (game[3]->tiles, 0, (*board)->length);
             strcpy (game[3]->tiles, atos (value));
         } else if (strcmp (name,  "current") == 0) {
             *n = atoi (value);
@@ -689,12 +699,13 @@ int loaddata (char *str, int *nbplayers, board_t **board, bag_t **bag, play_t *p
         line = strtok_r (NULL, "\n", &saveptr1);
     }
 
-    if (board) {
-        play->xsize = board->xsize;
-        play->ysize = board->ysize;
+    if (*board) {
+        int i;
+        play->xsize = (*board)->xsize;
+        play->ysize = (*board)->ysize;
         for (i = 0; i < MAXNBPLAYERS; i++) {
-            turn[i]->xsize = board->xsize;
-            turn[i]->ysize = board->ysize;
+            turn[i]->xsize = (*board)->xsize;
+            turn[i]->ysize = (*board)->ysize;
         }
     } else {
         ret = 0;
index 58199e0dc9193b27ff8244b2f2803f5afb6ec24c..fa3844ced9ac54af5e197b2e8dbb51e4f06a8f07 100644 (file)
@@ -128,6 +128,14 @@ int main (int argc, char *argv[])
             }
             nbplayers = atoi (arg);
             break;
+        case 'r':
+            arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
+            if (arg == NULL) {
+                VERBOSE (ERROR, fprintf (stderr, "%s: missing file name\n", progname));
+                return usage (1);
+            }
+            file = arg;
+            break;
         case 'v':
             arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
             if (arg == NULL) {
@@ -222,9 +230,6 @@ int main (int argc, char *argv[])
 
     int *score = (int *) calloc (MAXNBPLAYERS, sizeof (int));
     CHECKALLOC (score);
-    for (n = 0; n < nbplayers; n++) {
-        drawwindow (game[n], score[n], xdrawwin[n], ydrawwin[n], 0);
-    }
     n = 0;
 
     if (file) {
@@ -240,6 +245,11 @@ int main (int argc, char *argv[])
         }
     }
 
+    int i;
+    for (i = 0; i < nbplayers; i++) {
+        drawwindow (game[i], score[i], xdrawwin[i], ydrawwin[i], 0);
+    }
+
     int mode = 0;
     showboard (play, board, xoffset, yoffset, mode);