33#ifndef XML_STATUS_ERROR
34#define XML_STATUS_ERROR 0
39#define GXL_ATTR "_gxl_"
40#define GXL_ID "_gxl_id"
41#define GXL_ROLE "_gxl_role"
42#define GXL_HYPER "_gxl_hypergraph"
43#define GXL_FROM "_gxl_fromorder"
44#define GXL_TO "_gxl_toorder"
45#define GXL_TYPE "_gxl_type"
46#define GXL_COMP "_gxl_composite_"
47#define GXL_LOC "_gxl_locator_"
64 bool compositeReadState;
70static attr_t Current_class;
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');
173 fprintf(stderr,
"gxl2gv: Gstack underflow in graph parser\n");
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 (
streq(attrname, atts[count])) {
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 (
streq(name,
"name")) {
232 setName(ud->nameMap, &np->
base, 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 (
streq(name,
"headport")) {
276 if (ud->edgeinverted)
277 attrname =
"tailport";
279 attrname =
"headport";
283 }
else if (
streq(name,
"tailport")) {
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) &&
streq(name,
"strict") &&
streq(value,
"true")) {
329 }
else if (
streq(name,
"name"))
330 setName(ud->nameMap, &g->
base, 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 (
streq(name,
"gxl")) {
372 }
else if (
streq(name,
"graph")) {
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];
393 if (
streq(edgeMode,
"directed")) {
395 }
else if (
streq(edgeMode,
"undirected")) {
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 (
streq(name,
"node")) {
426 Current_class = TAG_NODE;
427 pos = get_xml_attr(
"id", atts);
429 const char *attrname;
430 attrname = atts[pos];
432 if (attrname !=
NULL && !
streq(attrname,
"")) {
437 }
else if (
streq(name,
"edge")) {
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 (
streq(tname, tail)) {
464 ud->edgeinverted =
false;
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 (
streq(name,
"attr")) {
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 (
streq(
"node", atts[pos]))
491 ud->globalAttrType = TAG_NODE;
492 else if (
streq(
"edge", atts[pos]))
493 ud->globalAttrType = TAG_EDGE;
494 else if (
streq(
"graph", atts[pos]))
495 ud->globalAttrType = TAG_GRAPH;
496 else if (
streq(
"HTML-like string", atts[pos]))
497 ud->globalAttrType = TAG_HTML_LIKE_STRING;
499 ud->globalAttrType = TAG_NONE;
502 }
else if (
streq(name,
"string")
503 ||
streq(name,
"bool")
504 ||
streq(name,
"int") ||
streq(name,
"float")) {
507 if (ud->compositeReadState) {
508 agxbprint(&ud->composite_buffer,
"<%s>", name);
510 }
else if (
streq(name,
"rel") ||
streq(name,
"relend")) {
511 fprintf(stderr,
"%s element is ignored by DOT\n", name);
512 }
else if (
streq(name,
"type")) {
513 pos = get_xml_attr(
"xlink:href", atts);
517 }
else if (
streq(name,
"locator")) {
518 pos = get_xml_attr(
"xlink:href", atts);
520 const char *href = atts[pos];
523 }
else if (
streq(name,
"seq")
524 ||
streq(name,
"set")
525 ||
streq(name,
"bag")
526 ||
streq(name,
"tup") ||
streq(name,
"enum")) {
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 (
streq(name,
"graph")) {
544 ud->closedElementType = TAG_GRAPH;
545 }
else if (
streq(name,
"node")) {
546 Current_class = TAG_GRAPH;
548 ud->closedElementType = TAG_NODE;
549 }
else if (
streq(name,
"edge")) {
550 Current_class = TAG_GRAPH;
552 ud->closedElementType = TAG_EDGE;
553 ud->edgeinverted =
false;
554 }
else if (
streq(name,
"attr")) {
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 (
streq(name,
"string")
591 ||
streq(name,
"bool")
592 ||
streq(name,
"int") ||
streq(name,
"float")) {
594 if (ud->compositeReadState) {
595 agxbprint(&ud->composite_buffer,
"</%s>", name);
597 }
else if (
streq(name,
"seq")
598 ||
streq(name,
"set")
599 ||
streq(name,
"bag")
600 ||
streq(name,
"tup") ||
streq(name,
"enum")) {
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);
Dynamically expanding string buffers.
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)
type-generic dynamically expanding list
#define LIST_POP_BACK(list)
#define LIST_IS_EMPTY(list)
#define LIST_PUSH_BACK(list, item)
static bool startswith(const char *s, const char *prefix)
does the string s begin with the string prefix?
static bool streq(const char *a, const char *b)
are a and b equal?
a generic header of Agraph_s, Agnode_s and Agedge_s