#include "vec.h" #include #include #include #include graph_vec vnew_graph(void) { graph_vec g; g.n = -1; g.n = -1; g.cap = 10; g.node = malloc(10 * sizeof(nodept)); assert(g.node); return g; } void vinsert_node(graph_vec *g, fix_str str) { ++g->n; if (g->n == g->cap) g->node = reallocarray(g->node, (g->cap += 128), sizeof(nodept)); struct node *n = malloc(sizeof(struct node)); assert(n); n->num = g->n; n->nbs = 0; n->vu = 0; n->cap = 5; n->succ = malloc(5 * sizeof(nodept)); strcpy(n->func, str); g->node[g->n] = n; } void vdelete_graph(graph_vec *g) { int i; for (i = 0; i <= g->n; ++i) { if (g->node[i]) { if (g->node[i]->succ) { free(g->node[i]->succ); g->node[i]->succ = NULL; } free(g->node[i]); g->node[i] = NULL; } } free(g->node); } int vis_in(graph_vec g, fix_str func) { int i; if (g.n < 0) { return -1; } for (i = 0; i <= g.n; i++) { if (strcmp(g.node[i]->func, func) == 0) { return i; } } return -1; } static void vprint_succ(nodept node) { // int i; printf(" Succ: %s\n", node->func); // for (i = 0; i < node->nbs; ++i) { // printf("SUCC %d: %s\n", i, node->succ[i]->func); // } } void vprint_nodes(nodept node) { int i; if (node->vu == 1) return; node->vu = 1; printf("NODE: %s\n", node->func); for (i = 0; i < node->nbs; ++i) { vprint_succ(node->succ[i]); } for (i = 0; i < node->nbs; ++i) { vprint_nodes(node->succ[i]); } } void vwrite_node_dot(FILE *fp, graph_vec g) { int i, j; for (i = 0; i <= g.n; i++) { for (j = 0; j < g.node[i]->nbs; j++) { // printf("\t%s -> %s;\n", node->func, node->succ[i]->func); fprintf(fp, "\t%s -> %s;\n", g.node[i]->func, g.node[i]->succ[j]->func); } } } void main_graph(FILE *fp, nodept node) { int i; if (node->vu == 1) return; node->vu = 1; printf("NODE: %s\n", node->func); for (i = 0; i < node->nbs; ++i) { fprintf(fp, "\t%s -> %s;\n", node->func, node->succ[i]->func); } for (i = 0; i < node->nbs; ++i) { main_graph(fp, node->succ[i]); } } void vdot_graph(graph_vec g, bool main) { FILE *fp; fp = fopen("graph.dot", "w"); fprintf(fp, "digraph main {\n"); if (main) { fix_str m = "main"; int n = vis_in(g, m); if (n != -1) { main_graph(fp, g.node[n]); } else { fprintf(stderr, "main not found.\n"); exit(1); } } else { vwrite_node_dot(fp, g); } fprintf(fp, "}"); fclose(fp); }