32#ifndef XML_STATUS_ERROR
33#define XML_STATUS_ERROR 0
38#define GXL_ATTR "_gxl_"
39#define GXL_ID "_gxl_id"
40#define GXL_ROLE "_gxl_role"
41#define GXL_HYPER "_gxl_hypergraph"
42#define GXL_FROM "_gxl_fromorder"
43#define GXL_TO "_gxl_toorder"
44#define GXL_TYPE "_gxl_type"
45#define GXL_COMP "_gxl_composite_"
46#define GXL_LOC "_gxl_locator_"
63 bool compositeReadState;
69static attr_t Current_class;
75static graph_stack_t Gstack;
95static void free_nitem(
void *name) {
104 .link = offsetof(
namev_t, link),
109static userdata_t genUserdata(
void) {
110 userdata_t user = {0};
112 user.closedElementType = TAG_NONE;
113 user.globalAttrType = TAG_NONE;
114 user.compositeReadState =
false;
115 user.edgeinverted =
false;
120static void freeUserdata(userdata_t ud) {
127static void addToMap(
Dt_t * map,
char *name,
char *uniqueName)
146static int isAnonGraph(
const char *name)
152 return (*name ==
'\0');
158 graph_stack_push_back(&Gstack, g);
161 if (graph_stack_size(&Gstack) == 1) {
172 if (graph_stack_is_empty(&Gstack)) {
173 fprintf(stderr,
"gxl2gv: Gstack underflow in graph parser\n");
178 Agraph_t *g = graph_stack_pop_back(&Gstack);
181 if (!graph_stack_is_empty(&Gstack))
182 G = *graph_stack_back(&Gstack);
187static Agnode_t *bind_node(
const char *name)
193static Agedge_t *bind_edge(
const char *tail,
const char *
head)
198 tailNode =
agnode(
G, (
char *) tail, 1);
200 E =
agedge(
G, tailNode, headNode, key, 1);
204static int get_xml_attr(
char *attrname,
const char **atts)
207 while (atts[count] !=
NULL) {
208 if (strcmp(attrname, atts[count]) == 0) {
216static void setName(
Dt_t * names,
Agobj_t * n,
char *value)
220 char *
const oldName =
agxget(n, ap);
225static char *defval =
"";
228setNodeAttr(
Agnode_t * np,
char *name,
char *value, userdata_t * ud,
231 if (strcmp(name,
"name") == 0) {
232 setName(ud->nameMap, (
Agobj_t *) np, value);
245#define NODELBL "node:"
246#define NLBLLEN (sizeof(NODELBL)-1)
247#define EDGELBL "edge:"
248#define ELBLLEN (sizeof(EDGELBL)-1)
255setGlobalNodeAttr(
Agraph_t * g,
char *name,
char *value)
259 "Warning: global node attribute %s in graph %s does not begin with the prefix %s\n",
269setEdgeAttr(
Agedge_t * ep,
char *name,
char *value, userdata_t * ud,
275 if (strcmp(name,
"headport") == 0) {
276 if (ud->edgeinverted)
277 attrname =
"tailport";
279 attrname =
"headport";
283 }
else if (strcmp(name,
"tailport") == 0) {
284 if (ud->edgeinverted)
285 attrname =
"headport";
287 attrname =
"tailport";
309setGlobalEdgeAttr(
Agraph_t * g,
char *name,
char *value)
313 "Warning: global edge attribute %s in graph %s does not begin with the prefix %s\n",
323setGraphAttr(
Agraph_t * g,
char *name,
char *value, userdata_t * ud)
327 if ((g == root) && !strcmp(name,
"strict") && !strcmp(value,
"true")) {
329 }
else if (strcmp(name,
"name") == 0)
330 setName(ud->nameMap, (
Agobj_t *) g, value);
344static void setAttr(
char *name,
char *value, userdata_t * ud,
bool is_html)
346 switch (Current_class) {
348 setGraphAttr(
G, name, value, ud);
351 setNodeAttr(
N, name, value, ud, is_html);
354 setEdgeAttr(
E, name, value, ud, is_html);
364startElementHandler(
void *userData,
const char *name,
const char **atts)
367 userdata_t *ud = userData;
370 if (strcmp(name,
"gxl") == 0) {
372 }
else if (strcmp(name,
"graph") == 0) {
373 const char *edgeMode =
"";
376 Current_class = TAG_GRAPH;
377 if (ud->closedElementType == TAG_GRAPH) {
379 "Warning: Node contains more than one graph.\n");
381 pos = get_xml_attr(
"id", atts);
383 fprintf(stderr,
"Error: Graph has no ID attribute.\n");
386 const char *
id = atts[pos];
387 pos = get_xml_attr(
"edgemode", atts);
389 edgeMode = atts[pos];
392 if (graph_stack_is_empty(&Gstack)) {
393 if (strcmp(edgeMode,
"directed") == 0) {
395 }
else if (strcmp(edgeMode,
"undirected") == 0) {
399 "Warning: graph has no edgemode attribute");
400 fprintf(stderr,
" - assume directed\n");
406 if (isAnonGraph(
id)) {
407 static int anon_id = 1;
408 snprintf(buf,
sizeof(buf),
"%%%d", anon_id++);
411 subg =
agsubg(
G, (
char *)
id, 1);
415 pos = get_xml_attr(
"role", atts);
417 setGraphAttr(
G,
GXL_ROLE, (
char *) atts[pos], ud);
420 pos = get_xml_attr(
"hypergraph", atts);
422 setGraphAttr(
G,
GXL_HYPER, (
char *) atts[pos], ud);
425 }
else if (strcmp(name,
"node") == 0) {
426 Current_class = TAG_NODE;
427 pos = get_xml_attr(
"id", atts);
429 const char *attrname;
430 attrname = atts[pos];
432 if (attrname !=
NULL && strcmp(attrname,
"") != 0) {
437 }
else if (strcmp(name,
"edge") == 0) {
438 const char *
head =
"", *tail =
"";
442 Current_class = TAG_EDGE;
443 pos = get_xml_attr(
"from", atts);
446 pos = get_xml_attr(
"to", atts);
458 bind_edge(tail,
head);
463 if (strcmp(tname, tail) == 0) {
464 ud->edgeinverted =
false;
465 }
else if (strcmp(tname,
head) == 0) {
466 ud->edgeinverted =
true;
469 pos = get_xml_attr(
"fromorder", atts);
471 setEdgeAttr(
E,
GXL_FROM, (
char *) atts[pos], ud,
false);
474 pos = get_xml_attr(
"toorder", atts);
476 setEdgeAttr(
E,
GXL_TO, (
char *) atts[pos], ud,
false);
479 pos = get_xml_attr(
"id", atts);
481 setEdgeAttr(
E,
GXL_ID, (
char *) atts[pos], ud,
false);
483 }
else if (strcmp(name,
"attr") == 0) {
484 const char *attrname = atts[get_xml_attr(
"name", atts)];
486 agxbput(&ud->xml_attr_name, attrname);
487 pos = get_xml_attr(
"kind", atts);
490 if (strcmp(
"node", atts[pos]) == 0)
491 ud->globalAttrType = TAG_NODE;
492 else if (strcmp(
"edge", atts[pos]) == 0)
493 ud->globalAttrType = TAG_EDGE;
494 else if (strcmp(
"graph", atts[pos]) == 0)
495 ud->globalAttrType = TAG_GRAPH;
496 else if (strcmp(
"HTML-like string", atts[pos]) == 0)
497 ud->globalAttrType = TAG_HTML_LIKE_STRING;
499 ud->globalAttrType = TAG_NONE;
502 }
else if (strcmp(name,
"string") == 0
503 || strcmp(name,
"bool") == 0
504 || strcmp(name,
"int") == 0 || strcmp(name,
"float") == 0) {
507 if (ud->compositeReadState) {
508 agxbprint(&ud->composite_buffer,
"<%s>", name);
510 }
else if (strcmp(name,
"rel") == 0 || strcmp(name,
"relend") == 0) {
511 fprintf(stderr,
"%s element is ignored by DOT\n", name);
512 }
else if (strcmp(name,
"type") == 0) {
513 pos = get_xml_attr(
"xlink:href", atts);
517 }
else if (strcmp(name,
"locator") == 0) {
518 pos = get_xml_attr(
"xlink:href", atts);
520 const char *href = atts[pos];
523 }
else if (strcmp(name,
"seq") == 0
524 || strcmp(name,
"set") == 0
525 || strcmp(name,
"bag") == 0
526 || strcmp(name,
"tup") == 0 || strcmp(name,
"enum") == 0) {
528 ud->compositeReadState =
true;
529 agxbprint(&ud->composite_buffer,
"<%s>", name);
533 "Unknown node %s; DOT does not support extensions.\n",
538static void endElementHandler(
void *userData,
const char *name)
540 userdata_t *ud = userData;
542 if (strcmp(name,
"graph") == 0) {
544 ud->closedElementType = TAG_GRAPH;
545 }
else if (strcmp(name,
"node") == 0) {
546 Current_class = TAG_GRAPH;
548 ud->closedElementType = TAG_NODE;
549 }
else if (strcmp(name,
"edge") == 0) {
550 Current_class = TAG_GRAPH;
552 ud->closedElementType = TAG_EDGE;
553 ud->edgeinverted =
false;
554 }
else if (strcmp(name,
"attr") == 0) {
558 ud->closedElementType = TAG_NONE;
559 if (ud->compositeReadState) {
561 value =
agxbuse(&ud->composite_buffer);
563 ud->compositeReadState =
false;
566 value =
agxbuse(&ud->xml_attr_value);
569 switch (ud->globalAttrType) {
574 setGlobalNodeAttr(
G,
agxbuse(&new_name), value);
577 setGlobalEdgeAttr(
G,
agxbuse(&new_name), value);
580 setGraphAttr(
G,
agxbuse(&new_name), value, ud);
582 case TAG_HTML_LIKE_STRING:
589 ud->globalAttrType = TAG_NONE;
590 }
else if (strcmp(name,
"string") == 0
591 || strcmp(name,
"bool") == 0
592 || strcmp(name,
"int") == 0 || strcmp(name,
"float") == 0) {
594 if (ud->compositeReadState) {
595 agxbprint(&ud->composite_buffer,
"</%s>", name);
597 }
else if (strcmp(name,
"seq") == 0
598 || strcmp(name,
"set") == 0
599 || strcmp(name,
"bag") == 0
600 || strcmp(name,
"tup") == 0 || strcmp(name,
"enum") == 0) {
601 agxbprint(&ud->composite_buffer,
"</%s>", name);
605static void characterDataHandler(
void *userData,
const char *
s,
int length)
607 userdata_t *ud = userData;
609 assert(
length >= 0 &&
"Expat returned negative length data");
615 if (ud->compositeReadState) {
627 userdata_t udata = genUserdata();
628 XML_Parser parser = XML_ParserCreate(
NULL);
630 XML_SetUserData(parser, &udata);
631 XML_SetElementHandler(parser, startElementHandler, endElementHandler);
632 XML_SetCharacterDataHandler(parser, characterDataHandler);
634 Current_class = TAG_GRAPH;
637 size_t len = fread(buf, 1,
sizeof(buf), gxlFile);
640 done =
len <
sizeof(buf);
641 assert(
len <= INT_MAX &&
"too large data for Expat API");
645 XML_ErrorString(XML_GetErrorCode(parser)),
646 XML_GetCurrentLineNumber(parser));
650 XML_ParserFree(parser);
652 graph_stack_free(&Gstack);
static void agxbfree(agxbuf *xb)
free any malloced resources
static size_t agxbput_n(agxbuf *xb, const char *s, size_t ssz)
append string s of length ssz into xb
static int agxbprint(agxbuf *xb, const char *fmt,...)
Printf-style output to an agxbuf.
static void agxbclear(agxbuf *xb)
resets pointer to data
static WUR char * agxbuse(agxbuf *xb)
Memory allocation wrappers that exit on failure.
static char * gv_strdup(const char *original)
CDT_API int dtclose(Dt_t *)
CDT_API Dtmethod_t * Dtoset
ordered set (self-adjusting tree)
CDT_API Dt_t * dtopen(Dtdisc_t *, Dtmethod_t *)
int agrename(Agobj_t *obj, char *newname)
Agsym_t * setAttr(graph_t *g, void *obj, char *name, char *value, Agsym_t *ap)
static Extype_t length(Exid_t *rhs, Exdisc_t *disc)
DOT-GXL converter API for gxl2gv.c and gv2gxl.c.
static NORETURN void graphviz_exit(int status)
static double len(glCompPoint p)
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)
int agxset_html(void *obj, Agsym_t *sym, const char *value)
char * agxget(void *obj, Agsym_t *sym)
Agedge_t * agedge(Agraph_t *g, Agnode_t *t, Agnode_t *h, char *name, int createflag)
Agdesc_t Agundirected
undirected
Agraph_t * agopen(char *name, Agdesc_t desc, Agdisc_t *disc)
creates a new graph with the given name and kind
Agdesc_t Agdirected
directed
Agnode_t * agnode(Agraph_t *g, char *name, int createflag)
char * agnameof(void *)
returns a string descriptor for the object.
#define AGTYPE(obj)
returns AGRAPH, AGNODE, or AGEDGE depending on the type of the object
Agraph_t * agsubg(Agraph_t *g, char *name, int cflag)
static char * mapLookup(Dt_t *nm, char *name)
static void * make_nitem(void *p, Dtdisc_t *disc)
static void addToMap(Dt_t *map, char *name, char *uniqueName)
replacements for ctype.h functions
static bool gv_isdigit(int c)
#define DEFINE_LIST(name, type)
static bool startswith(const char *s, const char *prefix)
does the string s begin with the string prefix?
a generic header of Agraph_s, Agnode_s and Agedge_s