public inbox for gdb-cvs@sourceware.org
help / color / mirror / Atom feed
* [binutils-gdb] Introduce read_remainder_of_file
@ 2024-02-27 16:56 Tom Tromey
  0 siblings, 0 replies; only message in thread
From: Tom Tromey @ 2024-02-27 16:56 UTC (permalink / raw)
  To: gdb-cvs

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=5c92d8456d3120bcfbbd0ad063d423411a8c685d

commit 5c92d8456d3120bcfbbd0ad063d423411a8c685d
Author: Tom Tromey <tromey@adacore.com>
Date:   Thu Feb 15 12:12:25 2024 -0700

    Introduce read_remainder_of_file
    
    This patch adds a new function, read_remainder_of_file.  This is like
    read_text_file_to_string, but reads from an existing 'FILE *'.  This
    will be used in a subsequent patch.
    
    Reviewed-By: Tom de Vries <tdevries@suse.de>

Diff:
---
 gdbsupport/filestuff.cc | 24 ++++++++++++++++--------
 gdbsupport/filestuff.h  |  4 ++++
 2 files changed, 20 insertions(+), 8 deletions(-)

diff --git a/gdbsupport/filestuff.cc b/gdbsupport/filestuff.cc
index c67e650c1a6..9d3b417359b 100644
--- a/gdbsupport/filestuff.cc
+++ b/gdbsupport/filestuff.cc
@@ -504,13 +504,9 @@ mkdir_recursive (const char *dir)
 
 /* See gdbsupport/filestuff.h.  */
 
-std::optional<std::string>
-read_text_file_to_string (const char *path)
+std::string
+read_remainder_of_file (FILE *file)
 {
-  gdb_file_up file = gdb_fopen_cloexec (path, "r");
-  if (file == nullptr)
-    return {};
-
   std::string res;
   for (;;)
     {
@@ -520,7 +516,7 @@ read_text_file_to_string (const char *path)
       /* Resize to accommodate CHUNK_SIZE bytes.  */
       res.resize (start_size + chunk_size);
 
-      int n = fread (&res[start_size], 1, chunk_size, file.get ());
+      int n = fread (&res[start_size], 1, chunk_size, file);
       if (n == chunk_size)
 	continue;
 
@@ -528,7 +524,7 @@ read_text_file_to_string (const char *path)
 
       /* Less than CHUNK means EOF or error.  If it's an error, return
 	 no value.  */
-      if (ferror (file.get ()))
+      if (ferror (file))
 	return {};
 
       /* Resize the string according to the data we read.  */
@@ -538,3 +534,15 @@ read_text_file_to_string (const char *path)
 
   return res;
 }
+
+/* See gdbsupport/filestuff.h.  */
+
+std::optional<std::string>
+read_text_file_to_string (const char *path)
+{
+  gdb_file_up file = gdb_fopen_cloexec (path, "r");
+  if (file == nullptr)
+    return {};
+
+  return read_remainder_of_file (file.get ());
+}
diff --git a/gdbsupport/filestuff.h b/gdbsupport/filestuff.h
index 1c43b7de0ca..e2ee141d46f 100644
--- a/gdbsupport/filestuff.h
+++ b/gdbsupport/filestuff.h
@@ -133,4 +133,8 @@ extern bool mkdir_recursive (const char *dir);
 
 extern std::optional<std::string> read_text_file_to_string (const char *path);
 
+/* Read the remaining content from FILE into an std::string.  */
+
+extern std::string read_remainder_of_file (FILE *file);
+
 #endif /* COMMON_FILESTUFF_H */

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2024-02-27 16:56 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-02-27 16:56 [binutils-gdb] Introduce read_remainder_of_file Tom Tromey

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).