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)
228static char *defval =
"";
231setNodeAttr(
Agnode_t * np,
char *name,
char *value, userdata_t * ud,
236 if (strcmp(name,
"name") == 0) {
237 setName(ud->nameMap, (
Agobj_t *) np, value);
250#define NODELBL "node:"
251#define NLBLLEN (sizeof(NODELBL)-1)
252#define EDGELBL "edge:"
253#define ELBLLEN (sizeof(EDGELBL)-1)
260setGlobalNodeAttr(
Agraph_t * g,
char *name,
char *value)
264 "Warning: global node attribute %s in graph %s does not begin with the prefix %s\n",
274setEdgeAttr(
Agedge_t * ep,
char *name,
char *value, userdata_t * ud,
280 if (strcmp(name,
"headport") == 0) {
281 if (ud->edgeinverted)
282 attrname =
"tailport";
284 attrname =
"headport";
288 }
else if (strcmp(name,
"tailport") == 0) {
289 if (ud->edgeinverted)
290 attrname =
"headport";
292 attrname =
"tailport";
314setGlobalEdgeAttr(
Agraph_t * g,
char *name,
char *value)
318 "Warning: global edge attribute %s in graph %s does not begin with the prefix %s\n",
328setGraphAttr(
Agraph_t * g,
char *name,
char *value, userdata_t * ud)
332 if ((g == root) && !strcmp(name,
"strict") && !strcmp(value,
"true")) {
334 }
else if (strcmp(name,
"name") == 0)
335 setName(ud->nameMap, (
Agobj_t *) g, value);
349static void setAttr(
char *name,
char *value, userdata_t * ud,
bool is_html)
351 switch (Current_class) {
353 setGraphAttr(
G, name, value, ud);
356 setNodeAttr(
N, name, value, ud, is_html);
359 setEdgeAttr(
E, name, value, ud, is_html);
369startElementHandler(
void *userData,
const char *name,
const char **atts)
372 userdata_t *ud = userData;
375 if (strcmp(name,
"gxl") == 0) {
377 }
else if (strcmp(name,
"graph") == 0) {
378 const char *edgeMode =
"";
381 Current_class = TAG_GRAPH;
382 if (ud->closedElementType == TAG_GRAPH) {
384 "Warning: Node contains more than one graph.\n");
386 pos = get_xml_attr(
"id", atts);
388 fprintf(stderr,
"Error: Graph has no ID attribute.\n");
391 const char *
id = atts[pos];
392 pos = get_xml_attr(
"edgemode", atts);
394 edgeMode = atts[pos];
397 if (graph_stack_is_empty(&Gstack)) {
398 if (strcmp(edgeMode,
"directed") == 0) {
400 }
else if (strcmp(edgeMode,
"undirected") == 0) {
404 "Warning: graph has no edgemode attribute");
405 fprintf(stderr,
" - assume directed\n");
411 if (isAnonGraph(
id)) {
412 static int anon_id = 1;
413 snprintf(buf,
sizeof(buf),
"%%%d", anon_id++);
416 subg =
agsubg(
G, (
char *)
id, 1);
420 pos = get_xml_attr(
"role", atts);
422 setGraphAttr(
G,
GXL_ROLE, (
char *) atts[pos], ud);
425 pos = get_xml_attr(
"hypergraph", atts);
427 setGraphAttr(
G,
GXL_HYPER, (
char *) atts[pos], ud);
430 }
else if (strcmp(name,
"node") == 0) {
431 Current_class = TAG_NODE;
432 pos = get_xml_attr(
"id", atts);
434 const char *attrname;
435 attrname = atts[pos];
437 if (attrname !=
NULL && strcmp(attrname,
"") != 0) {
442 }
else if (strcmp(name,
"edge") == 0) {
443 const char *
head =
"", *tail =
"";
447 Current_class = TAG_EDGE;
448 pos = get_xml_attr(
"from", atts);
451 pos = get_xml_attr(
"to", atts);
463 bind_edge(tail,
head);
468 if (strcmp(tname, tail) == 0) {
469 ud->edgeinverted =
false;
470 }
else if (strcmp(tname,
head) == 0) {
471 ud->edgeinverted =
true;
474 pos = get_xml_attr(
"fromorder", atts);
476 setEdgeAttr(
E,
GXL_FROM, (
char *) atts[pos], ud,
false);
479 pos = get_xml_attr(
"toorder", atts);
481 setEdgeAttr(
E,
GXL_TO, (
char *) atts[pos], ud,
false);
484 pos = get_xml_attr(
"id", atts);
486 setEdgeAttr(
E,
GXL_ID, (
char *) atts[pos], ud,
false);
488 }
else if (strcmp(name,
"attr") == 0) {
489 const char *attrname = atts[get_xml_attr(
"name", atts)];
491 agxbput(&ud->xml_attr_name, attrname);
492 pos = get_xml_attr(
"kind", atts);
495 if (strcmp(
"node", atts[pos]) == 0)
496 ud->globalAttrType = TAG_NODE;
497 else if (strcmp(
"edge", atts[pos]) == 0)
498 ud->globalAttrType = TAG_EDGE;
499 else if (strcmp(
"graph", atts[pos]) == 0)
500 ud->globalAttrType = TAG_GRAPH;
501 else if (strcmp(
"HTML-like string", atts[pos]) == 0)
502 ud->globalAttrType = TAG_HTML_LIKE_STRING;
504 ud->globalAttrType = TAG_NONE;
507 }
else if (strcmp(name,
"string") == 0
508 || strcmp(name,
"bool") == 0
509 || strcmp(name,
"int") == 0 || strcmp(name,
"float") == 0) {
512 if (ud->compositeReadState) {
513 agxbprint(&ud->composite_buffer,
"<%s>", name);
515 }
else if (strcmp(name,
"rel") == 0 || strcmp(name,
"relend") == 0) {
516 fprintf(stderr,
"%s element is ignored by DOT\n", name);
517 }
else if (strcmp(name,
"type") == 0) {
518 pos = get_xml_attr(
"xlink:href", atts);
522 }
else if (strcmp(name,
"locator") == 0) {
523 pos = get_xml_attr(
"xlink:href", atts);
525 const char *href = atts[pos];
528 }
else if (strcmp(name,
"seq") == 0
529 || strcmp(name,
"set") == 0
530 || strcmp(name,
"bag") == 0
531 || strcmp(name,
"tup") == 0 || strcmp(name,
"enum") == 0) {
533 ud->compositeReadState =
true;
534 agxbprint(&ud->composite_buffer,
"<%s>", name);
538 "Unknown node %s; DOT does not support extensions.\n",
543static void endElementHandler(
void *userData,
const char *name)
545 userdata_t *ud = userData;
547 if (strcmp(name,
"graph") == 0) {
549 ud->closedElementType = TAG_GRAPH;
550 }
else if (strcmp(name,
"node") == 0) {
551 Current_class = TAG_GRAPH;
553 ud->closedElementType = TAG_NODE;
554 }
else if (strcmp(name,
"edge") == 0) {
555 Current_class = TAG_GRAPH;
557 ud->closedElementType = TAG_EDGE;
558 ud->edgeinverted =
false;
559 }
else if (strcmp(name,
"attr") == 0) {
563 ud->closedElementType = TAG_NONE;
564 if (ud->compositeReadState) {
566 value =
agxbuse(&ud->composite_buffer);
568 ud->compositeReadState =
false;
571 value =
agxbuse(&ud->xml_attr_value);
574 switch (ud->globalAttrType) {
579 setGlobalNodeAttr(
G,
agxbuse(&new_name), value);
582 setGlobalEdgeAttr(
G,
agxbuse(&new_name), value);
585 setGraphAttr(
G,
agxbuse(&new_name), value, ud);
587 case TAG_HTML_LIKE_STRING:
594 ud->globalAttrType = TAG_NONE;
595 }
else if (strcmp(name,
"string") == 0
596 || strcmp(name,
"bool") == 0
597 || strcmp(name,
"int") == 0 || strcmp(name,
"float") == 0) {
599 if (ud->compositeReadState) {
600 agxbprint(&ud->composite_buffer,
"</%s>", name);
602 }
else if (strcmp(name,
"seq") == 0
603 || strcmp(name,
"set") == 0
604 || strcmp(name,
"bag") == 0
605 || strcmp(name,
"tup") == 0 || strcmp(name,
"enum") == 0) {
606 agxbprint(&ud->composite_buffer,
"</%s>", name);
610static void characterDataHandler(
void *userData,
const char *
s,
int length)
612 userdata_t *ud = userData;
614 assert(length >= 0 &&
"Expat returned negative length data");
615 size_t len = (size_t)length;
620 if (ud->compositeReadState) {
632 userdata_t udata = genUserdata();
633 XML_Parser parser = XML_ParserCreate(
NULL);
635 XML_SetUserData(parser, &udata);
636 XML_SetElementHandler(parser, startElementHandler, endElementHandler);
637 XML_SetCharacterDataHandler(parser, characterDataHandler);
639 Current_class = TAG_GRAPH;
642 size_t len = fread(buf, 1,
sizeof(buf), gxlFile);
645 done =
len <
sizeof(buf);
646 assert(
len <= INT_MAX &&
"too large data for Expat API");
650 XML_ErrorString(XML_GetErrorCode(parser)),
651 XML_GetCurrentLineNumber(parser));
655 XML_ParserFree(parser);
657 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)
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(Agraph_t *g, int kind, char *name, const char *value)
creates or looks up 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