From 98f3b39dc2172443c53a4fd69db97e161b7a1155 Mon Sep 17 00:00:00 2001 From: Tim Edwards Date: Tue, 17 Oct 2023 20:01:39 -0400 Subject: [PATCH] Two small tweaks on plot filename behavior: (1) "plot svg" without a filename causes an error instead of generating a trash filename or crashing, while (2) "plot pnm" with a filename that has the ".pnm" extension does not add another ".pnm" extension to the end of it. --- graphics/grTCairo1.c | 15 ++++++++++++++- plot/plotCmd.c | 4 ++-- plot/plotPNM.c | 10 +++++++++- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/graphics/grTCairo1.c b/graphics/grTCairo1.c index 57bfa2d5..29fc05eb 100644 --- a/graphics/grTCairo1.c +++ b/graphics/grTCairo1.c @@ -355,6 +355,7 @@ void GrTCairoPlotSVG (char *filename, MagWindow *mw) { int screenw, screenh; + char *fileptr; cairo_surface_t *wind_surface; cairo_t *wind_context; @@ -366,16 +367,28 @@ GrTCairoPlotSVG (char *filename, MagWindow *mw) return; } + /* Add file extension ".svg" if the filename does not already have an + * extension. + */ + fileptr = filename; + if (strchr(filename, '.') == NULL) + { + fileptr = mallocMagic(strlen(filename) + 5); + sprintf(fileptr, "%s.svg", filename); + } + screenw = mw->w_screenArea.r_xtop - mw->w_screenArea.r_xbot; screenh = mw->w_screenArea.r_ytop - mw->w_screenArea.r_ybot; wind_surface = tcairodata->surface; wind_context = tcairodata->context; - tcairodata->surface = (cairo_surface_t *)cairo_svg_surface_create(filename, + tcairodata->surface = (cairo_surface_t *)cairo_svg_surface_create(fileptr, (double)screenw, (double)screenh); cairo_svg_surface_restrict_to_version(tcairodata->surface, CAIRO_SVG_VERSION_1_2); + if (fileptr != filename) freeMagic(fileptr); + tcairodata->context = cairo_create(tcairodata->surface); WindRedisplay(mw); WindUpdate(); diff --git a/plot/plotCmd.c b/plot/plotCmd.c index 09ae6196..a8d1a0b7 100644 --- a/plot/plotCmd.c +++ b/plot/plotCmd.c @@ -269,9 +269,9 @@ CmdPlot(w, cmd) #if defined(HAVE_LIBCAIRO) && defined(MAGIC_WRAPPER) case PLOTSVG: - if (cmd->tx_argc > 3) + if (cmd->tx_argc != 3) { - TxError("Too many arguments:\n plot %s\n", + TxError("Wrong number of arguments:\n plot %s\n", cmdPlotOption[PLOTSVG]); return; } diff --git a/plot/plotPNM.c b/plot/plotPNM.c index 82b6e745..4e2a29a4 100644 --- a/plot/plotPNM.c +++ b/plot/plotPNM.c @@ -755,6 +755,12 @@ PlotPNM(fileName, scx, layers, xMask, width) } fileName = tempFile; } + else if (strchr(fileName, '.') == NULL) + { + /* Add extention ".pnm" if the filename does not have an extension */ + sprintf(tempFile, "%s.pnm", fileName); + fileName = tempFile; + } rtl_args.outfile = PaOpen(fileName, "w", (char *)NULL, ".", (char *)NULL, (char **)NULL); if (rtl_args.outfile == NULL) @@ -807,7 +813,9 @@ PlotPNM(fileName, scx, layers, xMask, width) { /* open PNM file */ - fp = PaOpen (fileName, "w", ".pnm", ".", NULL, NULL); + fp = PaOpen (fileName, "w", + (strchr(fileName, '.') == NULL) ? ".pnm" : NULL, + ".", NULL, NULL); if (fp == NULL) { TxError ("Could not open file `%s' for writing\n", fileName);