From: erg Date: Thu, 30 Sep 2010 15:03:39 +0000 (+0000) Subject: Update gml2gv to allow the user to specify the graph name; X-Git-Tag: LAST_LIBGRAPH~32^2~1180 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=972c43c22cd53b22cb5e1acae5faf21852585dbf;p=graphviz Update gml2gv to allow the user to specify the graph name; support verbose mode --- diff --git a/cmd/tools/gml2gv.1 b/cmd/tools/gml2gv.1 index 14fa6a6ec..8678c0638 100644 --- a/cmd/tools/gml2gv.1 +++ b/cmd/tools/gml2gv.1 @@ -1,10 +1,13 @@ -.TH GML2GV 1 "9 July 2009" +.TH GML2GV 1 "30 September 2010" .SH NAME gml2gv \- GML-DOT converter .SH SYNOPSIS .B gml2gv [ -.B \-? +.B \-?v +] +[ +.BI -g gname ] [ .BI -o outfile @@ -19,9 +22,17 @@ converts a graph specified in the GML format to a graph in the GV (formerly DOT) .SH OPTIONS The following options are supported: .TP +.B \-v +Turns on verbose mode +.TP .B \-? Prints usage information and exits. .TP +.BI \-g "gname" +The string \fIgname\fP is used as the name of the generated graph. +If multiple graphs are generated, subsequent graphs use the name +\fIgname\fP appended with an integer. +.TP .BI \-o "outfile" Prints output to the file \fIoutfile\fP. If not given, \fBgml2gv\fP uses stdout. diff --git a/cmd/tools/gml2gv.c b/cmd/tools/gml2gv.c index 2f2d37bac..05b9e9f60 100644 --- a/cmd/tools/gml2gv.c +++ b/cmd/tools/gml2gv.c @@ -31,6 +31,10 @@ #include "compat_getopt.h" #endif +#define N_NEW(n,t) (t*)malloc((n)*sizeof(t)) + +static int Verbose; +static char* gname = ""; static FILE *outFile; static char *CmdName; static char **Files; @@ -79,9 +83,11 @@ static FILE *openFile(char *name, char *mode) } -static char *useString = "Usage: %s [-p?] \n\ +static char *useString = "Usage: %s [-v?] [-g] [-o] \n\ + -g : use as template for graph names\n\ + -v : verbose mode\n\ -o : output to (stdout)\n\ - -? - print usage\n\ + -? : print usage\n\ If no files are specified, stdin is used\n"; static void usage(int v) @@ -108,8 +114,14 @@ static void initargs(int argc, char **argv) CmdName = cmdName(argv[0]); opterr = 0; - while ((c = getopt(argc, argv, ":gdo:")) != -1) { + while ((c = getopt(argc, argv, ":g:vo:")) != -1) { switch (c) { + case 'g': + gname = optarg; + break; + case 'v': + Verbose = 1; + break; case 'o': outFile = openFile(optarg, "w"); break; @@ -133,25 +145,48 @@ static void initargs(int argc, char **argv) outFile = stdout; } +static char* +nameOf (char* name, int cnt) +{ + static char* buf = 0; + + if (*name == '\0') + return name; + if (cnt) { + if (!buf) + buf = N_NEW (strlen(name)+32,char); /* 32 to handle any integer plus null byte */ + sprintf (buf, "%s%d", name, cnt); + return buf; + } + else + return name; +} + int main(int argc, char **argv) { Agraph_t *G; Agraph_t *prev = 0; FILE *inFile; - int cnt, rv; + int gcnt, cnt, rv; rv = 0; + gcnt = 0; initargs(argc, argv); while ((inFile = getFile())) { cnt = 0; - while ((G = gml_to_gv(inFile, cnt, &rv))) { + while ((G = gml_to_gv(nameOf(gname, gcnt), inFile, cnt, &rv))) { cnt++; + gcnt++; if (prev) agclose(prev); prev = G; + if (Verbose) + fprintf (stderr, "%s: %d nodes %d edges\n", + agnameof (G), agnnodes(G), agnedges(G)); agwrite(G, outFile); fflush(outFile); } } exit(rv); } + diff --git a/cmd/tools/gml2gv.h b/cmd/tools/gml2gv.h index b7e5cce7a..eb70a7e72 100644 --- a/cmd/tools/gml2gv.h +++ b/cmd/tools/gml2gv.h @@ -43,4 +43,4 @@ extern void gmllexeof(void); extern void gmlerror(char *); extern int gmlerrors(void); extern void initgmlscan (FILE*); -extern Agraph_t* gml_to_gv (FILE*, int, int*); +extern Agraph_t* gml_to_gv (char*, FILE*, int, int*); diff --git a/cmd/tools/gmlparse.y b/cmd/tools/gmlparse.y index 5306265a2..906bed655 100644 --- a/cmd/tools/gmlparse.y +++ b/cmd/tools/gmlparse.y @@ -732,7 +732,7 @@ addAttrs (Agobj_t* obj, Dt_t* alist, agxbuf* xb, agxbuf* unk) } static Agraph_t* -mkGraph (gmlgraph* G, Agraph_t* parent, agxbuf* xb, agxbuf* unk) +mkGraph (gmlgraph* G, Agraph_t* parent, char* name, agxbuf* xb, agxbuf* unk) { Agraph_t* g; Agnode_t* n; @@ -746,9 +746,9 @@ mkGraph (gmlgraph* G, Agraph_t* parent, agxbuf* xb, agxbuf* unk) g = agsubg (parent, NULL, 1); } else if (G->directed >= 1) - g = agopen ("", Agdirected, 0); + g = agopen (name, Agdirected, 0); else - g = agopen ("", Agundirected, 0); + g = agopen (name, Agundirected, 0); if (!parent && L) { addAttrs ((Agobj_t*)g, L, xb, unk); @@ -777,7 +777,7 @@ mkGraph (gmlgraph* G, Agraph_t* parent, agxbuf* xb, agxbuf* unk) addAttrs ((Agobj_t*)e, ep->attrlist, xb, unk); } for (gp = dtfirst(G->graphlist); gp; gp = dtnext (G->graphlist, gp)) { - mkGraph (gp, g, xb, unk); + mkGraph (gp, g, NULL, xb, unk); } addAttrs ((Agobj_t*)g, G->attrlist, xb, unk); @@ -786,7 +786,7 @@ mkGraph (gmlgraph* G, Agraph_t* parent, agxbuf* xb, agxbuf* unk) } Agraph_t* -gml_to_gv (FILE* fp, int cnt, int* errors) +gml_to_gv (char* name, FILE* fp, int cnt, int* errors) { Agraph_t* g; agxbuf xb; @@ -812,7 +812,7 @@ gml_to_gv (FILE* fp, int cnt, int* errors) else { agxbinit (&xb, BUFSIZ, buf); agxbinit (&unk, BUFSIZ, unknownb); - g = mkGraph (G, NULL, &xb, &unk); + g = mkGraph (G, name, NULL, &xb, &unk); agxbfree (&xb); }