Graphviz 13.0.0~dev.20241220.2304
Loading...
Searching...
No Matches
cvtgxl.c
Go to the documentation of this file.
1
6/*************************************************************************
7 * Copyright (c) 2011 AT&T Intellectual Property
8 * All rights reserved. This program and the accompanying materials
9 * are made available under the terms of the Eclipse Public License v1.0
10 * which accompanies this distribution, and is available at
11 * https://www.eclipse.org/legal/epl-v10.html
12 *
13 * Contributors: Details at https://graphviz.org
14 *************************************************************************/
15
16
17/*
18 * Written by Emden R. Gansner and Krishnam Pericherla
19 */
20
21#include "config.h"
22
23#include <getopt.h>
24#include <stdio.h>
25#include <stdlib.h>
26
27#include "convert.h"
28#include "openFile.h"
29#include <cgraph/ingraphs.h>
30#include <util/exit.h>
31#include <util/gv_ctype.h>
32
33typedef enum { Unset, ToGV, ToGXL } mode;
34
35static FILE *outFile;
36static char *CmdName;
37static char **Files;
38static mode act = Unset;
39
40#ifdef HAVE_EXPAT
41static FILE *getFile(void)
42{
43 FILE *rv = NULL;
44 static FILE *savef = NULL;
45 static int cnt = 0;
46
47 if (Files == NULL) {
48 if (cnt++ == 0) {
49 rv = stdin;
50 }
51 } else {
52 if (savef)
53 fclose(savef);
54 while (Files[cnt]) {
55 if ((rv = fopen(Files[cnt++], "r")) != 0)
56 break;
57 else
58 fprintf(stderr, "Can't open %s\n", Files[cnt - 1]);
59 }
60 }
61 savef = rv;
62 return rv;
63}
64#endif
65
66static const char *use = "Usage: %s [-gd?] [-o<file>] [<graphs>]\n\
67 -g : convert to GXL\n\
68 -d : convert to GV\n\
69 -o<file> : output to <file> (stdout)\n\
70 -? : usage\n";
71
72static void usage(int v)
73{
74 fprintf(stderr, use, CmdName);
76}
77
78static char *cmdName(char *path)
79{
80 char *sp;
81
82 sp = strrchr(path, '/');
83 if (sp)
84 sp++;
85 else
86 sp = path;
87#ifdef _WIN32
88 char *sp2 = strrchr(sp, '\\');
89 if (sp2 != NULL)
90 sp = sp2 + 1;
91#endif
92 return sp;
93}
94
95static void checkInput(void)
96{
97 char *ep;
98
99 ep = strrchr(*Files, '.');
100 if (!ep)
101 return;
102 ep++;
103 if (strcmp(ep, "gv") == 0)
104 act = ToGXL;
105 else if (strcmp(ep, "dot") == 0)
106 act = ToGXL;
107 else if (strcmp(ep, "gxl") == 0)
108 act = ToGV;
109}
110
111static void setAction(void)
112{
113 if (gv_tolower(CmdName[0]) == 'd')
114 act = ToGXL;
115 else if (gv_tolower(CmdName[0]) == 'g') {
116 if (gv_tolower(CmdName[1]) == 'v')
117 act = ToGXL;
118 else
119 act = ToGV;
120 }
121 else if (Files)
122 checkInput();
123
124 if (act == Unset) {
125 fprintf(stderr, "Cannot determine conversion type\n");
126 usage(1);
127 }
128}
129
130static void initargs(int argc, char **argv)
131{
132 int c;
133
134 CmdName = cmdName(argv[0]);
135 opterr = 0;
136 while ((c = getopt(argc, argv, ":gdo:")) != -1) {
137 switch (c) {
138 case 'd':
139 act = ToGV;
140 break;
141 case 'g':
142 act = ToGXL;
143 break;
144 case 'o':
145 if (outFile != NULL)
146 fclose(outFile);
147 outFile = openFile(CmdName, optarg, "w");
148 break;
149 case ':':
150 fprintf(stderr, "%s: option -%c missing argument\n", CmdName, optopt);
151 break;
152 case '?':
153 if (optopt == '?')
154 usage(0);
155 else {
156 fprintf(stderr, "%s: option -%c unrecognized\n", CmdName,
157 optopt);
158 graphviz_exit(1);
159 }
160 break;
161 default:
162 fprintf(stderr, "cvtgxl: unexpected error\n");
163 graphviz_exit(EXIT_FAILURE);
164 }
165 }
166
167 argv += optind;
168 argc -= optind;
169
170 if (argc > 0)
171 Files = argv;
172 if (!outFile)
173 outFile = stdout;
174 if (act == Unset)
175 setAction();
176}
177
178int main(int argc, char **argv)
179{
180 Agraph_t *G;
181 Agraph_t *prev = 0;
182
183 initargs(argc, argv);
184 if (act == ToGXL) {
185 ingraph_state ig;
186 newIngraph(&ig, Files);
187
188 while ((G = nextGraph(&ig))) {
189 if (prev)
190 agclose(prev);
191 prev = G;
193 fflush(outFile);
194 }
195 } else {
196#ifdef HAVE_EXPAT
197 FILE *inFile;
198 while ((inFile = getFile())) {
199 while ((G = gxl_to_gv(inFile))) {
200 if (prev)
201 agclose(prev);
202 prev = G;
203 agwrite(G, outFile);
204 fflush(outFile);
205 }
206 }
207#else
208 fputs("cvtgxl: not configured for conversion from GXL to GV\n",
209 stderr);
210 graphviz_exit(1);
211
212#endif
213 }
214 graphviz_exit(0);
215}
static FILE * inFile
Definition acyclic.c:36
DOT-GXL converter API for gxl2gv.c and gv2gxl.c.
void gv_to_gxl(Agraph_t *, FILE *)
Definition gv2gxl.c:761
static const char * use
Definition cvtgxl.c:66
mode
Definition cvtgxl.c:33
@ ToGV
Definition cvtgxl.c:33
@ Unset
Definition cvtgxl.c:33
@ ToGXL
Definition cvtgxl.c:33
static void checkInput(void)
Definition cvtgxl.c:95
static FILE * outFile
Definition cvtgxl.c:35
static void initargs(int argc, char **argv)
Definition cvtgxl.c:130
static char * cmdName(char *path)
Definition cvtgxl.c:78
static mode act
Definition cvtgxl.c:38
static void setAction(void)
Definition cvtgxl.c:111
static char ** Files
Definition cvtgxl.c:37
static char * CmdName
Definition cvtgxl.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:29
node NULL
Definition grammar.y:163
static int cnt(Dict_t *d, Dtlink_t **set)
Definition graph.c:210
int agclose(Agraph_t *g)
deletes a graph, freeing its associated storage
Definition graph.c:102
int agwrite(Agraph_t *g, void *chan)
Return 0 on success, EOF on failure.
Definition write.c:730
replacements for ctype.h functions
static char gv_tolower(int c)
Definition gv_ctype.h:81
static const char * usage
Definition gvpr.c:51
$2 u p prev
Definition htmlparse.y:297
Agraph_t * nextGraph(ingraph_state *sp)
Definition ingraphs.c:61
ingraph_state * newIngraph(ingraph_state *sp, char **files)
Definition ingraphs.c:140
supports user-supplied data
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()