36#ifndef XML_STATUS_ERROR
37#define XML_STATUS_ERROR 0
42#define GRAPHML_ID "_graphml_id"
53static char*
gname =
"";
55typedef LIST(
char *) strs_t;
57static void pushString(strs_t *stk,
const char *
s) {
66static void popString(strs_t *stk) {
69 fprintf(stderr,
"PANIC: graphml2gv: empty element stack\n");
77static char *topString(strs_t *stk) {
80 fprintf(stderr,
"PANIC: graphml2gv: empty element stack\n");
87static void freeString(strs_t *stk) {
94 int closedElementType;
104static userdata_t genUserdata(
char *dfltname) {
105 userdata_t user = {0};
107 user.closedElementType = TAG_NONE;
108 user.edgeinverted =
false;
109 user.gname = dfltname;
113static void freeUserdata(userdata_t ud) {
114 freeString(&ud.elements);
117static int isAnonGraph(
const char *name) {
122 return (*name ==
'\0');
142 fprintf(stderr,
"graphml2gv: Gstack underflow in graph parser\n");
157static Agnode_t *bind_node(
const char *name)
159 return agnode(
G, (
char *)name, 1);
162static Agedge_t *bind_edge(
const char *tail,
const char *
head)
167 tailNode =
agnode(
G, (
char *) tail, 1);
169 E =
agedge(
G, tailNode, headNode, key, 1);
173static int get_xml_attr(
char *attrname,
const char **atts)
176 while (atts[count] !=
NULL) {
177 if (strcmp(attrname, atts[count]) == 0) {
185static char *defval =
"";
187static void setEdgeAttr(
Agedge_t *ep,
char *name,
const char *value,
189 if (strcmp(name,
"headport") == 0) {
190 char *
const attrname = ud->edgeinverted ?
"tailport" :
"headport";
195 }
else if (strcmp(name,
"tailport") == 0) {
196 char *
const attrname = ud->edgeinverted ?
"headport" :
"tailport";
212startElementHandler(
void *userData,
const char *name,
const char **atts)
215 userdata_t *ud = userData;
218 if (strcmp(name,
"graphml") == 0) {
220 }
else if (strcmp(name,
"graph") == 0) {
221 const char *edgeMode =
"";
226 if (ud->closedElementType == TAG_GRAPH) {
228 "Warning: Node contains more than one graph.\n");
230 pos = get_xml_attr(
"id", atts);
236 pos = get_xml_attr(
"edgedefault", atts);
238 edgeMode = atts[pos];
242 if (strcmp(edgeMode,
"directed") == 0) {
244 }
else if (strcmp(edgeMode,
"undirected") == 0) {
247 GV_INFO(
"Warning: graph has no edgedefault attribute - assume directed");
254 if (isAnonGraph(
id)) {
255 static int anon_id = 1;
256 snprintf(buf,
sizeof(buf),
"%%%d", anon_id++);
259 subg =
agsubg(
G, (
char *)
id, 1);
263 pushString(&ud->elements,
id);
264 }
else if (strcmp(name,
"node") == 0) {
265 pos = get_xml_attr(
"id", atts);
267 const char *attrname;
268 attrname = atts[pos];
270 fprintf(stderr,
"node %s outside graph, ignored\n",attrname);
274 pushString(&ud->elements, attrname);
277 }
else if (strcmp(name,
"edge") == 0) {
278 const char *
head =
"", *tail =
"";
282 pos = get_xml_attr(
"source", atts);
285 pos = get_xml_attr(
"target", atts);
290 fprintf(stderr,
"edge source %s target %s outside graph, ignored\n",tail,
head);
292 bind_edge(tail,
head);
297 if (strcmp(tname, tail) == 0) {
298 ud->edgeinverted =
false;
299 }
else if (strcmp(tname,
head) == 0) {
300 ud->edgeinverted =
true;
303 pos = get_xml_attr(
"id", atts);
305 setEdgeAttr(
E, GRAPHML_ID, atts[pos], ud);
311 "Unknown node %s - ignoring.\n",
316static void endElementHandler(
void *userData,
const char *name)
318 userdata_t *ud = userData;
320 if (strcmp(name,
"graph") == 0) {
322 popString(&ud->elements);
323 ud->closedElementType = TAG_GRAPH;
324 }
else if (strcmp(name,
"node") == 0) {
325 char *ele_name = topString(&ud->elements);
326 if (ud->closedElementType == TAG_GRAPH) {
330 popString(&ud->elements);
331 ud->closedElementType = TAG_NODE;
332 }
else if (strcmp(name,
"edge") == 0) {
334 ud->closedElementType = TAG_EDGE;
335 ud->edgeinverted =
false;
339static Agraph_t *graphml_to_gv(
char *graphname, FILE *graphmlFile,
int *rv) {
342 userdata_t udata = genUserdata(graphname);
343 XML_Parser parser = XML_ParserCreate(
NULL);
346 XML_SetUserData(parser, &udata);
347 XML_SetElementHandler(parser, startElementHandler, endElementHandler);
351 size_t len = fread(buf, 1,
sizeof(buf), graphmlFile);
354 done =
len <
sizeof(buf);
355 assert(
len <= INT_MAX);
359 XML_ErrorString(XML_GetErrorCode(parser)),
360 XML_GetCurrentLineNumber(parser));
365 XML_ParserFree(parser);
374 static FILE *savef =
NULL;
385 if ((rv = fopen(
Files[
cnt++],
"r")) != 0)
388 fprintf(stderr,
"Can't open %s\n",
Files[
cnt - 1]);
395static const char *
use =
"Usage: %s [-gd?] [-o<file>] [<graphs>]\n\
396 -g<name> : use <name> as template for graph names\n\
397 -o<file> : output to <file> (stdout)\n\
401static void usage(
int v)
411 sp = strrchr(
path,
'/');
419static void initargs(
int argc,
char **argv)
425 while ((c = getopt(argc, argv,
":vg:o:")) != -1) {
439 fprintf(stderr,
"%s: option -%c missing argument\n",
CmdName, optopt);
446 fprintf(stderr,
"%s: option -%c unrecognized\n",
CmdName,
483 int rv = 0, gcnt = 0;
506 fputs(
"graphml2gv: not configured for conversion from GXL to GV\n", stderr);
Agobj_t * copy(Agraph_t *g, Agobj_t *obj)
static void agxbfree(agxbuf *xb)
free any malloced resources
static int agxbprint(agxbuf *xb, const char *fmt,...)
Printf-style output to an agxbuf.
static WUR char * agxbuse(agxbuf *xb)
Memory allocation wrappers that exit on failure.
static char * gv_strdup(const char *original)
DOT-GXL converter API for gxl2gv.c and gv2gxl.c.
static void initargs(int argc, char **argv)
static char * cmdName(char *path)
helpers for verbose/debug printing
static NORETURN void graphviz_exit(int status)
static double len(glCompPoint p)
static FILE * getFile(void)
static char * nameOf(agxbuf *buf, char *name, int cnt)
static int cnt(Dict_t *d, Dtlink_t **set)
int agnedges(Agraph_t *g)
int agnnodes(Agraph_t *g)
Agsym_t * agattr_text(Agraph_t *g, int kind, char *name, const char *value)
creates or looks up text attributes of a graph
int agxset(void *obj, Agsym_t *sym, const char *value)
Agedge_t * agedge(Agraph_t *g, Agnode_t *t, Agnode_t *h, char *name, int createflag)
Agdesc_t Agundirected
undirected
int agclose(Agraph_t *g)
deletes a graph, freeing its associated storage
Agraph_t * agopen(char *name, Agdesc_t desc, Agdisc_t *disc)
creates a new graph with the given name and kind
int agwrite(Agraph_t *g, void *chan)
Return 0 on success, EOF on failure.
Agdesc_t Agdirected
directed
Agnode_t * agnode(Agraph_t *g, char *name, int createflag)
char * agnameof(void *)
returns a string descriptor for the object.
int agdelete(Agraph_t *g, void *obj)
deletes object. Equivalent to agclose, agdelnode, and agdeledge for obj being a graph,...
Agraph_t * agsubg(Agraph_t *g, char *name, int cflag)
Agraph_t * graph(char *name)
replacements for ctype.h functions
static bool gv_isdigit(int c)
static const char * usage
type-generic dynamically expanding list
#define LIST_POP_BACK(list)
#define LIST_IS_EMPTY(list)
#define LIST_PUSH_BACK(list, item)
static FILE * openFile(const char *argv0, const char *name, const char *mode)