]> granicus.if.org Git - graphviz/commitdiff
cmd/tools: unify common 'openFile' function into a header
authorMatthew Fernandez <matthew.fernandez@gmail.com>
Thu, 25 Aug 2022 15:28:22 +0000 (08:28 -0700)
committerMatthew Fernandez <matthew.fernandez@gmail.com>
Mon, 29 Aug 2022 15:32:51 +0000 (08:32 -0700)
This also addresses a number of inconsistencies between these duplicated
functions, like `sccmap` calling itself `gvpack` in its error message.

26 files changed:
cmd/tools/Makefile.am
cmd/tools/acyclic.c
cmd/tools/acyclic.vcxproj
cmd/tools/acyclic.vcxproj.filters
cmd/tools/gml2gv.c
cmd/tools/gml2gv.vcxproj
cmd/tools/gml2gv.vcxproj.filters
cmd/tools/graphml2gv.c
cmd/tools/graphml2gv.vcxproj
cmd/tools/graphml2gv.vcxproj.filters
cmd/tools/gv2gml.c
cmd/tools/gv2gml.vcxproj
cmd/tools/gv2gml.vcxproj.filters
cmd/tools/gvgen.c
cmd/tools/gvgen.vcxproj
cmd/tools/gvgen.vcxproj.filters
cmd/tools/gvpack.cpp
cmd/tools/gvpack.vcxproj
cmd/tools/gvpack.vcxproj.filters
cmd/tools/openFile.h [new file with mode: 0644]
cmd/tools/sccmap.c
cmd/tools/sccmap.vcxproj
cmd/tools/sccmap.vcxproj.filters
cmd/tools/unflatten.c
cmd/tools/unflatten.vcxproj
cmd/tools/unflatten.vcxproj.filters

index 818fe5a58c3630de1cef719f1665ec76e22106af..1233149650f8cee6a679e1aa139d64f02ac34866 100644 (file)
@@ -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
index 9cedc411b6cef59956c288e9bbeb4c14a860d2d1..3cda485169123ea5393569fe027ed1fc1a6757df 100644 (file)
@@ -26,6 +26,7 @@
 #include <cgraph/cgraph.h>
 #include <cgraph/exit.h>
 #include <cgraph/unreachable.h>
+#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)
index 4a243e202585bae49dc74bddca36e3a73906d165..e4df1e9e5aad550a2f0fc6772253dfaa526f494b 100644 (file)
@@ -98,6 +98,9 @@
       <Command>copy $(SolutionDir)windows\dependencies\libraries\vcpkg\installed\x86-windows\bin\getopt.dll $(OutDir)getopt.dll</Command>
     </PostBuildEvent>
   </ItemDefinitionGroup>
+  <ItemGroup>
+    <ClInclude Include="openFile.h" />
+  </ItemGroup>
   <ItemGroup>
     <ClCompile Include="acyclic.c" />
   </ItemGroup>
index 743835affb9b9ae59b6c2a00b97aaf1134452c19..d83bf664031fad160a59f602f27c207957e838d8 100644 (file)
       <Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
     </Filter>
   </ItemGroup>
+  <ItemGroup>
+    <ClInclude Include="openFile.h">
+      <Filter>Header Files</Filter>
+    </ClInclude>
+  </ItemGroup>
   <ItemGroup>
     <ClCompile Include="acyclic.c">
       <Filter>Source Files</Filter>
index 1da26b68bfa9d4479892b2c0186def0bba021442..ef0f3ec514627ca27ceb0ffcc6508cafac92a895 100644 (file)
@@ -16,6 +16,7 @@
 #include <cgraph/alloc.h>
 #include <cgraph/exit.h>
 #include <cgraph/unreachable.h>
+#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<name>] [-o<file>] <files>\n\
   -g<name>  : use <name> 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);
index 69326a94e92231c7d9f835b0b2ef33027e5ec073..2990c0db7ba318fc43c0aabc593d7b3923fffae2 100644 (file)
@@ -107,6 +107,7 @@ win_bison -dy -Wno-yacc gmlparse.y -o gmlparse.c</Command>
   <ItemGroup>
     <ClInclude Include="gml2gv.h" />
     <ClInclude Include="gmlparse.h" />
+    <ClInclude Include="openFile.h" />
   </ItemGroup>
   <ItemGroup>
     <ClCompile Include="gml2gv.c" />
index 240d45258a31e36a1f283eef6111e64d45967d65..a3d4431ffee6c7ca6f32419cf423eac01e73ed2a 100644 (file)
@@ -21,6 +21,9 @@
     <ClInclude Include="gmlparse.h">
       <Filter>Header Files</Filter>
     </ClInclude>
