Skip to content

Add feature to export to svg-file #8

Merged
merged 1 commit into from
Jun 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
79 changes: 79 additions & 0 deletions xdu.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 <stdio.h>
Expand Down Expand Up @@ -922,6 +928,79 @@ void savetops(char *fname, int showsize)

}

void fprintsvgstart(FILE * fp)
{
fprintf(fp, "<svg xmlns=\"http://www.w3.org/2000/svg\">\n");
}

void fprintsvgend(FILE * fp)
{
fprintf(fp, "</svg>");
}

void fprintsvgnode(FILE * fp, struct rect rect)
{
fprintf(fp, " <rect x=\"%d\" y=\"%d\" width=\"%d\" height=\"%d\" stroke=\"black\" stroke-width=\"1px\" fill=\"white\"/>\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, " <text x=\"%d\" y=\"%d\" alignment-baseline=\"middle\" text-anchor=\"left\" style=\"font-size:12\">%s</text>\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;
Expand Down
41 changes: 40 additions & 1 deletion xwin.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <X11/Xaw/Label.h>

#include <stdio.h>
#include <unistd.h> /* for access() */

#ifndef X_NOT_STDC_ENV
#include <stdlib.h> /* for exit() */
Expand All @@ -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();
Expand All @@ -66,6 +68,7 @@ static String fallback_resources[] = {
"*help.height: 330",
"*order: first",
"*psfile: xdu_out.ps",
"*svgfile: xdu_out",
NULL
};

Expand All @@ -78,6 +81,7 @@ typedef struct {
int showsize;
char *order;
char *psfilename;
char *svgfilename;
} res_data, *res_data_ptr;
static res_data res;

Expand All @@ -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 */
Expand All @@ -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();
Expand All @@ -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},
Expand All @@ -149,6 +158,7 @@ static char defaultTranslations[] = "\
:<Key>/: reset()\n\
<Key>S: size()\n\
<Key>P: saveps()\n\
<Key>V: savesvg()\n\
<Key>I: info()\n\
<Key>H: help()\n\
<Key>Help: help()\n\
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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\
Expand Down