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.
This commit is contained in:
Tim Edwards 2023-10-17 20:01:39 -04:00
parent fdcc178bcd
commit 98f3b39dc2
3 changed files with 25 additions and 4 deletions

View File

@ -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();

View File

@ -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;
}

View File

@ -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);