groff-commit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[groff] 08/11: [libgroff]: Trivially refactor `xtmpfile`.


From: G. Branden Robinson
Subject: [groff] 08/11: [libgroff]: Trivially refactor `xtmpfile`.
Date: Mon, 27 Nov 2023 15:52:12 -0500 (EST)

gbranden pushed a commit to branch master
in repository groff.

commit 1c3fdbf4d1c7f4e72d82106e18cf8f4a957f6860
Author: G. Branden Robinson <g.branden.robinson@gmail.com>
AuthorDate: Mon Nov 27 13:33:46 2023 -0600

    [libgroff]: Trivially refactor `xtmpfile`.
    
    * src/include/lib.h:
    * src/libs/libgroff/tmpfile.cpp (xtmpfile): Boolify `int` parameter and
      rename it from `do_unlink` to `want_unlink`.
    
    * src/libs/libgroff/tmpfile.cpp (xtmpfile): Make null pointer comparison
      explicit.  Recast diagnostic to identify what operation failed instead
      of cryptically uttering only the name of a standard C library
      function.
    
    Also:
    
    * Clarify comments (and add one on an aspect of heap storage usage).
    * Also update editor aid comments; drop old style Emacs file-local
      variable setting.
    * Annotate null pointers with `nullptr` comment to ease any future
      transition to C++11, which defines it as a keyword.
---
 ChangeLog                     | 13 +++++++++++++
 src/include/lib.h             |  8 ++++----
 src/libs/libgroff/tmpfile.cpp | 21 ++++++++++++++-------
 3 files changed, 31 insertions(+), 11 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 6f9b467dc..e6d9f2556 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,16 @@
+2023-11-27  G. Branden Robinson <g.branden.robinson@gmail.com>
+
+       [libgroff]: Trivially refactor `xtmpfile`.
+
+       * src/include/lib.h:
+       * src/libs/libgroff/tmpfile.cpp (xtmpfile): Boolify `int`
+       parameter and rename it from `do_unlink` to `want_unlink`.
+
+       * src/libs/libgroff/tmpfile.cpp (xtmpfile): Make null pointer
+       comparison explicit. Recast diagnostic to identify what
+       operation failed instead of cryptically uttering only the name
+       of a standard C library function.
+
 2023-11-20  G. Branden Robinson <g.branden.robinson@gmail.com>
 
        [man]: Don't enforce tag separation on `IP`.
diff --git a/src/include/lib.h b/src/include/lib.h
index c08ab5b4b..7c83c45e5 100644
--- a/src/include/lib.h
+++ b/src/include/lib.h
@@ -85,10 +85,10 @@ int mkstemp(char *tmpl);
 int mksdir(char *tmpl);
 
 #ifdef __cplusplus
-  FILE *xtmpfile(char **namep = 0,
-                const char *postfix_long = 0,
-                const char *postfix_short = 0,
-                int do_unlink = 1);
+  FILE *xtmpfile(char **namep = 0 /* nullptr */,
+                const char *postfix_long = 0 /* nullptr */,
+                const char *postfix_short = 0 /* nullptr */,
+                bool want_unlink = true);
   char *xtmptemplate(const char *postfix_long,
                     const char *postfix_short);
 #endif
diff --git a/src/libs/libgroff/tmpfile.cpp b/src/libs/libgroff/tmpfile.cpp
index 5e807ae68..31bc55b8e 100644
--- a/src/libs/libgroff/tmpfile.cpp
+++ b/src/libs/libgroff/tmpfile.cpp
@@ -1,4 +1,3 @@
-// -*- C++ -*-
 /* Copyright (C) 1989-2020 Free Software Foundation, Inc.
      Written by James Clark (jjc@jclark.com)
 
@@ -163,26 +162,34 @@ static void add_tmp_file(const char *name)
   xtmpfiles_to_delete = x;
 }
 
-// Open a temporary file and with fatal error on failure.
+// Open a temporary file; any failure is fatal.
 
 FILE *xtmpfile(char **namep,
               const char *postfix_long, const char *postfix_short,
-              int do_unlink)
+              bool want_unlink)
 {
+  // `xtmptemplate()` allocates storage for `templ` with `new`.
   char *templ = xtmptemplate(postfix_long, postfix_short);
   errno = 0;
   int fd = mkstemp(templ);
   if (fd < 0)
     fatal("cannot create temporary file: %1", strerror(errno));
   errno = 0;
-  FILE *fp = fdopen(fd, FOPEN_RWB); // many callers of xtmpfile use binary I/O
+ // Many of our callers use binary I/O.
+  FILE *fp = fdopen(fd, FOPEN_RWB);
   if (!fp)
-    fatal("fdopen: %1", strerror(errno));
-  if (do_unlink)
+    fatal("cannot open temporary file: %1", strerror(errno));
+  if (want_unlink)
     add_tmp_file(templ);
-  if (namep)
+  if (namep != 0 /* nullptr */)
     *namep = templ;
   else
     delete[] templ;
   return fp;
 }
+
+// Local Variables:
+// fill-column: 72
+// mode: C++
+// End:
+// vim: set cindent noexpandtab shiftwidth=2 textwidth=72:



reply via email to

[Prev in Thread] Current Thread [Next in Thread]