+    <ClInclude Include="openFile.h">
+      <Filter>Header Files</Filter>
+    </ClInclude>
   </ItemGroup>
   <ItemGroup>
     <ClCompile Include="gml2gv.c">
index 5002496be3abdd55b2d3fa82583a92ba058a25e4..fe212decd46bc52a3c5a82e176779a917b055fcf 100644 (file)
@@ -23,6 +23,7 @@
 #include    <getopt.h>
 #include    <stdio.h>
 #include    <string.h>
+#include    "openFile.h"
 #ifdef HAVE_EXPAT
 #include    <expat.h>
 #include    <ctype.h>
@@ -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<file>] [<graphs>]\n\
  -g<name>  : use <name> as template for graph names\n\
  -o<file>  : output to <file> (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);
index e1f56b97a354f7a18999e2cdb9fc516e7993eb95..aba8aaacd51b48ab56d19cd6fddd496be1eae930 100644 (file)
@@ -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</Command>
     </PostBuildEvent>
   </ItemDefinitionGroup>
+  <ItemGroup>
+    <ClInclude Include="openFile.h" />
+  </ItemGroup>
   <ItemGroup>
     <ClCompile Include="graphml2gv.c" />
   </ItemGroup>
index 6a6fbbfb3f051da2a5b9da25fd9e2ffd0f5438d6..ce9d0179b75fa56e9fbb82482de349ab493eeb18 100644 (file)
       <Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
     </Filter>
   </ItemGroup>
+  <ItemGroup>
+    <ClInclude Include="openFile.h">
+      <Filter>Header Files</Filter>
+    </ClInclude>
+  </ItemGroup>
   <ItemGroup>
     <ClCompile Include="graphml2gv.c">
       <Filter>Source Files</Filter>
index 48a8eb9aaf45c17236d0239d160b13a31a4cb8f7..2421a48be455d2d82470437ecb610a3666ad2f69 100644 (file)
@@ -32,6 +32,7 @@
 #include <common/utils.h>
 #include <ctype.h>
 #include <ingraphs/ingraphs.h>
+#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 [-?] <files>\n\
   -o<file>  : output to <file> (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);
index 418526d39876d61c0134a9ed86e350b6e15671d6..3e246a180ffd080e085116dcf0702524119a0093 100644 (file)
@@ -96,6 +96,9 @@
       <Command>copy $(SolutionDir)windows\dependencies\libraries\vcpkg\installed\x86-windows\bin\getopt.dll $(OutDir)getopt.dll</Command>
     </PostBuildEvent>
   </ItemDefinitionGroup>
+  <ItemGroup>
+    <ClInclude Include="openFile.h" />
+  </ItemGroup>
   <ItemGroup>
     <ClCompile Include="gv2gml.c" />
   </ItemGroup>
index 4de6d0f8abb4b62d1324a98df608c9e459efe443..7ea69e2128b5cface426f406be15be018665dcd2 100644 (file)
       <Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
     </Filter>
   </ItemGroup>
+  <ItemGroup>
+    <ClInclude Include="openFile.h">
+      <Filter>Header Files</Filter>
+    </ClInclude>
+  </ItemGroup>
   <ItemGroup>
     <ClCompile Include="gv2gml.c">
       <Filter>Source Files</Filter>
index 4b45759e8ce3acdd945f8de15c097452bb9ba749..a0f3313c4cbf59cbc1e2055d1761b07f91cdd01f 100644 (file)
@@ -26,7 +26,7 @@
 #include <ctype.h>
 #include <getopt.h>
 #include "graph_generator.h"
-
+#include "openFile.h"
 #include <cgraph/exit.h>
 
 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<n>         : cycle \n\
  -C<x,y>       : 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;
index 3e54b59e3c13388b1b0725e73c1337505d4b3b01..4ee08d46254dd28b5536c716604a92e66a8bdc68 100644 (file)
@@ -93,6 +93,9 @@
       <Command>copy $(SolutionDir)windows\dependencies\libraries\vcpkg\installed\x86-windows\bin\getopt.dll $(OutDir)getopt.dll</Command>
     </PostBuildEvent>
   </ItemDefinitionGroup>
+  <ItemGroup>
+    <ClInclude Include="openFile.h" />
+  </ItemGroup>
   <ItemGroup>
     <ClCompile Include="graph_generator.c" />
     <ClCompile Include="gvgen.c" />
index a48a1b39f015f0b86b0b463e6098030398b192a4..367012528219121e7f6526294c1c969fe2b35ab0 100644 (file)
       <Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
     </Filter>
   </ItemGroup>
