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);
252#define NODELBL "node:"
253#define NLBLLEN (sizeof(NODELBL)-1)
254#define EDGELBL "edge:"
255#define ELBLLEN (sizeof(EDGELBL)-1)
262setGlobalNodeAttr(
Agraph_t * g,
char *name,
char *value)
266 "Warning: global node attribute %s in graph %s does not begin with the prefix %s\n",
276setEdgeAttr(
Agedge_t * ep,
char *name,
char *value, userdata_t * ud,
282 if (strcmp(name,
"headport") == 0) {
283 if (ud->edgeinverted)
284 attrname =
"tailport";
286 attrname =
"headport";
290 }
else if (strcmp(name,
"tailport") == 0) {
291 if (ud->edgeinverted)
292 attrname =
"headport";
294 attrname =
"tailport";
318setGlobalEdgeAttr(
Agraph_t * g,
char *name,
char *value)
322 "Warning: global edge attribute %s in graph %s does not begin with the prefix %s\n",
332setGraphAttr(
Agraph_t * g,
char *name,
char *value, userdata_t * ud)
336 if ((g == root) && !strcmp(name,
"strict") && !strcmp(value,
"true")) {
338 }
else if (strcmp(name,
"name") == 0)
339 setName(ud->nameMap, (
Agobj_t *) g, value);
353static void setAttr(
char *name,
char *value, userdata_t * ud,
bool is_html)
355 switch (Current_class) {
357 setGraphAttr(
G, name, value, ud);
360 setNodeAttr(
N, name, value, ud, is_html);
363 setEdgeAttr(
E, name, value, ud, is_html);
373startElementHandler(
void *userData,
const char *name,
const char **atts)
376 userdata_t *ud = userData;
379 if (strcmp(name,
"gxl") == 0) {
381 }
else if (strcmp(name,
"graph") == 0) {
382 const char *edgeMode =
"";
385 Current_class = TAG_GRAPH;
386 if (ud->closedElementType == TAG_GRAPH) {
388 "Warning: Node contains more than one graph.\n");
390 pos = get_xml_attr(
"id", atts);
392 fprintf(stderr,
"Error: Graph has no ID attribute.\n");
395 const char *
id = atts[pos];
396 pos = get_xml_attr(
"edgemode", atts);
398 edgeMode = atts[pos];
401 if (graph_stack_is_empty(&Gstack)) {
402 if (strcmp(edgeMode,
"directed") == 0) {
404 }
else if (strcmp(edgeMode,
"undirected") == 0) {
408 "Warning: graph has no edgemode attribute");
409 fprintf(stderr,
" - assume directed\n");
415 if (isAnonGraph(
id)) {
416 static int anon_id = 1;
417 snprintf(buf,
sizeof(buf),
"%%%d", anon_id++);
420 subg =
agsubg(
G, (
char *)
id, 1);
424 pos = get_xml_attr(
"role", atts);
426 setGraphAttr(
G,
GXL_ROLE, (
char *) atts[pos], ud);
429 pos = get_xml_attr(
"hypergraph", atts);
431 setGraphAttr(
G,
GXL_HYPER, (
char *) atts[pos], ud);
434 }
else if (strcmp(name,
"node") == 0) {
435 Current_class = TAG_NODE;
436 pos = get_xml_attr(
"id", atts);
438 const char *attrname;
439 attrname = atts[pos];
441 if (attrname !=
NULL && strcmp(attrname,
"") != 0) {
446 }
else if (strcmp(name,
"edge") == 0) {
447 const char *
head =
"", *tail =
"";
451 Current_class = TAG_EDGE;
452 pos = get_xml_attr(
"from", atts);
455 pos = get_xml_attr(
"to", atts);
467 bind_edge(tail,
head);
472 if (strcmp(tname, tail) == 0) {
473 ud->edgeinverted =
false;
474 }
else if (strcmp(tname,
head) == 0) {
475 ud->edgeinverted =
true;
478 pos = get_xml_attr(
"fromorder", atts);
480 setEdgeAttr(
E,
GXL_FROM, (
char *) atts[pos], ud,
false);
483 pos = get_xml_attr(
"toorder", atts);
485 setEdgeAttr(
E,
GXL_TO, (
char *) atts[pos], ud,
false);
488 pos = get_xml_attr(
"id", atts);
490 setEdgeAttr(
E,
GXL_ID, (
char *) atts[pos], ud,
false);
492 }
else if (strcmp(name,
"attr") == 0) {
493 const char *attrname = atts[get_xml_attr(
"name", atts)];
495 agxbput(&ud->xml_attr_name, attrname);
496 pos = get_xml_attr(
"kind", atts);
499 if (strcmp(
"node", atts[pos]) == 0)
500 ud->globalAttrType = TAG_NODE;
501 else if (strcmp(
"edge", atts[pos]) == 0)
502 ud->globalAttrType = TAG_EDGE;
503 else if (strcmp(
"graph", atts[pos]) == 0)
504 ud->globalAttrType = TAG_GRAPH;
505 else if (strcmp(
"HTML-like string", atts[pos]) == 0)
506 ud->globalAttrType = TAG_HTML_LIKE_STRING;
508 ud->globalAttrType = TAG_NONE;
511 }
else if (strcmp(name,
"string") == 0
512 || strcmp(name,
"bool") == 0
513 || strcmp(name,
"int") == 0 || strcmp(name,
"float") == 0) {
516 if (ud->compositeReadState) {
517 agxbprint(&ud->composite_buffer,
"<%s>", name);
519 }
else if (strcmp(name,
"rel") == 0 || strcmp(name,
"relend") == 0) {
520 fprintf(stderr,
"%s element is ignored by DOT\n", name);
521 }
else if (strcmp(name,
"type") == 0) {
522 pos = get_xml_attr(
"xlink:href", atts);
526 }
else if (strcmp(name,
"locator") == 0) {
527 pos = get_xml_attr(
"xlink:href", atts);
529 const char *href = atts[pos];
532 }
else if (strcmp(name,
"seq") == 0
533 || strcmp(name,
"set") == 0
534 || strcmp(name,
"bag") == 0
535 || strcmp(name,
"tup") == 0 || strcmp(name,
"enum") == 0) {
537 ud->compositeReadState =
true;
538 agxbprint(&ud->composite_buffer,
"<%s>", name);
542 "Unknown node %s; DOT does not support extensions.\n",
547static void endElementHandler(
void *userData,
const char *name)
549 userdata_t *ud = userData;
551 if (strcmp(name,
"graph") == 0) {
553 ud->closedElementType = TAG_GRAPH;
554 }
else if (strcmp(name,
"node") == 0) {
555 Current_class = TAG_GRAPH;
557 ud->closedElementType = TAG_NODE;
558 }
else if (strcmp(name,
"edge") == 0) {
559 Current_class = TAG_GRAPH;
561 ud->closedElementType = TAG_EDGE;
562 ud->edgeinverted =
false;
563 }
else if (strcmp(name,
"attr") == 0) {
567 ud->closedElementType = TAG_NONE;
568 if (ud->compositeReadState) {
570 value =
agxbuse(&ud->composite_buffer);
572 ud->compositeReadState =
false;
575 value =
agxbuse(&ud->xml_attr_value);
578 switch (ud->globalAttrType) {
583 setGlobalNodeAttr(
G,
agxbuse(&new_name), value);
586 setGlobalEdgeAttr(
G,
agxbuse(&new_name), value);
589 setGraphAttr(
G,
agxbuse(&new_name), value, ud);
591 case TAG_HTML_LIKE_STRING:
598 ud->globalAttrType = TAG_NONE;
599 }
else if (strcmp(name,
"string") == 0
600 || strcmp(name,
"bool") == 0
601 || strcmp(name,
"int") == 0 || strcmp(name,
"float") == 0) {
603 if (ud->compositeReadState) {
604 agxbprint(&ud->composite_buffer,
"</%s>", name);
606 }
else if (strcmp(name,
"seq") == 0
607 || strcmp(name,
"set") == 0
608 || strcmp(name,
"bag") == 0
609 || strcmp(name,
"tup") == 0 || strcmp(name,
"enum") == 0) {
610 agxbprint(&ud->composite_buffer,
"</%s>", name);
614static void characterDataHandler(
void *userData,
const char *
s,
int length)
616 userdata_t *ud = userData;
618 assert(length >= 0 &&
"Expat returned negative length data");
619 size_t len = (size_t)length;
624 if (ud->compositeReadState) {
636 userdata_t udata = genUserdata();
637 XML_Parser parser = XML_ParserCreate(
NULL);
639 XML_SetUserData(parser, &udata);
640 XML_SetElementHandler(parser, startElementHandler, endElementHandler);
641 XML_SetCharacterDataHandler(parser, characterDataHandler);
643 Current_class = TAG_GRAPH;
646 size_t len = fread(buf, 1,
sizeof(buf), gxlFile);
649 done =
len <
sizeof(buf);
650 assert(
len <= (
size_t)INT_MAX &&
"too large data for Expat API");
654 XML_ErrorString(XML_GetErrorCode(parser)),
655 XML_GetCurrentLineNumber(parser));
659 XML_ParserFree(parser);
661 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)
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
int agstrfree(Agraph_t *, const char *)
char * agstrdup_html(Agraph_t *, const char *)
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