if (gtk_selection_owner_set(fe->area, GDK_SELECTION_PRIMARY,
CurrentTime)) {
+ gtk_selection_clear_targets(fe->area, GDK_SELECTION_PRIMARY);
gtk_selection_add_target(fe->area, GDK_SELECTION_PRIMARY,
GDK_SELECTION_TYPE_STRING, 1);
gtk_selection_add_target(fe->area, GDK_SELECTION_PRIMARY,
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)
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);
}
}
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;
}
} 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",
* 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;
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);
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);