35#ifndef XML_STATUS_ERROR
36#define XML_STATUS_ERROR 0
41#define GXL_ATTR "_gxl_"
42#define GXL_ID "_gxl_id"
43#define GXL_ROLE "_gxl_role"
44#define GXL_HYPER "_gxl_hypergraph"
45#define GXL_FROM "_gxl_fromorder"
46#define GXL_TO "_gxl_toorder"
47#define GXL_TYPE "_gxl_type"
48#define GXL_COMP "_gxl_composite_"
49#define GXL_LOC "_gxl_locator_"
66 bool compositeReadState;
72static attr_t Current_class;
97static void free_nitem(
void *name) {
106 .link = offsetof(
namev_t, link),
111static userdata_t genUserdata(
void) {
112 userdata_t user = {0};
114 user.closedElementType = TAG_NONE;
115 user.globalAttrType = TAG_NONE;
116 user.compositeReadState =
false;
117 user.edgeinverted =
false;
122static void freeUserdata(userdata_t ud) {
129static void addToMap(
Dt_t * map,
char *name,
char *uniqueName)
148static int isAnonGraph(
const char *name)
154 return (*name ==
'\0');
175 fprintf(stderr,
"gxl2gv: Gstack underflow in graph parser\n");
189static Agnode_t *bind_node(
const char *name)
195static Agedge_t *bind_edge(
const char *tail,
const char *
head)
200 tailNode =
agnode(
G, (
char *) tail, 1);
202 E =
agedge(
G, tailNode, headNode, key, 1);
206static int get_xml_attr(
char *attrname,
const char **atts)
209 while (atts[count] !=
NULL) {
210 if (
streq(attrname, atts[count])) {
218static void setName(
Dt_t * names,
Agobj_t * n,
char *value)
222 char *
const oldName =
agxget(n, ap);
227static char *defval =
"";
230setNodeAttr(
Agnode_t * np,
char *name,
char *value, userdata_t * ud,
233 if (
streq(name,
"name")) {
234 setName(ud->nameMap, &np->
base, value);
247#define NODELBL "node:"
248#define NLBLLEN (sizeof(NODELBL)-1)
249#define EDGELBL "edge:"
250#define ELBLLEN (sizeof(EDGELBL)-1)
257setGlobalNodeAttr(
Agraph_t * g,
char *name,
char *value)
261 "Warning: global node attribute %s in graph %s does not begin with the prefix %s\n",
271setEdgeAttr(
Agedge_t * ep,
char *name,
char *value, userdata_t * ud,
277 if (
streq(name,
"headport")) {
278 if (ud->edgeinverted)
279 attrname =
"tailport";
281 attrname =
"headport";
285 }
else if (
streq(name,
"tailport")) {
286 if (ud->edgeinverted)
287 attrname =
"headport";
289 attrname =
"tailport";
311setGlobalEdgeAttr(
Agraph_t * g,
char *name,
char *value)
315 "Warning: global edge attribute %s in graph %s does not begin with the prefix %s\n",
325setGraphAttr(
Agraph_t * g,
char *name,
char *value, userdata_t * ud)
329 if ((g == root) &&
streq(name,
"strict") &&
streq(value,
"true")) {
331 }
else if (
streq(name,
"name"))
332 setName(ud->nameMap, &g->
base, value);
346static void setAttr(
char *name,
char *value, userdata_t * ud,
bool is_html)
348 switch (Current_class) {
350 setGraphAttr(
G, name, value, ud);
353 setNodeAttr(
N, name, value, ud, is_html);
356 setEdgeAttr(
E, name, value, ud, is_html);
366startElementHandler(
void *userData,
const char *name,
const char **atts)
369 userdata_t *ud = userData;
372 if (
streq(name,
"gxl")) {
374 }
else if (
streq(name,
"graph")) {
375 const char *edgeMode =
"";
378 Current_class = TAG_GRAPH;
379 if (ud->closedElementType == TAG_GRAPH) {
381 "Warning: Node contains more than one graph.\n");
383 pos = get_xml_attr(
"id", atts);
385 fprintf(stderr,
"Error: Graph has no ID attribute.\n");
388 const char *
id = atts[pos];
389 pos = get_xml_attr(
"edgemode", atts);
391 edgeMode = atts[pos];
395 if (
streq(edgeMode,
"directed")) {
397 }
else if (
streq(edgeMode,
"undirected")) {
401 "Warning: graph has no edgemode attribute");
402 fprintf(stderr,
" - assume directed\n");
408 if (isAnonGraph(
id)) {
409 static int anon_id = 1;
410 snprintf(buf,
sizeof(buf),
"%%%d", anon_id++);
413 subg =
agsubg(
G, (
char *)
id, 1);
417 pos = get_xml_attr(
"role", atts);
419 setGraphAttr(
G,
GXL_ROLE, (
char *) atts[pos], ud);
422 pos = get_xml_attr(
"hypergraph", atts);
424 setGraphAttr(
G,
GXL_HYPER, (
char *) atts[pos], ud);
427 }
else if (
streq(name,
"node")) {
428 Current_class = TAG_NODE;
429 pos = get_xml_attr(
"id", atts);
431 const char *attrname;
432 attrname = atts[pos];
434 if (attrname !=
NULL && !
streq(attrname,
"")) {
439 }
else if (
streq(name,
"edge")) {
440 const char *
head =
"", *tail =
"";
444 Current_class = TAG_EDGE;
445 pos = get_xml_attr(
"from", atts);
448 pos = get_xml_attr(
"to", atts);
460 bind_edge(tail,
head);
465 if (
streq(tname, tail)) {
466 ud->edgeinverted =
false;
468 ud->edgeinverted =
true;
471 pos = get_xml_attr(
"fromorder", atts);
473 setEdgeAttr(
E,
GXL_FROM, (
char *) atts[pos], ud,
false);
476 pos = get_xml_attr(
"toorder", atts);
478 setEdgeAttr(
E,
GXL_TO, (
char *) atts[pos], ud,
false);
481 pos = get_xml_attr(
"id", atts);
483 setEdgeAttr(
E,
GXL_ID, (
char *) atts[pos], ud,
false);
485 }
else if (
streq(name,
"attr")) {
486 const char *attrname = atts[get_xml_attr(
"name", atts)];
488 agxbput(&ud->xml_attr_name, attrname);
489 pos = get_xml_attr(
"kind", atts);
492 if (
streq(
"node", atts[pos]))
493 ud->globalAttrType = TAG_NODE;
494 else if (
streq(
"edge", atts[pos]))
495 ud->globalAttrType = TAG_EDGE;
496 else if (
streq(
"graph", atts[pos]))
497 ud->globalAttrType = TAG_GRAPH;
498 else if (
streq(
"HTML-like string", atts[pos]))
499 ud->globalAttrType = TAG_HTML_LIKE_STRING;
501 ud->globalAttrType = TAG_NONE;
504 }
else if (
streq(name,
"string")
505 ||
streq(name,
"bool")
506 ||
streq(name,
"int") ||
streq(name,
"float")) {
509 if (ud->compositeReadState) {
510 agxbprint(&ud->composite_buffer,
"<%s>", name);
512 }
else if (
streq(name,
"rel") ||
streq(name,
"relend")) {
513 fprintf(stderr,
"%s element is ignored by DOT\n", name);
514 }
else if (
streq(name,
"type")) {
515 pos = get_xml_attr(
"xlink:href", atts);
519 }
else if (
streq(name,
"locator")) {
520 pos = get_xml_attr(
"xlink:href", atts);
522 const char *href = atts[pos];
525 }
else if (
streq(name,
"seq")
526 ||
streq(name,
"set")
527 ||
streq(name,
"bag")
528 ||
streq(name,
"tup") ||
streq(name,
"enum")) {
530 ud->compositeReadState =
true;
531 agxbprint(&ud->composite_buffer,
"<%s>", name);
535 "Unknown node %s; DOT does not support extensions.\n",
540static void endElementHandler(
void *userData,
const char *name)
542 userdata_t *ud = userData;
544 if (
streq(name,
"graph")) {
546 ud->closedElementType = TAG_GRAPH;
547 }
else if (
streq(name,
"node")) {
548 Current_class = TAG_GRAPH;
550 ud->closedElementType = TAG_NODE;
551 }
else if (
streq(name,
"edge")) {
552 Current_class = TAG_GRAPH;
554 ud->closedElementType = TAG_EDGE;
555 ud->edgeinverted =
false;
556 }
else if (
streq(name,
"attr")) {
560 ud->closedElementType = TAG_NONE;
561 if (ud->compositeReadState) {
563 value =
agxbuse(&ud->composite_buffer);
565 ud->compositeReadState =
false;
568 value =
agxbuse(&ud->xml_attr_value);
571 switch (ud->globalAttrType) {
576 setGlobalNodeAttr(
G,
agxbuse(&new_name), value);
579 setGlobalEdgeAttr(
G,
agxbuse(&new_name), value);
582 setGraphAttr(
G,
agxbuse(&new_name), value, ud);
584 case TAG_HTML_LIKE_STRING:
591 ud->globalAttrType = TAG_NONE;
592 }
else if (
streq(name,
"string")
593 ||
streq(name,
"bool")
594 ||
streq(name,
"int") ||
streq(name,
"float")) {
596 if (ud->compositeReadState) {
597 agxbprint(&ud->composite_buffer,
"</%s>", name);
599 }
else if (
streq(name,
"seq")
600 ||
streq(name,
"set")
601 ||
streq(name,
"bag")
602 ||
streq(name,
"tup") ||
streq(name,
"enum")) {
603 agxbprint(&ud->composite_buffer,
"</%s>", name);
607static void characterDataHandler(
void *userData,
const char *
s,
int length)
609 userdata_t *ud = userData;
611 assert(
length >= 0 &&
"Expat returned negative length data");
617 if (ud->compositeReadState) {
629 userdata_t udata = genUserdata();
630 XML_Parser parser = XML_ParserCreate(
NULL);
632 XML_SetUserData(parser, &udata);
633 XML_SetElementHandler(parser, startElementHandler, endElementHandler);
634 XML_SetCharacterDataHandler(parser, characterDataHandler);
636 Current_class = TAG_GRAPH;
639 size_t len = fread(buf, 1,
sizeof(buf), gxlFile);
642 done =
len <
sizeof(buf);
643 assert(
len <= INT_MAX &&
"too large data for Expat API");
647 XML_ErrorString(XML_GetErrorCode(parser)),
648 XML_GetCurrentLineNumber(parser));
652 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