Graphviz 13.1.3~dev.20250829.0113
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 <stdbool.h>
13#include <stdlib.h>
14#include <string.h>
15
16#include "openFile.h"
17#include <getopt.h>
18#include <util/agxbuf.h>
19#include <util/alloc.h>
20#include <util/debug.h>
21#include <util/exit.h>
22#include <util/unreachable.h>
23
24static bool Verbose;
25static char *gname = "";
26static FILE *outFile;
27static char *CmdName;
28static char **Files;
29
30static FILE *getFile(void) {
31 FILE *rv = NULL;
32 static FILE *savef = NULL;
33 static int cnt = 0;
34
35 if (Files == NULL) {
36 if (cnt++ == 0) {
37 rv = stdin;
38 }
39 } else {
40 if (savef)
41 fclose(savef);
42 while (Files[cnt]) {
43 if ((rv = fopen(Files[cnt++], "r")) != 0)
44 break;
45 else
46 fprintf(stderr, "Can't open %s\n", Files[cnt - 1]);
47 }
48 }
49 savef = rv;
50 return rv;
51}
52
53static char *useString = "Usage: %s [-v?] [-g<name>] [-o<file>] <files>\n\
54 -g<name> : use <name> as template for graph names\n\
55 -v : verbose mode\n\
56 -o<file> : output to <file> (stdout)\n\
57 -? : print usage\n\
58If no files are specified, stdin is used\n";
59
60static void usage(int v) {
61 printf(useString, CmdName);
63}
64
65static char *cmdName(char *path) {
66 char *sp;
67
68 sp = strrchr(path, '/');
69 if (sp)
70 sp++;
71 else
72 sp = path;
73 return sp;
74}
75
76static void initargs(int argc, char **argv) {
77 int c;
78
79 CmdName = cmdName(argv[0]);
80 opterr = 0;
81 while ((c = getopt(argc, argv, ":g:vo:")) != -1) {
82 switch (c) {
83 case 'g':
84 gname = optarg;
85 break;
86 case 'v':
87 Verbose = true;
88 break;
89 case 'o':
90 if (outFile != NULL)
91 fclose(outFile);
92 outFile = openFile(CmdName, optarg, "w");
93 break;
94 case ':':
95 fprintf(stderr, "%s: option -%c missing argument\n", CmdName, optopt);
96 usage(1);
97 break;
98 case '?':
99 if (optopt == '?')
100 usage(0);
101 else {
102 fprintf(stderr, "%s: option -%c unrecognized\n", CmdName, optopt);
103 usage(1);
104 }
105 break;
106 default:
107 UNREACHABLE();
108 }
109 }
110
111 argv += optind;
112 argc -= optind;
113
114 if (argc)
115 Files = argv;
116 if (!outFile)
117 outFile = stdout;
118}
119
120static char *nameOf(agxbuf *buf, char *name, int cnt) {
121 if (*name == '\0')
122 return name;
123 if (cnt) {
124 agxbprint(buf, "%s%d", name, cnt);
125 return agxbuse(buf);
126 } else
127 return name;
128}
129
130int main(int argc, char **argv) {
131 Agraph_t *G;
132 FILE *inFile;
133 int gcnt, cnt, rv;
134 agxbuf buf = {0};
135
136 rv = 0;
137 gcnt = 0;
138 initargs(argc, argv);
139 while ((inFile = getFile())) {
140 cnt = 0;
141 while ((G = gml_to_gv(nameOf(&buf, gname, gcnt), inFile, cnt, &rv))) {
142 cnt++;
143 gcnt++;
144 GV_INFO("%s: %d nodes %d edges", agnameof(G), agnnodes(G), agnedges(G));
145 agwrite(G, outFile);
146 agclose(G);
147 fflush(outFile);
148 }
149 }
150 agxbfree(&buf);
151 graphviz_exit(rv);
152}
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:233
static WUR char * agxbuse(agxbuf *xb)
Definition agxbuf.h:306
Memory allocation wrappers that exit on failure.
static FILE * inFile
Definition acyclic.c:36
helpers for verbose/debug printing
#define GV_INFO(...)
Definition debug.h:15
static NORETURN void graphviz_exit(int status)
Definition exit.h:23
#define G
Definition gdefs.h:7
static FILE * getFile(void)
Definition gml2gv.c:30
static char * gname
Definition gml2gv.c:25
static bool Verbose
Definition gml2gv.c:24
static FILE * outFile
Definition gml2gv.c:26
static void initargs(int argc, char **argv)
Definition gml2gv.c:76
static char * cmdName(char *path)
Definition gml2gv.c:65
static char * nameOf(agxbuf *buf, char *name, int cnt)
Definition gml2gv.c:120
static char * useString
Definition gml2gv.c:53
static char ** Files
Definition gml2gv.c:28
static char * CmdName
Definition gml2gv.c:27
GML-DOT converter
Agraph_t * gml_to_gv(char *, FILE *, int, int *)
Definition gmlparse.c:2246
node NULL
Definition grammar.y:181
static int cnt(Dict_t *d, Dtlink_t **set)
Definition graph.c:196
int agnedges(Agraph_t *g)
Definition graph.c:161
int agnnodes(Agraph_t *g)
Definition graph.c:155
int agclose(Agraph_t *g)
deletes a graph, freeing its associated storage
Definition graph.c:95
int agwrite(Agraph_t *g, void *chan)
Return 0 on success, EOF on failure.
Definition write.c:696
char * agnameof(void *)
returns a string descriptor for the object.
Definition id.c:143
static const char * usage
Definition gvpr.c:51
static FILE * openFile(const char *argv0, const char *name, const char *mode)
Definition openFile.h:8
graph or subgraph
Definition cgraph.h:424
Definition types.h:81
int main()
#define UNREACHABLE()
Definition unreachable.h:30