+  <ItemGroup>
+    <ClInclude Include="openFile.h">
+      <Filter>Header Files</Filter>
+    </ClInclude>
+  </ItemGroup>
   <ItemGroup>
     <ClCompile Include="graph_generator.c">
       <Filter>Source Files</Filter>
index 5d01991b63c907fea2826b1efd5ca7edab18f01c..7d06b2baeb59a73a639584635078d1fe26dc6859 100644 (file)
@@ -36,6 +36,7 @@
 #include <stddef.h>
 #include <string>
 #include <vector>
+#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;
index 575b20222e40a84c17459cdbe1c1bbe30e1dc2d1..1b671bbaecbe4151969944656cbd5127b3b2a28f 100644 (file)
   </ItemDefinitionGroup>
   <ItemGroup>
     <ClInclude Include="graph_generator.h" />
+    <ClInclude Include="openFile.h" />
   </ItemGroup>
   <ItemGroup>
     <ClCompile Include="gvpack.cpp" />
index 32da2832ad1085582baa669add21fd897cb2d539..5efa9744bf3f0e7de49f97bf8165d21dc6003019 100644 (file)
@@ -18,6 +18,9 @@
     <ClInclude Include="graph_generator.h">
       <Filter>Header Files</Filter>
     </ClInclude>
+    <ClInclude Include="openFile.h">
+      <Filter>Header Files</Filter>
+    </ClInclude>
   </ItemGroup>
   <ItemGroup>
     <ClCompile Include="gvpack.cpp">
diff --git a/cmd/tools/openFile.h b/cmd/tools/openFile.h
new file mode 100644 (file)
index 0000000..6a354bf
--- /dev/null
@@ -0,0 +1,19 @@
+#pragma once
+
+#include <cgraph/exit.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+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;
+}
index 5be1d3c19abe5b2e7374cc2cccbc11e90c30f8a2..58a51171c5e91636b2d79ce930394e67b7185a7b 100644 (file)
@@ -35,6 +35,7 @@
 #include <ingraphs/ingraphs.h>
 
 #include <getopt.h>
+#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?] <files>\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;
index 836718022ba33978568a3f5d4eea2a06844b5f99..8005b5f1bce1e16fe00947f8f3a7f3b9a1f6a6b4 100644 (file)
@@ -98,6 +98,9 @@
       <Command>copy $(SolutionDir)windows\dependencies\libraries\vcpkg\installed\x86-windows\bin\getopt.dll $(OutDir)getopt.dll</Command>
     </PostBuildEvent>
   </ItemDefinitionGroup>
+  <ItemGroup>
+    <ClInclude Include="openFile.h" />
+  </ItemGroup>
   <ItemGroup>
     <ClCompile Include="sccmap.c" />
   </ItemGroup>
index 09bade2ceb5672865315fc581bd3dc05623d3b08..44f689706dbed122a6bbad2344d6f9085f0c6965 100644 (file)
       <Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
     </Filter>
   </ItemGroup>
+  <ItemGroup>
+    <ClInclude Include="openFile.h">
+      <Filter>Header Files</Filter>
+    </ClInclude>
+  </ItemGroup>
   <ItemGroup>
     <ClCompile Include="sccmap.c">
       <Filter>Source Files</Filter>
index 1e93e977dcfd2fd2738ec4109b1c333a96a80577..d6dcf25db24a4144a43107f7cefaa09265b845b9 100644 (file)
@@ -28,6 +28,7 @@
 #include    <ingraphs/ingraphs.h>
 
 #include <getopt.h>
+#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 == '?')
index f32c8fcae972a7425027265c8942dcb0b57ce016..ff13126b50742d9c085f28a2fa7d8f788ca649f8 100644 (file)
@@ -98,6 +98,9 @@
       <Command>copy $(SolutionDir)windows\dependencies\libraries\vcpkg\installed\x86-windows\bin\getopt.dll $(OutDir)getopt.dll</Command>
     </PostBuildEvent>
   </ItemDefinitionGroup>
+  <ItemGroup>
+    <ClInclude Include="openFile.h" />
+  </ItemGroup>
   <ItemGroup>
     <ClCompile Include="unflatten.c" />
   </ItemGroup>
index 7ae8a07139c0e4ac891ccc58393057e7bd79bb8e..51d2daebe1412de83afef891c2ed1809f89a13be 100644 (file)
       <Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
     </Filter>
   </ItemGroup>
+  <ItemGroup>
+    <ClInclude Include="openFile.h">
+      <Filter>Header Files</Filter>
+    </ClInclude>
+  </ItemGroup>
   <ItemGroup>
     <ClCompile Include="unflatten.c">
       <Filter>Source Files</Filter>