shithub: puzzles

Download patch

ref: d13ece698f02288c56cf46f3a20ecc1aaf8004f5
parent: 0058331aeb027f7441a04d99cc7c1e445bd896d9
author: Simon Tatham <anakin@pobox.com>
date: Fri Apr 21 12:19:49 EDT 2023

Generalise the midend serialisation callbacks.

These are currently only used for internal serialisation of midend
state in order to undo/redo across New Game, and are named
accordingly. But I'm about to reuse them for another set of
serialisation functions, so they'll want more general names, and also
need to be moved higher up in the source file so as to have been
defined where I'll want to use them.

--- a/midend.c
+++ b/midend.c
@@ -30,6 +30,11 @@
     int len, size;
 };
 
+struct midend_serialise_buf_read_ctx {
+    struct midend_serialise_buf *ser;
+    int len, pos;
+};
+
 struct midend {
     frontend *frontend;
     random_state *random;
@@ -477,7 +482,7 @@
     midend_redraw(me);
 }
 
-static void newgame_serialise_write(void *ctx, const void *buf, int len)
+static void midend_serialise_buf_write(void *ctx, const void *buf, int len)
 {
     struct midend_serialise_buf *ser = (struct midend_serialise_buf *)ctx;
     int new_len;
@@ -492,6 +497,18 @@
     ser->len = new_len;
 }
 
+static bool midend_serialise_buf_read(void *ctx, void *buf, int len)
+{
+    struct midend_serialise_buf_read_ctx *const rctx = ctx;
+
+    if (len > rctx->len - rctx->pos)
+        return false;
+
+    memcpy(buf, rctx->ser->buf + rctx->pos, len);
+    rctx->pos += len;
+    return true;
+}
+
 void midend_new_game(midend *me)
 {
     me->newgame_undo.len = 0;
@@ -511,7 +528,7 @@
          * worse, valid but wrong.
          */
         midend_purge_states(me);
-        midend_serialise(me, newgame_serialise_write, &me->newgame_undo);
+        midend_serialise(me, midend_serialise_buf_write, &me->newgame_undo);
     }
 
     midend_stop_anim(me);
@@ -640,23 +657,6 @@
     return (me->statepos < me->nstates || me->newgame_redo.len);
 }
 
-struct newgame_undo_deserialise_read_ctx {
-    struct midend_serialise_buf *ser;
-    int len, pos;
-};
-
-static bool newgame_undo_deserialise_read(void *ctx, void *buf, int len)
-{
-    struct newgame_undo_deserialise_read_ctx *const rctx = ctx;
-
-    if (len > rctx->len - rctx->pos)
-        return false;
-
-    memcpy(buf, rctx->ser->buf + rctx->pos, len);
-    rctx->pos += len;
-    return true;
-}
-
 struct newgame_undo_deserialise_check_ctx {
     bool refused;
 };
@@ -726,7 +726,7 @@
         me->dir = -1;
         return true;
     } else if (me->newgame_undo.len) {
-	struct newgame_undo_deserialise_read_ctx rctx;
+	struct midend_serialise_buf_read_ctx rctx;
 	struct newgame_undo_deserialise_check_ctx cctx;
         struct midend_serialise_buf serbuf;
 
@@ -737,7 +737,7 @@
          */
         serbuf.buf = NULL;
         serbuf.len = serbuf.size = 0;
-        midend_serialise(me, newgame_serialise_write, &serbuf);
+        midend_serialise(me, midend_serialise_buf_write, &serbuf);
 
 	rctx.ser = &me->newgame_undo;
 	rctx.len = me->newgame_undo.len; /* copy for reentrancy safety */
@@ -744,7 +744,7 @@
 	rctx.pos = 0;
         cctx.refused = false;
         deserialise_error = midend_deserialise_internal(
-            me, newgame_undo_deserialise_read, &rctx,
+            me, midend_serialise_buf_read, &rctx,
             newgame_undo_deserialise_check, &cctx);
         if (cctx.refused) {
             /*
@@ -777,7 +777,8 @@
              * the midend so that we can redo back into it later.
              */
             me->newgame_redo.len = 0;
-            newgame_serialise_write(&me->newgame_redo, serbuf.buf, serbuf.len);
+            midend_serialise_buf_write(&me->newgame_redo,
+                                       serbuf.buf, serbuf.len);
 
             sfree(serbuf.buf);
             return true;
@@ -799,7 +800,7 @@
         me->dir = +1;
         return true;
     } else if (me->newgame_redo.len) {
-	struct newgame_undo_deserialise_read_ctx rctx;
+	struct midend_serialise_buf_read_ctx rctx;
 	struct newgame_undo_deserialise_check_ctx cctx;
         struct midend_serialise_buf serbuf;
 
@@ -810,7 +811,7 @@
          */
         serbuf.buf = NULL;
         serbuf.len = serbuf.size = 0;
-        midend_serialise(me, newgame_serialise_write, &serbuf);
+        midend_serialise(me, midend_serialise_buf_write, &serbuf);
 
 	rctx.ser = &me->newgame_redo;
 	rctx.len = me->newgame_redo.len; /* copy for reentrancy safety */
@@ -817,7 +818,7 @@
 	rctx.pos = 0;
         cctx.refused = false;
         deserialise_error = midend_deserialise_internal(
-            me, newgame_undo_deserialise_read, &rctx,
+            me, midend_serialise_buf_read, &rctx,
             newgame_undo_deserialise_check, &cctx);
         if (cctx.refused) {
             /*
@@ -850,7 +851,8 @@
              * the midend so that we can undo back into it later.
              */
             me->newgame_undo.len = 0;
-            newgame_serialise_write(&me->newgame_undo, serbuf.buf, serbuf.len);
+            midend_serialise_buf_write(&me->newgame_undo,
+                                       serbuf.buf, serbuf.len);
 
             sfree(serbuf.buf);
             return true;