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,
const char *uniqueName) {
147static int isAnonGraph(
const char *name)
153 return (*name ==
'\0');
174 fprintf(stderr,
"gxl2gv: Gstack underflow in graph parser\n");
188static Agnode_t *bind_node(
const char *name)
194static Agedge_t *bind_edge(
const char *tail,
const char *
head)
199 tailNode =
agnode(
G, (
char *) tail, 1);
201 E =
agedge(
G, tailNode, headNode, key, 1);
205static int get_xml_attr(
char *attrname,
const char **atts)
208 while (atts[count] !=
NULL) {
209 if (
streq(attrname, atts[count])) {
217static void setName(
Dt_t * names,
Agobj_t * n,
char *value)
221 char *
const oldName =
agxget(n, ap);
226static char *defval =
"";
229setNodeAttr(
Agnode_t * np,
char *name,
char *value, userdata_t * ud,
232 if (
streq(name,
"name")) {
233 setName(ud->nameMap, &np->
base, value);
246#define NODELBL "node:"
247#define NLBLLEN (sizeof(NODELBL)-1)
248#define EDGELBL "edge:"
249#define ELBLLEN (sizeof(EDGELBL)-1)
256setGlobalNodeAttr(
Agraph_t * g,
char *name,
char *value)
260 "Warning: global node attribute %s in graph %s does not begin with the prefix %s\n",
269static void setEdgeAttr(
Agedge_t *ep,
char *name,
const char *value,
270 userdata_t *ud,
bool is_html) {
274 if (
streq(name,
"headport")) {
275 if (ud->edgeinverted)
276 attrname =
"tailport";
278 attrname =
"headport";
282 }
else if (
streq(name,
"tailport")) {
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) &&
streq(name,
"strict") &&
streq(value,
"true")) {
328 }
else if (
streq(name,
"name"))
329 setName(ud->nameMap, &g->
base, 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 (
streq(name,
"gxl")) {
371 }
else if (
streq(name,
"graph")) {
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 (
streq(edgeMode,
"directed")) {
394 }
else if (
streq(edgeMode,
"undirected")) {
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 (
streq(name,
"node")) {
425 Current_class = TAG_NODE;
426 pos = get_xml_attr(
"id", atts);
428 const char *attrname;
429 attrname = atts[pos];
431 if (attrname !=
NULL && !
streq(attrname,
"")) {
436 }
else if (
streq(name,
"edge")) {
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 (
streq(tname, tail)) {
463 ud->edgeinverted =
false;
465 ud->edgeinverted =
true;
468 pos = get_xml_attr(
"fromorder", atts);
470 setEdgeAttr(
E,
GXL_FROM, atts[pos], ud,
false);
473 pos = get_xml_attr(
"toorder", atts);
475 setEdgeAttr(
E,
GXL_TO, atts[pos], ud,
false);
478 pos = get_xml_attr(
"id", atts);
480 setEdgeAttr(
E,
GXL_ID, atts[pos], ud,
false);
482 }
else if (
streq(name,
"attr")) {
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 (
streq(
"node", atts[pos]))
490 ud->globalAttrType = TAG_NODE;
491 else if (
streq(
"edge", atts[pos]))
492 ud->globalAttrType = TAG_EDGE;
493 else if (
streq(
"graph", atts[pos]))
494 ud->globalAttrType = TAG_GRAPH;
495 else if (
streq(
"HTML-like string", atts[pos]))
496 ud->globalAttrType = TAG_HTML_LIKE_STRING;
498 ud->globalAttrType = TAG_NONE;
501 }
else if (
streq(name,
"string")
502 ||
streq(name,
"bool")
503 ||
streq(name,
"int") ||
streq(name,
"float")) {
506 if (ud->compositeReadState) {
507 agxbprint(&ud->composite_buffer,
"<%s>", name);
509 }
else if (
streq(name,
"rel") ||
streq(name,
"relend")) {
510 fprintf(stderr,
"%s element is ignored by DOT\n", name);
511 }
else if (
streq(name,
"type")) {
512 pos = get_xml_attr(
"xlink:href", atts);
516 }
else if (
streq(name,
"locator")) {
517 pos = get_xml_attr(
"xlink:href", atts);
519 const char *href = atts[pos];
522 }
else if (
streq(name,
"seq")
523 ||
streq(name,
"set")
524 ||
streq(name,
"bag")
525 ||
streq(name,
"tup") ||
streq(name,
"enum")) {
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 (
streq(name,
"graph")) {
543 ud->closedElementType = TAG_GRAPH;
544 }
else if (
streq(name,
"node")) {
545 Current_class = TAG_GRAPH;
547 ud->closedElementType = TAG_NODE;
548 }
else if (
streq(name,
"edge")) {
549 Current_class = TAG_GRAPH;
551 ud->closedElementType = TAG_EDGE;
552 ud->edgeinverted =
false;
553 }
else if (
streq(name,
"attr")) {
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 (
streq(name,
"string")
590 ||
streq(name,
"bool")
591 ||
streq(name,
"int") ||
streq(name,
"float")) {
593 if (ud->compositeReadState) {
594 agxbprint(&ud->composite_buffer,
"</%s>", name);
596 }
else if (
streq(name,
"seq")
597 ||
streq(name,
"set")
598 ||
streq(name,
"bag")
599 ||
streq(name,
"tup") ||
streq(name,
"enum")) {
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);
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