[1/5] Introduce new overload of read_text_file_to_string

Message ID 20240216-py-simple-exc-v1-1-c275bcfb5e4a@adacore.com
State New
Headers
Series Fix DAP pause bug |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 success Testing passed
linaro-tcwg-bot/tcwg_gdb_build--master-arm success Testing passed
linaro-tcwg-bot/tcwg_gdb_check--master-arm success Testing passed
linaro-tcwg-bot/tcwg_gdb_check--master-aarch64 success Testing passed

Commit Message

Tom Tromey Feb. 16, 2024, 6:37 p.m. UTC
  This patch adds a new overload of read_text_file_to_string.  This one
reads from an existing 'FILE *'.  This will be used in a subsequent
patch.
---
 gdbsupport/filestuff.cc | 24 ++++++++++++++++--------
 gdbsupport/filestuff.h  |  4 ++++
 2 files changed, 20 insertions(+), 8 deletions(-)
  

Comments

Tom de Vries Feb. 19, 2024, 3:36 p.m. UTC | #1
On 2/16/24 19:37, Tom Tromey wrote:
> +/* Read the entire content of FILE into an std::string.  */
> +
> +extern std::string read_text_file_to_string (FILE *file);
> +
>   #endif /* COMMON_FILESTUFF_H */
> 
It occurred to me that if this reads the entire content of FILE, 
shouldn't it start with an "fseek (file, 0, SEEK_SET)"?

We could add an argument at_start that lets us skip the fseek, and 
default it to false, and call it with at_start set to true from the 
"const char *path" version of read_text_file_to_string.

Thanks,
- Tom
  
Tom Tromey Feb. 20, 2024, 2:08 p.m. UTC | #2
>>>>> "Tom" == Tom de Vries <tdevries@suse.de> writes:

Tom> On 2/16/24 19:37, Tom Tromey wrote:
>> +/* Read the entire content of FILE into an std::string.  */
>> +
>> +extern std::string read_text_file_to_string (FILE *file);
>> +
>> #endif /* COMMON_FILESTUFF_H */
>> 
Tom> It occurred to me that if this reads the entire content of FILE,
Tom> shouldn't it start with an "fseek (file, 0, SEEK_SET)"?

Tom> We could add an argument at_start that lets us skip the fseek, and
Tom> default it to false, and call it with at_start set to true from the
Tom> "const char *path" version of read_text_file_to_string.

I'd rather not add things that we don't need.
How about I rename the function instead?

Tom
  
Tom de Vries Feb. 20, 2024, 2:48 p.m. UTC | #3
On 2/20/24 15:08, Tom Tromey wrote:
>>>>>> "Tom" == Tom de Vries <tdevries@suse.de> writes:
> 
> Tom> On 2/16/24 19:37, Tom Tromey wrote:
>>> +/* Read the entire content of FILE into an std::string.  */
>>> +
>>> +extern std::string read_text_file_to_string (FILE *file);
>>> +
>>> #endif /* COMMON_FILESTUFF_H */
>>>
> Tom> It occurred to me that if this reads the entire content of FILE,
> Tom> shouldn't it start with an "fseek (file, 0, SEEK_SET)"?
> 
> Tom> We could add an argument at_start that lets us skip the fseek, and
> Tom> default it to false, and call it with at_start set to true from the
> Tom> "const char *path" version of read_text_file_to_string.
> 
> I'd rather not add things that we don't need.
> How about I rename the function instead?

Fine by me.

Thanks,
- Tom
  

Patch

diff --git a/gdbsupport/filestuff.cc b/gdbsupport/filestuff.cc
index c67e650c1a6..70bcc3faf60 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_text_file_to_string (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_text_file_to_string (file.get ());
+}
diff --git a/gdbsupport/filestuff.h b/gdbsupport/filestuff.h
index 1c43b7de0ca..4a90b08c5f3 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 entire content of FILE into an std::string.  */
+
+extern std::string read_text_file_to_string (FILE *file);
+
 #endif /* COMMON_FILESTUFF_H */