groff-commit
[Top][All Lists]
Advanced

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

[groff] 06/34: [troff]: Refactor *roff stream handling (1/2).


From: G. Branden Robinson
Subject: [groff] 06/34: [troff]: Refactor *roff stream handling (1/2).
Date: Mon, 16 Sep 2024 20:48:32 -0400 (EDT)

gbranden pushed a commit to branch master
in repository groff.

commit 6acf9751d99975c552365ce57de581d053a1f1a9
Author: G. Branden Robinson <g.branden.robinson@gmail.com>
AuthorDate: Fri Sep 13 02:51:26 2024 -0500

    [troff]: Refactor *roff stream handling (1/2).
    
    * src/roff/troff/input.cpp: Declare global `stream_dictionary` as type
      `object_dictionary` instead of `dictionary`, since we want the stored
      values to be class objects instead of a primitive data type.
    
      (open_file, close_request): Update member function calls
      appropriately; the former class uses return values for `lookup()` and
      `remove()`, the latter does not.
    
      (open_file): Clarify diagnostic when we try to reorient an existing
      stream name to a new file, but can't close the old file associated
      with the stream name.
    
      (close_request): Remove the stream from the dictionary only if, and
      after, we have successfully closed the `FILE` stream.
---
 ChangeLog                | 15 +++++++++++++++
 src/roff/troff/input.cpp | 45 +++++++++++++++++++++++++++------------------
 2 files changed, 42 insertions(+), 18 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index ff567c655..dcdf74d00 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,18 @@
+2024-09-13  G. Branden Robinson <g.branden.robinson@gmail.com>
+
+       * src/roff/troff/input.cpp: Declare global `stream_dictionary`
+       as type `object_dictionary` instead of `dictionary`, since we
+       want the stored values to be class objects instead of a
+       primitive data type.
+       (open_file, close_request): Update member function calls
+       appropriately; the former class uses return values for
+       `lookup()` and `remove()`, the latter does not.
+       (open_file): Clarify diagnostic when we try to reorient an
+       existing stream name to a new file, but can't close the old file
+       associated with the stream name.
+       (close_request): Remove the stream from the dictionary only if,
+       and after, we have successfully closed the `FILE` stream.
+
 2024-09-13  G. Branden Robinson <g.branden.robinson@gmail.com>
 
        * src/roff/troff/input.cpp (class input_iterator): Trivially
diff --git a/src/roff/troff/input.cpp b/src/roff/troff/input.cpp
index 393fc7194..57a2032d0 100644
--- a/src/roff/troff/input.cpp
+++ b/src/roff/troff/input.cpp
@@ -7212,7 +7212,7 @@ void terminal_continue()
   do_terminal(0, 1);
 }
 
-dictionary stream_dictionary(20);
+object_dictionary stream_dictionary(20);
 
 static void open_file(bool appending)
 {
@@ -7227,13 +7227,18 @@ static void open_file(bool appending)
              filename.contents(),
              appending ? "appending" : "writing",
              strerror(errno));
-       fp = (FILE *)stream_dictionary.remove(stream);
+       // If we already had a key of this name in the dictionary, it's
+       // invalid now.
+       stream_dictionary.remove(stream);
+      }
+      else {
+       FILE *oldfp = (FILE *)stream_dictionary.lookup(stream);
+       if (oldfp != 0 /* nullptr */ && (fclose(oldfp) != 0))
+         error("cannot close file '%1' already associated with stream"
+               " '%2': %3", filename.contents(), strerror(errno));
+       else
+         stream_dictionary.define(stream, (object *)fp);
       }
-      else
-       fp = (FILE *)stream_dictionary.lookup(stream, fp);
-      if (fp != 0 /* nullptr */ && (fclose(fp) != 0))
-       error("cannot close file '%1': %2", filename.contents(),
-             strerror(errno));
     }
   }
 }
@@ -7277,18 +7282,22 @@ static void close_request() // .close
     skip_line();
     return;
   }
-  symbol stream = get_name(true /* required */);
-  if (!stream.is_null()) {
-    FILE *fp = (FILE *)stream_dictionary.remove(stream);
-    if (0 /* nullptr */ == fp)
-      error("cannot close nonexistent stream '%1'", stream.contents());
-    else {
-      int status = fclose(fp);
-       if (status != 0)
-         error("cannot close stream '%1': %2", stream.contents(),
-               strerror(errno));
-    }
+  symbol stream = get_name();
+  // Testing has_arg() should have ensured this.
+  assert(stream != 0 /* nullptr */);
+  FILE *fp = (FILE *)stream_dictionary.lookup(stream);
+  if (0 /* nullptr */ == fp) {
+    error("cannot close nonexistent stream '%1'", stream.contents());
+    skip_line();
+    return;
+  }
+  if (fclose(fp) != 0) {
+    error("cannot close stream '%1': %2", stream.contents(),
+         strerror(errno));
+    skip_line();
+    return;
   }
+  stream_dictionary.remove(stream);
   skip_line();
 }
 



reply via email to

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