From 7d5fcee02fe8a046c2ed8801426c1a1afda3f80b Mon Sep 17 00:00:00 2001 From: Matthew Fernandez Date: Thu, 25 Aug 2022 08:28:22 -0700 Subject: [PATCH] cmd/tools: unify common 'openFile' function into a header This also addresses a number of inconsistencies between these duplicated functions, like `sccmap` calling itself `gvpack` in its error message. --- cmd/tools/Makefile.am | 2 +- cmd/tools/acyclic.c | 23 +++-------------------- cmd/tools/acyclic.vcxproj | 3 +++ cmd/tools/acyclic.vcxproj.filters | 5 +++++ cmd/tools/gml2gv.c | 18 ++---------------- cmd/tools/gml2gv.vcxproj | 1 + cmd/tools/gml2gv.vcxproj.filters | 3 +++ cmd/tools/graphml2gv.c | 16 ++-------------- cmd/tools/graphml2gv.vcxproj | 3 +++ cmd/tools/graphml2gv.vcxproj.filters | 5 +++++ cmd/tools/gv2gml.c | 18 ++---------------- cmd/tools/gv2gml.vcxproj | 3 +++ cmd/tools/gv2gml.vcxproj.filters | 5 +++++ cmd/tools/gvgen.c | 16 ++-------------- cmd/tools/gvgen.vcxproj | 3 +++ cmd/tools/gvgen.vcxproj.filters | 5 +++++ cmd/tools/gvpack.cpp | 15 ++------------- cmd/tools/gvpack.vcxproj | 1 + cmd/tools/gvpack.vcxproj.filters | 3 +++ cmd/tools/openFile.h | 19 +++++++++++++++++++ cmd/tools/sccmap.c | 15 ++------------- cmd/tools/sccmap.vcxproj | 3 +++ cmd/tools/sccmap.vcxproj.filters | 5 +++++ cmd/tools/unflatten.c | 15 ++------------- cmd/tools/unflatten.vcxproj | 3 +++ cmd/tools/unflatten.vcxproj.filters | 5 +++++ 26 files changed, 93 insertions(+), 120 deletions(-) create mode 100644 cmd/tools/openFile.h diff --git a/cmd/tools/Makefile.am b/cmd/tools/Makefile.am index 818fe5a58..123314965 100644 --- a/cmd/tools/Makefile.am +++ b/cmd/tools/Makefile.am @@ -11,7 +11,7 @@ AM_CPPFLAGS = \ $(EXPAT_INCLUDES) noinst_HEADERS = colortbl.h convert.h mmio.h matrix_market.h \ - graph_generator.h gml2gv.h gmlparse.h + graph_generator.h gml2gv.h gmlparse.h openFile.h if ENABLE_STATIC bin_PROGRAMS = gc gvcolor gxl2gv acyclic nop ccomps sccmap tred \ unflatten gvpack gvpack_static dijkstra bcomps mm2gv gvgen gml2gv gv2gml graphml2gv diff --git a/cmd/tools/acyclic.c b/cmd/tools/acyclic.c index 9cedc411b..3cda48516 100644 --- a/cmd/tools/acyclic.c +++ b/cmd/tools/acyclic.c @@ -26,6 +26,7 @@ #include #include #include +#include "openFile.h" typedef struct { Agrec_t h; @@ -112,24 +113,6 @@ static void usage(int v) graphviz_exit(v); } -static FILE *openFile(const char *name, const char *mode) -{ - FILE *fp; - char *modestr; - - fp = fopen(name, mode); - if (!fp) { - if (*mode == 'r') - modestr = "reading"; - else - modestr = "writing"; - fprintf(stderr, "%s: could not open file %s for %s\n", - cmd, name, modestr); - graphviz_exit(-1); - } - return fp; -} - static void init(int argc, char *argv[]) { int c; @@ -141,7 +124,7 @@ static void init(int argc, char *argv[]) case 'o': if (outFile != NULL) fclose(outFile); - outFile = openFile(optarg, "w"); + outFile = openFile(argv[0], optarg, "w"); break; case 'n': doWrite = 0; @@ -167,7 +150,7 @@ static void init(int argc, char *argv[]) UNREACHABLE(); } if (optind < argc) { - inFile = openFile(argv[optind], "r"); + inFile = openFile(argv[0], argv[optind], "r"); } else inFile = stdin; if (!outFile) diff --git a/cmd/tools/acyclic.vcxproj b/cmd/tools/acyclic.vcxproj index 4a243e202..e4df1e9e5 100644 --- a/cmd/tools/acyclic.vcxproj +++ b/cmd/tools/acyclic.vcxproj @@ -98,6 +98,9 @@ copy $(SolutionDir)windows\dependencies\libraries\vcpkg\installed\x86-windows\bin\getopt.dll $(OutDir)getopt.dll + + + diff --git a/cmd/tools/acyclic.vcxproj.filters b/cmd/tools/acyclic.vcxproj.filters index 743835aff..d83bf6640 100644 --- a/cmd/tools/acyclic.vcxproj.filters +++ b/cmd/tools/acyclic.vcxproj.filters @@ -14,6 +14,11 @@ cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + Header Files + + Source Files diff --git a/cmd/tools/gml2gv.c b/cmd/tools/gml2gv.c index 1da26b68b..ef0f3ec51 100644 --- a/cmd/tools/gml2gv.c +++ b/cmd/tools/gml2gv.c @@ -16,6 +16,7 @@ #include #include #include +#include "openFile.h" static int Verbose; static char* gname = ""; @@ -47,21 +48,6 @@ static FILE *getFile(void) return rv; } -static FILE *openFile(const char *name) -{ - FILE *fp; - - fp = fopen(name, "w"); - if (!fp) { - fprintf(stderr, "%s: could not open file %s for writing\n", - CmdName, name); - perror(name); - graphviz_exit(1); - } - return fp; -} - - static char *useString = "Usage: %s [-v?] [-g] [-o] \n\ -g : use as template for graph names\n\ -v : verbose mode\n\ @@ -104,7 +90,7 @@ static void initargs(int argc, char **argv) case 'o': if (outFile != NULL) fclose(outFile); - outFile = openFile(optarg); + outFile = openFile(CmdName, optarg, "w"); break; case ':': fprintf(stderr, "%s: option -%c missing argument\n", CmdName, optopt); diff --git a/cmd/tools/gml2gv.vcxproj b/cmd/tools/gml2gv.vcxproj index 69326a94e..2990c0db7 100644 --- a/cmd/tools/gml2gv.vcxproj +++ b/cmd/tools/gml2gv.vcxproj @@ -107,6 +107,7 @@ win_bison -dy -Wno-yacc gmlparse.y -o gmlparse.c + diff --git a/cmd/tools/gml2gv.vcxproj.filters b/cmd/tools/gml2gv.vcxproj.filters index 240d45258..a3d4431ff 100644 --- a/cmd/tools/gml2gv.vcxproj.filters +++ b/cmd/tools/gml2gv.vcxproj.filters @@ -21,6 +21,9 @@ Header Files + + Header Files + diff --git a/cmd/tools/graphml2gv.c b/cmd/tools/graphml2gv.c index 5002496be..fe212decd 100644 --- a/cmd/tools/graphml2gv.c +++ b/cmd/tools/graphml2gv.c @@ -23,6 +23,7 @@ #include #include #include +#include "openFile.h" #ifdef HAVE_EXPAT #include #include @@ -551,19 +552,6 @@ static FILE *getFile(void) return rv; } -static FILE *openFile(const char *name) -{ - FILE *fp; - - fp = fopen(name, "w"); - if (!fp) { - fprintf(stderr, "%s: could not open file %s for writing\n", CmdName, name); - perror(name); - graphviz_exit(1); - } - return fp; -} - static const char *use = "Usage: %s [-gd?] [-o] []\n\ -g : use as template for graph names\n\ -o : output to (stdout)\n\ @@ -605,7 +593,7 @@ static void initargs(int argc, char **argv) case 'o': if (outFile != NULL) fclose(outFile); - outFile = openFile(optarg); + outFile = openFile(CmdName, optarg, "w"); break; case ':': fprintf(stderr, "%s: option -%c missing argument\n", CmdName, optopt); diff --git a/cmd/tools/graphml2gv.vcxproj b/cmd/tools/graphml2gv.vcxproj index e1f56b97a..aba8aaacd 100644 --- a/cmd/tools/graphml2gv.vcxproj +++ b/cmd/tools/graphml2gv.vcxproj @@ -98,6 +98,9 @@ copy $(SolutionDir)windows\dependencies\libraries\vcpkg\installed\x86-windows\bi copy $(SolutionDir)windows\dependencies\libraries\vcpkg\installed\x86-windows\bin\getopt.dll $(OutDir)getopt.dll + + + diff --git a/cmd/tools/graphml2gv.vcxproj.filters b/cmd/tools/graphml2gv.vcxproj.filters index 6a6fbbfb3..ce9d0179b 100644 --- a/cmd/tools/graphml2gv.vcxproj.filters +++ b/cmd/tools/graphml2gv.vcxproj.filters @@ -14,6 +14,11 @@ cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + Header Files + + Source Files diff --git a/cmd/tools/gv2gml.c b/cmd/tools/gv2gml.c index 48a8eb9aa..2421a48be 100644 --- a/cmd/tools/gv2gml.c +++ b/cmd/tools/gv2gml.c @@ -32,6 +32,7 @@ #include #include #include +#include "openFile.h" static FILE *outFile; static char *CmdName; @@ -640,21 +641,6 @@ static void gv_to_gml(Agraph_t *G) { fprintf (outFile, "]\n"); } -static FILE *openFile(const char *name) -{ - FILE *fp; - - fp = fopen(name, "w"); - if (!fp) { - fprintf(stderr, "%s: could not open file %s for writing\n", - CmdName, name); - perror(name); - graphviz_exit(1); - } - return fp; -} - - static char *useString = "Usage: %s [-?] \n\ -o : output to (stdout)\n\ -? - print usage\n\ @@ -689,7 +675,7 @@ static void initargs(int argc, char **argv) case 'o': if (outFile != NULL) fclose(outFile); - outFile = openFile(optarg); + outFile = openFile(CmdName, optarg, "w"); break; case ':': fprintf(stderr, "%s: option -%c missing parameter\n", CmdName, optopt); diff --git a/cmd/tools/gv2gml.vcxproj b/cmd/tools/gv2gml.vcxproj index 418526d39..3e246a180 100644 --- a/cmd/tools/gv2gml.vcxproj +++ b/cmd/tools/gv2gml.vcxproj @@ -96,6 +96,9 @@ copy $(SolutionDir)windows\dependencies\libraries\vcpkg\installed\x86-windows\bin\getopt.dll $(OutDir)getopt.dll + + + diff --git a/cmd/tools/gv2gml.vcxproj.filters b/cmd/tools/gv2gml.vcxproj.filters index 4de6d0f8a..7ea69e212 100644 --- a/cmd/tools/gv2gml.vcxproj.filters +++ b/cmd/tools/gv2gml.vcxproj.filters @@ -14,6 +14,11 @@ cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + Header Files + + Source Files diff --git a/cmd/tools/gvgen.c b/cmd/tools/gvgen.c index 4b45759e8..a0f3313c4 100644 --- a/cmd/tools/gvgen.c +++ b/cmd/tools/gvgen.c @@ -26,7 +26,7 @@ #include #include #include "graph_generator.h" - +#include "openFile.h" #include typedef enum { unknown, grid, circle, complete, completeb, @@ -51,18 +51,6 @@ typedef struct { static char *cmd; -static FILE *openFile(const char *name) -{ - FILE *fp; - - fp = fopen(name, "w"); - if (!fp) { - fprintf(stderr, "%s: could not open file %s for writing\n", cmd, name); - graphviz_exit(1); - } - return fp; -} - static char *Usage = "Usage: %s [-dv?] [options]\n\ -c : cycle \n\ -C : cylinder \n\ @@ -356,7 +344,7 @@ static GraphType init(int argc, char *argv[], opts_t* opts) opts->name = optarg; break; case 'o': - opts->outfile = openFile(optarg); + opts->outfile = openFile(cmd, optarg, "w"); break; case 'p': graphType = path; diff --git a/cmd/tools/gvgen.vcxproj b/cmd/tools/gvgen.vcxproj index 3e54b59e3..4ee08d462 100644 --- a/cmd/tools/gvgen.vcxproj +++ b/cmd/tools/gvgen.vcxproj @@ -93,6 +93,9 @@ copy $(SolutionDir)windows\dependencies\libraries\vcpkg\installed\x86-windows\bin\getopt.dll $(OutDir)getopt.dll + + + diff --git a/cmd/tools/gvgen.vcxproj.filters b/cmd/tools/gvgen.vcxproj.filters index a48a1b39f..367012528 100644 --- a/cmd/tools/gvgen.vcxproj.filters +++ b/cmd/tools/gvgen.vcxproj.filters @@ -14,6 +14,11 @@ cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + Header Files + + Source Files diff --git a/cmd/tools/gvpack.cpp b/cmd/tools/gvpack.cpp index 5d01991b6..7d06b2bae 100644 --- a/cmd/tools/gvpack.cpp +++ b/cmd/tools/gvpack.cpp @@ -36,6 +36,7 @@ #include #include #include +#include "openFile.h" extern "C" { #ifdef GVDLL @@ -105,18 +106,6 @@ static void usage(int v) graphviz_exit(v); } -static FILE *openFile(const char *name) -{ - FILE *fp; - - fp = fopen(name, "w"); - if (!fp) { - std::cerr << "gvpack: could not open file " << name << " for writing\n"; - graphviz_exit(1); - } - return (fp); -} - /* setNameValue: * If arg is a name-value pair, add it to the list * and return 0; otherwise, return 1. @@ -201,7 +190,7 @@ static void init(int argc, char *argv[], pack_info* pinfo) case 'o': if (outfp != nullptr) fclose(outfp); - outfp = openFile(optarg); + outfp = openFile("gvpack", optarg, "w"); break; case 'u': pinfo->mode = l_undef; diff --git a/cmd/tools/gvpack.vcxproj b/cmd/tools/gvpack.vcxproj index 575b20222..1b671bbae 100644 --- a/cmd/tools/gvpack.vcxproj +++ b/cmd/tools/gvpack.vcxproj @@ -100,6 +100,7 @@ + diff --git a/cmd/tools/gvpack.vcxproj.filters b/cmd/tools/gvpack.vcxproj.filters index 32da2832a..5efa9744b 100644 --- a/cmd/tools/gvpack.vcxproj.filters +++ b/cmd/tools/gvpack.vcxproj.filters @@ -18,6 +18,9 @@ Header Files + + Header Files + diff --git a/cmd/tools/openFile.h b/cmd/tools/openFile.h new file mode 100644 index 000000000..6a354bf47 --- /dev/null +++ b/cmd/tools/openFile.h @@ -0,0 +1,19 @@ +#pragma once + +#include +#include +#include +#include + +static inline FILE *openFile(const char *argv0, const char *name, + const char *mode) { + FILE *fp = fopen(name, mode); + if (fp == NULL) { + const char *modestr = strcmp(mode, "r") == 0 ? "reading" : "writing"; + fprintf(stderr, "%s: could not open file %s for %s\n", argv0, name, + modestr); + perror(name); + graphviz_exit(EXIT_FAILURE); + } + return fp; +} diff --git a/cmd/tools/sccmap.c b/cmd/tools/sccmap.c index 5be1d3c19..58a51171c 100644 --- a/cmd/tools/sccmap.c +++ b/cmd/tools/sccmap.c @@ -35,6 +35,7 @@ #include #include +#include "openFile.h" #define INF UINT_MAX @@ -247,18 +248,6 @@ static void process(Agraph_t * G) } -static FILE *openFile(const char *name) -{ - FILE *fp; - - fp = fopen(name, "w"); - if (!fp) { - fprintf(stderr, "gvpack: could not open file %s for writing\n", name); - graphviz_exit(1); - } - return (fp); -} - static char *useString = "Usage: %s [-sdv?] \n\ -s - only produce statistics\n\ -S - silent\n\ @@ -291,7 +280,7 @@ static void scanArgs(int argc, char **argv) case 'o': if (outfp != NULL) fclose(outfp); - outfp = openFile(optarg); + outfp = openFile(CmdName, optarg, "w"); break; case 'v': Verbose = 1; diff --git a/cmd/tools/sccmap.vcxproj b/cmd/tools/sccmap.vcxproj index 836718022..8005b5f1b 100644 --- a/cmd/tools/sccmap.vcxproj +++ b/cmd/tools/sccmap.vcxproj @@ -98,6 +98,9 @@ copy $(SolutionDir)windows\dependencies\libraries\vcpkg\installed\x86-windows\bin\getopt.dll $(OutDir)getopt.dll + + + diff --git a/cmd/tools/sccmap.vcxproj.filters b/cmd/tools/sccmap.vcxproj.filters index 09bade2ce..44f689706 100644 --- a/cmd/tools/sccmap.vcxproj.filters +++ b/cmd/tools/sccmap.vcxproj.filters @@ -14,6 +14,11 @@ cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + Header Files + + Source Files diff --git a/cmd/tools/unflatten.c b/cmd/tools/unflatten.c index 1e93e977d..d6dcf25db 100644 --- a/cmd/tools/unflatten.c +++ b/cmd/tools/unflatten.c @@ -28,6 +28,7 @@ #include #include +#include "openFile.h" static bool Do_fans = false; static int MaxMinlen = 0; @@ -147,18 +148,6 @@ static void usage(int v) graphviz_exit(v); } -static FILE *openFile(const char *name) -{ - FILE *fp; - - fp = fopen(name, "w"); - if (!fp) { - fprintf(stderr, "%s: could not open file %s for writing\n", cmd, name); - graphviz_exit(-1); - } - return fp; -} - static char **scanargs(int argc, char **argv) { int c, ival; @@ -183,7 +172,7 @@ static char **scanargs(int argc, char **argv) case 'o': if (outFile != NULL) fclose(outFile); - outFile = openFile(optarg); + outFile = openFile(cmd, optarg, "w"); break; case '?': if (optopt == '?') diff --git a/cmd/tools/unflatten.vcxproj b/cmd/tools/unflatten.vcxproj index f32c8fcae..ff13126b5 100644 --- a/cmd/tools/unflatten.vcxproj +++ b/cmd/tools/unflatten.vcxproj @@ -98,6 +98,9 @@ copy $(SolutionDir)windows\dependencies\libraries\vcpkg\installed\x86-windows\bin\getopt.dll $(OutDir)getopt.dll + + + diff --git a/cmd/tools/unflatten.vcxproj.filters b/cmd/tools/unflatten.vcxproj.filters index 7ae8a0713..51d2daebe 100644 --- a/cmd/tools/unflatten.vcxproj.filters +++ b/cmd/tools/unflatten.vcxproj.filters @@ -14,6 +14,11 @@ cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + Header Files + + Source Files -- 2.40.0