Graphviz 14.1.2~dev.20260118.2044
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 "config.h"
12
13#include "gml2gv.h"
14#include <stdbool.h>
15#include <stdlib.h>
16#include <string.h>
17
18#include "openFile.h"
19#include <getopt.h>
20#include <util/agxbuf.h>
21#include <util/alloc.h>
22#include <util/debug.h>
23#include <util/exit.h>
24#include <util/unreachable.h>
25
26static bool Verbose;
27static char *gname = "";
28static FILE *outFile;
29static char *CmdName;
30static char **Files;
31
32static FILE *getFile(void) {
33 FILE *rv = NULL;
34 static FILE *savef = NULL;
35 static int cnt = 0;
36
37 if (Files == NULL) {
38 if (cnt++ == 0) {
39 rv = stdin;
40 }
41 } else {
42 if (savef)
43 fclose(savef);
44 while (Files[cnt]) {
45 if ((rv = fopen(Files[cnt++], "r")) != 0)
46 break;
47 else
48 fprintf(stderr, "Can't open %s\n", Files[cnt - 1]);
49 }
50 }
51 savef = rv;
52 return rv;
53}
54
55static char *useString = "Usage: %s [-v?] [-g<name>] [-o<file>] <files>\n\
56 -g<name> : use <name> as template for graph names\n\
57 -v : verbose mode\n\
58 -o<file> : output to <file> (stdout)\n\
59 -? : print usage\n\
60If no files are specified, stdin is used\n";
61
62static void usage(int v) {
63 printf(useString, CmdName);
65}
66
67static char *cmdName(char *path) {
68 char *sp;
69
70 sp = strrchr(path, '/');
71 if (sp)
72 sp++;
73 else
74 sp = path;
75 return sp;
76}
77
78static void initargs(int argc, char **argv) {
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 = true;
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, optopt);
105 usage(1);
106 }
107 break;
108 default:
109 UNREACHABLE();
110 }
111 }
112
113 argv += optind;
114 argc -= optind;
115
116 if (argc)
117 Files = argv;
118 if (!outFile)
119 outFile = stdout;
120}
121
122static char *nameOf(agxbuf *buf, char *name, int cnt) {
123 if (*name == '\0')
124 return name;
125 if (cnt) {
126 agxbprint(buf, "%s%d", name, cnt);
127 return agxbuse(buf);
128 } else
129 return name;
130}
131
132int main(int argc, char **argv) {
133 Agraph_t *G;
134 FILE *inFile;
135 int gcnt, cnt, rv;
136 agxbuf buf = {0};
137
138 rv = 0;
139 gcnt = 0;
140 initargs(argc, argv);
141 while ((inFile = getFile())) {
142 cnt = 0;
143 while ((G = gml_to_gv(nameOf(&buf, gname, gcnt), inFile, cnt, &rv))) {
144 cnt++;
145 gcnt++;
146 GV_INFO("%s: %d nodes %d edges", agnameof(G), agnnodes(G), agnedges(G));
147 agwrite(G, outFile);
148 agclose(G);
149 fflush(outFile);
150 }
151 }
152 agxbfree(&buf);
153 graphviz_exit(rv);
154}
Dynamically expanding string buffers.
static void agxbfree(agxbuf *xb)
free any malloced resources
Definition agxbuf.h:97
static int agxbprint(agxbuf *xb, const char *fmt,...)
Printf-style output to an agxbuf.
Definition agxbuf.h:252
static WUR char * agxbuse(agxbuf *xb)
Definition agxbuf.h:325
Memory allocation wrappers that exit on failure.
static FILE * inFile
Definition acyclic.c:38
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:32
static char * gname
Definition gml2gv.c:27
static bool Verbose
Definition gml2gv.c:26
static FILE * outFile
Definition gml2gv.c:28
static void initargs(int argc, char **argv)
Definition gml2gv.c:78
static char * cmdName(char *path)
Definition gml2gv.c:67
static char * nameOf(agxbuf *buf, char *name, int cnt)
Definition gml2gv.c:122
static char * useString
Definition gml2gv.c:55
static char ** Files
Definition gml2gv.c:30
static char * CmdName
Definition gml2gv.c:29
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:198
int agnedges(Agraph_t *g)
Definition graph.c:163
int agnnodes(Agraph_t *g)
Definition graph.c:157
int agclose(Agraph_t *g)
deletes a graph, freeing its associated storage
Definition graph.c:97
int agwrite(Agraph_t *g, void *chan)
Return 0 on success, EOF on failure.
Definition write.c:669
char * agnameof(void *)
returns a string descriptor for the object.
Definition id.c:145
static const char * usage
Definition gvpr.c:54
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