commit f50583542799a9f5e6434bde0455fd055b29ac80
parent aea924ad6024503604067e50ae0b64a870bb9c96
Author: aabacchus <ben@bvnf.space>
Date: Sun, 3 Oct 2021 00:38:08 +0100
kplot: add -T output format switch
Diffstat:
M | kplot.c | | | 34 | +++++++++++++++++++++++++++------- |
1 file changed, 27 insertions(+), 7 deletions(-)
diff --git a/kplot.c b/kplot.c
@@ -5,23 +5,40 @@
#include <csv.h>
#include <kplot.h>
#include <math.h>
+#include <string.h>
#include <unistd.h>
void
usage(char *argv0) {
- fprintf(stderr, "usage: %s [-H] [file.csv]\n", argv0);
- fprintf(stderr, " -H treat first line as header\n");
+ fprintf(stderr, "usage: %s [-HT] [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");
}
int
main(int argc, char **argv) {
- int ch, flag_H;
+ int ch, flag_H, output_type;
flag_H = 0;
- while ((ch = getopt(argc, argv, "H")) != -1) {
+ enum {
+ OUT_PNG,
+ OUT_PDF
+ };
+ output_type = OUT_PDF;
+ while ((ch = getopt(argc, argv, "HT:")) != -1) {
switch (ch) {
case 'H':
flag_H = 1;
break;
+ case 'T':
+ if (strncmp("png", optarg, 3) == 0)
+ output_type = 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 '?':
usage(*argv);
return 1;
@@ -74,12 +91,15 @@ main(int argc, char **argv) {
p = kplot_alloc(NULL);
kplot_attach_data(p, d1, KPLOT_POINTS, NULL);
kdata_destroy(d1);
- //surf = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 600, 400);
- surf = cairo_pdf_surface_create("out.pdf", 600, 400);
+ 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);
cr = cairo_create(surf);
cairo_surface_destroy(surf);
kplot_draw(p, 600.0, 400.0, cr);
- //cairo_surface_write_to_png(cairo_get_target(cr), "example0.png");
+ if (output_type == OUT_PNG)
+ cairo_surface_write_to_png(cairo_get_target(cr), "example0.png");
cairo_destroy(cr);
kplot_free(p);
return 0;