123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- /**
- * @file
- * @brief
- * [transitive reduction](https://en.wikipedia.org/wiki/Transitive_reduction)
- * filter for directed graphs
- */
- /*************************************************************************
- * Copyright (c) 2011 AT&T Intellectual Property
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * https://www.eclipse.org/legal/epl-v10.html
- *************************************************************************/
- /*
- * Written by Stephen North
- * Updated by Emden Gansner
- */
- /*
- * reads a sequence of graphs on stdin, and writes their
- * transitive reduction
- */
- #include <cgraph/cgraph.h>
- #include <cgraph/ingraphs.h>
- #include <stdbool.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <getopt.h>
- #include <util/exit.h>
- #include <util/unreachable.h>
- static char **Files;
- static char *CmdName;
- typedef graphviz_tred_options_t opts_t;
- static char *useString = "Usage: %s [-vr?] <files>\n\
- -o FILE - redirect output (default to stdout)\n\
- -v - verbose (to stderr)\n\
- -r - print removed edges to stderr\n\
- -? - print usage\n\
- If no files are specified, stdin is used\n";
- static void usage(int v)
- {
- printf(useString, CmdName);
- graphviz_exit(v);
- }
- static void init(opts_t *opts, int argc, char *argv[]) {
- int c;
- CmdName = argv[0];
- opterr = 0;
- while ((c = getopt(argc, argv, "o:vr?")) != -1) {
- switch (c) {
- case 'o':
- (void)fclose(opts->out);
- opts->out = fopen(optarg, "w");
- if (opts->out == NULL) {
- fprintf(stderr, "cannot open %s for writing\n",
- optarg);
- usage(1);
- }
- break;
- case 'v':
- opts->Verbose = true;
- break;
- case 'r':
- opts->PrintRemovedEdges = true;
- break;
- case '?':
- if (optopt == '\0' || optopt == '?')
- usage(0);
- else {
- fprintf(stderr, "%s: option -%c unrecognized\n",
- CmdName, optopt);
- usage(1);
- }
- break;
- default:
- UNREACHABLE();
- }
- }
- argv += optind;
- argc -= optind;
- if (argc)
- Files = argv;
- }
- int main(int argc, char **argv)
- {
- Agraph_t *g;
- ingraph_state ig;
- opts_t opts = {.out = stdout, .err = stderr};
- init(&opts, argc, argv);
- newIngraph(&ig, Files);
- while ((g = nextGraph(&ig)) != 0) {
- if (agisdirected(g))
- graphviz_tred(g, &opts);
- agclose(g);
- }
- graphviz_exit(0);
- }
|