From 13a564e2399f2cea738ff25743aa3f54fa371b2c Mon Sep 17 00:00:00 2001 From: niclas Date: Mon, 11 Jun 2018 11:31:52 +0200 Subject: [PATCH] Add feature to export to svg-file --- README.md | 1 + xdu.c | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ xwin.c | 41 ++++++++++++++++++++++++++++- 3 files changed, 120 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c5262a9..de5223d 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,7 @@ This version has been patched for more functions. - key `n` : change size display ( bytes, MiB, GiB, TiB ) - key `p` : print as postscript +- key `v` : print as scalable-vector-graphic - option `-f` : set postscript file name This is a *works-for-me* release. diff --git a/xdu.c b/xdu.c index d11a04b..785cfa7 100644 --- a/xdu.c +++ b/xdu.c @@ -32,6 +32,12 @@ * - 'worksforme' Makefile * - -Wall -Werror -Wextra -pedantic * + * added ability to print svg-files Fri Jun 08 2018 01:04:45 PM CEST + * Niclas Hofmann, niclas@molgen.mpg.de + * (new key v) + * - 'worksforme' Makefile + * - -Wall -Werror -Wextra -pedantic + * */ #include @@ -922,6 +928,79 @@ void savetops(char *fname, int showsize) } +void fprintsvgstart(FILE * fp) +{ + fprintf(fp, "\n"); +} + +void fprintsvgend(FILE * fp) +{ + fprintf(fp, ""); +} + +void fprintsvgnode(FILE * fp, struct rect rect) +{ + fprintf(fp, " \n", rect.left, rect.top, rect.width, rect.height); +} + +void fprintsvgnodetext(FILE * fp, int x, int y, long long size, char * name, int showsize) +{ + char buffer[1024], *text; + + switch (showsize) { + case 1: + sprintf(buffer, "%s (%lldk)", name, size); + text = buffer; + break; + case 2: + sprintf(buffer, "%s (%.2fM)", name, (double)size/1024.0); + text = buffer; + break; + case 3: + sprintf(buffer, "%s (%.2fG)", name, (double)size/(1024.0*1024.0)); + text = buffer; + break; + case 4: + sprintf(buffer, "%s (%.2fT)", name, (double)size/(1024.0*1024.0*1024.0)); + text = buffer; + break; + default: + printf("arghhhhh"); + break; + } + + fprintf(fp, " %s\n", x, y, text); +} + +void savesvgnode(FILE * fp, struct node * nodep, int showsize, int depth) +{ + struct node *np; + + if (depth >= ncols) + return; + else + depth++; + + for (np = nodep; np != NULL; np = np->peer) { + fprintsvgnode(fp, np->rect); + if (np->rect.height > 13) + fprintsvgnodetext(fp, np->rect.left+4, np->rect.top+np->rect.height/2+6, np->size, np->name, showsize); + savesvgnode(fp, np->child, showsize, depth); + } +} + +void savetosvg(char *fname, int showsize) +{ + FILE *fp; + + if ((fp = fopen(fname, "w")) != NULL) { + fprintsvgstart(fp); + savesvgnode(fp, topp, showsize, 0); + fprintsvgend(fp); + fclose(fp); + } +} + void uponechild(void) { struct node *np, *parent = NULL; diff --git a/xwin.c b/xwin.c index 62607c4..4654084 100644 --- a/xwin.c +++ b/xwin.c @@ -29,6 +29,7 @@ #include #include +#include /* for access() */ #ifndef X_NOT_STDC_ENV #include /* for exit() */ @@ -46,6 +47,7 @@ extern int nodeinfo(); extern int helpinfo(); extern int ncols; extern void savetops(char *, int); +extern void savetosvg(char*, int); /* EXPORTS: routines that this module exports outside */ extern int xsetup(); @@ -66,6 +68,7 @@ static String fallback_resources[] = { "*help.height: 330", "*order: first", "*psfile: xdu_out.ps", + "*svgfile: xdu_out", NULL }; @@ -78,6 +81,7 @@ typedef struct { int showsize; char *order; char *psfilename; + char *svgfilename; } res_data, *res_data_ptr; static res_data res; @@ -100,6 +104,9 @@ static XtResource application_resources[] = { , {"psfile", "PSFile", XtRString, sizeof(String), XtOffset(res_data_ptr, psfilename), XtRString, "xdu_out.ps"} + , + {"svgfile", "SVGFile", XtRString, sizeof(String), + XtOffset(res_data_ptr, svgfilename), XtRString, "xdu_out"} }; /* Command Line Options */ @@ -121,6 +128,7 @@ static void a_quit(); static void a_reorder(); static void a_size(); static void a_saveps(); +static void a_savesvg(); static void a_up(); static void a_down(); static void a_ncol(); @@ -136,6 +144,7 @@ static XtActionsRec actionsTable[] = { {"size", a_size}, {"ncol", a_ncol}, {"saveps", a_saveps}, + {"savesvg", a_savesvg}, {"uponechild", a_up}, {"downonechild", a_down}, {"info", a_info}, @@ -149,6 +158,7 @@ static char defaultTranslations[] = "\ :/: reset()\n\ S: size()\n\ P: saveps()\n\ +V: savesvg()\n\ I: info()\n\ H: help()\n\ Help: help()\n\ @@ -255,6 +265,34 @@ static void a_saveps(Widget w, XEvent * e, String * params, Cardinal * num_param fprintf(stderr, "saved !\n"); } +static void a_savesvg(Widget w, XEvent * e, String * params, Cardinal * num_params) +{ + char *extension = ".svg"; + int len = strlen(res.svgfilename) + 3 + strlen(extension) + 1; + char tmp[len]; + char *fname; + int i; + + UNUSED(w, e, params, num_params); + + for (i = 0; i > -1; ++i) { + snprintf(tmp, len,"%s-%02d%s", res.svgfilename, i, extension); + if (access(tmp, F_OK) == -1) { + fname = tmp; + break; + } + } + + if (i < 0) { + fprintf(stderr, "ERROR: there are too many xdu_output files!!!\n"); + exit(1); + } + + fprintf(stderr, "saving as scalable vector graphic to file: %s ..", fname); + savetosvg(fname, res.showsize); + fprintf(stderr, "saved !\n"); +} + static void a_up(Widget w, XEvent * e, String * params, Cardinal * num_params) { UNUSED(w, e, params, num_params); @@ -544,7 +582,8 @@ Keyboard Commands\n\ q quit (also Escape)\n\ 0-9 set number of columns (0=10)\n\ \n\ - p print to file\n\ + p print to ps-file\n\ + v print to svg-file\n\ u go up one child\n\ p go down one child\n\ \n\