]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
Introduce read_remainder_of_file
authorTom Tromey <tromey@adacore.com>
Thu, 15 Feb 2024 19:12:25 +0000 (12:12 -0700)
committerTom Tromey <tromey@adacore.com>
Tue, 27 Feb 2024 16:46:31 +0000 (09:46 -0700)
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>
gdbsupport/filestuff.cc
gdbsupport/filestuff.h

index c67e650c1a6f5d4dc24ae224509ae212ef661c5f..9d3b417359b1a2727be93cb703a899f6124b538e 100644 (file)
@@ -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 ());
+}
index 1c43b7de0cab7be36baace961fe98fa800461a85..e2ee141d46f01bc0888a871830a5559419b63f03 100644 (file)
@@ -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 */