ckplot

Unnamed repository
Log | Files | Refs

commit 2729af259a99d7a65dd21904dc6fc8cb4d376d18
parent f50583542799a9f5e6434bde0455fd055b29ac80
Author: aabacchus <ben@bvnf.space>
Date:   Sun,  3 Oct 2021 00:48:10 +0100

kplot: add -o output filename arg

Diffstat:
Mkplot.c | 25+++++++++++++++++--------
1 file changed, 17 insertions(+), 8 deletions(-)

diff --git a/kplot.c b/kplot.c @@ -10,35 +10,44 @@ void usage(char *argv0) { - fprintf(stderr, "usage: %s [-HT] [file.csv]\n", argv0); + fprintf(stderr, "usage: %s [-HTo] [file.csv]\n", argv0); fprintf(stderr, " -H treat first line as header\n"); fprintf(stderr, " -T format output format (options \"png\", \"pdf\")" " (default: \"pdf\")\n"); + fprintf(stderr, " -o outfile output filename\n"); } int main(int argc, char **argv) { - int ch, flag_H, output_type; - flag_H = 0; + int ch, flag_H, flag_o, output_type; + char *outfile; + flag_H = flag_o = 0; enum { OUT_PNG, OUT_PDF }; output_type = OUT_PDF; - while ((ch = getopt(argc, argv, "HT:")) != -1) { + outfile = "out.pdf"; + while ((ch = getopt(argc, argv, "HT:o:")) != -1) { switch (ch) { case 'H': flag_H = 1; break; case 'T': - if (strncmp("png", optarg, 3) == 0) + if (strncmp("png", optarg, 3) == 0) { output_type = OUT_PNG; - else if (strncmp("pdf", optarg, 3) == 0) + if (flag_o == 0) + outfile = "out.png"; + } else if (strncmp("pdf", optarg, 3) == 0) output_type = OUT_PDF; else fprintf(stderr, "output format \"%s\" not recognised " "(supported: \"png\", \"pdf\")\n", optarg); break; + case 'o': + flag_o = 1; + outfile = optarg; + break; case '?': usage(*argv); return 1; @@ -94,12 +103,12 @@ main(int argc, char **argv) { if (output_type == OUT_PNG) surf = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 600, 400); else - surf = cairo_pdf_surface_create("out.pdf", 600, 400); + surf = cairo_pdf_surface_create(outfile, 600, 400); cr = cairo_create(surf); cairo_surface_destroy(surf); kplot_draw(p, 600.0, 400.0, cr); if (output_type == OUT_PNG) - cairo_surface_write_to_png(cairo_get_target(cr), "example0.png"); + cairo_surface_write_to_png(cairo_get_target(cr), outfile); cairo_destroy(cr); kplot_free(p); return 0;