Graphviz 12.0.1~dev.20240715.2254
Loading...
Searching...
No Matches
gml2gv.c
Go to the documentation of this file.
1/*************************************************************************
2 * Copyright (c) 2011 AT&T Intellectual Property
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * https://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors: Details at https://graphviz.org
9 *************************************************************************/
10
11#include "gml2gv.h"
12#include <stdlib.h>
13#include <string.h>
14
15#include <getopt.h>
16#include <cgraph/agxbuf.h>
17#include <cgraph/alloc.h>
18#include <cgraph/exit.h>
19#include <cgraph/unreachable.h>
20#include "openFile.h"
21
22static int Verbose;
23static char* gname = "";
24static FILE *outFile;
25static char *CmdName;
26static char **Files;
27
28static FILE *getFile(void)
29{
30 FILE *rv = NULL;
31 static FILE *savef = NULL;
32 static int cnt = 0;
33
34 if (Files == NULL) {
35 if (cnt++ == 0) {
36 rv = stdin;
37 }
38 } else {
39 if (savef)
40 fclose(savef);
41 while (Files[cnt]) {
42 if ((rv = fopen(Files[cnt++], "r")) != 0)
43 break;
44 else
45 fprintf(stderr, "Can't open %s\n", Files[cnt - 1]);
46 }
47 }
48 savef = rv;
49 return rv;
50}
51
52static char *useString = "Usage: %s [-v?] [-g<name>] [-o<file>] <files>\n\
53 -g<name> : use <name> as template for graph names\n\
54 -v : verbose mode\n\
55 -o<file> : output to <file> (stdout)\n\
56 -? : print usage\n\
57If no files are specified, stdin is used\n";
58
59static void usage(int v)
60{
61 printf(useString, CmdName);
63}
64
65static char *cmdName(char *path)
66{
67 char *sp;
68
69 sp = strrchr(path, '/');
70 if (sp)
71 sp++;
72 else
73 sp = path;
74 return sp;
75}
76
77static void initargs(int argc, char **argv)
78{
79 int c;
80
81 CmdName = cmdName(argv[0]);
82 opterr = 0;
83 while ((c = getopt(argc, argv, ":g:vo:")) != -1) {
84 switch (c) {
85 case 'g':
86 gname = optarg;
87 break;
88 case 'v':
89 Verbose = 1;
90 break;
91 case 'o':
92 if (outFile != NULL)
93 fclose(outFile);
94 outFile = openFile(CmdName, optarg, "w");
95 break;
96 case ':':
97 fprintf(stderr, "%s: option -%c missing argument\n", CmdName, optopt);
98 usage(1);
99 break;
100 case '?':
101 if (optopt == '?')
102 usage(0);
103 else {
104 fprintf(stderr, "%s: option -%c unrecognized\n", CmdName,
105 optopt);
106 usage(1);
107 }
108 break;
109 default:
110 UNREACHABLE();
111 }
112 }
113
114 argv += optind;
115 argc -= optind;
116
117 if (argc)
118 Files = argv;
119 if (!outFile)
120 outFile = stdout;
121}
122
123static char *nameOf(agxbuf *buf, char *name, int cnt) {
124 if (*name == '\0')
125 return name;
126 if (cnt) {
127 agxbprint(buf, "%s%d", name, cnt);
128 return agxbuse(buf);
129 }
130 else
131 return name;
132}
133
134int main(int argc, char **argv)
135{
136 Agraph_t *G;
137 Agraph_t *prev = 0;
138 FILE *inFile;
139 int gcnt, cnt, rv;
140 agxbuf buf = {0};
141
142 rv = 0;
143 gcnt = 0;
144 initargs(argc, argv);
145 while ((inFile = getFile())) {
146 cnt = 0;
147 while ((G = gml_to_gv(nameOf(&buf, gname, gcnt), inFile, cnt, &rv))) {
148 cnt++;
149 gcnt++;
150 if (prev)
151 agclose(prev);
152 prev = G;
153 if (Verbose)
154 fprintf (stderr, "%s: %d nodes %d edges\n",
155 agnameof (G), agnnodes(G), agnedges(G));
156 agwrite(G, outFile);
157 fflush(outFile);
158 }
159 }
160 agxbfree(&buf);
161 graphviz_exit(rv);
162}
163
static void agxbfree(agxbuf *xb)
free any malloced resources
Definition agxbuf.h:77
static int agxbprint(agxbuf *xb, const char *fmt,...)
Printf-style output to an agxbuf.
Definition agxbuf.h:213
static char * agxbuse(agxbuf *xb)
Definition agxbuf.h:286
Memory allocation wrappers that exit on failure.
static FILE * inFile
Definition acyclic.c:36
static NORETURN void graphviz_exit(int status)
Definition exit.h:23
#define G
Definition gdefs.h:7
static FILE * getFile(void)
Definition gml2gv.c:28
static char * gname
Definition gml2gv.c:23
static FILE * outFile
Definition gml2gv.c:24
static void initargs(int argc, char **argv)
Definition gml2gv.c:77
static char * cmdName(char *path)
Definition gml2gv.c:65
static int Verbose
Definition gml2gv.c:22
static char * nameOf(agxbuf *buf, char *name, int cnt)
Definition gml2gv.c:123
static char * useString
Definition gml2gv.c:52
static char ** Files
Definition gml2gv.c:26
static char * CmdName
Definition gml2gv.c:25
GML-DOT converter
Agraph_t * gml_to_gv(char *, FILE *, int, int *)
Definition gmlparse.c:2609
node NULL
Definition grammar.y:149
static int cnt(Dict_t *d, Dtlink_t **set)
Definition graph.c:199
int agnedges(Agraph_t *g)
Definition graph.c:164
int agnnodes(Agraph_t *g)
Definition graph.c:158
int agclose(Agraph_t *g)
deletes a graph, freeing its associated storage
Definition graph.c:96
int agwrite(Agraph_t *g, void *chan)
Return 0 on success, EOF on failure.
Definition write.c:708
char * agnameof(void *)
returns a string descriptor for the object.
Definition id.c:158
static const char * usage
Definition gvpr.c:53
$2 u p prev
Definition htmlparse.y:495
static FILE * openFile(const char *argv0, const char *name, const char *mode)
Definition openFile.h:8
graph or subgraph
Definition cgraph.h:425
Definition types.h:81
int main()
#define UNREACHABLE()
Definition unreachable.h:30