The other day I found it useful for a (silly) special purpose to
[sgt/puzzles] / gtk.c
diff --git a/gtk.c b/gtk.c
index 94e6975..c17ffe2 100644 (file)
--- a/gtk.c
+++ b/gtk.c
@@ -1328,10 +1328,16 @@ static char *file_selector(frontend *fe, char *title, int save)
     return fe->filesel_name;
 }
 
+struct savefile_write_ctx {
+    FILE *fp;
+    int error;
+};
+
 static void savefile_write(void *wctx, void *buf, int len)
 {
-    FILE *fp = (FILE *)wctx;
-    fwrite(buf, 1, len, fp);
+    struct savefile_write_ctx *ctx = (struct savefile_write_ctx *)wctx;
+    if (fwrite(buf, 1, len, ctx->fp) < len)
+       ctx->error = errno;
 }
 
 static int savefile_read(void *wctx, void *buf, int len)
@@ -1373,9 +1379,21 @@ static void menu_save_event(GtkMenuItem *menuitem, gpointer data)
             return;
         }
 
-        midend_serialise(fe->me, savefile_write, fp);
+       {
+           struct savefile_write_ctx ctx;
+           ctx.fp = fp;
+           ctx.error = 0;
+           midend_serialise(fe->me, savefile_write, &ctx);
+           fclose(fp);
+           if (ctx.error) {
+               char boxmsg[512];
+               sprintf(boxmsg, "Error writing save file: %.400s",
+                       strerror(errno));
+               error_box(fe->window, boxmsg);
+               return;
+           }
+       }
 
-        fclose(fp);
     }
 }
 
@@ -1689,7 +1707,6 @@ static frontend *new_window(char *arg, int argtype, char **error)
        } else
            fe->preset_custom_bullet = NULL;
 
-       changed_preset(fe);
     } else {
        fe->npresets = 0;
        fe->preset_bullets = NULL;
@@ -1745,6 +1762,8 @@ static frontend *new_window(char *arg, int argtype, char **error)
                       GTK_SIGNAL_FUNC(menu_about_event), fe);
     gtk_widget_show(menuitem);
 
+    changed_preset(fe);
+
     {
         int i, ncolours;
         float *colours;
@@ -1763,11 +1782,12 @@ static frontend *new_window(char *arg, int argtype, char **error)
         gdk_colormap_alloc_colors(fe->colmap, fe->colours, ncolours,
                                   FALSE, FALSE, success);
         for (i = 0; i < ncolours; i++) {
-            if (!success[i])
+            if (!success[i]) {
                 g_error("couldn't allocate colour %d (#%02x%02x%02x)\n",
                         i, fe->colours[i].red >> 8,
                         fe->colours[i].green >> 8,
                         fe->colours[i].blue >> 8);
+            }
         }
     }
 
@@ -1893,6 +1913,7 @@ int main(int argc, char **argv)
     int soln = FALSE, colour = FALSE;
     float scale = 1.0F;
     float redo_proportion = 0.0F;
+    char *savefile = NULL, *savesuffix = NULL;
     char *arg = NULL;
     int argtype = ARG_EITHER;
     char *screenshot_file = NULL;
@@ -1941,6 +1962,23 @@ int main(int argc, char **argv)
                }
            } else
                ngenerate = 1;
+       } else if (doing_opts && !strcmp(p, "--save")) {
+           if (--ac > 0) {
+               savefile = *++av;
+           } else {
+               fprintf(stderr, "%s: '--save' expected a filename\n",
+                       pname);
+               return 1;
+           }
+       } else if (doing_opts && (!strcmp(p, "--save-suffix") ||
+                                 !strcmp(p, "--savesuffix"))) {
+           if (--ac > 0) {
+               savesuffix = *++av;
+           } else {
+               fprintf(stderr, "%s: '--save-suffix' expected a filename\n",
+                       pname);
+               return 1;
+           }
        } else if (doing_opts && !strcmp(p, "--print")) {
            if (!thegame.can_print) {
                fprintf(stderr, "%s: this game does not support printing\n",
@@ -2071,7 +2109,7 @@ int main(int argc, char **argv)
      * you may specify it to be 1). Sorry; that was the
      * simplest-to-parse command-line syntax I came up with.
      */
-    if (ngenerate > 0 || print) {
+    if (ngenerate > 0 || print || savefile || savesuffix) {
        int i, n = 1;
        midend *me;
        char *id;
@@ -2082,6 +2120,11 @@ int main(int argc, char **argv)
        me = midend_new(NULL, &thegame, NULL, NULL);
        i = 0;
 
+       if (savefile && !savesuffix)
+           savesuffix = "";
+       if (!savefile && savesuffix)
+           savefile = "";
+
        if (print)
            doc = document_new(px, py, scale);
 
@@ -2138,7 +2181,32 @@ int main(int argc, char **argv)
                    fprintf(stderr, "%s: error in printing: %s\n", pname, err);
                    return 1;
                }
-           } else {
+           }
+           if (savefile) {
+               struct savefile_write_ctx ctx;
+               char *realname = snewn(40 + strlen(savefile) +
+                                      strlen(savesuffix), char);
+               sprintf(realname, "%s%d%s", savefile, i, savesuffix);
+               ctx.fp = fopen(realname, "w");
+               if (!ctx.fp) {
+                   fprintf(stderr, "%s: open: %s\n", realname,
+                           strerror(errno));
+                   return 1;
+               }
+               sfree(realname);
+               midend_serialise(me, savefile_write, &ctx);
+               if (ctx.error) {
+                   fprintf(stderr, "%s: write: %s\n", realname,
+                           strerror(ctx.error));
+                   return 1;
+               }
+               if (fclose(ctx.fp)) {
+                   fprintf(stderr, "%s: close: %s\n", realname,
+                           strerror(errno));
+                   return 1;
+               }
+           }
+           if (!doc && !savefile) {
                id = midend_get_game_id(me);
                puts(id);
                sfree(id);