| 1 |
/** |
| 2 |
* @file |
| 3 |
* @brief supports user-supplied data |
| 4 |
* @ingroup cgraph_app |
| 5 |
*/ |
| 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 Gansner |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "config.h" |
| 22 |
|
| 23 |
#include <cgraph/cgraph.h> |
| 24 |
#include <cgraph/ingraphs.h> |
| 25 |
#include <stdbool.h> |
| 26 |
#include <stdio.h> |
| 27 |
#include <stdlib.h> |
| 28 |
#include <util/gv_fopen.h> |
| 29 |
|
| 30 |
/* Set next available file. |
| 31 |
* If Files is NULL, we just read from stdin. |
| 32 |
*/ |
| 33 |
static void nextFile(ingraph_state * sp) |
| 34 |
{ |
| 35 |
void *rv = NULL; |
| 36 |
char *fname; |
| 37 |
|
| 38 |
if (sp->u.Files == NULL) { |
| 39 |
if (sp->ctr++ == 0) { |
| 40 |
rv = stdin; |
| 41 |
} |
| 42 |
} else { |
| 43 |
while ((fname = sp->u.Files[sp->ctr++])) { |
| 44 |
if (*fname == '-') { |
| 45 |
rv = stdin; |
| 46 |
break; |
| 47 |
} else if ((rv = gv_fopen(fname, "r")) != 0) |
| 48 |
break; |
| 49 |
else { |
| 50 |
fprintf(stderr, "Can't open %s\n", sp->u.Files[sp->ctr - 1]); |
| 51 |
sp->errors++; |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
sp->fp = rv; |
| 56 |
} |
| 57 |
|
| 58 |
/* Read and return next graph; return NULL if done. |
| 59 |
* Read graph from currently open file. If none, open next file. |
| 60 |
*/ |
| 61 |
Agraph_t *nextGraph(ingraph_state * sp) |
| 62 |
{ |
| 63 |
Agraph_t *g; |
| 64 |
|
| 65 |
if (sp->ingraphs) { |
| 66 |
g = sp->u.Graphs[sp->ctr]; |
| 67 |
if (g) sp->ctr++; |
| 68 |
return g; |
| 69 |
} |
| 70 |
if (sp->fp == NULL) |
| 71 |
nextFile(sp); |
| 72 |
g = NULL; |
| 73 |
|
| 74 |
while (sp->fp != NULL) { |
| 75 |
if ((g = sp->readf(fileName(sp), sp->fp)) != 0) |
| 76 |
break; |
| 77 |
if (sp->u.Files) /* Only close if not using stdin */ |
| 78 |
(void)fclose(sp->fp); |
| 79 |
nextFile(sp); |
| 80 |
} |
| 81 |
return g; |
| 82 |
} |
| 83 |
|
| 84 |
/* Create new ingraph state. If sp is non-NULL, we |
| 85 |
* assume user is supplying memory. |
| 86 |
*/ |
| 87 |
static ingraph_state *new_ing(ingraph_state *sp, char **files, |
| 88 |
Agraph_t **graphs, |
| 89 |
Agraph_t *(*readf)(const char *, void *)) { |
| 90 |
if (!sp) { |
| 91 |
sp = malloc(sizeof(ingraph_state)); |
| 92 |
if (!sp) { |
| 93 |
fprintf(stderr, "ingraphs: out of memory\n"); |
| 94 |
return 0; |
| 95 |
} |
| 96 |
sp->heap = true; |
| 97 |
} else |
| 98 |
sp->heap = false; |
| 99 |
if (graphs) { |
| 100 |
sp->ingraphs = 1; |
| 101 |
sp->u.Graphs = graphs; |
| 102 |
} |
| 103 |
else { |
| 104 |
sp->ingraphs = 0; |
| 105 |
sp->u.Files = files; |
| 106 |
} |
| 107 |
sp->ctr = 0; |
| 108 |
sp->errors = 0; |
| 109 |
sp->fp = NULL; |
| 110 |
if (!readf) { |
| 111 |
if (sp->heap) |
| 112 |
free(sp); |
| 113 |
fprintf(stderr, "ingraphs: NULL read function\n"); |
| 114 |
return 0; |
| 115 |
} |
| 116 |
sp->readf = readf; |
| 117 |
return sp; |
| 118 |
} |
| 119 |
|
| 120 |
ingraph_state *newIng(ingraph_state *sp, char **files, |
| 121 |
Agraph_t *(*readf)(const char *, void *)) { |
| 122 |
return new_ing(sp, files, 0, readf); |
| 123 |
} |
| 124 |
|
| 125 |
/* Create new ingraph state using supplied graphs. If sp is non-NULL, we |
| 126 |
* assume user is supplying memory. |
| 127 |
*/ |
| 128 |
ingraph_state *newIngGraphs(ingraph_state *sp, Agraph_t **graphs, |
| 129 |
Agraph_t *(*readf)(const char *, void *)) { |
| 130 |
return new_ing(sp, 0, graphs, readf); |
| 131 |
} |
| 132 |
|
| 133 |
static Agraph_t *dflt_read(const char *filename, void *fp) { |
| 134 |
return agconcat(NULL, filename, fp, NULL); |
| 135 |
} |
| 136 |
|
| 137 |
/* At present, we require opf to be non-NULL. In |
| 138 |
* theory, we could assume a function agread(FILE*,void*) |
| 139 |
*/ |
| 140 |
ingraph_state *newIngraph(ingraph_state * sp, char **files) { |
| 141 |
return newIng(sp, files, dflt_read); |
| 142 |
} |
| 143 |
|
| 144 |
/* Close any open files and free discipline |
| 145 |
* Free sp if necessary. |
| 146 |
*/ |
| 147 |
void closeIngraph(ingraph_state * sp) |
| 148 |
{ |
| 149 |
if (!sp->ingraphs && sp->u.Files && sp->fp) |
| 150 |
(void)fclose(sp->fp); |
| 151 |
if (sp->heap) |
| 152 |
free(sp); |
| 153 |
} |
| 154 |
|
| 155 |
/// Return name of current file being processed. |
| 156 |
char *fileName(ingraph_state * sp) |
| 157 |
{ |
| 158 |
char *fname; |
| 159 |
|
| 160 |
if (sp->ingraphs) { |
| 161 |
return "<>"; |
| 162 |
} |
| 163 |
else if (sp->u.Files) { |
| 164 |
if (sp->ctr) { |
| 165 |
fname = sp->u.Files[sp->ctr - 1]; |
| 166 |
if (*fname == '-') |
| 167 |
return "<stdin>"; |
| 168 |
else |
| 169 |
return fname; |
| 170 |
} else |
| 171 |
return "<>"; |
| 172 |
} else |
| 173 |
return "<stdin>"; |
| 174 |
} |