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;
94static void free_nitem(
void *name) {
103 .link = offsetof(
namev_t, link),
108static userdata_t genUserdata(
void) {
109 userdata_t user = {0};
111 user.closedElementType = TAG_NONE;
112 user.globalAttrType = TAG_NONE;
113 user.compositeReadState =
false;
114 user.edgeinverted =
false;
119static void freeUserdata(userdata_t ud) {
126static void addToMap(
Dt_t * map,
char *name,
char *uniqueName)
145static int isAnonGraph(
const char *name)
151 return (*name ==
'\0');
172 fprintf(stderr,
"gxl2gv: Gstack underflow in graph parser\n");
186static Agnode_t *bind_node(
const char *name)
192static Agedge_t *bind_edge(
const char *tail,
const char *
head)
197 tailNode =
agnode(
G, (
char *) tail, 1);
199 E =
agedge(
G, tailNode, headNode, key, 1);
203static int get_xml_attr(
char *attrname,
const char **atts)
206 while (atts[count] !=
NULL) {
207 if (strcmp(attrname, atts[count]) == 0) {
215static void setName(
Dt_t * names,
Agobj_t * n,
char *value)
219 char *
const oldName =
agxget(n, ap);
224static char *defval =
"";
227setNodeAttr(
Agnode_t * np,
char *name,
char *value, userdata_t * ud,
230 if (strcmp(name,
"name") == 0) {
231 setName(ud->nameMap, (
Agobj_t *) np, value);
244#define NODELBL "node:"
245#define NLBLLEN (sizeof(NODELBL)-1)
246#define EDGELBL "edge:"
247#define ELBLLEN (sizeof(EDGELBL)-1)
254setGlobalNodeAttr(
Agraph_t * g,
char *name,
char *value)
258 "Warning: global node attribute %s in graph %s does not begin with the prefix %s\n",
268setEdgeAttr(
Agedge_t * ep,
char *name,
char *value, userdata_t * ud,
274 if (strcmp(name,
"headport") == 0) {
275 if (ud->edgeinverted)
276 attrname =
"tailport";
278 attrname =
"headport";
282 }
else if (strcmp(name,
"tailport") == 0) {
283 if (ud->edgeinverted)
284 attrname =
"headport";
286 attrname =
"tailport";
308setGlobalEdgeAttr(
Agraph_t * g,
char *name,
char *value)
312 "Warning: global edge attribute %s in graph %s does not begin with the prefix %s\n",
322setGraphAttr(
Agraph_t * g,
char *name,
char *value, userdata_t * ud)
326 if ((g == root) && !strcmp(name,
"strict") && !strcmp(value,
"true")) {
328 }
else if (strcmp(name,
"name") == 0)
329 setName(ud->nameMap, (
Agobj_t *) g, value);
343static void setAttr(
char *name,
char *value, userdata_t * ud,
bool is_html)
345 switch (Current_class) {
347 setGraphAttr(
G, name, value, ud);
350 setNodeAttr(
N, name, value, ud, is_html);
353 setEdgeAttr(
E, name, value, ud, is_html);
363startElementHandler(
void *userData,
const char *name,
const char **atts)
366 userdata_t *ud = userData;
369 if (strcmp(name,
"gxl") == 0) {
371 }
else if (strcmp(name,
"graph") == 0) {
372 const char *edgeMode =
"";
375 Current_class = TAG_GRAPH;
376 if (ud->closedElementType == TAG_GRAPH) {
378 "Warning: Node contains more than one graph.\n");
380 pos = get_xml_attr(
"id", atts);
382 fprintf(stderr,
"Error: Graph has no ID attribute.\n");
385 const char *
id = atts[pos];
386 pos = get_xml_attr(
"edgemode", atts);
388 edgeMode = atts[pos];
392 if (strcmp(edgeMode,
"directed") == 0) {
394 }
else if (strcmp(edgeMode,
"undirected") == 0) {
398 "Warning: graph has no edgemode attribute");
399 fprintf(stderr,
" - assume directed\n");
405 if (isAnonGraph(
id)) {
406 static int anon_id = 1;
407 snprintf(buf,
sizeof(buf),
"%%%d", anon_id++);
410 subg =
agsubg(
G, (
char *)
id, 1);
414 pos = get_xml_attr(
"role", atts);
416 setGraphAttr(
G,
GXL_ROLE, (
char *) atts[pos], ud);
419 pos = get_xml_attr(
"hypergraph", atts);
421 setGraphAttr(
G,
GXL_HYPER, (
char *) atts[pos], ud);
424 }
else if (strcmp(name,
"node") == 0) {
425 Current_class = TAG_NODE;
426 pos = get_xml_attr(
"id", atts);
428 const char *attrname;
429 attrname = atts[pos];
431 if (attrname !=
NULL && strcmp(attrname,
"") != 0) {
436 }
else if (strcmp(name,
"edge") == 0) {
437 const char *
head =
"", *tail =
"";
441 Current_class = TAG_EDGE;
442 pos = get_xml_attr(
"from", atts);
445 pos = get_xml_attr(
"to", atts);
457 bind_edge(tail,
head);
462 if (strcmp(tname, tail) == 0) {
463 ud->edgeinverted =
false;
464 }
else if (strcmp(tname,
head) == 0) {
465 ud->edgeinverted =
true;
468 pos = get_xml_attr(
"fromorder", atts);
470 setEdgeAttr(
E,
GXL_FROM, (
char *) atts[pos], ud,
false);
473 pos = get_xml_attr(
"toorder", atts);
475 setEdgeAttr(
E,
GXL_TO, (
char *) atts[pos], ud,
false);
478 pos = get_xml_attr(
"id", atts);
480 setEdgeAttr(
E,
GXL_ID, (
char *) atts[pos], ud,
false);
482 }
else if (strcmp(name,
"attr") == 0) {
483 const char *attrname = atts[get_xml_attr(
"name", atts)];
485 agxbput(&ud->xml_attr_name, attrname);
486 pos = get_xml_attr(
"kind", atts);
489 if (strcmp(
"node", atts[pos]) == 0)
490 ud->globalAttrType = TAG_NODE;
491 else if (strcmp(
"edge", atts[pos]) == 0)
492 ud->globalAttrType = TAG_EDGE;
493 else if (strcmp(
"graph", atts[pos]) == 0)
494 ud->globalAttrType = TAG_GRAPH;
495 else if (strcmp(
"HTML-like string", atts[pos]) == 0)
496 ud->globalAttrType = TAG_HTML_LIKE_STRING;
498 ud->globalAttrType = TAG_NONE;
501 }
else if (strcmp(name,
"string") == 0
502 || strcmp(name,
"bool") == 0
503 || strcmp(name,
"int") == 0 || strcmp(name,
"float") == 0) {
506 if (ud->compositeReadState) {
507 agxbprint(&ud->composite_buffer,
"<%s>", name);
509 }
else if (strcmp(name,
"rel") == 0 || strcmp(name,
"relend") == 0) {
510 fprintf(stderr,
"%s element is ignored by DOT\n", name);
511 }
else if (strcmp(name,
"type") == 0) {
512 pos = get_xml_attr(
"xlink:href", atts);
516 }
else if (strcmp(name,
"locator") == 0) {
517 pos = get_xml_attr(
"xlink:href", atts);
519 const char *href = atts[pos];
522 }
else if (strcmp(name,
"seq") == 0
523 || strcmp(name,
"set") == 0
524 || strcmp(name,
"bag") == 0
525 || strcmp(name,
"tup") == 0 || strcmp(name,
"enum") == 0) {
527 ud->compositeReadState =
true;
528 agxbprint(&ud->composite_buffer,
"<%s>", name);
532 "Unknown node %s; DOT does not support extensions.\n",
537static void endElementHandler(
void *userData,
const char *name)
539 userdata_t *ud = userData;
541 if (strcmp(name,
"graph") == 0) {
543 ud->closedElementType = TAG_GRAPH;
544 }
else if (strcmp(name,
"node") == 0) {
545 Current_class = TAG_GRAPH;
547 ud->closedElementType = TAG_NODE;
548 }
else if (strcmp(name,
"edge") == 0) {
549 Current_class = TAG_GRAPH;
551 ud->closedElementType = TAG_EDGE;
552 ud->edgeinverted =
false;
553 }
else if (strcmp(name,
"attr") == 0) {
557 ud->closedElementType = TAG_NONE;
558 if (ud->compositeReadState) {
560 value =
agxbuse(&ud->composite_buffer);
562 ud->compositeReadState =
false;
565 value =
agxbuse(&ud->xml_attr_value);
568 switch (ud->globalAttrType) {
573 setGlobalNodeAttr(
G,
agxbuse(&new_name), value);
576 setGlobalEdgeAttr(
G,
agxbuse(&new_name), value);
579 setGraphAttr(
G,
agxbuse(&new_name), value, ud);
581 case TAG_HTML_LIKE_STRING:
588 ud->globalAttrType = TAG_NONE;
589 }
else if (strcmp(name,
"string") == 0
590 || strcmp(name,
"bool") == 0
591 || strcmp(name,
"int") == 0 || strcmp(name,
"float") == 0) {
593 if (ud->compositeReadState) {
594 agxbprint(&ud->composite_buffer,
"</%s>", name);
596 }
else if (strcmp(name,
"seq") == 0
597 || strcmp(name,
"set") == 0
598 || strcmp(name,
"bag") == 0
599 || strcmp(name,
"tup") == 0 || strcmp(name,
"enum") == 0) {
600 agxbprint(&ud->composite_buffer,
"</%s>", name);
604static void characterDataHandler(
void *userData,
const char *
s,
int length)
606 userdata_t *ud = userData;
608 assert(
length >= 0 &&
"Expat returned negative length data");
614 if (ud->compositeReadState) {
626 userdata_t udata = genUserdata();
627 XML_Parser parser = XML_ParserCreate(
NULL);
629 XML_SetUserData(parser, &udata);
630 XML_SetElementHandler(parser, startElementHandler, endElementHandler);
631 XML_SetCharacterDataHandler(parser, characterDataHandler);
633 Current_class = TAG_GRAPH;
636 size_t len = fread(buf, 1,
sizeof(buf), gxlFile);
639 done =
len <
sizeof(buf);
640 assert(
len <= INT_MAX &&
"too large data for Expat API");
644 XML_ErrorString(XML_GetErrorCode(parser)),
645 XML_GetCurrentLineNumber(parser));
649 XML_ParserFree(parser);
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?
a generic header of Agraph_s, Agnode_s and Agedge_s